Skip to content

Commit d52e410

Browse files
nyqykknyqykkfi3eworkTimeless0911
authored
feat: support use storybook to dev rslib module federation format assets and also support hmr (#349)
Co-authored-by: nyqykk <[email protected]> Co-authored-by: fi3ework <[email protected]> Co-authored-by: Timeless0911 <[email protected]>
1 parent 7dd9a48 commit d52e410

File tree

17 files changed

+1515
-63
lines changed

17 files changed

+1515
-63
lines changed

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
registry = 'https://registry.npmjs.org/'
22
strict-peer-dependencies=false
3+
auto-install-peers=false
34
hoist-patterns[]=[]

examples/module-federation/mf-host/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@example/mf-host",
2+
"name": "@examples/mf-host",
33
"version": "1.0.0",
44
"private": true,
55
"scripts": {

examples/module-federation/mf-host/rsbuild.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export default defineConfig({
1818
singleton: true,
1919
},
2020
},
21+
// Enable this when the output of Rslib is build under 'production' mode, while the host app is 'development'.
22+
// Reference: https://lib.rsbuild.dev/guide/advanced/module-federation#faqs
2123
shareStrategy: 'loaded-first',
2224
}),
2325
],
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { dirname, join } from 'node:path';
2+
import type { StorybookConfig } from 'storybook-react-rsbuild';
3+
4+
/**
5+
* This function is used to resolve the absolute path of a package.
6+
* It is needed in projects that use Yarn PnP or are set up within a monorepo.
7+
*/
8+
function getAbsolutePath(value: string): any {
9+
return dirname(require.resolve(join(value, 'package.json')));
10+
}
11+
12+
const config: StorybookConfig = {
13+
stories: [
14+
'../stories/**/*.mdx',
15+
'../stories/**/*.stories.@(js|jsx|mjs|ts|tsx)',
16+
],
17+
framework: {
18+
name: getAbsolutePath('storybook-react-rsbuild'),
19+
options: {},
20+
},
21+
addons: [
22+
{
23+
name: getAbsolutePath('storybook-addon-rslib'),
24+
options: {
25+
rslib: {
26+
include: ['**/stories/**'],
27+
},
28+
},
29+
},
30+
{
31+
name: '@module-federation/storybook-addon/preset',
32+
options: {
33+
remotes: {
34+
'rslib-module':
35+
'rslib-module@http://localhost:3001/mf/mf-manifest.json',
36+
},
37+
},
38+
},
39+
],
40+
};
41+
42+
export default config;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,37 @@
11
# @examples/mf-react-component
22

33
This example demonstrates how to use Rslib to build a simple Module Federation React component.
4+
5+
### Usage
6+
7+
Dev MF module
8+
9+
1. Start remote module which is loaded by Rslib module.
10+
11+
```
12+
nx run @examples/mf-remote:dev
13+
```
14+
15+
2. Start MF dev mode.
16+
17+
```
18+
nx run @examples/mf-react-component:dev
19+
```
20+
21+
3. Use Storybook to development component.
22+
23+
```
24+
nx run @examples/mf-react-component:storybook
25+
```
26+
27+
Build
28+
29+
```
30+
nx run @examples/mf-react-component:build
31+
```
32+
33+
Build and Serve dist
34+
35+
```
36+
nx run @examples/mf-react-component:serve
37+
```

examples/module-federation/mf-react-component/package.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,33 @@
33
"private": true,
44
"exports": {
55
".": {
6+
"types": "./dist/cjs/index.d.ts",
67
"import": "./dist/esm/index.mjs",
7-
"require": "./dist/cjs/index.js",
8-
"types": "./dist/cjs/index.d.ts"
8+
"require": "./dist/cjs/index.js"
99
}
1010
},
1111
"main": "./dist/cjs/index.js",
1212
"module": "./dist/esm/index.mjs",
1313
"types": "./dist/cjs/index.d.ts",
1414
"scripts": {
1515
"build": "rslib build",
16-
"serve": "pnpm build && http-server -p 3001 ./dist/ --cors"
16+
"dev": "rslib mf dev",
17+
"serve": "pnpm build & http-server -p 3001 ./dist/ --cors",
18+
"storybook": "storybook dev -p 6006"
1719
},
1820
"devDependencies": {
1921
"@module-federation/enhanced": "^0.7.1",
2022
"@module-federation/rsbuild-plugin": "^0.7.1",
23+
"@module-federation/storybook-addon": "0.0.0-next-20241106024856",
2124
"@rsbuild/plugin-react": "^1.0.7",
2225
"@rslib/core": "workspace:*",
2326
"@types/react": "^18.3.12",
2427
"http-server": "^14.1.1",
2528
"react": "^18.3.1",
26-
"react-dom": "^18.3.1"
29+
"react-dom": "^18.3.1",
30+
"storybook": "^8.3.6",
31+
"storybook-addon-rslib": "^0.1.3",
32+
"storybook-react-rsbuild": "^0.1.3"
2733
},
2834
"peerDependencies": {
2935
"react": "*"

examples/module-federation/mf-react-component/rslib.config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ export default defineConfig({
3737
},
3838
assetPrefix: 'http://localhost:3001/mf',
3939
},
40+
dev: {
41+
assetPrefix: 'http://localhost:3001/mf',
42+
},
43+
// just for dev
44+
server: {
45+
port: 3001,
46+
},
4047
plugins: [
4148
pluginModuleFederation({
4249
name: 'rslib_provider',
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React from 'react';
2+
// @ts-ignore ignore remote module type check for passing ci run build because we set @mf-types folder in .gitignore
3+
import { Counter } from 'rslib-module';
4+
5+
const Component = () => <Counter />;
6+
7+
export default {
8+
title: 'App Component',
9+
component: Component,
10+
};
11+
12+
export const Primary = {};

examples/module-federation/mf-react-component/tsconfig.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
"compilerOptions": {
33
"jsx": "react-jsx",
44
"strict": true,
5-
"skipLibCheck": true
5+
"skipLibCheck": true,
6+
"esModuleInterop": true,
7+
"paths": {
8+
"*": ["./@mf-types/*"]
9+
}
610
},
7-
"include": ["src/**/*"]
11+
"include": ["src/**/*", "stories"]
812
}

examples/module-federation/mf-remote/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@example/mf-remote",
2+
"name": "@examples/mf-remote",
33
"version": "1.0.0",
44
"private": true,
55
"scripts": {

0 commit comments

Comments
 (0)