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

WIP: chore(rsbuild-plugin): split setUp function to help extend #3215

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/angry-spoons-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@module-federation/rsbuild-plugin': patch
---

chore(rsbuild-plugin): split setUp function to help extend
5 changes: 5 additions & 0 deletions .changeset/spicy-roses-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@module-federation/storybook-addon': patch
---

chore: export plugin name
14 changes: 11 additions & 3 deletions packages/rsbuild-plugin/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@ import logger from '../logger';
type ModuleFederationOptions =
moduleFederationPlugin.ModuleFederationPluginOptions;

const PLUGIN_MODULE_FEDERATION_NAME = 'rsbuild:module-federation-enhanced';
const RSBUILD_PLUGIN_MODULE_FEDERATION_NAME =
'rsbuild:module-federation-enhanced';
const RSPACK_PLUGIN_MODULE_FEDERATION_NAME = 'module-federation';

export {
RSBUILD_PLUGIN_MODULE_FEDERATION_NAME,
RSPACK_PLUGIN_MODULE_FEDERATION_NAME,
};

export const pluginModuleFederation = (
moduleFederationOptions: ModuleFederationOptions,
): RsbuildPlugin => ({
name: PLUGIN_MODULE_FEDERATION_NAME,
name: RSBUILD_PLUGIN_MODULE_FEDERATION_NAME,
setup: (api) => {
const sharedOptions: [string, sharePlugin.SharedConfig][] = parseOptions(
moduleFederationOptions.shared || [],
Expand Down Expand Up @@ -153,7 +161,7 @@ export const pluginModuleFederation = (

api.modifyBundlerChain(async (chain) => {
chain
.plugin('module-federation')
.plugin(RSPACK_PLUGIN_MODULE_FEDERATION_NAME)
.use(ModuleFederationPlugin, [moduleFederationOptions]);

// `uniqueName` is required for react refresh to work
Expand Down
3 changes: 3 additions & 0 deletions packages/storybook-addon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
"*": {
".": [
"./dist/index.d.ts"
],
"preset": [
"./dist/preset.d.ts"
]
}
},
Expand Down
1 change: 1 addition & 0 deletions packages/storybook-addon/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export default {
});
},
};
export { PLUGIN_NAME } from './src/utils/with-module-federation-enhanced-rsbuild';
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { RsbuildConfig, RsbuildPlugin } from '@rsbuild/core';
import type { moduleFederationPlugin } from '@module-federation/sdk';

const tempDirPath = path.resolve(process.cwd(), `node_modules/${TEMP_DIR}`);
export const PLUGIN_NAME = 'module-federation-storybook-addon';
// add bootstrap for host project
const bootstrapPath = path.resolve(
process.cwd(),
Expand All @@ -27,7 +28,7 @@ const writeBootstrap = (context: string, entryPath: string) => {
}
fs.writeFileSync(bootstrapPath, generateBootstrap(context, entryPath));
};
export const withModuleFederation = async (
export const withModuleFederation = (
rsbuildConfig: RsbuildConfig,
options: moduleFederationPlugin.ModuleFederationPluginOptions,
) => {
Expand All @@ -52,36 +53,30 @@ export const withModuleFederation = async (
// remoteEntry already includes one hmr runtime, and an additional one is not necessary.
hmr: false,
},
tools: {
rspack: (config) => {
config.output ??= {};
config.output.chunkLoadingGlobal =
'module-federation-storybook-addon';
config.plugins?.push(
new ModuleFederationPlugin({
name: options.name || 'module-federation-storybook-addon',
shared: {
react: {
singleton: true,
},
'react-dom': {
singleton: true,
},
...options.shared,
},
remotes: {
...options.remotes,
},
shareStrategy: options.shareStrategy,
}),
);
return config;
},
},
};

return mergeRsbuildConfig(config, mfConfig);
});

api.modifyBundlerChain(async (chain) => {
chain.plugin(PLUGIN_NAME).use(ModuleFederationPlugin, [
{
name: options.name || PLUGIN_NAME,
shared: {
react: {
singleton: true,
},
'react-dom': {
singleton: true,
},
...options.shared,
},
remotes: {
...options.remotes,
},
shareStrategy: options.shareStrategy,
},
]);
});
},
};

Expand Down
4 changes: 2 additions & 2 deletions pnpm-lock.yaml

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