Skip to content

Commit

Permalink
feat: support for resolve and context in api.transform (#4426)
Browse files Browse the repository at this point in the history
  • Loading branch information
9aoy authored Jan 24, 2025
1 parent beeb2a8 commit 4855f2d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/core/src/loader/transformLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ export default async function transform(

const result = await transform({
code: source,
context: this.context,
resource: this.resource,
resourcePath: this.resourcePath,
resourceQuery: this.resourceQuery,
environment: getEnvironment(),
addDependency: this.addDependency.bind(this),
emitFile: this.emitFile.bind(this),
importModule: this.importModule.bind(this),
resolve: this.resolve.bind(this),
});

if (result === null || result === undefined) {
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ export type TransformContext = {
* The code of the module.
*/
code: string;
/**
* The directory path of the currently processed module,
* which changes with the location of each processed module.
*/
context: string | null;
/**
* The absolute path of the module, including the query.
* @example '/home/user/project/src/index.js?foo=123'
Expand Down Expand Up @@ -268,6 +273,10 @@ export type TransformContext = {
* Compile and execute a module at the build time.
*/
importModule: Rspack.LoaderContext['importModule'];
/**
* Resolve a module specifier.
*/
resolve: Rspack.LoaderContext['resolve'];
};

export type TransformHandler = (
Expand Down
4 changes: 4 additions & 0 deletions website/docs/en/plugins/dev/core.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,15 @@ The handler param is a transformation function that takes the current module cod
```ts
type TransformContext = {
code: string;
context: string | null;
resource: string;
resourcePath: string;
resourceQuery: string;
environment: EnvironmentContext;
addDependency: (file: string) => void;
emitFile: Rspack.LoaderContext['emitFile'];
importModule: Rspack.LoaderContext['importModule'];
resolve: Rspack.LoaderContext['resolve'];
};

type TransformResult =
Expand All @@ -298,6 +300,8 @@ type TransformHandler = (
The `handler` function provides the following params:

- `code`: The code of the module.
- `context`: The directory path of the currently processed module. The same as Rspack loader's [this.context](https://rspack.dev/api/loader-api/context#thiscontext).
- `resolve`: Resolve a module specifier. The same as Rspack loader's [this.resolve](https://rspack.dev/api/loader-api/context#thisresolve).
- `resource`: The absolute path of the module, including the query.
- `resourcePath`: The absolute path of the module, without the query.
- `resourceQuery`: The query of the module.
Expand Down
4 changes: 4 additions & 0 deletions website/docs/zh/plugins/dev/core.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,15 @@ handler 参数是一个转换函数,接收模块当前的代码,并返回转
```ts
type TransformContext = {
code: string;
context: string | null;
resource: string;
resourcePath: string;
resourceQuery: string;
environment: EnvironmentContext;
addDependency: (file: string) => void;
emitFile: Rspack.LoaderContext['emitFile'];
importModule: Rspack.LoaderContext['importModule'];
resolve: Rspack.LoaderContext['resolve'];
};

type TransformResult =
Expand All @@ -296,6 +298,8 @@ type TransformHandler = (
handler 函数提供以下参数:

- `code`:模块的代码。
- `context`:当前被处理的模块所在的目录路径。与 Rspack loader 的 [this.context](https://rspack.dev/zh/api/loader-api/context#thiscontext) 相同。
- `resolve`:解析一个模块标识符。与 Rspack loader 的 [this.resolve](https://rspack.dev/zh/api/loader-api/context#thisresolve) 相同。
- `resource`:模块的绝对路径,包含 query。
- `resourcePath`:模块的绝对路径,不包含 query。
- `resourceQuery`:模块路径上的 query。
Expand Down

1 comment on commit 4855f2d

@github-actions
Copy link
Contributor

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.