From 3e33012d0ed5d858fc7d5ebd7badf4b38850aea6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90?= Date: Mon, 10 Oct 2022 11:41:12 +0800 Subject: [PATCH 1/6] feat: support nested plugin --- src/rollup/types.d.ts | 4 +++- src/utils/flatten.ts | 15 +++++++++++++ src/utils/options/normalizeInputOptions.ts | 3 ++- test/cli/samples/watch/bundle-error/main.js | 1 + .../function/samples/nested-plugin/_config.js | 21 +++++++++++++++++++ test/function/samples/nested-plugin/main.js | 5 +++++ 6 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 src/utils/flatten.ts create mode 100644 test/cli/samples/watch/bundle-error/main.js create mode 100644 test/function/samples/nested-plugin/_config.js create mode 100644 test/function/samples/nested-plugin/main.js diff --git a/src/rollup/types.d.ts b/src/rollup/types.d.ts index 18dcf4311a4..0142275ee37 100644 --- a/src/rollup/types.d.ts +++ b/src/rollup/types.d.ts @@ -492,6 +492,8 @@ export type SourcemapPathTransformOption = ( sourcemapPath: string ) => string; +export type InputPlugin = Plugin | null | false | undefined | InputPlugin[]; + export interface InputOptions { acorn?: Record; acornInjectPlugins?: (() => unknown)[] | (() => unknown); @@ -511,7 +513,7 @@ export interface InputOptions { moduleContext?: ((id: string) => string | null | void) | { [id: string]: string }; onwarn?: WarningHandlerWithDefault; perf?: boolean; - plugins?: (Plugin | null | false | undefined)[]; + plugins?: InputPlugin[]; preserveEntrySignatures?: PreserveEntrySignaturesOption; /** @deprecated Use the "preserveModules" output option instead. */ preserveModules?: boolean; diff --git a/src/utils/flatten.ts b/src/utils/flatten.ts new file mode 100644 index 00000000000..b2d589dfc24 --- /dev/null +++ b/src/utils/flatten.ts @@ -0,0 +1,15 @@ +export function flatten(items: any[]): any[] { + if (!items || items.length === 0) { + return items; + } + + const results: any[] = []; + for (const item of items) { + if (Array.isArray(item)) { + results.push(...flatten(item)); + } else { + results.push(item); + } + } + return results; +} diff --git a/src/utils/options/normalizeInputOptions.ts b/src/utils/options/normalizeInputOptions.ts index 7bef62ab0c3..af40fa71b57 100644 --- a/src/utils/options/normalizeInputOptions.ts +++ b/src/utils/options/normalizeInputOptions.ts @@ -10,6 +10,7 @@ import type { } from '../../rollup/types'; import { ensureArray } from '../ensureArray'; import { errInvalidOption, error, warnDeprecationWithOptions } from '../error'; +import { flatten } from '../flatten'; import { resolve } from '../path'; import relativeId from '../relativeId'; import { @@ -54,7 +55,7 @@ export function normalizeInputOptions(config: InputOptions): { moduleContext: getModuleContext(config, context), onwarn, perf: config.perf || false, - plugins: ensureArray(config.plugins), + plugins: flatten(ensureArray(config.plugins)), preserveEntrySignatures: config.preserveEntrySignatures ?? 'exports-only', preserveModules: getPreserveModules(config, onwarn, strictDeprecations), preserveSymlinks: config.preserveSymlinks || false, diff --git a/test/cli/samples/watch/bundle-error/main.js b/test/cli/samples/watch/bundle-error/main.js new file mode 100644 index 00000000000..a4012bff06c --- /dev/null +++ b/test/cli/samples/watch/bundle-error/main.js @@ -0,0 +1 @@ +export default 42; \ No newline at end of file diff --git a/test/function/samples/nested-plugin/_config.js b/test/function/samples/nested-plugin/_config.js new file mode 100644 index 00000000000..1e265e37531 --- /dev/null +++ b/test/function/samples/nested-plugin/_config.js @@ -0,0 +1,21 @@ +const plugin = [ + { + name: 'nested-plugin-1', + transform(code) { + return code.replace('foo = 1', 'foo = 2'); + } + }, + { + name: 'nested-plugin-2', + transform(code) { + return code.replace('answer = 41', 'answer = 42'); + } + } +]; + +module.exports = { + description: 'works when nested plugin', + options: { + plugins: [plugin] + } +}; diff --git a/test/function/samples/nested-plugin/main.js b/test/function/samples/nested-plugin/main.js new file mode 100644 index 00000000000..1eb0e2232b3 --- /dev/null +++ b/test/function/samples/nested-plugin/main.js @@ -0,0 +1,5 @@ +const foo = 1; +const answer = 41; + +assert.equal(foo, 2); +assert.equal(answer, 42); From 51bb174fb3d876e382a52d6d46c5011e2ee76244 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90?= Date: Wed, 12 Oct 2022 14:37:11 +0800 Subject: [PATCH 2/6] refactor --- cli/run/commandPlugins.ts | 2 +- docs/999-big-list-of-options.md | 8 ++++---- src/rollup/types.d.ts | 8 +++++--- src/utils/flatten.ts | 15 --------------- src/utils/options/normalizeInputOptions.ts | 4 ++-- src/utils/options/normalizeOutputOptions.ts | 4 ++-- test/function/samples/nested-plugin/_config.js | 3 ++- 7 files changed, 16 insertions(+), 28 deletions(-) delete mode 100644 src/utils/flatten.ts diff --git a/cli/run/commandPlugins.ts b/cli/run/commandPlugins.ts index 4fcc70f4f26..7968dc1a4ba 100644 --- a/cli/run/commandPlugins.ts +++ b/cli/run/commandPlugins.ts @@ -22,7 +22,7 @@ export async function addPluginsFromCommandOption( inputOptions: InputOptions ): Promise { if (commandPlugin) { - const plugins = Array.isArray(commandPlugin) ? commandPlugin : [commandPlugin]; + const plugins: any[] = [commandPlugin].flat(Infinity).filter(Boolean); for (const plugin of plugins) { if (/[={}]/.test(plugin)) { // -p plugin=value diff --git a/docs/999-big-list-of-options.md b/docs/999-big-list-of-options.md index f3535b2afb8..a3479ee4e8f 100755 --- a/docs/999-big-list-of-options.md +++ b/docs/999-big-list-of-options.md @@ -258,9 +258,9 @@ this.a.b.c = ... #### output.plugins -Type: `OutputPlugin | (OutputPlugin | void)[]` +Type: `OutputPlugin | (OutputPlugin | OutputPlugin[] | void)[]` -Adds a plugin just to this output. See [Using output plugins](guide/en/#using-output-plugins) for more information on how to use output-specific plugins and [Plugins](guide/en/#plugin-development) on how to write your own. For plugins imported from packages, remember to call the imported plugin function (i.e. `commonjs()`, not just `commonjs`). Falsy plugins will be ignored, which can be used to easily activate or deactivate plugins. +Adds a plugin just to this output. See [Using output plugins](guide/en/#using-output-plugins) for more information on how to use output-specific plugins and [Plugins](guide/en/#plugin-development) on how to write your own. For plugins imported from packages, remember to call the imported plugin function (i.e. `commonjs()`, not just `commonjs`). Falsy plugins will be ignored, which can be used to easily activate or deactivate plugins. Nested plugins will be flatten. Not every plugin can be used here. `output.plugins` is limited to plugins that only use hooks that run during `bundle.generate()` or `bundle.write()`, i.e. after Rollup's main analysis is complete. If you are a plugin author, see [output generation hooks](guide/en/#output-generation-hooks) to find out which hooks can be used. @@ -288,9 +288,9 @@ export default { #### plugins -Type: `Plugin | (Plugin | void)[]` +Type: `Plugin | (Plugin | Plugin[] | void)[]` -See [Using plugins](guide/en/#using-plugins) for more information on how to use plugins and [Plugins](guide/en/#plugin-development) on how to write your own (try it out, it's not as difficult as it may sound and very much extends what you can do with Rollup). For plugins imported from packages, remember to call the imported plugin function (i.e. `commonjs()`, not just `commonjs`). Falsy plugins will be ignored, which can be used to easily activate or deactivate plugins. +See [Using plugins](guide/en/#using-plugins) for more information on how to use plugins and [Plugins](guide/en/#plugin-development) on how to write your own (try it out, it's not as difficult as it may sound and very much extends what you can do with Rollup). For plugins imported from packages, remember to call the imported plugin function (i.e. `commonjs()`, not just `commonjs`). Falsy plugins will be ignored, which can be used to easily activate or deactivate plugins. Nested plugins will be flatten. ```js // rollup.config.js diff --git a/src/rollup/types.d.ts b/src/rollup/types.d.ts index 0142275ee37..0ae8263f530 100644 --- a/src/rollup/types.d.ts +++ b/src/rollup/types.d.ts @@ -492,7 +492,7 @@ export type SourcemapPathTransformOption = ( sourcemapPath: string ) => string; -export type InputPlugin = Plugin | null | false | undefined | InputPlugin[]; +export type InputPluginOption = Plugin | null | false | undefined | InputPluginOption[]; export interface InputOptions { acorn?: Record; @@ -513,7 +513,7 @@ export interface InputOptions { moduleContext?: ((id: string) => string | null | void) | { [id: string]: string }; onwarn?: WarningHandlerWithDefault; perf?: boolean; - plugins?: InputPlugin[]; + plugins?: InputPluginOption; preserveEntrySignatures?: PreserveEntrySignaturesOption; /** @deprecated Use the "preserveModules" output option instead. */ preserveModules?: boolean; @@ -612,6 +612,8 @@ export type NormalizedAmdOptions = ( type AddonFunction = (chunk: RenderedChunk) => string | Promise; +type OutputPluginOption = OutputPlugin | null | false | undefined | OutputPluginOption[]; + export interface OutputOptions { amd?: AmdOptions; assetFileNames?: string | ((chunkInfo: PreRenderedAsset) => string); @@ -649,7 +651,7 @@ export interface OutputOptions { noConflict?: boolean; outro?: string | AddonFunction; paths?: OptionsPaths; - plugins?: (OutputPlugin | null | false | undefined)[]; + plugins?: OutputPluginOption; /** @deprecated Use "generatedCode.constBindings" instead. */ preferConst?: boolean; preserveModules?: boolean; diff --git a/src/utils/flatten.ts b/src/utils/flatten.ts deleted file mode 100644 index b2d589dfc24..00000000000 --- a/src/utils/flatten.ts +++ /dev/null @@ -1,15 +0,0 @@ -export function flatten(items: any[]): any[] { - if (!items || items.length === 0) { - return items; - } - - const results: any[] = []; - for (const item of items) { - if (Array.isArray(item)) { - results.push(...flatten(item)); - } else { - results.push(item); - } - } - return results; -} diff --git a/src/utils/options/normalizeInputOptions.ts b/src/utils/options/normalizeInputOptions.ts index af40fa71b57..f6e793c4627 100644 --- a/src/utils/options/normalizeInputOptions.ts +++ b/src/utils/options/normalizeInputOptions.ts @@ -5,12 +5,12 @@ import type { InputOptions, ModuleSideEffectsOption, NormalizedInputOptions, + Plugin, RollupBuild, WarningHandler } from '../../rollup/types'; import { ensureArray } from '../ensureArray'; import { errInvalidOption, error, warnDeprecationWithOptions } from '../error'; -import { flatten } from '../flatten'; import { resolve } from '../path'; import relativeId from '../relativeId'; import { @@ -55,7 +55,7 @@ export function normalizeInputOptions(config: InputOptions): { moduleContext: getModuleContext(config, context), onwarn, perf: config.perf || false, - plugins: flatten(ensureArray(config.plugins)), + plugins: ([config.plugins] as Plugin[]).flat(Infinity).filter(Boolean), preserveEntrySignatures: config.preserveEntrySignatures ?? 'exports-only', preserveModules: getPreserveModules(config, onwarn, strictDeprecations), preserveSymlinks: config.preserveSymlinks || false, diff --git a/src/utils/options/normalizeOutputOptions.ts b/src/utils/options/normalizeOutputOptions.ts index 6451375503f..6584c187ce4 100644 --- a/src/utils/options/normalizeOutputOptions.ts +++ b/src/utils/options/normalizeOutputOptions.ts @@ -4,9 +4,9 @@ import type { NormalizedInputOptions, NormalizedOutputOptions, OutputOptions, + OutputPlugin, SourcemapPathTransformOption } from '../../rollup/types'; -import { ensureArray } from '../ensureArray'; import { errInvalidExportOptionValue, errInvalidOption, error, warnDeprecation } from '../error'; import { resolve } from '../path'; import { sanitizeFileName as defaultSanitizeFileName } from '../sanitizeFileName'; @@ -68,7 +68,7 @@ export function normalizeOutputOptions( noConflict: config.noConflict || false, outro: getAddon(config, 'outro'), paths: config.paths || {}, - plugins: ensureArray(config.plugins), + plugins: ([config.plugins] as OutputPlugin[]).flat(Infinity).filter(Boolean), preferConst, preserveModules, preserveModulesRoot: getPreserveModulesRoot(config), diff --git a/test/function/samples/nested-plugin/_config.js b/test/function/samples/nested-plugin/_config.js index 1e265e37531..6e62a4d6fda 100644 --- a/test/function/samples/nested-plugin/_config.js +++ b/test/function/samples/nested-plugin/_config.js @@ -16,6 +16,7 @@ const plugin = [ module.exports = { description: 'works when nested plugin', options: { - plugins: [plugin] + // eslint-disable-next-line no-sparse-arrays + plugins: [plugin, [undefined, [null]], ,] } }; From 13097ac9c1002683eb88e4703828a7a9f00a6f9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90?= Date: Wed, 12 Oct 2022 14:54:01 +0800 Subject: [PATCH 3/6] chore: rm --- test/cli/samples/watch/bundle-error/main.js | 1 - 1 file changed, 1 deletion(-) delete mode 100644 test/cli/samples/watch/bundle-error/main.js diff --git a/test/cli/samples/watch/bundle-error/main.js b/test/cli/samples/watch/bundle-error/main.js deleted file mode 100644 index a4012bff06c..00000000000 --- a/test/cli/samples/watch/bundle-error/main.js +++ /dev/null @@ -1 +0,0 @@ -export default 42; \ No newline at end of file From 1712c120b2a28db2ee797f3da7f90c86edc92b61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90?= Date: Wed, 12 Oct 2022 15:09:13 +0800 Subject: [PATCH 4/6] fix: types --- cli/run/commandPlugins.ts | 14 +++++++++----- src/utils/timers.ts | 9 +++++++-- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/cli/run/commandPlugins.ts b/cli/run/commandPlugins.ts index 7968dc1a4ba..582c64d2110 100644 --- a/cli/run/commandPlugins.ts +++ b/cli/run/commandPlugins.ts @@ -8,11 +8,14 @@ export async function addCommandPluginsToInputOptions( inputOptions: InputOptions, command: Record ): Promise { + if (!Array.isArray(inputOptions.plugins)) { + inputOptions.plugins = [inputOptions.plugins]; + } if (command.stdin !== false) { - inputOptions.plugins!.push(stdinPlugin(command.stdin)); + inputOptions.plugins.push(stdinPlugin(command.stdin)); } if (command.waitForBundleInput === true) { - inputOptions.plugins!.push(waitForInputPlugin()); + inputOptions.plugins.push(waitForInputPlugin()); } await addPluginsFromCommandOption(command.plugin, inputOptions); } @@ -96,9 +99,10 @@ async function loadAndRegisterPlugin( )}" for Rollup to recognize it.` ); } - inputOptions.plugins!.push( - typeof plugin === 'function' ? plugin.call(plugin, pluginArg) : plugin - ); + if (!Array.isArray(inputOptions.plugins)) { + inputOptions.plugins = [inputOptions.plugins]; + } + inputOptions.plugins.push(typeof plugin === 'function' ? plugin.call(plugin, pluginArg) : plugin); } function getCamelizedPluginBaseName(pluginText: string): string { diff --git a/src/utils/timers.ts b/src/utils/timers.ts index c5ec310fc1f..c951d1162c0 100644 --- a/src/utils/timers.ts +++ b/src/utils/timers.ts @@ -1,4 +1,9 @@ -import type { InputOptions, Plugin, PluginHooks, SerializedTimings } from '../rollup/types'; +import type { + NormalizedInputOptions, + Plugin, + PluginHooks, + SerializedTimings +} from '../rollup/types'; import performance from './performance'; import process from './process'; @@ -117,7 +122,7 @@ function getPluginWithTimers(plugin: any, index: number): Plugin { return plugin; } -export function initialiseTimers(inputOptions: InputOptions): void { +export function initialiseTimers(inputOptions: NormalizedInputOptions): void { if (inputOptions.perf) { timers = new Map(); timeStart = timeStartImpl; From 5c8baf5d93f087d342339395b2565e36f5b081e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90?= Date: Wed, 12 Oct 2022 15:26:07 +0800 Subject: [PATCH 5/6] fix: types --- cli/run/commandPlugins.ts | 14 ++++---------- src/rollup/types.d.ts | 6 +++++- src/utils/options/mergeOptions.ts | 5 +++-- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/cli/run/commandPlugins.ts b/cli/run/commandPlugins.ts index 582c64d2110..0207543239e 100644 --- a/cli/run/commandPlugins.ts +++ b/cli/run/commandPlugins.ts @@ -1,16 +1,13 @@ import { resolve } from 'node:path'; import { pathToFileURL } from 'node:url'; -import type { InputOptions } from '../../src/rollup/types'; +import type { InputOptionsWithPlugins } from '../../src/rollup/types'; import { stdinPlugin } from './stdin'; import { waitForInputPlugin } from './waitForInput'; export async function addCommandPluginsToInputOptions( - inputOptions: InputOptions, + inputOptions: InputOptionsWithPlugins, command: Record ): Promise { - if (!Array.isArray(inputOptions.plugins)) { - inputOptions.plugins = [inputOptions.plugins]; - } if (command.stdin !== false) { inputOptions.plugins.push(stdinPlugin(command.stdin)); } @@ -22,7 +19,7 @@ export async function addCommandPluginsToInputOptions( export async function addPluginsFromCommandOption( commandPlugin: unknown, - inputOptions: InputOptions + inputOptions: InputOptionsWithPlugins ): Promise { if (commandPlugin) { const plugins: any[] = [commandPlugin].flat(Infinity).filter(Boolean); @@ -43,7 +40,7 @@ export async function addPluginsFromCommandOption( } async function loadAndRegisterPlugin( - inputOptions: InputOptions, + inputOptions: InputOptionsWithPlugins, pluginText: string ): Promise { let plugin: any = null; @@ -99,9 +96,6 @@ async function loadAndRegisterPlugin( )}" for Rollup to recognize it.` ); } - if (!Array.isArray(inputOptions.plugins)) { - inputOptions.plugins = [inputOptions.plugins]; - } inputOptions.plugins.push(typeof plugin === 'function' ? plugin.call(plugin, pluginArg) : plugin); } diff --git a/src/rollup/types.d.ts b/src/rollup/types.d.ts index 0ae8263f530..ed7b60cb45c 100644 --- a/src/rollup/types.d.ts +++ b/src/rollup/types.d.ts @@ -524,6 +524,10 @@ export interface InputOptions { watch?: WatcherOptions | false; } +export interface InputOptionsWithPlugins extends InputOptions { + plugins: Plugin[]; +} + export interface NormalizedInputOptions { acorn: Record; acornInjectPlugins: (() => unknown)[]; @@ -804,7 +808,7 @@ export interface RollupOptions extends InputOptions { output?: OutputOptions | OutputOptions[]; } -export interface MergedRollupOptions extends InputOptions { +export interface MergedRollupOptions extends InputOptionsWithPlugins { output: OutputOptions[]; } diff --git a/src/utils/options/mergeOptions.ts b/src/utils/options/mergeOptions.ts index bf3ba84824b..e7284403a99 100644 --- a/src/utils/options/mergeOptions.ts +++ b/src/utils/options/mergeOptions.ts @@ -3,6 +3,7 @@ import type { InputOptions, MergedRollupOptions, OutputOptions, + Plugin, RollupCache, WarningHandler, WarningHandlerWithDefault @@ -126,7 +127,7 @@ function mergeInputOptions( moduleContext: getOption('moduleContext'), onwarn: getOnWarn(config, defaultOnWarnHandler), perf: getOption('perf'), - plugins: ensureArray(config.plugins) as Plugin[], + plugins: [config.plugins].flat().filter(Boolean) as Plugin[], preserveEntrySignatures: getOption('preserveEntrySignatures'), preserveModules: getOption('preserveModules'), preserveSymlinks: getOption('preserveSymlinks'), @@ -261,7 +262,7 @@ function mergeOutputOptions( noConflict: getOption('noConflict'), outro: getOption('outro'), paths: getOption('paths'), - plugins: ensureArray(config.plugins) as Plugin[], + plugins: [config.plugins].flat().filter(Boolean) as Plugin[], preferConst: getOption('preferConst'), preserveModules: getOption('preserveModules'), preserveModulesRoot: getOption('preserveModulesRoot'), From 15582959f9713d4413381c689dc8a25f07406e9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90?= Date: Wed, 12 Oct 2022 15:40:09 +0800 Subject: [PATCH 6/6] refactor: extract --- cli/run/commandPlugins.ts | 3 ++- src/utils/options/mergeOptions.ts | 8 +++++--- src/utils/options/normalizeInputOptions.ts | 4 ++-- src/utils/options/normalizeOutputOptions.ts | 4 ++-- src/utils/options/options.ts | 9 +++++++++ 5 files changed, 20 insertions(+), 8 deletions(-) diff --git a/cli/run/commandPlugins.ts b/cli/run/commandPlugins.ts index 0207543239e..27912931670 100644 --- a/cli/run/commandPlugins.ts +++ b/cli/run/commandPlugins.ts @@ -1,6 +1,7 @@ import { resolve } from 'node:path'; import { pathToFileURL } from 'node:url'; import type { InputOptionsWithPlugins } from '../../src/rollup/types'; +import { normalizePluginOption } from '../../src/utils/options/options'; import { stdinPlugin } from './stdin'; import { waitForInputPlugin } from './waitForInput'; @@ -22,7 +23,7 @@ export async function addPluginsFromCommandOption( inputOptions: InputOptionsWithPlugins ): Promise { if (commandPlugin) { - const plugins: any[] = [commandPlugin].flat(Infinity).filter(Boolean); + const plugins: any[] = normalizePluginOption(commandPlugin as any); for (const plugin of plugins) { if (/[={}]/.test(plugin)) { // -p plugin=value diff --git a/src/utils/options/mergeOptions.ts b/src/utils/options/mergeOptions.ts index e7284403a99..9380a9a148a 100644 --- a/src/utils/options/mergeOptions.ts +++ b/src/utils/options/mergeOptions.ts @@ -1,9 +1,10 @@ import type { ExternalOption, InputOptions, + InputPluginOption, MergedRollupOptions, OutputOptions, - Plugin, + OutputPluginOption, RollupCache, WarningHandler, WarningHandlerWithDefault @@ -14,6 +15,7 @@ import { defaultOnWarn, generatedCodePresets, type GenericConfigObject, + normalizePluginOption, objectifyOption, objectifyOptionWithPresets, treeshakePresets, @@ -127,7 +129,7 @@ function mergeInputOptions( moduleContext: getOption('moduleContext'), onwarn: getOnWarn(config, defaultOnWarnHandler), perf: getOption('perf'), - plugins: [config.plugins].flat().filter(Boolean) as Plugin[], + plugins: normalizePluginOption(config.plugins as InputPluginOption), preserveEntrySignatures: getOption('preserveEntrySignatures'), preserveModules: getOption('preserveModules'), preserveSymlinks: getOption('preserveSymlinks'), @@ -262,7 +264,7 @@ function mergeOutputOptions( noConflict: getOption('noConflict'), outro: getOption('outro'), paths: getOption('paths'), - plugins: [config.plugins].flat().filter(Boolean) as Plugin[], + plugins: normalizePluginOption(config.plugins as OutputPluginOption), preferConst: getOption('preferConst'), preserveModules: getOption('preserveModules'), preserveModulesRoot: getOption('preserveModulesRoot'), diff --git a/src/utils/options/normalizeInputOptions.ts b/src/utils/options/normalizeInputOptions.ts index f6e793c4627..9d77a018ae4 100644 --- a/src/utils/options/normalizeInputOptions.ts +++ b/src/utils/options/normalizeInputOptions.ts @@ -5,7 +5,6 @@ import type { InputOptions, ModuleSideEffectsOption, NormalizedInputOptions, - Plugin, RollupBuild, WarningHandler } from '../../rollup/types'; @@ -17,6 +16,7 @@ import { defaultOnWarn, type GenericConfigObject, getOptionWithPreset, + normalizePluginOption, treeshakePresets, warnUnknownOptions } from './options'; @@ -55,7 +55,7 @@ export function normalizeInputOptions(config: InputOptions): { moduleContext: getModuleContext(config, context), onwarn, perf: config.perf || false, - plugins: ([config.plugins] as Plugin[]).flat(Infinity).filter(Boolean), + plugins: normalizePluginOption(config.plugins), preserveEntrySignatures: config.preserveEntrySignatures ?? 'exports-only', preserveModules: getPreserveModules(config, onwarn, strictDeprecations), preserveSymlinks: config.preserveSymlinks || false, diff --git a/src/utils/options/normalizeOutputOptions.ts b/src/utils/options/normalizeOutputOptions.ts index 6584c187ce4..dd562c2ba29 100644 --- a/src/utils/options/normalizeOutputOptions.ts +++ b/src/utils/options/normalizeOutputOptions.ts @@ -4,7 +4,6 @@ import type { NormalizedInputOptions, NormalizedOutputOptions, OutputOptions, - OutputPlugin, SourcemapPathTransformOption } from '../../rollup/types'; import { errInvalidExportOptionValue, errInvalidOption, error, warnDeprecation } from '../error'; @@ -15,6 +14,7 @@ import { generatedCodePresets, type GenericConfigObject, getOptionWithPreset, + normalizePluginOption, warnUnknownOptions } from './options'; @@ -68,7 +68,7 @@ export function normalizeOutputOptions( noConflict: config.noConflict || false, outro: getAddon(config, 'outro'), paths: config.paths || {}, - plugins: ([config.plugins] as OutputPlugin[]).flat(Infinity).filter(Boolean), + plugins: normalizePluginOption(config.plugins), preferConst, preserveModules, preserveModulesRoot: getPreserveModulesRoot(config), diff --git a/src/utils/options/options.ts b/src/utils/options/options.ts index 1349b5f549c..2b19273bea5 100644 --- a/src/utils/options/options.ts +++ b/src/utils/options/options.ts @@ -1,9 +1,13 @@ import type { InputOptions, + InputPluginOption, NormalizedGeneratedCodeOptions, NormalizedOutputOptions, NormalizedTreeshakingOptions, OutputOptions, + OutputPlugin, + OutputPluginOption, + Plugin, WarningHandler } from '../../rollup/types'; import { errInvalidOption, error, errUnknownOption } from '../error'; @@ -145,3 +149,8 @@ export const getOptionWithPreset = ( const getHashFromObjectOption = (optionName: string): string => optionName.split('.').join('').toLowerCase(); + +export const normalizePluginOption: { + (plugins: InputPluginOption): Plugin[]; + (plugins: OutputPluginOption): OutputPlugin[]; +} = (plugins: any) => [plugins].flat(Infinity).filter(Boolean);