diff --git a/packages/run/src/index.ts b/packages/run/src/index.ts index afeda6862..39f6806a0 100644 --- a/packages/run/src/index.ts +++ b/packages/run/src/index.ts @@ -16,7 +16,7 @@ export default function run(opts: RollupRunOptions = {}): Plugin { return { name: 'run', - options(options) { + buildStart(options) { let inputs = options.input!; if (typeof inputs === 'string') { @@ -32,7 +32,6 @@ export default function run(opts: RollupRunOptions = {}): Plugin { } input = path.resolve(inputs[0]); - return options; }, generateBundle(_outputOptions, _bundle, isWrite) { @@ -43,23 +42,14 @@ export default function run(opts: RollupRunOptions = {}): Plugin { writeBundle(outputOptions, bundle) { const dir = outputOptions.dir || path.dirname(outputOptions.file!); - - let dest: string | undefined; - for (const fileName of Object.keys(bundle)) { + const entryFileName = Object.keys(bundle).find((fileName) => { const chunk = bundle[fileName] as RenderedChunk; + return chunk.isEntry && chunk.facadeModuleId === input; + }); - // eslint-disable-next-line no-continue - if (!chunk.isEntry) continue; - - if (chunk.isEntry && chunk.modules[input]) { - dest = path.join(dir, fileName); - break; - } - } - - if (dest) { + if (entryFileName) { if (proc) proc.kill(); - proc = fork(dest, args, forkOptions); + proc = fork(path.join(dir, entryFileName), args, forkOptions); } else { this.error(`@rollup/plugin-run could not find output chunk`); } diff --git a/packages/run/test/fixtures/facade-entry/dynamic.js b/packages/run/test/fixtures/facade-entry/dynamic.js new file mode 100644 index 000000000..6c82d092b --- /dev/null +++ b/packages/run/test/fixtures/facade-entry/dynamic.js @@ -0,0 +1,3 @@ +import log from './library'; + +log(0); diff --git a/packages/run/test/fixtures/facade-entry/index.js b/packages/run/test/fixtures/facade-entry/index.js new file mode 100644 index 000000000..a2002c175 --- /dev/null +++ b/packages/run/test/fixtures/facade-entry/index.js @@ -0,0 +1,6 @@ +import log from './library'; + +log(0); +(async () => { + await import('./dynamic'); +})(); diff --git a/packages/run/test/fixtures/facade-entry/library.js b/packages/run/test/fixtures/facade-entry/library.js new file mode 100644 index 000000000..b92fec258 --- /dev/null +++ b/packages/run/test/fixtures/facade-entry/library.js @@ -0,0 +1,2 @@ +const log = (value) => console.log(value); +export default log; diff --git a/packages/run/test/test.js b/packages/run/test/test.js index c8a53e68b..99e76a190 100644 --- a/packages/run/test/test.js +++ b/packages/run/test/test.js @@ -38,6 +38,33 @@ test('builds the bundle and forks a child process', async (t) => { t.true(mockChildProcess.calledWithExactly(outputOptions.file, [], {})); }); +test('takes input from the latest options', async (t) => { + const bundle = await rollup({ + input: 'incorrect', + plugins: [ + run(), + { + options(options) { + options.input = input; + return options; + } + } + ] + }); + await bundle.write(outputOptions); + t.true(mockChildProcess.calledWithExactly(outputOptions.file, [], {})); +}); + +test('checks entry point facade module', async (t) => { + const bundle = await rollup({ + input: join(cwd, 'facade-entry/index.js'), + plugins: [run()] + }); + const outputDir = join(cwd, 'output'); + await bundle.write({ dir: outputDir, format: 'cjs' }); + t.true(mockChildProcess.calledWithExactly(join(outputDir, 'index.js'), [], {})); +}); + test('allows pass-through options for child_process.fork', async (t) => { const forkOptions = { cwd, @@ -48,7 +75,6 @@ test('allows pass-through options for child_process.fork', async (t) => { input, plugins: [run(forkOptions)] }); - await bundle.write(outputOptions); t.true(mockChildProcess.calledWithExactly(outputOptions.file, [], forkOptions)); });