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

Do not watch unless --watch is specified explicitly #4335

Merged
merged 1 commit into from Jan 4, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion cli/run/index.ts
@@ -1,4 +1,5 @@
import { MergedRollupOptions } from '../../src/rollup/types';
import { isWatchEnabled } from '../../src/utils/options/mergeOptions';
import { getAliasName } from '../../src/utils/relativeId';
import { loadFsEvents } from '../../src/watch/fsevents-importer';
import { handleError } from '../logging';
Expand Down Expand Up @@ -56,7 +57,7 @@ export default async function runRollup(command: Record<string, any>): Promise<v
});
}

if (command.watch) {
if (isWatchEnabled(command.watch)) {
await loadFsEvents();
const { watch } = await import('./watch-cli');
watch(command);
Expand Down
20 changes: 15 additions & 5 deletions src/utils/options/mergeOptions.ts
Expand Up @@ -13,6 +13,7 @@ import {
defaultOnWarn,
generatedCodePresets,
GenericConfigObject,
objectifyOption,
objectifyOptionWithPresets,
treeshakePresets,
warnUnknownOptions
Expand Down Expand Up @@ -135,7 +136,7 @@ function mergeInputOptions(
'treeshake',
objectifyOptionWithPresets(treeshakePresets, 'treeshake', 'false, true, ')
),
watch: getWatch(config, overrides, 'watch')
watch: getWatch(config, overrides)
};

warnUnknownOptions(
Expand Down Expand Up @@ -171,8 +172,7 @@ const getObjectOption = (
config: GenericConfigObject,
overrides: GenericConfigObject,
name: string,
objectifyValue: (value: unknown) => Record<string, unknown> | undefined = value =>
(typeof value === 'object' ? value : {}) as Record<string, unknown> | undefined
objectifyValue = objectifyOption
) => {
const commandOption = normalizeObjectOptionValue(overrides[name], objectifyValue);
const configOption = normalizeObjectOptionValue(config[name], objectifyValue);
Expand All @@ -182,8 +182,18 @@ const getObjectOption = (
return configOption;
};

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

export const isWatchEnabled = (optionValue: unknown): boolean => {
if (Array.isArray(optionValue)) {
return optionValue.reduce(
(result, value) => (typeof value === 'boolean' ? value : result),
false
);
}
return optionValue === true;
};

export const normalizeObjectOptionValue = (
optionValue: unknown,
Expand Down
5 changes: 4 additions & 1 deletion src/utils/options/options.ts
Expand Up @@ -94,6 +94,9 @@ type ObjectOptionWithPresets =
| Partial<NormalizedTreeshakingOptions>
| Partial<NormalizedGeneratedCodeOptions>;

export const objectifyOption = (value: unknown): Record<string, unknown> =>
value && typeof value === 'object' ? (value as Record<string, unknown>) : {};

export const objectifyOptionWithPresets =
<T extends ObjectOptionWithPresets>(
presets: Record<string, T>,
Expand All @@ -117,7 +120,7 @@ export const objectifyOptionWithPresets =
)
);
}
return value && typeof value === 'object' ? (value as Record<string, unknown>) : {};
return objectifyOption(value);
};

export const getOptionWithPreset = <T extends ObjectOptionWithPresets>(
Expand Down
12 changes: 12 additions & 0 deletions test/cli/samples/watch/no-watch-by-default/_config.js
@@ -0,0 +1,12 @@
const { assertIncludes } = require('../../../../utils');

module.exports = {
description: 'does not watch if --watch is missing',
command: 'node wrapper.js -c --no-watch.clearScreen',
stderr: stderr => assertIncludes(stderr, 'main.js → _actual.js...\ncreated _actual.js in'),
abortOnStderr(data) {
if (data.includes('waiting for changes')) {
throw new Error('Watch initiated');
}
}
};
1 change: 1 addition & 0 deletions test/cli/samples/watch/no-watch-by-default/main.js
@@ -0,0 +1 @@
export default 42;
9 changes: 9 additions & 0 deletions test/cli/samples/watch/no-watch-by-default/rollup.config.js
@@ -0,0 +1,9 @@
export default [
{
input: 'main.js',
output: {
file: '_actual.js',
format: 'es',
},
}
];
5 changes: 5 additions & 0 deletions test/cli/samples/watch/no-watch-by-default/wrapper.js
@@ -0,0 +1,5 @@
#!/usr/bin/env node

process.stdout.isTTY = true;
process.stderr.isTTY = true;
require('../../../../../dist/bin/rollup');