Skip to content

Commit 6623262

Browse files
committed
chore(rsbuild-plugin): split setUp function to help extend
1 parent 0166862 commit 6623262

File tree

10 files changed

+183
-155
lines changed

10 files changed

+183
-155
lines changed

.changeset/angry-spoons-wink.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/rsbuild-plugin': patch
3+
---
4+
5+
chore(rsbuild-plugin): split setUp function to help extend

.changeset/spicy-roses-hear.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/storybook-addon': patch
3+
---
4+
5+
chore: export plugin name

apps/rslib-module/.storybook/main.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const config: StorybookConfig = {
2929
options: {
3030
remotes: {
3131
'rslib-module':
32-
'rslib-module@http://localhost:3001/mf/mf-manifest.json',
32+
'rslib_provider@http://localhost:3001/mf-manifest.json',
3333
},
3434
},
3535
},

apps/rslib-module/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"@module-federation/rsbuild-plugin": "workspace:*",
2323
"@module-federation/storybook-addon": "workspace:*",
2424
"@rsbuild/plugin-react": "^1.0.6",
25-
"@rslib/core": "^0.0.18",
25+
"@rslib/core": "^0.1.2",
2626
"@types/react": "^18.3.11",
2727
"http-server": "^14.1.1",
2828
"react": "^18.3.1",

apps/rslib-module/rslib.config.ts

-6
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,7 @@ export default defineConfig({
3535
distPath: {
3636
root: './dist/mf',
3737
},
38-
assetPrefix: 'http://localhost:3000/mf',
39-
minify: true,
4038
},
41-
dev: {
42-
assetPrefix: 'http://localhost:3001/mf',
43-
},
44-
// just for dev
4539
server: {
4640
port: 3001,
4741
},

packages/rsbuild-plugin/src/cli/index.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,19 @@ import logger from '../logger';
1515
type ModuleFederationOptions =
1616
moduleFederationPlugin.ModuleFederationPluginOptions;
1717

18-
const PLUGIN_MODULE_FEDERATION_NAME = 'rsbuild:module-federation-enhanced';
18+
const RSBUILD_PLUGIN_MODULE_FEDERATION_NAME =
19+
'rsbuild:module-federation-enhanced';
20+
const RSPACK_PLUGIN_MODULE_FEDERATION_NAME = 'module-federation';
21+
22+
export {
23+
RSBUILD_PLUGIN_MODULE_FEDERATION_NAME,
24+
RSPACK_PLUGIN_MODULE_FEDERATION_NAME,
25+
};
26+
1927
export const pluginModuleFederation = (
2028
moduleFederationOptions: ModuleFederationOptions,
2129
): RsbuildPlugin => ({
22-
name: PLUGIN_MODULE_FEDERATION_NAME,
30+
name: RSBUILD_PLUGIN_MODULE_FEDERATION_NAME,
2331
setup: (api) => {
2432
const sharedOptions: [string, sharePlugin.SharedConfig][] = parseOptions(
2533
moduleFederationOptions.shared || [],
@@ -153,7 +161,7 @@ export const pluginModuleFederation = (
153161

154162
api.modifyBundlerChain(async (chain) => {
155163
chain
156-
.plugin('module-federation')
164+
.plugin(RSPACK_PLUGIN_MODULE_FEDERATION_NAME)
157165
.use(ModuleFederationPlugin, [moduleFederationOptions]);
158166

159167
// `uniqueName` is required for react refresh to work

packages/storybook-addon/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
"*": {
4545
".": [
4646
"./dist/index.d.ts"
47+
],
48+
"preset": [
49+
"./dist/preset.d.ts"
4750
]
4851
}
4952
},

packages/storybook-addon/preset.ts

+1
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ export default {
1818
});
1919
},
2020
};
21+
export { PLUGIN_NAME } from './src/utils/with-module-federation-enhanced-rsbuild';

packages/storybook-addon/src/utils/with-module-federation-enhanced-rsbuild.ts

+23-28
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { RsbuildConfig, RsbuildPlugin } from '@rsbuild/core';
99
import type { moduleFederationPlugin } from '@module-federation/sdk';
1010

1111
const tempDirPath = path.resolve(process.cwd(), `node_modules/${TEMP_DIR}`);
12+
export const PLUGIN_NAME = 'module-federation-storybook-addon';
1213
// add bootstrap for host project
1314
const bootstrapPath = path.resolve(
1415
process.cwd(),
@@ -27,7 +28,7 @@ const writeBootstrap = (context: string, entryPath: string) => {
2728
}
2829
fs.writeFileSync(bootstrapPath, generateBootstrap(context, entryPath));
2930
};
30-
export const withModuleFederation = async (
31+
export const withModuleFederation = (
3132
rsbuildConfig: RsbuildConfig,
3233
options: moduleFederationPlugin.ModuleFederationPluginOptions,
3334
) => {
@@ -52,36 +53,30 @@ export const withModuleFederation = async (
5253
// remoteEntry already includes one hmr runtime, and an additional one is not necessary.
5354
hmr: false,
5455
},
55-
tools: {
56-
rspack: (config) => {
57-
config.output ??= {};
58-
config.output.chunkLoadingGlobal =
59-
'module-federation-storybook-addon';
60-
config.plugins?.push(
61-
new ModuleFederationPlugin({
62-
name: options.name || 'module-federation-storybook-addon',
63-
shared: {
64-
react: {
65-
singleton: true,
66-
},
67-
'react-dom': {
68-
singleton: true,
69-
},
70-
...options.shared,
71-
},
72-
remotes: {
73-
...options.remotes,
74-
},
75-
shareStrategy: options.shareStrategy,
76-
}),
77-
);
78-
return config;
79-
},
80-
},
8156
};
82-
8357
return mergeRsbuildConfig(config, mfConfig);
8458
});
59+
60+
api.modifyBundlerChain(async (chain) => {
61+
chain.plugin(PLUGIN_NAME).use(ModuleFederationPlugin, [
62+
{
63+
name: options.name || PLUGIN_NAME,
64+
shared: {
65+
react: {
66+
singleton: true,
67+
},
68+
'react-dom': {
69+
singleton: true,
70+
},
71+
...options.shared,
72+
},
73+
remotes: {
74+
...options.remotes,
75+
},
76+
shareStrategy: options.shareStrategy,
77+
},
78+
]);
79+
});
8580
},
8681
};
8782

0 commit comments

Comments
 (0)