Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support for importModule in api.transform #4217

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading