Skip to content

Commit 167b7c9

Browse files
committed
wip: fuse _ongenerate functionality into buildEnd, _onwrite into generateBundle
- per ezolenko, the whole generateRound etc stuff was a workaround because the buildEnd hook actually _didn't exist_ before - so now that it does, we can use it to simplify some logic - no longer need `_ongenerate` as that should be in `buildEnd`, and no longer need `_onwrite` as it is the only thing called in `generateBundle`, so just combine them - importantly, `buildEnd` also occurs before output generation, so this ensures that type-checking still occurs even if `bundle.generate()` is not called - also move the `walkTree` call to above the "missed" type-checking as it needs to come first - it does unconditional type-checking once per watch cycle, whereas "missed" only type-checks those that were, well, "missed" - so in order to not have duplication, make "missed" come after, when the `checkedFiles` Set has been filled by `walkTree` already - and for simplification, just return early on error to match the current behavior - in the future, may want to print the error and continue type-checking other files - so that all type-check errors are reported, not just the first one NOTE: this is WIP because the `cache.done()` call and the `!noErrors` message are incorrectly blocked behind the `pluginOptions.check` conditional right now - `cache.done()` needs to be called regardless of check or error or not, i.e. in all scenarios - but ideally it should be called after all the type-checking here - `!noErrors` should be logged regardless of check or not - and similarly, after the type-checking
1 parent 2e26a0e commit 167b7c9

File tree

1 file changed

+22
-30
lines changed

1 file changed

+22
-30
lines changed

src/index.ts

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { relative, dirname, normalize as pathNormalize, resolve } from "path";
22
import * as tsTypes from "typescript";
3-
import { PluginImpl, PluginContext, InputOptions, OutputOptions, TransformResult, SourceMap, Plugin } from "rollup";
3+
import { PluginImpl, InputOptions, TransformResult, SourceMap, Plugin } from "rollup";
44
import { normalizePath as normalize } from "@rollup/pluginutils";
55
import * as _ from "lodash";
66
import { blue, red, yellow, green } from "colors/safe";
@@ -90,7 +90,7 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
9090
}
9191
setTypescriptModule(pluginOptions.typescript);
9292

93-
const self: Plugin & { _ongenerate: () => void, _onwrite: (this: PluginContext, _output: OutputOptions) => void } = {
93+
const self: Plugin = {
9494

9595
name: "rpt2",
9696

@@ -257,11 +257,25 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
257257
return transformResult;
258258
},
259259

260-
buildEnd()
260+
buildEnd(err)
261261
{
262-
if (!pluginOptions.check)
262+
if (!pluginOptions.check || err)
263263
return
264264

265+
// walkTree once on each cycle when in watch mode
266+
if (watchMode)
267+
{
268+
cache().walkTree((id) =>
269+
{
270+
if (!filter(id))
271+
return;
272+
273+
const snapshot = servicesHost.getScriptSnapshot(id);
274+
if (snapshot)
275+
typecheckFile(id, snapshot, context);
276+
});
277+
}
278+
265279
// type-check missed files as well
266280
parsedConfig.fileNames.forEach((name) =>
267281
{
@@ -275,40 +289,18 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
275289
if (snapshot)
276290
typecheckFile(key, snapshot, context);
277291
});
278-
},
279-
280-
generateBundle(bundleOptions)
281-
{
282-
self._ongenerate();
283-
self._onwrite.call(this, bundleOptions);
284-
},
285-
286-
_ongenerate(): void
287-
{
288-
context.debug(() => `generating target ${generateRound + 1}`);
289-
290-
if (pluginOptions.check && watchMode && generateRound === 0)
291-
{
292-
cache().walkTree((id) =>
293-
{
294-
if (!filter(id))
295-
return;
296-
297-
const snapshot = servicesHost.getScriptSnapshot(id);
298-
if (snapshot)
299-
typecheckFile(id, snapshot, context);
300-
});
301-
}
302292

303293
if (!watchMode && !noErrors)
304294
context.info(yellow("there were errors or warnings."));
305295

306296
cache().done();
307-
generateRound++;
308297
},
309298

310-
_onwrite(this: PluginContext, _output: OutputOptions): void
299+
generateBundle(this, _output)
311300
{
301+
context.debug(() => `generating target ${generateRound + 1}`);
302+
generateRound++;
303+
312304
if (!parsedConfig.options.declaration)
313305
return;
314306

0 commit comments

Comments
 (0)