Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(run): add input option #1699

Merged
merged 3 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading