Skip to content

Commit ba7fc53

Browse files
authored
Do not watch unless --watch is specified explicitly (#4335)
1 parent 7f55571 commit ba7fc53

7 files changed

Lines changed: 48 additions & 7 deletions

File tree

cli/run/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { MergedRollupOptions } from '../../src/rollup/types';
2+
import { isWatchEnabled } from '../../src/utils/options/mergeOptions';
23
import { getAliasName } from '../../src/utils/relativeId';
34
import { loadFsEvents } from '../../src/watch/fsevents-importer';
45
import { handleError } from '../logging';
@@ -56,7 +57,7 @@ export default async function runRollup(command: Record<string, any>): Promise<v
5657
});
5758
}
5859

59-
if (command.watch) {
60+
if (isWatchEnabled(command.watch)) {
6061
await loadFsEvents();
6162
const { watch } = await import('./watch-cli');
6263
watch(command);

src/utils/options/mergeOptions.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
defaultOnWarn,
1414
generatedCodePresets,
1515
GenericConfigObject,
16+
objectifyOption,
1617
objectifyOptionWithPresets,
1718
treeshakePresets,
1819
warnUnknownOptions
@@ -135,7 +136,7 @@ function mergeInputOptions(
135136
'treeshake',
136137
objectifyOptionWithPresets(treeshakePresets, 'treeshake', 'false, true, ')
137138
),
138-
watch: getWatch(config, overrides, 'watch')
139+
watch: getWatch(config, overrides)
139140
};
140141

141142
warnUnknownOptions(
@@ -171,8 +172,7 @@ const getObjectOption = (
171172
config: GenericConfigObject,
172173
overrides: GenericConfigObject,
173174
name: string,
174-
objectifyValue: (value: unknown) => Record<string, unknown> | undefined = value =>
175-
(typeof value === 'object' ? value : {}) as Record<string, unknown> | undefined
175+
objectifyValue = objectifyOption
176176
) => {
177177
const commandOption = normalizeObjectOptionValue(overrides[name], objectifyValue);
178178
const configOption = normalizeObjectOptionValue(config[name], objectifyValue);
@@ -182,8 +182,18 @@ const getObjectOption = (
182182
return configOption;
183183
};
184184

185-
const getWatch = (config: GenericConfigObject, overrides: GenericConfigObject, name: string) =>
186-
config.watch !== false && getObjectOption(config, overrides, name);
185+
export const getWatch = (config: GenericConfigObject, overrides: GenericConfigObject) =>
186+
config.watch !== false && getObjectOption(config, overrides, 'watch');
187+
188+
export const isWatchEnabled = (optionValue: unknown): boolean => {
189+
if (Array.isArray(optionValue)) {
190+
return optionValue.reduce(
191+
(result, value) => (typeof value === 'boolean' ? value : result),
192+
false
193+
);
194+
}
195+
return optionValue === true;
196+
};
187197

188198
export const normalizeObjectOptionValue = (
189199
optionValue: unknown,

src/utils/options/options.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ type ObjectOptionWithPresets =
9494
| Partial<NormalizedTreeshakingOptions>
9595
| Partial<NormalizedGeneratedCodeOptions>;
9696

97+
export const objectifyOption = (value: unknown): Record<string, unknown> =>
98+
value && typeof value === 'object' ? (value as Record<string, unknown>) : {};
99+
97100
export const objectifyOptionWithPresets =
98101
<T extends ObjectOptionWithPresets>(
99102
presets: Record<string, T>,
@@ -117,7 +120,7 @@ export const objectifyOptionWithPresets =
117120
)
118121
);
119122
}
120-
return value && typeof value === 'object' ? (value as Record<string, unknown>) : {};
123+
return objectifyOption(value);
121124
};
122125

123126
export const getOptionWithPreset = <T extends ObjectOptionWithPresets>(
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const { assertIncludes } = require('../../../../utils');
2+
3+
module.exports = {
4+
description: 'does not watch if --watch is missing',
5+
command: 'node wrapper.js -c --no-watch.clearScreen',
6+
stderr: stderr => assertIncludes(stderr, 'main.js → _actual.js...\ncreated _actual.js in'),
7+
abortOnStderr(data) {
8+
if (data.includes('waiting for changes')) {
9+
throw new Error('Watch initiated');
10+
}
11+
}
12+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 42;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export default [
2+
{
3+
input: 'main.js',
4+
output: {
5+
file: '_actual.js',
6+
format: 'es',
7+
},
8+
}
9+
];
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env node
2+
3+
process.stdout.isTTY = true;
4+
process.stderr.isTTY = true;
5+
require('../../../../../dist/bin/rollup');

0 commit comments

Comments
 (0)