Skip to content

Commit

Permalink
feat: add issuer and with options to api.transform (#4354)
Browse files Browse the repository at this point in the history
  • Loading branch information
9aoy authored Jan 11, 2025
1 parent 7840b2b commit 718c89d
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { build, rspackOnlyTest } from '@e2e/helper';
import { expect } from '@playwright/test';

rspackOnlyTest(
'should allow plugin to transform code with import attributes',
async () => {
const rsbuild = await build({
cwd: __dirname,
});

const indexJs = await rsbuild.getIndexFile();
expect(indexJs.content).toContain('with import attributes');
},
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { RsbuildPlugin } from '@rsbuild/core';

export const myPlugin: RsbuildPlugin = {
name: 'my-plugin',
setup(api) {
api.transform({ with: { type: 'json' } }, () => {
return JSON.stringify({ type: 'with import attributes' });
});
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from '@rsbuild/core';
import { myPlugin } from './myPlugin';

export default defineConfig({
plugins: [myPlugin],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import one from './pkg-1.json' with { type: 'json' };

console.log('one', one);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "none"
}
8 changes: 8 additions & 0 deletions packages/core/src/initPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,14 @@ export function initPluginAPI({
rule.issuerLayer(descriptor.issuerLayer);
}

if (descriptor.issuer) {
rule.issuer(descriptor.issuer);
}

if (descriptor.with) {
rule.with(descriptor.with);
}

const loaderName = descriptor.raw
? 'transformRawLoader.mjs'
: 'transformLoader.mjs';
Expand Down
11 changes: 11 additions & 0 deletions packages/core/src/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,17 @@ export type TransformDescriptor = {
* @see https://rspack.dev/config/module#ruleissuerlayer
*/
issuerLayer?: string;
/**
* Matches all modules that match this resource, and will match against Resource
* (the absolute path without query and fragment) of the module that issued the current module.
* @see https://rspack.dev/config/module#ruleissuer
*/
issuer?: Rspack.RuleSetCondition;
/**
* `with` can be used in conjunction with [import attributes](https://github.com/tc39/proposal-import-attributes).
* @see https://rspack.dev/config/module#rulewith
*/
with?: Record<string, Rspack.RuleSetCondition>;
};

export type TransformFn = (
Expand Down
2 changes: 2 additions & 0 deletions website/docs/en/plugins/dev/core.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ api.transform({ test: /\.node$/, raw: true }, ({ code }) => {

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

### Handler param

Expand Down
2 changes: 2 additions & 0 deletions website/docs/zh/plugins/dev/core.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ api.transform({ test: /\.node$/, raw: true }, ({ code }) => {

- `layer`:等同于 Rspack 的 [Rule.layer](https://rspack.dev/config/module#rulelayer)
- `issuerLayer`:等同于 Rspack 的 [Rule.issuerLayer](https://rspack.dev/config/module#ruleissuerlayer)
- `issuer`:等同于 Rspack 的 [Rule.issuer](https://rspack.dev/config/module#ruleissuer)
- `with`:等同于 Rspack 的 [Rule.with](https://rspack.dev/config/module#rulewith)

### handler 参数

Expand Down

1 comment on commit 718c89d

@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 ❌ failure
examples ✅ success

Please sign in to comment.