Skip to content

Commit 03a167b

Browse files
authored
feat(run): add input option (#1699)
* chore(run): add test for multiple entry points * refactor(run): extract reusable consts from a test * feat(run): add `input` option
1 parent 3c28d2c commit 03a167b

File tree

4 files changed

+61
-5
lines changed

4 files changed

+61
-5
lines changed

packages/run/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ Default: `false`
8383

8484
If `true`, instructs the plugin to listen to `stdin` for the sequences listed below followed by enter (carriage return).
8585

86+
### `input`
87+
88+
Type: `String`<br>
89+
Default: `null`
90+
91+
Specifies the entry point this plugin will use. Not necessary if you only have a single entry point.
92+
8693
#### `stdin` Input Actions
8794

8895
When this option is enabled, `stdin` will listen for the following input and perform the associated action:

packages/run/src/index.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default function run(opts: RollupRunOptions = {}): Plugin {
1212

1313
const args = opts.args || [];
1414
const allowRestarts = opts.allowRestarts || false;
15+
const overrideInput = opts.input;
1516
const forkOptions = opts.options || opts;
1617
delete (forkOptions as RollupRunOptions).args;
1718
delete (forkOptions as RollupRunOptions).allowRestarts;
@@ -20,7 +21,7 @@ export default function run(opts: RollupRunOptions = {}): Plugin {
2021
name: 'run',
2122

2223
buildStart(options) {
23-
let inputs = options.input;
24+
let inputs = overrideInput ?? options.input;
2425

2526
if (typeof inputs === 'string') {
2627
inputs = [inputs];
@@ -31,7 +32,9 @@ export default function run(opts: RollupRunOptions = {}): Plugin {
3132
}
3233

3334
if (inputs.length > 1) {
34-
throw new Error(`@rollup/plugin-run only works with a single entry point`);
35+
throw new Error(
36+
`@rollup/plugin-run must have a single entry point; consider setting the \`input\` option`
37+
);
3538
}
3639

3740
input = resolve(inputs[0]);

packages/run/test/test.js

+48-3
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ const sinon = require('sinon');
1515
const run = require('../');
1616

1717
const cwd = join(__dirname, 'fixtures/');
18-
const file = join(cwd, 'output/bundle.js');
18+
const outputDir = join(cwd, 'output');
19+
const file = join(outputDir, 'bundle.js');
1920
const input = join(cwd, 'input.js');
2021

2122
process.chdir(cwd);
2223

2324
const outputOptions = { file, format: 'cjs' };
25+
const outputDirOptions = { dir: outputDir, format: 'cjs' };
2426

2527
let mockChildProcess;
2628
test.before(() => {
@@ -62,8 +64,7 @@ test('checks entry point facade module', async (t) => {
6264
preserveEntrySignatures: 'strict',
6365
plugins: [run()]
6466
});
65-
const outputDir = join(cwd, 'output');
66-
await bundle.write({ dir: outputDir, format: 'cjs' });
67+
await bundle.write(outputDirOptions);
6768
t.true(mockChildProcess.calledWithExactly(join(outputDir, 'index.js'), [], {}));
6869
});
6970

@@ -97,6 +98,40 @@ test('throws an error when bundle is not written to disk', async (t) => {
9798
);
9899
});
99100

101+
test('throws an error when input option is invalid', async (t) => {
102+
const testInput = join(cwd, 'change-detect-input.js');
103+
const bundle = await rollup({
104+
input: [input, testInput],
105+
plugins: [run({ input: 'something that is not an input' })]
106+
});
107+
await t.throwsAsync(
108+
async () => {
109+
await bundle.write(outputDirOptions);
110+
},
111+
{
112+
instanceOf: Error,
113+
message: '@rollup/plugin-run could not find output chunk'
114+
}
115+
);
116+
});
117+
118+
test('throws an error when there are multiple entry points', async (t) => {
119+
const testInput = join(cwd, 'change-detect-input.js');
120+
await t.throwsAsync(
121+
async () => {
122+
await rollup({
123+
input: [input, testInput],
124+
plugins: [run()]
125+
});
126+
},
127+
{
128+
instanceOf: Error,
129+
message:
130+
'@rollup/plugin-run must have a single entry point; consider setting the `input` option'
131+
}
132+
);
133+
});
134+
100135
test('detects changes - forks a new child process and kills older process', async (t) => {
101136
// eslint-disable-next-line no-shadow
102137
const testInput = join(cwd, 'change-detect-input.js');
@@ -120,6 +155,16 @@ test('allow the allowRestart option', async (t) => {
120155
t.true(mockChildProcess.calledWithExactly(outputOptions.file, [], {}));
121156
});
122157

158+
test('allow the input option', async (t) => {
159+
const testInput = join(cwd, 'change-detect-input.js');
160+
const bundle = await rollup({
161+
input: [input, testInput],
162+
plugins: [run({ input })]
163+
});
164+
await bundle.write(outputDirOptions);
165+
t.true(mockChildProcess.calledWithExactly(join(outputDir, 'input.js'), [], { input }));
166+
});
167+
123168
test.after(async () => {
124169
await del(['output']);
125170
});

packages/run/types/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface RollupRunOptions extends ForkOptions {
66
args?: readonly string[];
77
options?: ForkOptions;
88
allowRestarts?: boolean;
9+
input?: string;
910
}
1011

1112
/**

0 commit comments

Comments
 (0)