From fa5ea86f47c06e4a663d803930a40e661c39b4ae Mon Sep 17 00:00:00 2001 From: neverland Date: Thu, 12 Dec 2024 18:53:03 +0800 Subject: [PATCH] fix: shared Rsbuild plugin should run only once --- packages/core/src/cli/build.ts | 1 + packages/core/src/cli/inspect.ts | 1 + packages/core/src/config.ts | 2 +- pnpm-lock.yaml | 2 ++ tests/integration/plugins/index.test.ts | 12 ++++++++++++ tests/integration/plugins/package.json | 6 ++++++ tests/integration/plugins/rslib.config.ts | 21 +++++++++++++++++++++ tests/integration/plugins/src/index.ts | 1 + 8 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 tests/integration/plugins/index.test.ts create mode 100644 tests/integration/plugins/package.json create mode 100644 tests/integration/plugins/rslib.config.ts create mode 100644 tests/integration/plugins/src/index.ts diff --git a/packages/core/src/cli/build.ts b/packages/core/src/cli/build.ts index c305c84d5..b1a7ec41e 100644 --- a/packages/core/src/cli/build.ts +++ b/packages/core/src/cli/build.ts @@ -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), }, }); diff --git a/packages/core/src/cli/inspect.ts b/packages/core/src/cli/inspect.ts index cfe67a6c7..1710e8d74 100644 --- a/packages/core/src/cli/inspect.ts +++ b/packages/core/src/cli/inspect.ts @@ -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), }, }); diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index 30f019b71..33b8d2c1d 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -1258,7 +1258,7 @@ export async function composeCreateRsbuildConfig( rslibConfig: RslibConfig, ): Promise { const constantRsbuildConfig = await createConstantRsbuildConfig(); - const { lib: libConfigsArray, ...sharedRsbuildConfig } = rslibConfig; + const { lib: libConfigsArray, plugins, ...sharedRsbuildConfig } = rslibConfig; if (!libConfigsArray) { throw new Error( diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e740298b8..9585662ec 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -687,6 +687,8 @@ importers: specifier: ^1.2.0 version: 1.2.0(@rsbuild/core@1.1.9) + tests/integration/plugins: {} + tests/integration/polyfill: dependencies: core-js-pure: diff --git a/tests/integration/plugins/index.test.ts b/tests/integration/plugins/index.test.ts new file mode 100644 index 000000000..5fac540ba --- /dev/null +++ b/tests/integration/plugins/index.test.ts @@ -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'); +}); diff --git a/tests/integration/plugins/package.json b/tests/integration/plugins/package.json new file mode 100644 index 000000000..e89a7a76b --- /dev/null +++ b/tests/integration/plugins/package.json @@ -0,0 +1,6 @@ +{ + "name": "plugins-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/plugins/rslib.config.ts b/tests/integration/plugins/rslib.config.ts new file mode 100644 index 000000000..cba12e767 --- /dev/null +++ b/tests/integration/plugins/rslib.config.ts @@ -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], +}); diff --git a/tests/integration/plugins/src/index.ts b/tests/integration/plugins/src/index.ts new file mode 100644 index 000000000..54cc258f8 --- /dev/null +++ b/tests/integration/plugins/src/index.ts @@ -0,0 +1 @@ +export const test = 'hello';