Skip to content

Commit

Permalink
fix: shared Rsbuild plugin should run only once
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan committed Dec 12, 2024
1 parent e3f98f1 commit fa5ea86
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/core/src/cli/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export async function build(
const environments = await composeRsbuildEnvironments(config);
const rsbuildInstance = await createRsbuild({
rsbuildConfig: {
plugins: config.plugins,
environments: pruneEnvironments(environments, options.lib),
},
});
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/cli/inspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export async function inspect(
const environments = await composeRsbuildEnvironments(config);
const rsbuildInstance = await createRsbuild({
rsbuildConfig: {
plugins: config.plugins,
environments: pruneEnvironments(environments, options.lib),
},
});
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1258,7 +1258,7 @@ export async function composeCreateRsbuildConfig(
rslibConfig: RslibConfig,
): Promise<RsbuildConfigWithLibInfo[]> {
const constantRsbuildConfig = await createConstantRsbuildConfig();
const { lib: libConfigsArray, ...sharedRsbuildConfig } = rslibConfig;
const { lib: libConfigsArray, plugins, ...sharedRsbuildConfig } = rslibConfig;

if (!libConfigsArray) {
throw new Error(
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions tests/integration/plugins/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import fs from 'node:fs';
import { buildAndGetResults } from 'test-helper';
import { expect, test } from 'vitest';
import { distIndex } from './rslib.config';

test('should run shared plugins only once', async () => {
const fixturePath = __dirname;
await buildAndGetResults({ fixturePath });

const content = fs.readFileSync(distIndex, 'utf-8');
expect(content).toEqual('count: 1');
});
6 changes: 6 additions & 0 deletions tests/integration/plugins/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "plugins-test",
"version": "1.0.0",
"private": true,
"type": "module"
}
21 changes: 21 additions & 0 deletions tests/integration/plugins/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import fs from 'node:fs';
import path from 'node:path';
import type { RsbuildPlugin } from '@rsbuild/core';
import { defineConfig } from '@rslib/core';

let count = 0;
export const distIndex = path.resolve(__dirname, 'dist/count.txt');

const testPlugin: RsbuildPlugin = {
name: 'test',
setup(api) {
api.onAfterBuild(() => {
fs.writeFileSync(distIndex, `count: ${++count}`);
});
},
};

export default defineConfig({
lib: [{ format: 'esm' }, { format: 'cjs' }],
plugins: [testPlugin],
});
1 change: 1 addition & 0 deletions tests/integration/plugins/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const test = 'hello';

0 comments on commit fa5ea86

Please sign in to comment.