Skip to content

Commit

Permalink
feat(run): add allowRestarts option (#820)
Browse files Browse the repository at this point in the history
* feat(run): add `allowRestarts` option that enables manual bundle restarts by entering sequences on the terminal

* minor: change `forkBundle` function to an arrow function

* minor: clarify `stdin` behavior in `README.md` when `allowRestarts` is `true`
  • Loading branch information
nicholas-ochoa authored and guybedford committed Apr 30, 2021
1 parent 95ffe66 commit a90583f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
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

0 comments on commit a90583f

Please sign in to comment.