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..d681ca8bcce --- /dev/null +++ b/packages/rspack/tests/configCases/module/issue-4224/webpack.config.js @@ -0,0 +1,35 @@ +module.exports = { + module: { + rules: [ + { + oneOf: [ + { + 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"] + } + ] + } + ] + } +};