From 018e5e90a96350765ad6826cefe725381c296627 Mon Sep 17 00:00:00 2001 From: Kamontat Chantrachirathumrong <14089557+kamontat@users.noreply.github.com> Date: Mon, 15 Jan 2024 10:00:27 +0700 Subject: [PATCH] feat(rspack-config): support modify plugins on config wrapper --- packages/rspack-config/src/wrapper.ts | 29 ++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/packages/rspack-config/src/wrapper.ts b/packages/rspack-config/src/wrapper.ts index 9b7a930d..f4597769 100644 --- a/packages/rspack-config/src/wrapper.ts +++ b/packages/rspack-config/src/wrapper.ts @@ -1,9 +1,17 @@ -import type { Configuration } from "@rspack/cli"; +import type { + Configuration, + RspackPluginFunction, + RspackPluginInstance, +} from "@rspack/core"; + +type Condition = () => boolean; type Entry = Exclude; type EntryRecord = Exclude; type EntryRecordValue = EntryRecord[string]; -type EntryCondition = () => boolean; + +type Plugin = RspackPluginInstance | RspackPluginFunction; +type Plugins = Exclude; export class ConfigWrapper { constructor(private base: Configuration) {} @@ -20,13 +28,13 @@ export class ConfigWrapper { return new ConfigWrapper({ ...this.base, ...config }); } - setEntry(input: Entry, condition?: EntryCondition): ConfigWrapper { + setEntry(input: Entry, condition?: Condition): ConfigWrapper { if (condition === undefined || condition()) return this.merge({ entry: input }); return this; } - addEntry(input: EntryRecord, condition?: EntryCondition): ConfigWrapper { + addEntry(input: EntryRecord, condition?: Condition): ConfigWrapper { const entry = { ...this.convertEntryRecord(this.base.entry), ...input, @@ -37,12 +45,23 @@ export class ConfigWrapper { addEntryByName( name: string, value: EntryRecordValue, - condition?: EntryCondition + condition?: Condition ): ConfigWrapper { const record = { [name]: value }; return this.addEntry(record, condition); } + setPlugins(plugins: Plugins, condition?: Condition) { + if (condition === undefined || condition()) + return this.merge({ plugins: plugins }); + return this; + } + + addPlugins(plugin: Plugin, condition?: Condition) { + const plugins = this.base.plugins ?? []; + return this.setPlugins([...plugins, plugin], condition); + } + private convertEntryRecord(entry: Entry | undefined): EntryRecord { if (entry === undefined) return {}; if (typeof entry === "string") return { default: entry };