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

fix(config): support pass false to plugins #4949

Merged
merged 10 commits into from
Dec 12, 2023
4 changes: 3 additions & 1 deletion packages/rspack/src/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,9 @@ class Compiler {
childCompiler.root = this.root;
if (Array.isArray(plugins)) {
for (const plugin of plugins) {
plugin.apply(childCompiler);
if (plugin) {
plugin.apply(childCompiler);
}
}
}
for (const name in this.hooks) {
Expand Down
23 changes: 18 additions & 5 deletions packages/rspack/src/config/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ const mode = z.enum(["development", "production", "none"]);
export type Mode = z.infer<typeof mode>;
//#endregion

//#region Falsy
const falsy = z.union([
z.literal(false),
z.literal(0),
z.literal(""),
z.null(),
z.undefined()
]);

export type Falsy = z.infer<typeof falsy>;
//#endregion

//#region Entry
const rawPublicPath = z.string();
export type RawPublicPath = z.infer<typeof rawPublicPath>;
Expand Down Expand Up @@ -459,7 +471,7 @@ const ruleSetRule: z.ZodType<RuleSetRule> = baseRuleSetRule.extend({
rules: z.lazy(() => ruleSetRule.array()).optional()
});

const ruleSetRules = z.array(z.literal("...").or(ruleSetRule));
const ruleSetRules = z.array(z.literal("...").or(ruleSetRule).or(falsy));
export type RuleSetRules = z.infer<typeof ruleSetRules>;

const assetParserDataUrlOptions = z.strictObject({
Expand Down Expand Up @@ -888,7 +900,8 @@ export type RspackPluginFunction = (this: Compiler, compiler: Compiler) => void;

const plugin = z.union([
z.custom<RspackPluginInstance>(),
z.custom<RspackPluginFunction>()
z.custom<RspackPluginFunction>(),
falsy,
]);
const plugins = plugin.array();
export type Plugins = z.infer<typeof plugins>;
Expand Down Expand Up @@ -1062,9 +1075,9 @@ const experiments = z.strictObject({
val
)}' has been deprecated, please switch to 'experiments.newSplitChunks = true' to use webpack's behavior.
See the discussion ${termlink(
"here",
"https://github.com/web-infra-dev/rspack/discussions/4168"
)}`
"here",
"https://github.com/web-infra-dev/rspack/discussions/4168"
)}`
);
}
return true;
Expand Down
8 changes: 5 additions & 3 deletions packages/rspack/src/rspack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function createCompiler(userOptions: RspackOptions): Compiler {
for (const plugin of options.plugins) {
if (typeof plugin === "function") {
(plugin as RspackPluginFunction).call(compiler, compiler);
} else {
} else if (plugin) {
plugin.apply(compiler);
}
}
Expand Down Expand Up @@ -148,8 +148,10 @@ function rspack(
} else {
const { compiler, watch } = create();
if (watch) {
util.deprecate(() => {},
"A 'callback' argument needs to be provided to the 'rspack(options, callback)' function when the 'watch' option is set. There is no way to handle the 'watch' option without a callback.")();
util.deprecate(
() => { },
"A 'callback' argument needs to be provided to the 'rspack(options, callback)' function when the 'watch' option is set. There is no way to handle the 'watch' option without a callback."
)();
}
return compiler;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/rspack/src/rspackOptionsApply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export function optionsApply_compat(
}

export class RspackOptionsApply {
constructor() {}
constructor() { }
process(options: RspackOptionsNormalized, compiler: Compiler) {
assert(
options.output.path,
Expand Down Expand Up @@ -182,7 +182,7 @@ export class RspackOptionsApply {
for (const item of minimizer) {
if (typeof item === "function") {
(item as RspackPluginFunction).call(compiler, compiler);
} else if (item !== "...") {
} else if (item !== "..." && item) {
item.apply(compiler);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "test";
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const test = require("./a");

it("should work with falsy plugins", function () {
expect(test.default).toBe("test");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const nullValue = null;
const undefinedValue = undefined;
const falseValue = false;
const zeroValue = 0;
const emptyStringValue = "";

class FailPlugin {
apply() {
throw new Error("FailedPlugin");
}
}

/** @type {import("../../../../src/index").RspackOptions} */
module.exports = {
plugins: [
undefinedValue && new FailPlugin(),
nullValue && new FailPlugin(),
falseValue && new FailPlugin(),
zeroValue && new FailPlugin(),
emptyStringValue && new FailPlugin()
]
};
Loading