From a3af71831d9a7bb2f37c1b99b293751f307361bb Mon Sep 17 00:00:00 2001 From: Hana Date: Tue, 26 Sep 2023 19:11:50 +0800 Subject: [PATCH 1/2] fix: fix `Rule.oneOf` and `Rule.rules` --- crates/rspack_core/src/utils/module_rules.rs | 14 ++++++--- .../configCases/module/issue-4224/index.js | 3 ++ .../configCases/module/issue-4224/index.scss | 9 ++++++ .../module/issue-4224/webpack.config.js | 31 +++++++++++++++++++ .../module/one-of-nested/webpack.config.js | 2 -- .../module/one-of/webpack.config.js | 2 -- .../configCases/module/order-nested/index.js | 5 +++ .../configCases/module/order-nested/lib.js | 1 + .../configCases/module/order-nested/loader.js | 4 +++ .../module/order-nested/loader1.js | 4 +++ .../module/order-nested/loader2.js | 4 +++ .../module/order-nested/webpack.config.js | 27 ++++++++++++++++ 12 files changed, 97 insertions(+), 9 deletions(-) create mode 100644 packages/rspack/tests/configCases/module/issue-4224/index.js create mode 100644 packages/rspack/tests/configCases/module/issue-4224/index.scss create mode 100644 packages/rspack/tests/configCases/module/issue-4224/webpack.config.js create mode 100644 packages/rspack/tests/configCases/module/order-nested/index.js create mode 100644 packages/rspack/tests/configCases/module/order-nested/lib.js create mode 100644 packages/rspack/tests/configCases/module/order-nested/loader.js create mode 100644 packages/rspack/tests/configCases/module/order-nested/loader1.js create mode 100644 packages/rspack/tests/configCases/module/order-nested/loader2.js create mode 100644 packages/rspack/tests/configCases/module/order-nested/webpack.config.js diff --git a/crates/rspack_core/src/utils/module_rules.rs b/crates/rspack_core/src/utils/module_rules.rs index 54d903b6ee6..25387ad5067 100644 --- a/crates/rspack_core/src/utils/module_rules.rs +++ b/crates/rspack_core/src/utils/module_rules.rs @@ -121,18 +121,22 @@ pub async fn module_rule_matcher<'a>( } } + if let Some(rules) = &module_rule.rules { + module_rules_matcher(rules, resource_data, issuer, dependency, matched_rules).await?; + } + if let Some(one_of) = &module_rule.one_of { + let mut matched_once = false; for rule in one_of { if module_rule_matcher(rule, resource_data, issuer, dependency, matched_rules).await? { + matched_once = true; break; } } + if !matched_once { + return Ok(false); + } } - - if let Some(rules) = &module_rule.rules { - module_rules_matcher(rules, resource_data, issuer, dependency, matched_rules).await?; - } - matched_rules.push(module_rule); Ok(true) } diff --git a/packages/rspack/tests/configCases/module/issue-4224/index.js b/packages/rspack/tests/configCases/module/issue-4224/index.js new file mode 100644 index 00000000000..bdb0e094d49 --- /dev/null +++ b/packages/rspack/tests/configCases/module/issue-4224/index.js @@ -0,0 +1,3 @@ +it("should generate asset/resource", () => { + expect(require("./index.scss").endsWith(".scss")).toBeTruthy(); +}); diff --git a/packages/rspack/tests/configCases/module/issue-4224/index.scss b/packages/rspack/tests/configCases/module/issue-4224/index.scss new file mode 100644 index 00000000000..16a72e0010c --- /dev/null +++ b/packages/rspack/tests/configCases/module/issue-4224/index.scss @@ -0,0 +1,9 @@ +.a { + text-align: center; + line-height: 1.5; +} + +.b { + font-size: 1.5rem; + background: #fafafa; +} diff --git a/packages/rspack/tests/configCases/module/issue-4224/webpack.config.js b/packages/rspack/tests/configCases/module/issue-4224/webpack.config.js new file mode 100644 index 00000000000..d81d50a4eec --- /dev/null +++ b/packages/rspack/tests/configCases/module/issue-4224/webpack.config.js @@ -0,0 +1,31 @@ +module.exports = { + module: { + rules: [ + { + test: /\.s[ac]ss$/i, + oneOf: [ + { + test: /\.module\.\w+$/i, + exclude: [/index\.scss$/], + use: [{ loader: "sass-loader" }], + type: "css/module" + }, + { + exclude: [/index\.scss$/], + use: [{ loader: "sass-loader" }], + type: "css" + } + ] + }, + { + exclude: [ + /\.(js|mjs|cjs|jsx)$/, + /\.(ts|mts|cts|tsx)$/, + /\.html$/, + /\.json$/ + ], + type: "asset/resource" + } + ] + } +}; diff --git a/packages/rspack/tests/configCases/module/one-of-nested/webpack.config.js b/packages/rspack/tests/configCases/module/one-of-nested/webpack.config.js index 486cd646ae6..6eeda7e7024 100644 --- a/packages/rspack/tests/configCases/module/one-of-nested/webpack.config.js +++ b/packages/rspack/tests/configCases/module/one-of-nested/webpack.config.js @@ -1,5 +1,3 @@ -const path = require("path"); - /** * @type {import('@rspack/core').RspackOptions} */ diff --git a/packages/rspack/tests/configCases/module/one-of/webpack.config.js b/packages/rspack/tests/configCases/module/one-of/webpack.config.js index d66b29c380e..7ee83a976fa 100644 --- a/packages/rspack/tests/configCases/module/one-of/webpack.config.js +++ b/packages/rspack/tests/configCases/module/one-of/webpack.config.js @@ -1,5 +1,3 @@ -const path = require("path"); - /** * @type {import('@rspack/core').RspackOptions} */ diff --git a/packages/rspack/tests/configCases/module/order-nested/index.js b/packages/rspack/tests/configCases/module/order-nested/index.js new file mode 100644 index 00000000000..03045ffba18 --- /dev/null +++ b/packages/rspack/tests/configCases/module/order-nested/index.js @@ -0,0 +1,5 @@ +import { lib } from "./lib"; + +it("should iterate `rules` first, then `oneOf`", () => { + expect(lib).toEqual("abc02"); +}); diff --git a/packages/rspack/tests/configCases/module/order-nested/lib.js b/packages/rspack/tests/configCases/module/order-nested/lib.js new file mode 100644 index 00000000000..21e58fddbc9 --- /dev/null +++ b/packages/rspack/tests/configCases/module/order-nested/lib.js @@ -0,0 +1 @@ +exports.lib = "abc"; diff --git a/packages/rspack/tests/configCases/module/order-nested/loader.js b/packages/rspack/tests/configCases/module/order-nested/loader.js new file mode 100644 index 00000000000..01ba22d0b43 --- /dev/null +++ b/packages/rspack/tests/configCases/module/order-nested/loader.js @@ -0,0 +1,4 @@ +module.exports = function (content) { + content += 'exports.lib += "0";\n'; + this.callback(null, content); +}; diff --git a/packages/rspack/tests/configCases/module/order-nested/loader1.js b/packages/rspack/tests/configCases/module/order-nested/loader1.js new file mode 100644 index 00000000000..b94a554769c --- /dev/null +++ b/packages/rspack/tests/configCases/module/order-nested/loader1.js @@ -0,0 +1,4 @@ +module.exports = function (content) { + content += 'exports.lib += "1";\n'; + this.callback(null, content); +}; diff --git a/packages/rspack/tests/configCases/module/order-nested/loader2.js b/packages/rspack/tests/configCases/module/order-nested/loader2.js new file mode 100644 index 00000000000..a0ced2fd3d5 --- /dev/null +++ b/packages/rspack/tests/configCases/module/order-nested/loader2.js @@ -0,0 +1,4 @@ +module.exports = function (content) { + content += 'exports.lib += "2";\n'; + this.callback(null, content); +}; diff --git a/packages/rspack/tests/configCases/module/order-nested/webpack.config.js b/packages/rspack/tests/configCases/module/order-nested/webpack.config.js new file mode 100644 index 00000000000..ae0efb7c1d6 --- /dev/null +++ b/packages/rspack/tests/configCases/module/order-nested/webpack.config.js @@ -0,0 +1,27 @@ +/** + * @type {import('@rspack/core').RspackOptions} + */ +module.exports = { + context: __dirname, + module: { + rules: [ + { + test: /lib.js/, + rules: [ + { + use: ["./loader2.js"] + } + ], + oneOf: [ + { + resourceQuery: /random-string/, + use: ["./loader1.js"] + }, + { + use: ["./loader.js"] + } + ] + } + ] + } +}; From e454bcda6ee97eb2d40049cc6e7d3c7c2ec72ddf Mon Sep 17 00:00:00 2001 From: Hana Date: Tue, 26 Sep 2023 20:51:48 +0800 Subject: [PATCH 2/2] fix: fix test cases --- .../module/issue-4224/webpack.config.js | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/packages/rspack/tests/configCases/module/issue-4224/webpack.config.js b/packages/rspack/tests/configCases/module/issue-4224/webpack.config.js index d81d50a4eec..d681ca8bcce 100644 --- a/packages/rspack/tests/configCases/module/issue-4224/webpack.config.js +++ b/packages/rspack/tests/configCases/module/issue-4224/webpack.config.js @@ -2,29 +2,33 @@ module.exports = { module: { rules: [ { - test: /\.s[ac]ss$/i, oneOf: [ { - test: /\.module\.\w+$/i, - exclude: [/index\.scss$/], - use: [{ loader: "sass-loader" }], - type: "css/module" + test: /\.s[ac]ss$/i, + oneOf: [ + { + test: /\.module\.\w+$/i, + exclude: [/index\.scss$/], + use: [{ loader: "sass-loader" }], + type: "css/module" + }, + { + exclude: [/index\.scss$/], + use: [{ loader: "sass-loader" }], + type: "css" + } + ] }, { - exclude: [/index\.scss$/], - use: [{ loader: "sass-loader" }], - type: "css" + exclude: [ + /\.(js|mjs|cjs|jsx)$/, + /\.(ts|mts|cts|tsx)$/, + /\.html$/, + /\.json$/ + ], + type: "asset/resource" } ] - }, - { - exclude: [ - /\.(js|mjs|cjs|jsx)$/, - /\.(ts|mts|cts|tsx)$/, - /\.html$/, - /\.json$/ - ], - type: "asset/resource" } ] }