Skip to content

Commit 841e0f0

Browse files
authored
docs: update TransformDescriptor to include more conditions (#5158)
1 parent 7bd5750 commit 841e0f0

File tree

2 files changed

+79
-19
lines changed

2 files changed

+79
-19
lines changed

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

+39-9
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ const pluginPug = () => ({
203203

204204
### Descriptor param
205205

206-
The descriptor param is an object describing the module's matching conditions.
206+
The `descriptor` param is an object describing the module's matching conditions.
207207

208208
- **Type:**
209209

@@ -215,6 +215,9 @@ type TransformDescriptor = {
215215
resourceQuery?: RuleSetCondition;
216216
raw?: boolean;
217217
layer?: string;
218+
issuer?: RuleSetCondition;
219+
issuerLayer?: string;
220+
with?: Record<string, RuleSetCondition>;
218221
};
219222
```
220223

@@ -223,23 +226,23 @@ The `descriptor` param supports the following matching conditions:
223226
- `test`: matches module's path (without query), the same as Rspack's [Rule.test](https://rspack.dev/config/module#ruletest).
224227

225228
```js
226-
api.transform({ test: /\.pug$/ }, ({ code }) => {
229+
api.transform({ test: /\.md$/ }, ({ code }) => {
227230
// ...
228231
});
229232
```
230233

231234
- `targets`: matches the Rsbuild [output.target](/config/output/target), and applies the current transform function only to the matched targets.
232235

233236
```js
234-
api.transform({ test: /\.pug$/, targets: ['web'] }, ({ code }) => {
237+
api.transform({ test: /\.md$/, targets: ['web'] }, ({ code }) => {
235238
// ...
236239
});
237240
```
238241

239242
- `environments`: matches the Rsbuild [environment](/guide/advanced/environments) name, and applies the current transform function only to the matched environments.
240243

241244
```js
242-
api.transform({ test: /\.pug$/, environments: ['web'] }, ({ code }) => {
245+
api.transform({ test: /\.md$/, environments: ['web'] }, ({ code }) => {
243246
// ...
244247
});
245248
```
@@ -260,10 +263,37 @@ api.transform({ test: /\.node$/, raw: true }, ({ code }) => {
260263
});
261264
```
262265

263-
- `layer`: the same as Rspack's [Rule.layer](https://rspack.dev/config/module#rulelayer).
264-
- `issuerLayer`: the same as Rspack's [Rule.issuerLayer](https://rspack.dev/config/module#ruleissuerlayer).
265-
- `issuer`: the same as Rspack's [Rule.issuer](https://rspack.dev/config/module#ruleissuer).
266-
- `with`: the same as Rspack's [Rule.with](https://rspack.dev/config/module#rulewith).
266+
- `layer`: mark the layer of the matching module, can be used to group a group of modules into one layer, the same as Rspack's [Rule.layer](https://rspack.dev/config/module#rulelayer).
267+
268+
```js
269+
api.transform({ test: /\.md$/, layer: 'foo' }, ({ code }) => {
270+
// ...
271+
});
272+
```
273+
274+
- `issuerLayer`: match the layer of the module that issues the current module, the same as Rspack's [Rule.issuerLayer](https://rspack.dev/config/module#ruleissuerlayer).
275+
276+
```js
277+
api.transform({ test: /\.md$/, issuerLayer: 'foo' }, ({ code }) => {
278+
// ...
279+
});
280+
```
281+
282+
- `issuer`: match the absolute path of the module that issues the current module, the same as Rspack's [Rule.issuer](https://rspack.dev/config/module#ruleissuer).
283+
284+
```js
285+
api.transform({ test: /\.md$/, issuer: /\.js$/ }, ({ code }) => {
286+
// ...
287+
});
288+
```
289+
290+
- `with`: match [import attributes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import/with), the same as Rspack's [Rule.with](https://rspack.dev/config/module#rulewith).
291+
292+
```js
293+
api.transform({ test: /\.md$/, with: { type: 'url' } }, ({ code }) => {
294+
// ...
295+
});
296+
```
267297

268298
### Handler param
269299

@@ -318,7 +348,7 @@ For example:
318348

319349
```js
320350
api.transform(
321-
{ test: /\.pug$/ },
351+
{ test: /\.md$/ },
322352
({ code, resource, resourcePath, resourceQuery }) => {
323353
console.log(code); // -> some code
324354
console.log(resource); // -> '/home/user/project/src/template.pug?foo=123'

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

+40-10
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ const pluginPug = () => ({
201201

202202
### descriptor 参数
203203

204-
descriptor 参数是一个对象,用于描述模块的匹配条件。
204+
`descriptor` 参数是一个对象,用于描述模块的匹配条件。
205205

206206
- **类型:**
207207

@@ -213,31 +213,34 @@ type TransformDescriptor = {
213213
resourceQuery?: RuleSetCondition;
214214
raw?: boolean;
215215
layer?: string;
216+
issuer?: RuleSetCondition;
217+
issuerLayer?: string;
218+
with?: Record<string, RuleSetCondition>;
216219
};
217220
```
218221

219-
descriptor 参数支持设置以下匹配条件:
222+
`descriptor` 参数支持设置以下匹配条件:
220223

221224
- `test`:匹配模块的路径(不包含 query),等价于 Rspack 的 [Rule.test](https://rspack.dev/zh/config/module#ruletest)
222225

223226
```js
224-
api.transform({ test: /\.pug$/ }, ({ code }) => {
227+
api.transform({ test: /\.md$/ }, ({ code }) => {
225228
// ...
226229
});
227230
```
228231

229232
- `targets`:匹配 Rsbuild [output.target](/config/output/target),仅对匹配的 targets 应用当前 transform 函数。
230233

231234
```js
232-
api.transform({ test: /\.pug$/, targets: ['web'] }, ({ code }) => {
235+
api.transform({ test: /\.md$/, targets: ['web'] }, ({ code }) => {
233236
// ...
234237
});
235238
```
236239

237240
- `environments`:匹配 Rsbuild [environment](/guide/advanced/environments) name,仅对匹配的 environments 应用当前 transform 函数。
238241

239242
```js
240-
api.transform({ test: /\.pug$/, environments: ['web'] }, ({ code }) => {
243+
api.transform({ test: /\.md$/, environments: ['web'] }, ({ code }) => {
241244
// ...
242245
});
243246
```
@@ -258,10 +261,37 @@ api.transform({ test: /\.node$/, raw: true }, ({ code }) => {
258261
});
259262
```
260263

261-
- `layer`:等同于 Rspack 的 [Rule.layer](https://rspack.dev/zh/config/module#rulelayer)
262-
- `issuerLayer`:等同于 Rspack 的 [Rule.issuerLayer](https://rspack.dev/zh/config/module#ruleissuerlayer)
263-
- `issuer`:等同于 Rspack 的 [Rule.issuer](https://rspack.dev/zh/config/module#ruleissuer)
264-
- `with`:等同于 Rspack 的 [Rule.with](https://rspack.dev/zh/config/module#rulewith)
264+
- `layer`:标识匹配的模块的 [layer](https://rspack.dev/zh/config/experiments#experimentslayers),可以将一组模块聚合到一个 layer 中,等同于 Rspack 的 [Rule.layer](https://rspack.dev/zh/config/module#rulelayer)
265+
266+
```js
267+
api.transform({ test: /\.md$/, layer: 'foo' }, ({ code }) => {
268+
// ...
269+
});
270+
```
271+
272+
- `issuerLayer`:与"引入当前模块"的模块的 [layer](https://rspack.dev/zh/config/experiments#experimentslayers) 进行匹配,等同于 Rspack 的 [Rule.issuerLayer](https://rspack.dev/zh/config/module#ruleissuerlayer)
273+
274+
```js
275+
api.transform({ test: /\.md$/, issuerLayer: 'foo' }, ({ code }) => {
276+
// ...
277+
});
278+
```
279+
280+
- `issuer`:匹配"引入当前模块"的模块的绝对路径,等同于 Rspack 的 [Rule.issuer](https://rspack.dev/zh/config/module#ruleissuer)
281+
282+
```js
283+
api.transform({ test: /\.md$/, issuer: /\.js$/ }, ({ code }) => {
284+
// ...
285+
});
286+
```
287+
288+
- `with`:匹配 [import attributes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import/with),等同于 Rspack 的 [Rule.with](https://rspack.dev/zh/config/module#rulewith)
289+
290+
```js
291+
api.transform({ test: /\.md$/, with: { type: 'url' } }, ({ code }) => {
292+
// ...
293+
});
294+
```
265295

266296
### handler 参数
267297

@@ -316,7 +346,7 @@ handler 函数提供以下参数:
316346

317347
```js
318348
api.transform(
319-
{ test: /\.pug$/ },
349+
{ test: /\.md$/ },
320350
({ code, resource, resourcePath, resourceQuery }) => {
321351
console.log(code); // -> some code
322352
console.log(resource); // -> '/home/user/project/src/template.pug?foo=123'

0 commit comments

Comments
 (0)