Skip to content

Commit 0b98ab3

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 9ba4ced commit 0b98ab3

File tree

1 file changed

+20
-28
lines changed

1 file changed

+20
-28
lines changed

src/index.ts

Lines changed: 20 additions & 28 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 { blue, red, yellow, green } from "colors/safe";
66
import findCacheDir from "find-cache-dir";
@@ -89,7 +89,7 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
8989
}
9090
setTypescriptModule(pluginOptions.typescript);
9191

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

9494
name: "rpt2",
9595

@@ -270,6 +270,20 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
270270
if (!pluginOptions.check)
271271
return
272272

273+
// walkTree once on each cycle when in watch mode
274+
if (watchMode)
275+
{
276+
cache().walkTree((id) =>
277+
{
278+
if (!filter(id))
279+
return;
280+
281+
const snapshot = servicesHost.getScriptSnapshot(id);
282+
if (snapshot)
283+
typecheckFile(id, snapshot, context);
284+
});
285+
}
286+
273287
// type-check missed files as well
274288
parsedConfig.fileNames.forEach((name) =>
275289
{
@@ -283,40 +297,18 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
283297
if (snapshot)
284298
typecheckFile(key, snapshot, context);
285299
});
286-
},
287-
288-
generateBundle(bundleOptions)
289-
{
290-
self._ongenerate();
291-
self._onwrite.call(this, bundleOptions);
292-
},
293-
294-
_ongenerate(): void
295-
{
296-
context.debug(() => `generating target ${generateRound + 1}`);
297-
298-
if (pluginOptions.check && watchMode && generateRound === 0)
299-
{
300-
cache().walkTree((id) =>
301-
{
302-
if (!filter(id))
303-
return;
304-
305-
const snapshot = servicesHost.getScriptSnapshot(id);
306-
if (snapshot)
307-
typecheckFile(id, snapshot, context);
308-
});
309-
}
310300

311301
if (!watchMode && !noErrors)
312302
context.info(yellow("there were errors or warnings."));
313303

314304
cache().done();
315-
generateRound++;
316305
},
317306

318-
_onwrite(this: PluginContext, _output: OutputOptions): void
307+
generateBundle(this, _output)
319308
{
309+
context.debug(() => `generating target ${generateRound + 1}`);
310+
generateRound++;
311+
320312
if (!parsedConfig.options.declaration)
321313
return;
322314

0 commit comments

Comments
 (0)