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

fix: shared Rsbuild plugins should run only once #552

Merged
merged 2 commits into from
Dec 13, 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
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,
Timeless0911 marked this conversation as resolved.
Show resolved Hide resolved
environments: pruneEnvironments(environments, options.lib),
},
});
Expand Down
9 changes: 8 additions & 1 deletion packages/core/src/cli/mf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,15 @@ async function initMFRsbuild(
}

mfRsbuildConfig.config = changeEnvToDev(mfRsbuildConfig.config);

const rsbuildInstance = await createRsbuild({
rsbuildConfig: mfRsbuildConfig.config,
rsbuildConfig: {
...mfRsbuildConfig.config,
plugins: [
...(rslibConfig.plugins || []),
...(mfRsbuildConfig.config.plugins || []),
Timeless0911 marked this conversation as resolved.
Show resolved Hide resolved
],
},
});
const devServer = await rsbuildInstance.startDevServer();

Expand Down
19 changes: 15 additions & 4 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
type EnvironmentConfig,
type RsbuildConfig,
type RsbuildPlugin,
type RsbuildPlugins,
defineConfig as defineRsbuildConfig,
loadConfig as loadRsbuildConfig,
mergeRsbuildConfig,
Expand Down Expand Up @@ -1143,8 +1144,11 @@ const composeExternalHelpersConfig = (
return defaultConfig;
};

async function composeLibRsbuildConfig(config: LibConfig) {
checkMFPlugin(config);
async function composeLibRsbuildConfig(
config: LibConfig,
sharedPlugins?: RsbuildPlugins,
) {
checkMFPlugin(config, sharedPlugins);

// Get the absolute path of the root directory to align with Rsbuild's default behavior
const rootPath = config.root
Expand Down Expand Up @@ -1258,7 +1262,11 @@ export async function composeCreateRsbuildConfig(
rslibConfig: RslibConfig,
): Promise<RsbuildConfigWithLibInfo[]> {
const constantRsbuildConfig = await createConstantRsbuildConfig();
const { lib: libConfigsArray, ...sharedRsbuildConfig } = rslibConfig;
const {
lib: libConfigsArray,
plugins: sharedPlugins,
...sharedRsbuildConfig
} = rslibConfig;

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

// Merge the configuration of each environment based on the shared Rsbuild
// configuration and Lib configuration in the settings.
const libRsbuildConfig = await composeLibRsbuildConfig(userConfig);
const libRsbuildConfig = await composeLibRsbuildConfig(
userConfig,
sharedPlugins,
);

// Reset certain fields because they will be completely overridden by the upcoming merge.
// We don't want to retain them in the final configuration.
Expand Down
14 changes: 9 additions & 5 deletions packages/core/src/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,16 +184,20 @@ export function isPluginIncluded(
);
}

export function checkMFPlugin(config: LibConfig): boolean {
export function checkMFPlugin(
config: LibConfig,
sharedPlugins?: RsbuildPlugins,
): boolean {
if (config.format !== 'mf') {
return true;
}

// https://github.com/module-federation/core/blob/4e5c4b96ee45899f3ba5904b8927768980d5ad0e/packages/rsbuild-plugin/src/cli/index.ts#L17
const added = isPluginIncluded(
'rsbuild:module-federation-enhanced',
config.plugins,
);
const added = isPluginIncluded('rsbuild:module-federation-enhanced', [
...(sharedPlugins || []),
...(config.plugins || []),
]);

if (!added) {
logger.warn(
`${color.green('format: "mf"')} should be used with ${color.blue(
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';
Loading