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 allowRestarts option #820

Merged
merged 3 commits into from
Apr 22, 2021
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
21 changes: 19 additions & 2 deletions packages/run/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export default {
input: 'src/index.js',
output: {
file: 'dist/index.js',
format: 'cjs'
format: 'cjs',
},
plugins: [run()]
plugins: [run()],
};
```

Expand Down Expand Up @@ -72,6 +72,23 @@ export default {
};
```

### `allowRestarts`

Type: `Boolean`<br>
Default: `false`

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

#### `stdin` Input Actions

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

- `restart` → Kills the currently running bundle and starts it again. _Note: This does not create a new bundle, the bundle is run again "as-is". This can be used to test configuration changes or other changes that are made without modifying your source_
Also allowed: `rs`, `CTRL+K`

- `clear` → Clears the screen of all text
Also allowed: `cls`, `CTRL+L`

## Practical Example

The feature is usually intended for development use, you may prefer to only include it when Rollup is being run in watch mode:
Expand Down
25 changes: 23 additions & 2 deletions packages/run/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ export default function run(opts: RollupRunOptions = {}): Plugin {
let proc: ChildProcess;

const args = opts.args || [];
const allowRestarts = opts.allowRestarts || false;
const forkOptions = opts.options || opts;
delete (forkOptions as RollupRunOptions).args;
delete (forkOptions as RollupRunOptions).allowRestarts;

return {
name: 'run',
Expand Down Expand Up @@ -41,15 +43,34 @@ export default function run(opts: RollupRunOptions = {}): Plugin {
},

writeBundle(outputOptions, bundle) {
const forkBundle = (dir: string, entryFileName: string) => {
if (proc) proc.kill();
proc = fork(path.join(dir, entryFileName), args, forkOptions);
};

const dir = outputOptions.dir || path.dirname(outputOptions.file!);
const entryFileName = Object.keys(bundle).find((fileName) => {
const chunk = bundle[fileName] as RenderedChunk;
return chunk.isEntry && chunk.facadeModuleId === input;
});

if (entryFileName) {
if (proc) proc.kill();
proc = fork(path.join(dir, entryFileName), args, forkOptions);
forkBundle(dir, entryFileName);

if (allowRestarts) {
process.stdin.resume();
process.stdin.setEncoding('utf8');

process.stdin.on('data', (data) => {
const line = data.toString().trim().toLowerCase();

if (line === 'rs' || line === 'restart' || data.toString().charCodeAt(0) === 11) {
forkBundle(dir, entryFileName);
} else if (line === 'cls' || line === 'clear' || data.toString().charCodeAt(0) === 12) {
console.clear();
}
});
}
} else {
this.error(`@rollup/plugin-run could not find output chunk`);
}
Expand Down
9 changes: 9 additions & 0 deletions packages/run/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ test('detects changes - forks a new child process and kills older process', asyn
t.is(mockChildProcess().kill.callCount, 1);
});

test('allow the allowRestart option', async (t) => {
const bundle = await rollup({
input,
plugins: [run({ allowRestarts: true })]
});
await bundle.write(outputOptions);
t.true(mockChildProcess.calledWithExactly(outputOptions.file, [], {}));
});

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 @@ -5,6 +5,7 @@ import { Plugin } from 'rollup';
export interface RollupRunOptions extends ForkOptions {
args?: readonly string[];
options?: ForkOptions;
allowRestarts?: boolean;
}

/**
Expand Down