Skip to content

Commit 5316efc

Browse files
authored
fix: shared Rsbuild plugins should run only once (#552)
1 parent aa4c5f3 commit 5316efc

File tree

10 files changed

+76
-10
lines changed

10 files changed

+76
-10
lines changed

packages/core/src/cli/build.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export async function build(
1111
const environments = await composeRsbuildEnvironments(config);
1212
const rsbuildInstance = await createRsbuild({
1313
rsbuildConfig: {
14+
plugins: config.plugins,
1415
environments: pruneEnvironments(environments, options.lib),
1516
},
1617
});

packages/core/src/cli/inspect.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export async function inspect(
1010
const environments = await composeRsbuildEnvironments(config);
1111
const rsbuildInstance = await createRsbuild({
1212
rsbuildConfig: {
13+
plugins: config.plugins,
1314
environments: pruneEnvironments(environments, options.lib),
1415
},
1516
});

packages/core/src/cli/mf.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,15 @@ async function initMFRsbuild(
2525
}
2626

2727
mfRsbuildConfig.config = changeEnvToDev(mfRsbuildConfig.config);
28+
2829
const rsbuildInstance = await createRsbuild({
29-
rsbuildConfig: mfRsbuildConfig.config,
30+
rsbuildConfig: {
31+
...mfRsbuildConfig.config,
32+
plugins: [
33+
...(rslibConfig.plugins || []),
34+
...(mfRsbuildConfig.config.plugins || []),
35+
],
36+
},
3037
});
3138
const devServer = await rsbuildInstance.startDevServer();
3239

packages/core/src/config.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
type EnvironmentConfig,
55
type RsbuildConfig,
66
type RsbuildPlugin,
7+
type RsbuildPlugins,
78
defineConfig as defineRsbuildConfig,
89
loadConfig as loadRsbuildConfig,
910
mergeRsbuildConfig,
@@ -1143,8 +1144,11 @@ const composeExternalHelpersConfig = (
11431144
return defaultConfig;
11441145
};
11451146

1146-
async function composeLibRsbuildConfig(config: LibConfig) {
1147-
checkMFPlugin(config);
1147+
async function composeLibRsbuildConfig(
1148+
config: LibConfig,
1149+
sharedPlugins?: RsbuildPlugins,
1150+
) {
1151+
checkMFPlugin(config, sharedPlugins);
11481152

11491153
// Get the absolute path of the root directory to align with Rsbuild's default behavior
11501154
const rootPath = config.root
@@ -1258,7 +1262,11 @@ export async function composeCreateRsbuildConfig(
12581262
rslibConfig: RslibConfig,
12591263
): Promise<RsbuildConfigWithLibInfo[]> {
12601264
const constantRsbuildConfig = await createConstantRsbuildConfig();
1261-
const { lib: libConfigsArray, ...sharedRsbuildConfig } = rslibConfig;
1265+
const {
1266+
lib: libConfigsArray,
1267+
plugins: sharedPlugins,
1268+
...sharedRsbuildConfig
1269+
} = rslibConfig;
12621270

12631271
if (!libConfigsArray) {
12641272
throw new Error(
@@ -1274,7 +1282,10 @@ export async function composeCreateRsbuildConfig(
12741282

12751283
// Merge the configuration of each environment based on the shared Rsbuild
12761284
// configuration and Lib configuration in the settings.
1277-
const libRsbuildConfig = await composeLibRsbuildConfig(userConfig);
1285+
const libRsbuildConfig = await composeLibRsbuildConfig(
1286+
userConfig,
1287+
sharedPlugins,
1288+
);
12781289

12791290
// Reset certain fields because they will be completely overridden by the upcoming merge.
12801291
// We don't want to retain them in the final configuration.

packages/core/src/utils/helper.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,16 +184,20 @@ export function isPluginIncluded(
184184
);
185185
}
186186

187-
export function checkMFPlugin(config: LibConfig): boolean {
187+
export function checkMFPlugin(
188+
config: LibConfig,
189+
sharedPlugins?: RsbuildPlugins,
190+
): boolean {
188191
if (config.format !== 'mf') {
189192
return true;
190193
}
191194

192195
// https://github.com/module-federation/core/blob/4e5c4b96ee45899f3ba5904b8927768980d5ad0e/packages/rsbuild-plugin/src/cli/index.ts#L17
193-
const added = isPluginIncluded(
194-
'rsbuild:module-federation-enhanced',
195-
config.plugins,
196-
);
196+
const added = isPluginIncluded('rsbuild:module-federation-enhanced', [
197+
...(sharedPlugins || []),
198+
...(config.plugins || []),
199+
]);
200+
197201
if (!added) {
198202
logger.warn(
199203
`${color.green('format: "mf"')} should be used with ${color.blue(

pnpm-lock.yaml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import fs from 'node:fs';
2+
import { buildAndGetResults } from 'test-helper';
3+
import { expect, test } from 'vitest';
4+
import { distIndex } from './rslib.config';
5+
6+
test('should run shared plugins only once', async () => {
7+
const fixturePath = __dirname;
8+
await buildAndGetResults({ fixturePath });
9+
10+
const content = fs.readFileSync(distIndex, 'utf-8');
11+
expect(content).toEqual('count: 1');
12+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "plugins-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module"
6+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import type { RsbuildPlugin } from '@rsbuild/core';
4+
import { defineConfig } from '@rslib/core';
5+
6+
let count = 0;
7+
export const distIndex = path.resolve(__dirname, 'dist/count.txt');
8+
9+
const testPlugin: RsbuildPlugin = {
10+
name: 'test',
11+
setup(api) {
12+
api.onAfterBuild(() => {
13+
fs.writeFileSync(distIndex, `count: ${++count}`);
14+
});
15+
},
16+
};
17+
18+
export default defineConfig({
19+
lib: [{ format: 'esm' }, { format: 'cjs' }],
20+
plugins: [testPlugin],
21+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const test = 'hello';

0 commit comments

Comments
 (0)