Skip to content

Commit bb574ca

Browse files
committed
docs: add docs for storybook
1 parent ddca4df commit bb574ca

File tree

2 files changed

+100
-18
lines changed

2 files changed

+100
-18
lines changed

examples/module-federation/mf-react-component/stories/index.stories.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import React from 'react';
21
// @ts-ignore ignore remote module type check for passing ci run build because we set @mf-types folder in .gitignore
32
import { Counter } from 'rslib-module';
43

website/docs/en/guide/advanced/module-federation.mdx

Lines changed: 100 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,7 @@ First install the Module Federation Rsbuild Plugin (Rslib is based on Rsbuild).
2727

2828
import { PackageManagerTabs } from '@theme';
2929

30-
<PackageManagerTabs
31-
command={{
32-
npm: 'npm add @module-federation/rsbuild-plugin -D',
33-
yarn: 'yarn add @module-federation/rsbuild-plugin -D',
34-
pnpm: 'pnpm add @module-federation/rsbuild-plugin -D',
35-
bun: 'bun add @module-federation/rsbuild-plugin -D',
36-
}}
37-
/>
30+
<PackageManagerTabs command="install @module-federation/rsbuild-plugin -D" />
3831

3932
Add the plugin to the `rslib.config.ts` file:
4033

@@ -52,9 +45,17 @@ export default defineConfig({
5245
distPath: {
5346
root: './dist/mf',
5447
},
55-
// add your assetPrefix here
48+
// for production, add online assetPrefix here
5649
assetPrefix: 'http://localhost:3001/mf',
5750
},
51+
// for storybook to dev
52+
dev: {
53+
assetPrefix: 'http://localhost:3001/mf',
54+
},
55+
// for storybook to dev
56+
server: {
57+
port: 3001,
58+
},
5859
plugins: [
5960
pluginModuleFederation({
6061
name: 'rslib_provider',
@@ -85,20 +86,100 @@ In the above example we added a new `format: 'mf'` , which will help you add an
8586

8687
However, if you want this Rslib Module to consume other producers at the same time, do not use the build configuration `remote` parameter, because in other formats, this may cause errors, please refer to the example below using the Module Federation runtime
8788

89+
## Develop Module Federation remote module
90+
91+
Rslib support use `storybook` to develop Module Federation Rslib module
92+
93+
#### 1. install storybook and rsbuild storybook addon
94+
95+
<PackageManagerTabs command="install storybook storybook-addon-rslib @module-federation/storybook-addon storybook-react-rsbuild -D" />
96+
97+
#### 2. create `.storybook/main.ts` and add addons
98+
99+
```ts title=".storybook/main.ts" {13-35}
100+
import { dirname, join } from 'node:path';
101+
import type { StorybookConfig } from 'storybook-react-rsbuild';
102+
103+
function getAbsolutePath(value: string): any {
104+
return dirname(require.resolve(join(value, 'package.json')));
105+
}
106+
107+
const config: StorybookConfig = {
108+
stories: [
109+
'../stories/**/*.mdx',
110+
'../stories/**/*.stories.@(js|jsx|mjs|ts|tsx)',
111+
],
112+
framework: {
113+
name: getAbsolutePath('storybook-react-rsbuild'),
114+
options: {},
115+
},
116+
addons: [
117+
{
118+
name: getAbsolutePath('storybook-addon-rslib'),
119+
options: {
120+
rslib: {
121+
include: ['**/stories/**'],
122+
},
123+
},
124+
},
125+
{
126+
name: '@module-federation/storybook-addon/preset',
127+
options: {
128+
// add your rslib module manifest here for storybook dev
129+
// we have set dev.assetPrefix and server.port to 3001 in rslib.config.ts above
130+
remotes: {
131+
'rslib-module':
132+
'rslib-module@http://localhost:3001/mf/mf-manifest.json',
133+
},
134+
},
135+
},
136+
],
137+
};
138+
139+
export default config;
140+
```
141+
142+
#### 3. Start writing stories
143+
144+
```ts title="stories/index.stories.tsx" {1-2}
145+
// load your remote module here, storybook like a host app
146+
import { Counter } from 'rslib-module';
147+
148+
const Component = () => <Counter />;
149+
150+
export default {
151+
title: 'App Component',
152+
component: Component,
153+
};
154+
155+
export const Primary = {};
156+
```
157+
158+
#### 4. Add Module Federation types and stories into `tsconfig.json`.
159+
160+
```json title="tsconfig.json"
161+
{
162+
"compilerOptions": {
163+
// ...
164+
"paths": {
165+
"*": ["./@mf-types/*"]
166+
}
167+
},
168+
"include": ["src/**/*", ".storybook/**/*", "stories/**/*"]
169+
}
170+
```
171+
172+
#### 5. Start storybook app
173+
174+
There you go, start Storybook with `npx storybook`.
175+
88176
## Consume other Module Federation modules
89177

90178
Because there are multiple formats in Rslib, if you configure the `remote` parameter to consume other modules during construction, it may not work properly in all formats. It is recommended to access through the [Module Federation Runtime](https://module-federation.io/guide/basic/runtime.html)
91179

92180
First install the runtime dependencies
93181

94-
<PackageManagerTabs
95-
command={{
96-
npm: 'npm add @module-federation/enhanced -D',
97-
yarn: 'yarn add @module-federation/enhanced -D',
98-
pnpm: 'pnpm add @module-federation/enhanced -D',
99-
bun: 'bun add @module-federation/enhanced -D',
100-
}}
101-
/>
182+
<PackageManagerTabs command="install @module-federation/enhanced -D" />
102183

103184
Then consume other Module Federation modules at runtime, for example
104185

@@ -156,3 +237,5 @@ pluginModuleFederation({
156237
- `mf-host`: Rsbuild App Consumer
157238
- `mf-react-component`: Rslib Module, which is both a producer and a consumer, provides the module to the `mf-host` as a producer, and consumes the `mf-remote`
158239
- `mf-remote`: Rsbuild App Producer
240+
241+
[Rslib Module Federation Storybook Example](https://github.com/web-infra-dev/rslib/tree/main/examples/module-federation/mf-react-component)

0 commit comments

Comments
 (0)