Skip to content

Commit

Permalink
feat: add layer option to api.transform() (#3883)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Nov 2, 2024
1 parent 842dd1e commit d2449ba
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/core/src/initPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ export function initPluginAPI({
if (descriptor.resourceQuery) {
rule.resourceQuery(descriptor.resourceQuery);
}
if (descriptor.layer) {
rule.layer(descriptor.layer);
}

const loaderName = descriptor.raw
? 'transformRawLoader.cjs'
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,13 @@ export type TransformDescriptor = {
* @see https://rspack.dev/api/loader-api/examples#raw-loader
*/
raw?: boolean;
/**
* Used to mark the layer of the matching module.
* A group of modules could be united in one layer which could then be used in
* split chunks, stats or entry options.
* @see https://rspack.dev/config/module#rulelayer
*/
layer?: string;
};

export type TransformFn = (
Expand Down
3 changes: 3 additions & 0 deletions website/docs/en/plugins/dev/core.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ type TransformDescriptor = {
environments?: string[];
resourceQuery?: RuleSetCondition;
raw?: boolean;
layer?: string;
};
```

Expand Down Expand Up @@ -257,6 +258,8 @@ api.transform({ test: /\.node$/, raw: true }, ({ code }) => {
});
```

- `layer`: the same as Rspack's [Rule.layer](https://rspack.dev/config/module#rulelayer).

### Handler Param

The handler param is a transformation function that takes the current module code and returns the transformed code.
Expand Down
3 changes: 3 additions & 0 deletions website/docs/zh/plugins/dev/core.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ type TransformDescriptor = {
environments?: string[];
resourceQuery?: RuleSetCondition;
raw?: boolean;
layer?: string;
};
```

Expand Down Expand Up @@ -255,6 +256,8 @@ api.transform({ test: /\.node$/, raw: true }, ({ code }) => {
});
```

- `layer`:等同于 Rspack 的 [Rule.layer](https://rspack.dev/config/module#rulelayer)

### handler 参数

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

0 comments on commit d2449ba

Please sign in to comment.