diff --git a/packages/run/README.md b/packages/run/README.md index a57b375db..4eb5bb8d1 100644 --- a/packages/run/README.md +++ b/packages/run/README.md @@ -83,6 +83,13 @@ Default: `false` If `true`, instructs the plugin to listen to `stdin` for the sequences listed below followed by enter (carriage return). +### `input` + +Type: `String`
+Default: `null` + +Specifies the entry point this plugin will use. Not necessary if you only have a single entry point. + #### `stdin` Input Actions When this option is enabled, `stdin` will listen for the following input and perform the associated action: diff --git a/packages/run/src/index.ts b/packages/run/src/index.ts index a4f5e575e..63c7dbe7a 100644 --- a/packages/run/src/index.ts +++ b/packages/run/src/index.ts @@ -12,6 +12,7 @@ export default function run(opts: RollupRunOptions = {}): Plugin { const args = opts.args || []; const allowRestarts = opts.allowRestarts || false; + const overrideInput = opts.input; const forkOptions = opts.options || opts; delete (forkOptions as RollupRunOptions).args; delete (forkOptions as RollupRunOptions).allowRestarts; @@ -20,7 +21,7 @@ export default function run(opts: RollupRunOptions = {}): Plugin { name: 'run', buildStart(options) { - let inputs = options.input; + let inputs = overrideInput ?? options.input; if (typeof inputs === 'string') { inputs = [inputs]; @@ -31,7 +32,9 @@ export default function run(opts: RollupRunOptions = {}): Plugin { } if (inputs.length > 1) { - throw new Error(`@rollup/plugin-run only works with a single entry point`); + throw new Error( + `@rollup/plugin-run must have a single entry point; consider setting the \`input\` option` + ); } input = resolve(inputs[0]); diff --git a/packages/run/test/test.js b/packages/run/test/test.js index 98c1a4d40..587850433 100644 --- a/packages/run/test/test.js +++ b/packages/run/test/test.js @@ -98,6 +98,23 @@ test('throws an error when bundle is not written to disk', async (t) => { ); }); +test('throws an error when input option is invalid', async (t) => { + const testInput = join(cwd, 'change-detect-input.js'); + const bundle = await rollup({ + input: [input, testInput], + plugins: [run({ input: 'something that is not an input' })] + }); + await t.throwsAsync( + async () => { + await bundle.write(outputDirOptions); + }, + { + instanceOf: Error, + message: '@rollup/plugin-run could not find output chunk' + } + ); +}); + test('throws an error when there are multiple entry points', async (t) => { const testInput = join(cwd, 'change-detect-input.js'); await t.throwsAsync( @@ -109,7 +126,8 @@ test('throws an error when there are multiple entry points', async (t) => { }, { instanceOf: Error, - message: '@rollup/plugin-run only works with a single entry point' + message: + '@rollup/plugin-run must have a single entry point; consider setting the `input` option' } ); }); @@ -137,6 +155,16 @@ test('allow the allowRestart option', async (t) => { t.true(mockChildProcess.calledWithExactly(outputOptions.file, [], {})); }); +test('allow the input option', async (t) => { + const testInput = join(cwd, 'change-detect-input.js'); + const bundle = await rollup({ + input: [input, testInput], + plugins: [run({ input })] + }); + await bundle.write(outputDirOptions); + t.true(mockChildProcess.calledWithExactly(join(outputDir, 'input.js'), [], { input })); +}); + test.after(async () => { await del(['output']); }); diff --git a/packages/run/types/index.d.ts b/packages/run/types/index.d.ts index d546be160..fe9fbe599 100644 --- a/packages/run/types/index.d.ts +++ b/packages/run/types/index.d.ts @@ -6,6 +6,7 @@ export interface RollupRunOptions extends ForkOptions { args?: readonly string[]; options?: ForkOptions; allowRestarts?: boolean; + input?: string; } /**