Skip to content

Commit

Permalink
fix: falsy rule should not pass the validation (#8297)
Browse files Browse the repository at this point in the history
  • Loading branch information
LingyuCoder authored Nov 1, 2024
1 parent a3ae37c commit 638b82f
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { lib, lib1 } from "./lib";

it("`oneOf` should work", () => {
// should run loader & loader2 and not run loader1
expect(lib).toEqual("aabc");

expect(lib1).toEqual("effg");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const lib = "abc";

export const lib1 = "efg";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function (content) {
this.callback(null, content.replace("ab", "aab"));
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function (content) {
this.callback(null, content.replace("bc", "bcc"));
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function (content) {
this.callback(null, content.replace("ef", "eff"));
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @type {import('@rspack/core').RspackOptions}
*/
module.exports = {
context: __dirname,
module: {
rules: [
undefined,
{
test: /lib.js/,
use: ["./loader2.js"]
},
{
test: /lib.js/,
oneOf: [
undefined,
{
resourceQuery: "/(__inline=false|url)/",
use: ["./loader1.js"]
},
{
use: ["./loader.js"]
},
{
use: ["./loader1.js"]
}
]
}
]
}
};
4 changes: 2 additions & 2 deletions packages/rspack/etc/core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -9316,8 +9316,8 @@ export type RuleSetRule = {
resolve?: ResolveOptions;
sideEffects?: boolean;
enforce?: "pre" | "post";
oneOf?: RuleSetRule[];
rules?: RuleSetRule[];
oneOf?: (RuleSetRule | Falsy)[];
rules?: (RuleSetRule | Falsy)[];
};

// @public
Expand Down
32 changes: 18 additions & 14 deletions packages/rspack/src/config/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,24 +460,28 @@ const getRawModuleRule = (
: undefined,
resolve: rule.resolve ? getRawResolve(rule.resolve) : undefined,
oneOf: rule.oneOf
? rule.oneOf.map((rule, index) =>
getRawModuleRule(
rule,
`${path}.oneOf[${index}]`,
options,
rule.type ?? upperType
? rule.oneOf
.filter(Boolean)
.map((rule, index) =>
getRawModuleRule(
rule as RuleSetRule,
`${path}.oneOf[${index}]`,
options,
(rule as RuleSetRule).type ?? upperType
)
)
)
: undefined,
rules: rule.rules
? rule.rules.map((rule, index) =>
getRawModuleRule(
rule,
`${path}.rules[${index}]`,
options,
rule.type ?? upperType
? rule.rules
.filter(Boolean)
.map((rule, index) =>
getRawModuleRule(
rule as RuleSetRule,
`${path}.rules[${index}]`,
options,
(rule as RuleSetRule).type ?? upperType
)
)
)
: undefined,
enforce: rule.enforce
};
Expand Down
4 changes: 2 additions & 2 deletions packages/rspack/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -897,10 +897,10 @@ export type RuleSetRule = {
enforce?: "pre" | "post";

/** A kind of Nested Rule, an array of Rules from which only the first matching Rule is used when the parent Rule matches. */
oneOf?: RuleSetRule[];
oneOf?: (RuleSetRule | Falsy)[];

/** A kind of Nested Rule, an array of Rules that is also used when the parent Rule matches. */
rules?: RuleSetRule[];
rules?: (RuleSetRule | Falsy)[];
};

/** A list of rules. */
Expand Down
4 changes: 2 additions & 2 deletions packages/rspack/src/config/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,8 @@ const baseRuleSetRule = z.strictObject({
}) satisfies z.ZodType<t.RuleSetRule>;

const ruleSetRule: z.ZodType<t.RuleSetRule> = baseRuleSetRule.extend({
oneOf: z.lazy(() => ruleSetRule.array()).optional(),
rules: z.lazy(() => ruleSetRule.array()).optional()
oneOf: z.lazy(() => ruleSetRule.or(falsy).array()).optional(),
rules: z.lazy(() => ruleSetRule.or(falsy).array()).optional()
});

const ruleSetRules = z.array(
Expand Down
4 changes: 2 additions & 2 deletions website/docs/en/config/module.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ Same as [`module.generator["css/auto"].esModule`](#modulegeneratorcssautoesmodul

## module.rules

- **Type:** `Rule[]`
- **Type:** `(Rule | Falsy)[]`
- **Default:** `[]`

An array of rules that match the module's requests when it is created. These rules can modify the creation behavior of the module. They can apply Loader, etc. to the module.
Expand Down Expand Up @@ -1223,7 +1223,7 @@ A kind of [Nested Rule](#nested-rule), an array of Rules that is also used when

### Rule.oneOf

- **Type:** <code>[Rule](#rule)[]</code>
- **Type:** <code>([Rule](#rule) | Falsy)[]</code>
- **Default:** `undefined`

A kind of [Nested Rule](#nested-rule), an array of Rules from which only the first matching Rule is used when the parent Rule matches.
4 changes: 2 additions & 2 deletions website/docs/zh/config/module.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import WebpackLicense from '@components/WebpackLicense';

## module.defaultRules

- **类型:** `Rule[]`
- **类型:** `(Rule | Falsy)[]`

应用于模块的默认规则。

Expand Down Expand Up @@ -1221,7 +1221,7 @@ module.exports = {

### Rule.oneOf

- **类型:** <code>[Rule](#rule)[]</code>
- **类型:** <code>([Rule](#rule) | Falsy)[]</code>
- **默认值:** `undefined`

[嵌套 Rule](#nested-rule) 的一种,当其上层 Rule 匹配成功后会使用这些 Rule 进行匹配,并且只使用匹配成功的第一个 Rule

2 comments on commit 638b82f

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ✅ success
_selftest ✅ success
rspress ✅ success
rslib ✅ success
rsbuild ✅ success
examples ✅ success
devserver ✅ success

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-11-01 4aeee85) Current Change
10000_big_production-mode + exec 47.5 s ± 1.45 s 47.7 s ± 1.39 s +0.38 %
10000_development-mode + exec 2.08 s ± 13 ms 2.09 s ± 22 ms +0.56 %
10000_development-mode_hmr + exec 649 ms ± 13 ms 644 ms ± 6.5 ms -0.76 %
10000_production-mode + exec 2.62 s ± 29 ms 2.61 s ± 32 ms -0.53 %
arco-pro_development-mode + exec 1.78 s ± 79 ms 1.79 s ± 91 ms +0.85 %
arco-pro_development-mode_hmr + exec 428 ms ± 1.1 ms 429 ms ± 1.9 ms +0.11 %
arco-pro_production-mode + exec 3.22 s ± 82 ms 3.21 s ± 77 ms -0.30 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.22 s ± 79 ms 3.27 s ± 79 ms +1.46 %
threejs_development-mode_10x + exec 1.64 s ± 12 ms 1.59 s ± 26 ms -3.06 %
threejs_development-mode_10x_hmr + exec 784 ms ± 14 ms 783 ms ± 12 ms -0.06 %
threejs_production-mode_10x + exec 5.03 s ± 52 ms 4.97 s ± 26 ms -1.14 %

Please sign in to comment.