From c1b5f6a57c7d80e2bb7d29df643432c74d33c4ba Mon Sep 17 00:00:00 2001 From: puppet-666 <1063771217@qq.com> Date: Fri, 8 Dec 2023 11:02:16 +0800 Subject: [PATCH 01/10] fix(config): support pass false to plugins --- packages/rspack/src/rspack.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/rspack/src/rspack.ts b/packages/rspack/src/rspack.ts index 2ee3d658475..0a408717674 100644 --- a/packages/rspack/src/rspack.ts +++ b/packages/rspack/src/rspack.ts @@ -70,7 +70,11 @@ function createCompiler(userOptions: RspackOptions): Compiler { for (const plugin of options.plugins) { if (typeof plugin === "function") { (plugin as RspackPluginFunction).call(compiler, compiler); - } else { + } + else if (typeof plugin === 'boolean') { + continue; + } + else { plugin.apply(compiler); } } @@ -148,8 +152,8 @@ 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; } From e47e8ea36c9102dd7b648c97c1cac33b9e3fb714 Mon Sep 17 00:00:00 2001 From: puppet-666 <1063771217@qq.com> Date: Fri, 8 Dec 2023 11:14:21 +0800 Subject: [PATCH 02/10] fix: prettier format --- packages/rspack/src/rspack.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/rspack/src/rspack.ts b/packages/rspack/src/rspack.ts index 0a408717674..810b3db46b4 100644 --- a/packages/rspack/src/rspack.ts +++ b/packages/rspack/src/rspack.ts @@ -70,11 +70,9 @@ function createCompiler(userOptions: RspackOptions): Compiler { for (const plugin of options.plugins) { if (typeof plugin === "function") { (plugin as RspackPluginFunction).call(compiler, compiler); - } - else if (typeof plugin === 'boolean') { + } else if (typeof plugin === "boolean") { continue; - } - else { + } else { plugin.apply(compiler); } } @@ -152,8 +150,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; } From c4dac9c085f33ece858a3064117e525a7630db86 Mon Sep 17 00:00:00 2001 From: puppet-666 <1063771217@qq.com> Date: Fri, 8 Dec 2023 17:01:57 +0800 Subject: [PATCH 03/10] test(config): support pass false to plugins --- .../tests/configCases/plugins/issue-4949/index.js | 3 +++ .../configCases/plugins/issue-4949/webpack.config.js | 10 ++++++++++ 2 files changed, 13 insertions(+) create mode 100644 packages/rspack/tests/configCases/plugins/issue-4949/index.js create mode 100644 packages/rspack/tests/configCases/plugins/issue-4949/webpack.config.js diff --git a/packages/rspack/tests/configCases/plugins/issue-4949/index.js b/packages/rspack/tests/configCases/plugins/issue-4949/index.js new file mode 100644 index 00000000000..e6ad1affa86 --- /dev/null +++ b/packages/rspack/tests/configCases/plugins/issue-4949/index.js @@ -0,0 +1,3 @@ +it("pass false to plugins", function () { + expect(STRING).toBe(undefined); +}); diff --git a/packages/rspack/tests/configCases/plugins/issue-4949/webpack.config.js b/packages/rspack/tests/configCases/plugins/issue-4949/webpack.config.js new file mode 100644 index 00000000000..4813fa0b9bb --- /dev/null +++ b/packages/rspack/tests/configCases/plugins/issue-4949/webpack.config.js @@ -0,0 +1,10 @@ +const rspack = require("@rspack/core"); + +/** @type {import("../../../../src/index").RspackOptions} */ +module.exports = { + plugins: [ + false && new rspack.DefinePlugin({ + STRING: '"string"', + }) + ] +}; From 83bacb3292114e28e3567973f86483b30fc16862 Mon Sep 17 00:00:00 2001 From: puppet-666 <1063771217@qq.com> Date: Fri, 8 Dec 2023 18:06:31 +0800 Subject: [PATCH 04/10] fix(test): support pass false to plugins --- packages/rspack/tests/configCases/plugins/issue-4949/a.js | 1 + packages/rspack/tests/configCases/plugins/issue-4949/index.js | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 packages/rspack/tests/configCases/plugins/issue-4949/a.js diff --git a/packages/rspack/tests/configCases/plugins/issue-4949/a.js b/packages/rspack/tests/configCases/plugins/issue-4949/a.js new file mode 100644 index 00000000000..63ad93afa99 --- /dev/null +++ b/packages/rspack/tests/configCases/plugins/issue-4949/a.js @@ -0,0 +1 @@ +export const txt = `${process.env.TITLE ? process.env.TITLE : ''}`; diff --git a/packages/rspack/tests/configCases/plugins/issue-4949/index.js b/packages/rspack/tests/configCases/plugins/issue-4949/index.js index e6ad1affa86..712657add17 100644 --- a/packages/rspack/tests/configCases/plugins/issue-4949/index.js +++ b/packages/rspack/tests/configCases/plugins/issue-4949/index.js @@ -1,3 +1,5 @@ +const txt = require("./a"); + it("pass false to plugins", function () { - expect(STRING).toBe(undefined); + expect(txt).toBe(''); }); From c18a1ad6f9eb072b492681b20f358015d6e705a2 Mon Sep 17 00:00:00 2001 From: puppetk <1063771217@qq.com> Date: Fri, 8 Dec 2023 22:20:33 +0800 Subject: [PATCH 05/10] fix(config): support falsy --- packages/rspack/src/Compiler.ts | 4 ++- packages/rspack/src/config/zod.ts | 29 ++++++++++++++----- packages/rspack/src/rspack.ts | 6 ++-- .../tests/configCases/plugins/issue-4949/a.js | 1 - .../configCases/plugins/issue-4949/index.js | 5 ---- .../plugins/issue-4949/webpack.config.js | 10 ------- .../configCases/plugins/plugins-falsy/a.js | 1 + .../plugins/plugins-falsy/index.js | 5 ++++ .../plugins/plugins-falsy/webpack.config.js | 22 ++++++++++++++ 9 files changed, 54 insertions(+), 29 deletions(-) delete mode 100644 packages/rspack/tests/configCases/plugins/issue-4949/a.js delete mode 100644 packages/rspack/tests/configCases/plugins/issue-4949/index.js delete mode 100644 packages/rspack/tests/configCases/plugins/issue-4949/webpack.config.js create mode 100644 packages/rspack/tests/configCases/plugins/plugins-falsy/a.js create mode 100644 packages/rspack/tests/configCases/plugins/plugins-falsy/index.js create mode 100644 packages/rspack/tests/configCases/plugins/plugins-falsy/webpack.config.js diff --git a/packages/rspack/src/Compiler.ts b/packages/rspack/src/Compiler.ts index 8eafd94f8ef..51f4592db9a 100644 --- a/packages/rspack/src/Compiler.ts +++ b/packages/rspack/src/Compiler.ts @@ -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) { diff --git a/packages/rspack/src/config/zod.ts b/packages/rspack/src/config/zod.ts index fb103845981..820d2fc4c6c 100644 --- a/packages/rspack/src/config/zod.ts +++ b/packages/rspack/src/config/zod.ts @@ -26,6 +26,18 @@ const mode = z.enum(["development", "production", "none"]); export type Mode = z.infer; //#endregion +//#region Falsy +const falsy = z.union([ + z.literal(false), + z.number().int().min(0), + z.string().min(0), + z.null(), + z.undefined() +]); + +export type Falsy = z.infer; +//#endregion + //#region Entry const rawPublicPath = z.string(); export type RawPublicPath = z.infer; @@ -416,7 +428,7 @@ const ruleSetLoaderWithOptions = z.strictObject({ }); export type RuleSetLoaderWithOptions = z.infer; -const ruleSetUseItem = ruleSetLoader.or(ruleSetLoaderWithOptions); +const ruleSetUseItem = ruleSetLoader.or(ruleSetLoaderWithOptions).or(falsy); export type RuleSetUseItem = z.infer; const ruleSetUse = ruleSetUseItem @@ -450,8 +462,8 @@ const baseRuleSetRule = z.strictObject({ }); export type RuleSetRule = z.infer & { - oneOf?: RuleSetRule[]; - rules?: RuleSetRule[]; + oneOf?: Falsy | RuleSetRule[]; + rules?: Falsy | RuleSetRule[]; }; const ruleSetRule: z.ZodType = baseRuleSetRule.extend({ @@ -459,7 +471,7 @@ const ruleSetRule: z.ZodType = 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; const assetParserDataUrlOptions = z.strictObject({ @@ -888,7 +900,8 @@ export type RspackPluginFunction = (this: Compiler, compiler: Compiler) => void; const plugin = z.union([ z.custom(), - z.custom() + z.custom(), + falsy, ]); const plugins = plugin.array(); export type Plugins = z.infer; @@ -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; diff --git a/packages/rspack/src/rspack.ts b/packages/rspack/src/rspack.ts index 810b3db46b4..2e892f06c5d 100644 --- a/packages/rspack/src/rspack.ts +++ b/packages/rspack/src/rspack.ts @@ -70,9 +70,7 @@ function createCompiler(userOptions: RspackOptions): Compiler { for (const plugin of options.plugins) { if (typeof plugin === "function") { (plugin as RspackPluginFunction).call(compiler, compiler); - } else if (typeof plugin === "boolean") { - continue; - } else { + } else if (plugin) { plugin.apply(compiler); } } @@ -151,7 +149,7 @@ function rspack( 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." )(); } diff --git a/packages/rspack/tests/configCases/plugins/issue-4949/a.js b/packages/rspack/tests/configCases/plugins/issue-4949/a.js deleted file mode 100644 index 63ad93afa99..00000000000 --- a/packages/rspack/tests/configCases/plugins/issue-4949/a.js +++ /dev/null @@ -1 +0,0 @@ -export const txt = `${process.env.TITLE ? process.env.TITLE : ''}`; diff --git a/packages/rspack/tests/configCases/plugins/issue-4949/index.js b/packages/rspack/tests/configCases/plugins/issue-4949/index.js deleted file mode 100644 index 712657add17..00000000000 --- a/packages/rspack/tests/configCases/plugins/issue-4949/index.js +++ /dev/null @@ -1,5 +0,0 @@ -const txt = require("./a"); - -it("pass false to plugins", function () { - expect(txt).toBe(''); -}); diff --git a/packages/rspack/tests/configCases/plugins/issue-4949/webpack.config.js b/packages/rspack/tests/configCases/plugins/issue-4949/webpack.config.js deleted file mode 100644 index 4813fa0b9bb..00000000000 --- a/packages/rspack/tests/configCases/plugins/issue-4949/webpack.config.js +++ /dev/null @@ -1,10 +0,0 @@ -const rspack = require("@rspack/core"); - -/** @type {import("../../../../src/index").RspackOptions} */ -module.exports = { - plugins: [ - false && new rspack.DefinePlugin({ - STRING: '"string"', - }) - ] -}; diff --git a/packages/rspack/tests/configCases/plugins/plugins-falsy/a.js b/packages/rspack/tests/configCases/plugins/plugins-falsy/a.js new file mode 100644 index 00000000000..58c57157d36 --- /dev/null +++ b/packages/rspack/tests/configCases/plugins/plugins-falsy/a.js @@ -0,0 +1 @@ +export default "test"; diff --git a/packages/rspack/tests/configCases/plugins/plugins-falsy/index.js b/packages/rspack/tests/configCases/plugins/plugins-falsy/index.js new file mode 100644 index 00000000000..a84221a1f1e --- /dev/null +++ b/packages/rspack/tests/configCases/plugins/plugins-falsy/index.js @@ -0,0 +1,5 @@ +const test = require("./a"); + +it("should work with falsy plugins", function () { + expect(test).toBe("test"); +}); diff --git a/packages/rspack/tests/configCases/plugins/plugins-falsy/webpack.config.js b/packages/rspack/tests/configCases/plugins/plugins-falsy/webpack.config.js new file mode 100644 index 00000000000..60669738df4 --- /dev/null +++ b/packages/rspack/tests/configCases/plugins/plugins-falsy/webpack.config.js @@ -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() + ] +}; From c131bfbb803e37dd7426839a95eac1ca5d374937 Mon Sep 17 00:00:00 2001 From: puppet-666 <1063771217@qq.com> Date: Tue, 12 Dec 2023 09:09:26 +0800 Subject: [PATCH 06/10] fix: zod falsy type --- packages/rspack/src/config/zod.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/rspack/src/config/zod.ts b/packages/rspack/src/config/zod.ts index 820d2fc4c6c..47d76647608 100644 --- a/packages/rspack/src/config/zod.ts +++ b/packages/rspack/src/config/zod.ts @@ -29,8 +29,8 @@ export type Mode = z.infer; //#region Falsy const falsy = z.union([ z.literal(false), - z.number().int().min(0), - z.string().min(0), + z.literal(0), + z.string().trim().max(0).min(0), z.null(), z.undefined() ]); From 9e9d673b1cf5eace12a50fd84648f0806270af7e Mon Sep 17 00:00:00 2001 From: puppet-666 <1063771217@qq.com> Date: Tue, 12 Dec 2023 11:40:56 +0800 Subject: [PATCH 07/10] fix: zod falsy string init value --- packages/rspack/src/config/zod.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/rspack/src/config/zod.ts b/packages/rspack/src/config/zod.ts index 47d76647608..c398a8275b0 100644 --- a/packages/rspack/src/config/zod.ts +++ b/packages/rspack/src/config/zod.ts @@ -30,7 +30,7 @@ export type Mode = z.infer; const falsy = z.union([ z.literal(false), z.literal(0), - z.string().trim().max(0).min(0), + z.literal(""), z.null(), z.undefined() ]); From 980a79830a784a7a2f90a82ba2ed724b740ebd1a Mon Sep 17 00:00:00 2001 From: puppet-666 <1063771217@qq.com> Date: Tue, 12 Dec 2023 12:57:50 +0800 Subject: [PATCH 08/10] fix: Temporarily remove support for falsy in rules --- packages/rspack/src/config/zod.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/rspack/src/config/zod.ts b/packages/rspack/src/config/zod.ts index c398a8275b0..3fee24c6215 100644 --- a/packages/rspack/src/config/zod.ts +++ b/packages/rspack/src/config/zod.ts @@ -428,7 +428,7 @@ const ruleSetLoaderWithOptions = z.strictObject({ }); export type RuleSetLoaderWithOptions = z.infer; -const ruleSetUseItem = ruleSetLoader.or(ruleSetLoaderWithOptions).or(falsy); +const ruleSetUseItem = ruleSetLoader.or(ruleSetLoaderWithOptions); export type RuleSetUseItem = z.infer; const ruleSetUse = ruleSetUseItem @@ -462,8 +462,8 @@ const baseRuleSetRule = z.strictObject({ }); export type RuleSetRule = z.infer & { - oneOf?: Falsy | RuleSetRule[]; - rules?: Falsy | RuleSetRule[]; + oneOf?: RuleSetRule[]; + rules?: RuleSetRule[]; }; const ruleSetRule: z.ZodType = baseRuleSetRule.extend({ From 732673743f5cec184b8930c01c02baf5762f5e21 Mon Sep 17 00:00:00 2001 From: puppet-666 <1063771217@qq.com> Date: Tue, 12 Dec 2023 13:13:44 +0800 Subject: [PATCH 09/10] fix: minimizer apply --- packages/rspack/src/rspackOptionsApply.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/rspack/src/rspackOptionsApply.ts b/packages/rspack/src/rspackOptionsApply.ts index a3be3998ecb..0c97297ca6f 100644 --- a/packages/rspack/src/rspackOptionsApply.ts +++ b/packages/rspack/src/rspackOptionsApply.ts @@ -149,7 +149,7 @@ export function optionsApply_compat( } export class RspackOptionsApply { - constructor() {} + constructor() { } process(options: RspackOptionsNormalized, compiler: Compiler) { assert( options.output.path, @@ -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); } } From b2db9720cdd348d195b84aedf9ef60a60822703b Mon Sep 17 00:00:00 2001 From: puppet-666 <1063771217@qq.com> Date: Tue, 12 Dec 2023 13:55:02 +0800 Subject: [PATCH 10/10] fix: falsy test --- .../rspack/tests/configCases/plugins/plugins-falsy/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/rspack/tests/configCases/plugins/plugins-falsy/index.js b/packages/rspack/tests/configCases/plugins/plugins-falsy/index.js index a84221a1f1e..f2944e9172d 100644 --- a/packages/rspack/tests/configCases/plugins/plugins-falsy/index.js +++ b/packages/rspack/tests/configCases/plugins/plugins-falsy/index.js @@ -1,5 +1,5 @@ const test = require("./a"); it("should work with falsy plugins", function () { - expect(test).toBe("test"); + expect(test.default).toBe("test"); });