-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support use storybook to dev rslib module federation format ass…
…ets and also support hmr (#349) Co-authored-by: nyqykk <[email protected]> Co-authored-by: fi3ework <[email protected]> Co-authored-by: Timeless0911 <[email protected]>
- Loading branch information
1 parent
7dd9a48
commit d52e410
Showing
17 changed files
with
1,515 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
registry = 'https://registry.npmjs.org/' | ||
strict-peer-dependencies=false | ||
auto-install-peers=false | ||
hoist-patterns[]=[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"name": "@example/mf-host", | ||
"name": "@examples/mf-host", | ||
"version": "1.0.0", | ||
"private": true, | ||
"scripts": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
examples/module-federation/mf-react-component/.storybook/main.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { dirname, join } from 'node:path'; | ||
import type { StorybookConfig } from 'storybook-react-rsbuild'; | ||
|
||
/** | ||
* This function is used to resolve the absolute path of a package. | ||
* It is needed in projects that use Yarn PnP or are set up within a monorepo. | ||
*/ | ||
function getAbsolutePath(value: string): any { | ||
return dirname(require.resolve(join(value, 'package.json'))); | ||
} | ||
|
||
const config: StorybookConfig = { | ||
stories: [ | ||
'../stories/**/*.mdx', | ||
'../stories/**/*.stories.@(js|jsx|mjs|ts|tsx)', | ||
], | ||
framework: { | ||
name: getAbsolutePath('storybook-react-rsbuild'), | ||
options: {}, | ||
}, | ||
addons: [ | ||
{ | ||
name: getAbsolutePath('storybook-addon-rslib'), | ||
options: { | ||
rslib: { | ||
include: ['**/stories/**'], | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: '@module-federation/storybook-addon/preset', | ||
options: { | ||
remotes: { | ||
'rslib-module': | ||
'rslib-module@http://localhost:3001/mf/mf-manifest.json', | ||
}, | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
export default config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,37 @@ | ||
# @examples/mf-react-component | ||
|
||
This example demonstrates how to use Rslib to build a simple Module Federation React component. | ||
|
||
### Usage | ||
|
||
Dev MF module | ||
|
||
1. Start remote module which is loaded by Rslib module. | ||
|
||
``` | ||
nx run @examples/mf-remote:dev | ||
``` | ||
|
||
2. Start MF dev mode. | ||
|
||
``` | ||
nx run @examples/mf-react-component:dev | ||
``` | ||
|
||
3. Use Storybook to development component. | ||
|
||
``` | ||
nx run @examples/mf-react-component:storybook | ||
``` | ||
|
||
Build | ||
|
||
``` | ||
nx run @examples/mf-react-component:build | ||
``` | ||
|
||
Build and Serve dist | ||
|
||
``` | ||
nx run @examples/mf-react-component:serve | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
examples/module-federation/mf-react-component/stories/index.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import React from 'react'; | ||
// @ts-ignore ignore remote module type check for passing ci run build because we set @mf-types folder in .gitignore | ||
import { Counter } from 'rslib-module'; | ||
|
||
const Component = () => <Counter />; | ||
|
||
export default { | ||
title: 'App Component', | ||
component: Component, | ||
}; | ||
|
||
export const Primary = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
{ | ||
"name": "@example/mf", | ||
"name": "@examples/mf", | ||
"private": true, | ||
"scripts": { | ||
"dev:all": "pnpm dev:host & pnpm dev:lib & pnpm dev:remote", | ||
"dev:all": "pnpm dev:host & pnpm serve:lib & pnpm dev:remote", | ||
"dev:host": "pnpm --filter mf-host dev", | ||
"dev:lib": "pnpm --filter mf-react-component run serve", | ||
"dev:remote": "pnpm --filter mf-remote dev" | ||
"dev:remote": "pnpm --filter mf-remote dev", | ||
"serve:lib": "pnpm --filter mf-react-component run serve" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { createRsbuild, mergeRsbuildConfig } from '@rsbuild/core'; | ||
import { composeCreateRsbuildConfig } from './config'; | ||
|
||
import type { RsbuildConfig, RsbuildInstance } from '@rsbuild/core'; | ||
import type { RslibConfig } from './types'; | ||
|
||
export async function startMFDevServer( | ||
config: RslibConfig, | ||
): Promise<RsbuildInstance | undefined> { | ||
const rsbuildInstance = await initMFRsbuild(config); | ||
return rsbuildInstance; | ||
} | ||
|
||
async function initMFRsbuild( | ||
rslibConfig: RslibConfig, | ||
): Promise<RsbuildInstance | undefined> { | ||
const rsbuildConfigObject = await composeCreateRsbuildConfig(rslibConfig); | ||
const mfRsbuildConfig = rsbuildConfigObject.find( | ||
(config) => config.format === 'mf', | ||
); | ||
|
||
if (!mfRsbuildConfig) { | ||
// no mf format, return. | ||
return; | ||
} | ||
|
||
mfRsbuildConfig.config = changeEnvToDev(mfRsbuildConfig.config); | ||
const rsbuildInstance = await createRsbuild({ | ||
rsbuildConfig: mfRsbuildConfig.config, | ||
}); | ||
await rsbuildInstance.startDevServer(); | ||
return rsbuildInstance; | ||
} | ||
|
||
function changeEnvToDev(rsbuildConfig: RsbuildConfig) { | ||
return mergeRsbuildConfig(rsbuildConfig, { | ||
mode: 'development', | ||
dev: { | ||
writeToDisk: true, | ||
}, | ||
tools: { | ||
rspack: { | ||
optimization: { | ||
nodeEnv: 'development', | ||
}, | ||
}, | ||
}, | ||
}); | ||
} |
Oops, something went wrong.