Skip to content

Commit 47ebad1

Browse files
authored
feat: add enforce option to api.transform (#5174)
1 parent 5a51ae7 commit 47ebad1

File tree

8 files changed

+69
-0
lines changed

8 files changed

+69
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { build, rspackOnlyTest } from '@e2e/helper';
2+
import { expect } from '@playwright/test';
3+
4+
rspackOnlyTest(
5+
'should allow plugin to specify the execution order via `enforce`',
6+
async () => {
7+
const rsbuild = await build({
8+
cwd: __dirname,
9+
});
10+
11+
const indexJs = await rsbuild.getIndexFile();
12+
expect(indexJs.content).toContain('with enforce: pre');
13+
},
14+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { RsbuildPlugin } from '@rsbuild/core';
2+
3+
export const myPlugin: RsbuildPlugin = {
4+
name: 'my-plugin',
5+
setup(api) {
6+
api.transform({ test: /\.js$/ }, ({ code }) => {
7+
return code.replace('__placeholder__', 'without enforce');
8+
});
9+
10+
api.transform({ test: /\.js$/, enforce: 'pre' }, ({ code }) => {
11+
return code.replace('__placeholder__', 'with enforce: pre');
12+
});
13+
},
14+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { defineConfig } from '@rsbuild/core';
2+
import { myPlugin } from './myPlugin';
3+
4+
export default defineConfig({
5+
plugins: [myPlugin],
6+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('__placeholder__');

packages/core/src/initPlugins.ts

+3
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,9 @@ export function initPluginAPI({
295295
if (descriptor.mimetype) {
296296
rule.mimetype(descriptor.mimetype);
297297
}
298+
if (descriptor.enforce) {
299+
rule.enforce(descriptor.enforce);
300+
}
298301

299302
const loaderName = descriptor.raw
300303
? 'transformRawLoader.mjs'

packages/core/src/types/plugin.ts

+9
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,15 @@ export type TransformDescriptor = {
378378
* @see https://rspack.dev/config/module#rulemimetype
379379
*/
380380
mimetype?: Rspack.RuleSetCondition;
381+
/**
382+
* Specifies the execution order of the transform function.
383+
* - When specified as 'pre', the transform function will execute before other
384+
* transform functions (or Rspack loaders).
385+
* - When specified as 'post', the transform function will execute after other
386+
* transform functions (or Rspack loaders).
387+
* @see https://rspack.dev/config/module#ruleenforce
388+
*/
389+
enforce?: 'pre' | 'post';
381390
};
382391

383392
export type TransformHook = (

website/docs/en/plugins/dev/core.mdx

+11
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ type TransformDescriptor = {
219219
issuerLayer?: string;
220220
with?: Record<string, RuleSetCondition>;
221221
mimetype?: RuleSetCondition;
222+
enforce?: 'pre' | 'post';
222223
};
223224
```
224225

@@ -304,6 +305,16 @@ api.transform({ mimetype: 'text/javascript' }, ({ code }) => {
304305
});
305306
```
306307

308+
- `enforce`: Specifies the execution order of the transform function, the same as Rspack's [Rule.enforce](https://rspack.dev/config/module#ruleenforce).
309+
- When `enforce` is `pre`, the transform function will be executed before other transform functions (or Rspack loaders).
310+
- When `enforce` is `post`, the transform function will be executed after other transform functions (or Rspack loaders).
311+
312+
```js
313+
api.transform({ test: /\.md$/, enforce: 'pre' }, ({ code }) => {
314+
// ...
315+
});
316+
```
317+
307318
### Handler param
308319

309320
The handler param is a transformation function that takes the current module code and returns the transformed code.

website/docs/zh/plugins/dev/core.mdx

+11
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ type TransformDescriptor = {
217217
issuerLayer?: string;
218218
with?: Record<string, RuleSetCondition>;
219219
mimetype?: RuleSetCondition;
220+
enforce?: 'pre' | 'post';
220221
};
221222
```
222223

@@ -302,6 +303,16 @@ api.transform({ mimetype: 'text/javascript' }, ({ code }) => {
302303
});
303304
```
304305

306+
- `enforce`:指定 transform 函数的执行顺序,等同于 Rspack 的 [Rule.enforce](https://rspack.dev/zh/config/module#ruleenforce)
307+
-`enforce``pre` 时,transform 函数会在其他 transform 函数(或 Rspack loader)之前执行。
308+
-`enforce``post` 时,transform 函数会在其他 transform 函数(或 Rspack loader)之后执行。
309+
310+
```js
311+
api.transform({ test: /\.md$/, enforce: 'pre' }, ({ code }) => {
312+
// ...
313+
});
314+
```
315+
305316
### handler 参数
306317

307318
handler 参数是一个转换函数,接收模块当前的代码,并返回转换后的代码。

0 commit comments

Comments
 (0)