Skip to content

Commit

Permalink
feat(watch): add skipWrite option (#3454)
Browse files Browse the repository at this point in the history
* feat(watch): add `skipWrite` option;

- Closes #3225

* chore(docs): add `skipWrite` doc mentions

* chore: add `skipWrite` test

Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>
  • Loading branch information
lukeed and lukastaegert committed Mar 24, 2020
1 parent e9ece1d commit 485700e
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 12 deletions.
13 changes: 7 additions & 6 deletions docs/01-command-line-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export default { // can be an array (for multiple inputs)
watch: {
chokidar,
clearScreen,
skipWrite,
exclude,
include
}
Expand Down Expand Up @@ -272,20 +273,20 @@ Print the help document.
Use the specified plugin. There are several ways to specify plugins here:

- Via a relative path:

```
rollup -i input.js -f es -p ./my-plugin.js
```

The file should export a plugin object or a function returning such an object.
- Via the name of a plugin that is installed in a local or global `node_modules` folder:

```
rollup -i input.js -f es -p @rollup/plugin-node-resolve
```

If the plugin name does not start with `rollup-plugin-` or `@rollup/plugin-`, Rollup will automatically try adding these prefixes:

```
rollup -i input.js -f es -p node-resolve
```
Expand All @@ -295,7 +296,7 @@ Use the specified plugin. There are several ways to specify plugins here:
```
rollup -i input.js -f es -p '{transform: (c, i) => `/* ${JSON.stringify(i)} */\n${c}`}'
```

If you want to load more than one plugin, you can repeat the option or supply a comma-separated list of names:

```
Expand Down
1 change: 1 addition & 0 deletions docs/02-javascript-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ const watchOptions = {
watch: {
chokidar,
clearScreen,
skipWrite,
exclude,
include
}
Expand Down
17 changes: 12 additions & 5 deletions docs/999-big-list-of-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,10 @@ export default {
onwarn (warning, warn) {
// skip certain warnings
if (warning.code === 'UNUSED_EXTERNAL_IMPORT') return;

// throw on others
if (warning.code === 'NON_EXISTENT_EXPORT') throw new Error(warning.message);

// Use default for everything else
warn(warning);
}
Expand Down Expand Up @@ -754,7 +754,7 @@ Type: `boolean`<br>
CLI: `--externalLiveBindings`/`--no-externalLiveBindings`<br>
Default: `true`

When set to `false`, Rollup will not generate code to support live bindings for external imports but instead assume that exports do not change over time. This will enable Rollup to generate more optimized code. Note that this can cause issues when there are circular dependencies involving an external dependency.
When set to `false`, Rollup will not generate code to support live bindings for external imports but instead assume that exports do not change over time. This will enable Rollup to generate more optimized code. Note that this can cause issues when there are circular dependencies involving an external dependency.

This will avoid most cases where Rollup generates getters in the code and can therefore be used to make code IE8 compatible in many cases.

Expand Down Expand Up @@ -1005,11 +1005,11 @@ function test(callback) {
// calls to otherwise side-effect-free global functions are retained
// inside try-statements for tryCatchDeoptimization: true
Object.create(null);

// calls to other function are retained as well but the body of this
// function may again be subject to tree-shaking
otherFn();

// if a parameter is called, then all arguments passed to that function
// parameter will be deoptimized
callback();
Expand Down Expand Up @@ -1096,6 +1096,13 @@ Default: `true`

Whether to clear the screen when a rebuild is triggered.

#### watch.skipWrite
Type: `boolean`<br>
<!-- CLI: `--watch.skipWrite`<br> -->
Default: `false`

Whether to skip the `bundle.write()` step when a rebuild is triggered.

#### watch.exclude
Type: `string`<br>
CLI: `--watch.exclude <excludedPattern>`
Expand Down
1 change: 1 addition & 0 deletions src/rollup/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ export interface WatcherOptions {
clearScreen?: boolean;
exclude?: string[];
include?: string[];
skipWrite?: boolean;
}

export interface RollupWatchOptions extends InputOptions {
Expand Down
4 changes: 3 additions & 1 deletion src/watch/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export class Task {
private invalidated = true;
private outputFiles: string[];
private outputs: OutputOptions[];
private skipWrite: boolean;
private watched: Set<string>;
private watcher: Watcher;

Expand All @@ -117,6 +118,7 @@ export class Task {
this.closed = false;
this.watched = new Set();

this.skipWrite = config.watch && !!(config.watch as GenericConfigObject).skipWrite;
const { inputOptions, outputOptions } = mergeOptions(config);
this.inputOptions = inputOptions;
this.outputs = outputOptions;
Expand Down Expand Up @@ -174,7 +176,7 @@ export class Task {
return;
}
this.updateWatchedFiles(result);
await Promise.all(this.outputs.map(output => result.write(output)));
this.skipWrite || (await Promise.all(this.outputs.map(output => result.write(output))));
this.watcher.emit('event', {
code: 'BUNDLE_END',
duration: Date.now() - start,
Expand Down
65 changes: 65 additions & 0 deletions test/watch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,71 @@ describe('rollup.watch', () => {
]);
});

it('skips filesystem writes when configured', () => {
let watchChangeCnt = 0;
return sander
.copydir('test/watch/samples/skip-writes')
.to('test/_tmp/input')
.then(() => {
watcher = rollup.watch({
input: 'test/_tmp/input/main.js',
output: {
file: 'test/_tmp/output/bundle.js',
format: 'cjs'
},
watch: {
skipWrite: true
},
plugins: {
watchChange(id) {
watchChangeCnt++;
assert.strictEqual(id, path.resolve('test/_tmp/input/main.js'));
}
}
});

return sequence(watcher, [
'START',
'BUNDLE_START',
'BUNDLE_END',
'END',
() => {
assert.strictEqual(sander.existsSync('../_tmp/output/bundle.js'), false);
assert.strictEqual(watchChangeCnt, 0);
sander.writeFileSync('test/_tmp/input/main.js', 'export default 43;');
},
'START',
'BUNDLE_START',
'BUNDLE_END',
'END',
() => {
assert.strictEqual(sander.existsSync('../_tmp/output/bundle.js'), false);
assert.strictEqual(watchChangeCnt, 1);
sander.writeFileSync('test/_tmp/input/main.js', 'export default 43;');
},
'START',
'BUNDLE_START',
'BUNDLE_END',
'END',
() => {
assert.strictEqual(sander.existsSync('../_tmp/output/bundle.js'), false);
assert.strictEqual(watchChangeCnt, 2);
sander.writeFileSync('test/_tmp/input/main.js', 'export default 43;');
},
'START',
'BUNDLE_START',
'BUNDLE_END',
// 'END',
evt => {
assert.strictEqual(sander.existsSync('../_tmp/output/bundle.js'), false);
assert.strictEqual(watchChangeCnt, 3);
// still aware of its output destination
assert.strictEqual(evt.output[0], path.resolve('test/_tmp/output/bundle.js'));
}
]);
});
});

describe('addWatchFile', () => {
it('supports adding additional watch files in plugin hooks', () => {
const watchChangeIds = [];
Expand Down
1 change: 1 addition & 0 deletions test/watch/samples/skip-writes/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 42;

0 comments on commit 485700e

Please sign in to comment.