Skip to content

Commit

Permalink
feat(run): add input option (rollup#1699)
Browse files Browse the repository at this point in the history
* chore(run): add test for multiple entry points

* refactor(run): extract reusable consts from a test

* feat(run): add `input` option
  • Loading branch information
mkantor authored and younggglcy committed Jun 6, 2024
1 parent c28a65d commit 1f2b9f6
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 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
51 changes: 48 additions & 3 deletions packages/run/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ const sinon = require('sinon');
const run = require('../');

const cwd = join(__dirname, 'fixtures/');
const file = join(cwd, 'output/bundle.js');
const outputDir = join(cwd, 'output');
const file = join(outputDir, 'bundle.js');
const input = join(cwd, 'input.js');

process.chdir(cwd);

const outputOptions = { file, format: 'cjs' };
const outputDirOptions = { dir: outputDir, format: 'cjs' };

let mockChildProcess;
test.before(() => {
Expand Down Expand Up @@ -62,8 +64,7 @@ test('checks entry point facade module', async (t) => {
preserveEntrySignatures: 'strict',
plugins: [run()]
});
const outputDir = join(cwd, 'output');
await bundle.write({ dir: outputDir, format: 'cjs' });
await bundle.write(outputDirOptions);
t.true(mockChildProcess.calledWithExactly(join(outputDir, 'index.js'), [], {}));
});

Expand Down Expand Up @@ -97,6 +98,40 @@ 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(
async () => {
await rollup({
input: [input, testInput],
plugins: [run()]
});
},
{
instanceOf: Error,
message:
'@rollup/plugin-run must have a single entry point; consider setting the `input` option'
}
);
});

test('detects changes - forks a new child process and kills older process', async (t) => {
// eslint-disable-next-line no-shadow
const testInput = join(cwd, 'change-detect-input.js');
Expand All @@ -120,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 1f2b9f6

Please sign in to comment.