Skip to content

Commit

Permalink
feat: support for importModule in api.transform (#4217)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Dec 18, 2024
1 parent a74ee52 commit 5cdaa16
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 9 deletions.
18 changes: 18 additions & 0 deletions e2e/cases/plugin-api/plugin-transform-import-module/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { build, rspackOnlyTest } from '@e2e/helper';
import { expect } from '@playwright/test';

rspackOnlyTest(
'should allow plugin to transform code and call `importModule`',
async () => {
const rsbuild = await build({
cwd: __dirname,
});

const files = await rsbuild.unwrapOutputJSON();
const indexCss = Object.keys(files).find(
(file) => file.includes('index') && file.endsWith('.css'),
);

expect(files[indexCss!].includes('#00f')).toBeTruthy();
},
);
13 changes: 13 additions & 0 deletions e2e/cases/plugin-api/plugin-transform-import-module/myPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { join } from 'node:path';
import type { RsbuildPlugin } from '@rsbuild/core';

export const myPlugin: RsbuildPlugin = {
name: 'my-plugin',
setup(api) {
api.transform({ test: /\.css$/ }, async ({ code, importModule }) => {
// @ts-expect-error TODO: Rspack type issue
const { foo } = await importModule(join(__dirname, './src/foo.ts'));
return code.replace('red', foo);
});
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { myPlugin } from './myPlugin';

export default {
plugins: [myPlugin],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'blue';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
color: red;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import './index.css';

console.log('hello');
7 changes: 4 additions & 3 deletions packages/core/src/loader/transformLoader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { LoaderContext } from '@rspack/core';
import type { EnvironmentContext, Rspack, TransformContext } from '../types';
import type { EnvironmentContext, Rspack } from '../types';

export type TransformLoaderOptions = {
id: string;
Expand Down Expand Up @@ -30,8 +30,9 @@ export default async function transform(
resourcePath: this.resourcePath,
resourceQuery: this.resourceQuery,
environment: getEnvironment(),
addDependency: this.addDependency,
emitFile: this.emitFile as TransformContext['emitFile'],
addDependency: this.addDependency.bind(this),
emitFile: this.emitFile.bind(this),
importModule: this.importModule.bind(this),
});

if (result === null || result === undefined) {
Expand Down
11 changes: 5 additions & 6 deletions packages/core/src/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,11 @@ export type TransformContext = {
* @param sourceMap source map of the asset
* @param assetInfo additional asset information
*/
emitFile: (
name: string,
content: string | Buffer,
sourceMap?: string,
assetInfo?: Record<string, any>,
) => void;
emitFile: Rspack.LoaderContext['emitFile'];
/**
* Compile and execute a module at the build time.
*/
importModule: Rspack.LoaderContext['importModule'];
};

export type TransformHandler = (
Expand Down

1 comment on commit 5cdaa16

@rspack-bot
Copy link

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.