Skip to content

Commit

Permalink
Issue/3607 rollup watch only one config in exported array (#3620)
Browse files Browse the repository at this point in the history
* Added watch: false as a way to exclude certain configs from watch mode (#3607)

* updated types (#3607)

* updated cli documentation (#3607)

* updated big list of all options (#3607)

* fixed types (#3607)

* throws an error if no config has to be watched (#3607)

* added example config to the 999-big-list-of-options.md file (#3607)
for excluding some configs from the watch mode

* Add tests

* Polish docs

Co-authored-by: Lukas Taegert-Atkinson <lukas.taegert-atkinson@tngtech.com>
Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>
  • Loading branch information
3 people committed Jun 8, 2020
1 parent 37b876c commit 38fc513
Show file tree
Hide file tree
Showing 15 changed files with 131 additions and 25 deletions.
6 changes: 5 additions & 1 deletion cli/run/watch-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ export async function watch(command: any) {
const resetScreen = getResetScreen(configs!, isTTY);

function start(configs: MergedRollupOptions[]) {
watcher = rollup.watch(configs as any);
try {
watcher = rollup.watch(configs as any);
} catch (err) {
return handleError(err);
}

watcher.on('event', event => {
switch (event.code) {
Expand Down
2 changes: 1 addition & 1 deletion docs/01-command-line-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export default { // can be an array (for multiple inputs)
skipWrite,
exclude,
include
}
} | false
};
```

Expand Down
34 changes: 27 additions & 7 deletions docs/999-big-list-of-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,26 @@ For each key, the first number represents the elapsed time while the second repr

### Watch options

Type: `{ buildDelay?: number, chokidar?: ChokidarOptions, clearScreen?: boolean, exclude?: string, include?: string, skipWrite?: boolean } | false`<br>
Default: `{}`<br>

Specify options for watch mode or prevent this configuration from being watched. Specifying `false` is only really useful when an array of configurations is used. In that case, this configuration will not be built or rebuilt on change in watch mode, but it will be built when running Rollup regularly:

```js
// rollup.config.js
export default [
{
input: 'main.js',
output: { file: 'bundle.cjs.js', format: 'cjs' }
},
{
input: 'main.js',
watch: false,
output: { file: 'bundle.es.js', format: 'es' }
}
]
```

These options only take effect when running Rollup with the `--watch` flag, or using `rollup.watch`.

#### watch.buildDelay
Expand All @@ -1300,13 +1320,6 @@ Default: `true`

Whether to clear the screen when a rebuild is triggered.

#### watch.skipWrite
Type: `boolean`<br>
CLI: `--watch.skipWrite`/`--no-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 <files>`
Expand Down Expand Up @@ -1339,6 +1352,13 @@ export default {
};
```

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

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

### Deprecated options

☢️ These options have been deprecated and may be removed in a future Rollup version.
Expand Down
4 changes: 2 additions & 2 deletions src/rollup/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ export interface InputOptions {
shimMissingExports?: boolean;
strictDeprecations?: boolean;
treeshake?: boolean | TreeshakingOptions;
watch?: WatcherOptions;
watch?: WatcherOptions | false;
}

export interface NormalizedInputOptions {
Expand Down Expand Up @@ -740,7 +740,7 @@ export interface WatcherOptions {

export interface RollupWatchOptions extends InputOptions {
output?: OutputOptions | OutputOptions[];
watch?: WatcherOptions;
watch?: WatcherOptions | false;
}

interface TypedEventEmitter<T> {
Expand Down
5 changes: 4 additions & 1 deletion src/utils/options/mergeOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ function mergeInputOptions(
shimMissingExports: getOption('shimMissingExports'),
strictDeprecations: getOption('strictDeprecations'),
treeshake: getObjectOption(config, overrides, 'treeshake'),
watch: getObjectOption(config, overrides, 'watch')
watch: getWatch(config, overrides, 'watch')
};

warnUnknownOptions(
Expand Down Expand Up @@ -161,6 +161,9 @@ const getObjectOption = (
return configOption;
};

const getWatch = (config: GenericConfigObject, overrides: GenericConfigObject, name: string) =>
config.watch !== false && getObjectOption(config, overrides, name);

export const normalizeObjectOptionValue = (optionValue: any) => {
if (!optionValue) {
return optionValue;
Expand Down
14 changes: 13 additions & 1 deletion src/watch/watch-proxy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { EventEmitter } from 'events';
import { RollupWatcher } from '../rollup/types';
import { ensureArray } from '../utils/ensureArray';
import { errInvalidOption, error } from '../utils/error';
import { GenericConfigObject } from '../utils/options/options';
import { loadFsEvents } from './fsevents-importer';

Expand All @@ -16,8 +18,18 @@ class WatchEmitter extends EventEmitter {

export default function watch(configs: GenericConfigObject[] | GenericConfigObject): RollupWatcher {
const emitter = new WatchEmitter() as RollupWatcher;
const configArray = ensureArray(configs);
const watchConfigs = configArray.filter(config => config.watch !== false);
if (watchConfigs.length === 0) {
throw error(
errInvalidOption(
'watch',
'there must be at least one config where "watch" is not set to "false"'
)
);
}
loadFsEvents()
.then(() => import('./watch'))
.then(({ Watcher }) => new Watcher(configs, emitter));
.then(({ Watcher }) => new Watcher(watchConfigs, emitter));
return emitter;
}
8 changes: 3 additions & 5 deletions src/watch/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
RollupWatcher,
WatcherOptions
} from '../rollup/types';
import { ensureArray } from '../utils/ensureArray';
import { mergeOptions } from '../utils/options/mergeOptions';
import { GenericConfigObject } from '../utils/options/options';
import { FileWatcher } from './fileWatcher';
Expand All @@ -24,12 +23,11 @@ export class Watcher {
private running: boolean;
private tasks: Task[];

constructor(configs: GenericConfigObject[] | GenericConfigObject, emitter: RollupWatcher) {
constructor(configs: GenericConfigObject[], emitter: RollupWatcher) {
this.emitter = emitter;
emitter.close = this.close.bind(this);
const configArray = ensureArray(configs);
this.tasks = configArray.map(config => new Task(this, config));
this.buildDelay = configArray.reduce(
this.tasks = configs.map(config => new Task(this, config));
this.buildDelay = configs.reduce(
(buildDelay, { watch }: any) =>
watch && typeof watch.buildDelay === 'number'
? Math.max(buildDelay, (watch as WatcherOptions).buildDelay!)
Expand Down
13 changes: 13 additions & 0 deletions test/cli/samples/watch/no-watched-config/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { assertStderrIncludes } = require('../../../../utils.js');

module.exports = {
description: 'throws if no config is watched',
command: 'rollup -cw',
error: () => true,
stderr(stderr) {
assertStderrIncludes(
stderr,
'[!] Error: Invalid value for option "watch" - there must be at least one config where "watch" is not set to "false".'
);
}
};
1 change: 1 addition & 0 deletions test/cli/samples/watch/no-watched-config/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
assert.equal( 1 + 1, 2 );
18 changes: 18 additions & 0 deletions test/cli/samples/watch/no-watched-config/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export default [
{
input: 'main.js',
watch: false,
output: {
dir: '_actual/es',
format: 'es'
}
},
{
input: 'main.js',
watch: false,
output: {
dir: '_actual/cjs',
format: 'cjs'
}
}
];
44 changes: 44 additions & 0 deletions test/watch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,50 @@ describe('rollup.watch', () => {
});
});

it('allows watching only some configs', () => {
return sander
.copydir('test/watch/samples/multiple')
.to('test/_tmp/input')
.then(() => wait(100))
.then(() => {
watcher = rollup.watch([
{
input: 'test/_tmp/input/main1.js',
watch: false,
output: {
file: 'test/_tmp/output/bundle1.js',
format: 'cjs'
}
},
{
input: 'test/_tmp/input/main2.js',
output: {
file: 'test/_tmp/output/bundle2.js',
format: 'cjs'
}
}
]);

return sequence(watcher, [
'START',
'BUNDLE_START',
'BUNDLE_END',
'END',
() => {
assert.strictEqual(
sander.existsSync(path.resolve(__dirname, '../_tmp/output/bundle1.js')),
false
);
assert.strictEqual(
sander.existsSync(path.resolve(__dirname, '../_tmp/output/bundle2.js')),
true
);
assert.deepStrictEqual(run('../_tmp/output/bundle2.js'), 43);
}
]);
});
});

it('respects output.globals', () => {
return sander
.copydir('test/watch/samples/globals')
Expand Down
1 change: 0 additions & 1 deletion test/watch/samples/watch-files-multiple/dep.txt

This file was deleted.

1 change: 0 additions & 1 deletion test/watch/samples/watch-files-multiple/depdep.txt

This file was deleted.

1 change: 0 additions & 1 deletion test/watch/samples/watch-files-multiple/first.js

This file was deleted.

4 changes: 0 additions & 4 deletions test/watch/samples/watch-files-multiple/index.js

This file was deleted.

0 comments on commit 38fc513

Please sign in to comment.