From 6a0bfb64a843ddc28f6b2e8d4d719df1eec40f36 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Fri, 31 Dec 2021 08:12:45 +0100 Subject: [PATCH] Do not watch unless --watch is specified explicitly --- cli/run/index.ts | 3 ++- src/utils/options/mergeOptions.ts | 20 ++++++++++++++----- src/utils/options/options.ts | 5 ++++- .../watch/no-watch-by-default/_config.js | 12 +++++++++++ .../samples/watch/no-watch-by-default/main.js | 1 + .../no-watch-by-default/rollup.config.js | 9 +++++++++ .../watch/no-watch-by-default/wrapper.js | 5 +++++ 7 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 test/cli/samples/watch/no-watch-by-default/_config.js create mode 100644 test/cli/samples/watch/no-watch-by-default/main.js create mode 100644 test/cli/samples/watch/no-watch-by-default/rollup.config.js create mode 100755 test/cli/samples/watch/no-watch-by-default/wrapper.js diff --git a/cli/run/index.ts b/cli/run/index.ts index 6ff743c79f8..ed4478fa6d4 100644 --- a/cli/run/index.ts +++ b/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'; @@ -56,7 +57,7 @@ export default async function runRollup(command: Record): Promise Record | undefined = value => - (typeof value === 'object' ? value : {}) as Record | undefined + objectifyValue = objectifyOption ) => { const commandOption = normalizeObjectOptionValue(overrides[name], objectifyValue); const configOption = normalizeObjectOptionValue(config[name], objectifyValue); @@ -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, diff --git a/src/utils/options/options.ts b/src/utils/options/options.ts index d6555064c63..cfcf75b9306 100644 --- a/src/utils/options/options.ts +++ b/src/utils/options/options.ts @@ -94,6 +94,9 @@ type ObjectOptionWithPresets = | Partial | Partial; +export const objectifyOption = (value: unknown): Record => + value && typeof value === 'object' ? (value as Record) : {}; + export const objectifyOptionWithPresets = ( presets: Record, @@ -117,7 +120,7 @@ export const objectifyOptionWithPresets = ) ); } - return value && typeof value === 'object' ? (value as Record) : {}; + return objectifyOption(value); }; export const getOptionWithPreset = ( diff --git a/test/cli/samples/watch/no-watch-by-default/_config.js b/test/cli/samples/watch/no-watch-by-default/_config.js new file mode 100644 index 00000000000..b43ee5fbb19 --- /dev/null +++ b/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'); + } + } +}; diff --git a/test/cli/samples/watch/no-watch-by-default/main.js b/test/cli/samples/watch/no-watch-by-default/main.js new file mode 100644 index 00000000000..7a4e8a723a4 --- /dev/null +++ b/test/cli/samples/watch/no-watch-by-default/main.js @@ -0,0 +1 @@ +export default 42; diff --git a/test/cli/samples/watch/no-watch-by-default/rollup.config.js b/test/cli/samples/watch/no-watch-by-default/rollup.config.js new file mode 100644 index 00000000000..161653a6b43 --- /dev/null +++ b/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', + }, + } +]; diff --git a/test/cli/samples/watch/no-watch-by-default/wrapper.js b/test/cli/samples/watch/no-watch-by-default/wrapper.js new file mode 100755 index 00000000000..8f733cc8815 --- /dev/null +++ b/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');