Skip to content

Commit

Permalink
feat(run): add input option
Browse files Browse the repository at this point in the history
  • Loading branch information
mkantor committed Jun 5, 2024
1 parent fe7360d commit aaad25c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
7 changes: 7 additions & 0 deletions packages/run/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`<br>
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:
Expand Down
7 changes: 5 additions & 2 deletions packages/run/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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];
Expand All @@ -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]);
Expand Down
30 changes: 29 additions & 1 deletion packages/run/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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'
}
);
});
Expand Down Expand Up @@ -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']);
});
1 change: 1 addition & 0 deletions packages/run/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface RollupRunOptions extends ForkOptions {
args?: readonly string[];
options?: ForkOptions;
allowRestarts?: boolean;
input?: string;
}

/**
Expand Down

0 comments on commit aaad25c

Please sign in to comment.