Skip to content

Commit

Permalink
feat: add appendRules util to tools.rspack config (#3755)
Browse files Browse the repository at this point in the history
Co-authored-by: neverland <[email protected]>
  • Loading branch information
9aoy and chenjiahan authored Oct 18, 2024
1 parent 454d046 commit 34339d5
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 9 deletions.
11 changes: 11 additions & 0 deletions packages/core/src/provider/rspackConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ export async function getConfigUtils(
config.module.rules.unshift(...ruleArr);
},

appendRules(rules) {
const ruleArr = castArray(rules);
if (!config.module) {
config.module = {};
}
if (!config.module.rules) {
config.module.rules = [];
}
config.module.rules.push(...ruleArr);
},

prependPlugins(plugins) {
const pluginArr = castArray(plugins);
if (!config.plugins) {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export type ToolsHtmlPluginConfig = ConfigChainWithContext<

export type ModifyRspackConfigUtils = ModifyChainUtils & {
addRules: (rules: RspackRule | RspackRule[]) => void;
appendRules: (rules: RspackRule | RspackRule[]) => void;
prependPlugins: (
plugins: BundlerPluginInstance | BundlerPluginInstance[],
) => void;
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export type ModifyWebpackChainUtils = ModifyChainUtils & {

export type ModifyWebpackConfigUtils = ModifyWebpackChainUtils & {
addRules: (rules: RuleSetRule | RuleSetRule[]) => void;
appendRules: (rules: RuleSetRule | RuleSetRule[]) => void;
prependPlugins: (
plugins: WebpackPluginInstance | WebpackPluginInstance[],
) => void;
Expand Down
4 changes: 4 additions & 0 deletions packages/core/tests/__snapshots__/default.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,10 @@ exports[`tools.rspack > should match snapshot 1`] = `
"test": /\\\\\\.wasm\\$/,
"type": "asset/resource",
},
{
"loader": "foo-loader",
"test": /\\\\\\.foo/,
},
],
},
"name": "web",
Expand Down
7 changes: 6 additions & 1 deletion packages/core/tests/default.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('tools.rspack', () => {
const rsbuild = await createStubRsbuild({
rsbuildConfig: {
tools: {
rspack: (_config, { addRules, prependPlugins }) => {
rspack: (_config, { addRules, prependPlugins, appendRules }) => {
addRules({
test: /\.test$/,
use: [
Expand All @@ -67,6 +67,11 @@ describe('tools.rspack', () => {
},
],
});
appendRules({
test: /\.foo/,
loader: 'foo-loader',
});

prependPlugins([new TestPlugin()]);
},
},
Expand Down
40 changes: 36 additions & 4 deletions website/docs/en/config/tools/rspack.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,9 @@ export default {
- **Type:** `(rules: RuleSetRule | RuleSetRule[]) => void`
Add additional [Rspack rules](https://rspack.dev/config/module#modulerules).
Add additional [Rspack rules](https://rspack.dev/config/module#modulerules) to the head of the internal Rspack module rules array.
It should be noted that Rspack loaders will be executed in right-to-left order. If you want the loader you added to be executed before other loaders (Normal Phase), you should use [appendRules](#appendrules) to add the rule to the end.
For example:
Expand Down Expand Up @@ -265,9 +267,39 @@ export default {
};
```
:::tip
The `addRules(...)` method will prepend rules to Rspack rule list. To append rules, use `config.module.rules.push(...)` instead.
:::
### appendRules
- **Type:** `(rules: RuleSetRule | RuleSetRule[]) => void`
Add additional [Rspack rules](https://rspack.dev/config/module#modulerules) to the end of the internal Rspack module rules array.
For example:
```ts
export default {
tools: {
rspack: (config, { appendRules }) => {
// add a single rule
appendRules({
test: /\.foo/,
loader: require.resolve('foo-loader'),
});

// Add multiple rules as an array
appendRules([
{
test: /\.foo/,
loader: require.resolve('foo-loader'),
},
{
test: /\.bar/,
loader: require.resolve('bar-loader'),
},
]);
},
},
};
```
### prependPlugins
Expand Down
40 changes: 36 additions & 4 deletions website/docs/zh/config/tools/rspack.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,9 @@ export default {
- **类型:** `(rules: RuleSetRule | RuleSetRule[]) => void`
添加额外的 [Rspack rules](https://rspack.dev/config/module#modulerules)。
添加额外的 [Rspack rules](https://rspack.dev/config/module#modulerules) 到 Rspack rules 列表的最前面。
需要注意的是,Rspack loaders 会按照从右到左的顺序执行,如果你希望你添加的 loader(Normal Phase)先于其他 loader 执行,应使用 [appendRules](#appendrules) 将该规则添加到最后面。
示例:
Expand Down Expand Up @@ -265,9 +267,39 @@ export default {
};
```
:::tip
`addRules(...)` 会将新规则插入到 Rspack rules 列表的最前面。如果想要在末尾插入新规则,请改用 `config.module.rules.push(...)`
:::
### appendRules
- **类型:** `(rules: RuleSetRule | RuleSetRule[]) => void`
添加额外的 [Rspack rules](https://rspack.dev/config/module#modulerules) 到 Rspack rules 列表的最后面。
示例:
```ts
export default {
tools: {
rspack: (config, { appendRules }) => {
// 追加单条规则
appendRules({
test: /\.foo/,
loader: require.resolve('foo-loader'),
});

// 以数组形式追加多条规则
appendRules([
{
test: /\.foo/,
loader: require.resolve('foo-loader'),
},
{
test: /\.bar/,
loader: require.resolve('bar-loader'),
},
]);
},
},
};
```
### prependPlugins
Expand Down

2 comments on commit 34339d5

@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 ❌ failure
plugins ✅ success
rspress ✅ success
rslib ✅ success
examples ✅ 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.

📝 Ran ecosystem CI: Open

suite result
modernjs ❌ failure
plugins ✅ success
rspress ✅ success
rslib ✅ success
examples ✅ success

Please sign in to comment.