Skip to content

Commit

Permalink
feat(rspack-config): support modify plugins on config wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
kamontat committed Jan 15, 2024
1 parent dc14c14 commit 018e5e9
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions packages/rspack-config/src/wrapper.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import type { Configuration } from "@rspack/cli";
import type {
Configuration,
RspackPluginFunction,
RspackPluginInstance,
} from "@rspack/core";

type Condition = () => boolean;

type Entry = Exclude<Configuration["entry"], undefined>;
type EntryRecord = Exclude<Entry, string | string[]>;
type EntryRecordValue = EntryRecord[string];
type EntryCondition = () => boolean;

type Plugin = RspackPluginInstance | RspackPluginFunction;
type Plugins = Exclude<Configuration["plugins"], undefined>;

export class ConfigWrapper {
constructor(private base: Configuration) {}
Expand All @@ -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,
Expand All @@ -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 };
Expand Down

0 comments on commit 018e5e9

Please sign in to comment.