-
Notifications
You must be signed in to change notification settings - Fork 46
feat: support Module Federation format #240
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
Merged
Merged
Changes from all commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
2eee0c6
feat(mf): support mf format and add example
b452af3
chore: remove useless change
d4ffe70
feat: add mf rsbuild plugin for rslib and add both host and remote ex…
07b0db0
chore: resolve conflict
43f5a14
chore: add changeset
0cd4b3c
chore: remove judgement
023444f
chore: modify readme
e0e6fa3
chore: use mf rsbuild plugin to remove rslib repo plugin
c0569a4
chore: sync main code
adbe668
chore: resolve conversation in issue
1296f93
chore: remove useless
04cd00e
chore: remove empty folder
a16c82e
chore: sync code
c0f84a4
chore: revert change
49f834f
chore: rename some folder
Timeless0911 3711099
Merge branch 'main' into feat/support-mf
nyqykk 2a4f895
test: add e2e test for mf example
b94f1df
chore: resolve conflict
c73ac3d
chore: rename example
5a288b3
chore: update lock file
7c63d69
chore: sync code
5a2de62
feat: support hint when users enable mf format but not regist mf rsbu…
3a7c07b
chore: sync code
cbce384
fix: add shim enum
e171f96
docs: add doc for rsbuild plugin
e533134
fix: add nodeEnv for mf format
686350b
Merge branch 'main' into feat/support-mf
nyqykk 279d597
docs: add en doc
4312475
test: add timeout for e2e
f16e3a9
test: add timeout for e2e
01d3e4d
chore: add dep
d5d5056
chore: add comment
0ff4265
chore: add todo
5ce3013
chore: solve conversation issue
fc538af
chore: modify issues
e470fc4
chore: solve conversation issue
82511cc
chore: change rsbuild plugin to dev dep
51cd7ed
chore: revert modify semver version
89b5a1e
chore: use 1.x to remove specific version
784fc3b
chore: fix pnpm lint check-dependency-version error
30571f1
chore: revert version change
454ac08
chore: update lock
696ed9d
chore: revert change dts plugin package.json
c052d93
chore: do not specify rsbuild plugin version
4720213
chore: add introduction for mf example
5cbadc4
chore: use external type map to simplify code
ea26ce3
chore: add benchmark
f28da36
chore: just keep mf format in benchmark
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -18,4 +18,6 @@ test-results | |
!.vscode/settings.json | ||
!.vscode/extensions.json | ||
.idea/ | ||
.nx/ | ||
.nx/ | ||
**/@mf-types | ||
**/@mf-types/** |
This file contains hidden or 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,31 @@ | ||
# Rsbuild Project | ||
|
||
Module Federation Host Example | ||
|
||
## Setup | ||
|
||
Install the dependencies: | ||
|
||
```bash | ||
pnpm install | ||
``` | ||
|
||
## Get Started | ||
|
||
Start the dev server: | ||
|
||
```bash | ||
pnpm dev | ||
``` | ||
|
||
Build the app for production: | ||
|
||
```bash | ||
pnpm build | ||
``` | ||
|
||
Preview the production build locally: | ||
|
||
```bash | ||
pnpm preview | ||
``` |
This file contains hidden or 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,22 @@ | ||
{ | ||
"name": "@example/mf-host", | ||
"version": "1.0.0", | ||
"private": true, | ||
"scripts": { | ||
"build": "rsbuild build", | ||
"dev": "rsbuild dev", | ||
"preview": "rsbuild preview" | ||
}, | ||
"dependencies": { | ||
"react": "^18.3.1", | ||
"react-dom": "^18.3.1" | ||
}, | ||
"devDependencies": { | ||
"@module-federation/rsbuild-plugin": "^0.0.2", | ||
"@rsbuild/core": "~1.0.14", | ||
"@rsbuild/plugin-react": "^1.0.4", | ||
"@types/react": "^18.3.11", | ||
"@types/react-dom": "^18.3.1", | ||
"typescript": "^5.6.3" | ||
} | ||
} |
This file contains hidden or 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,24 @@ | ||
import { pluginModuleFederation } from '@module-federation/rsbuild-plugin'; | ||
import { defineConfig } from '@rsbuild/core'; | ||
import { pluginReact } from '@rsbuild/plugin-react'; | ||
|
||
export default defineConfig({ | ||
plugins: [ | ||
pluginReact(), | ||
pluginModuleFederation({ | ||
name: 'rsbuild_host', | ||
remotes: { | ||
rslib: 'rslib@http://localhost:3001/mf/mf-manifest.json', | ||
}, | ||
shared: { | ||
react: { | ||
singleton: true, | ||
}, | ||
'react-dom': { | ||
singleton: true, | ||
}, | ||
}, | ||
shareStrategy: 'loaded-first', | ||
}), | ||
], | ||
}); |
This file contains hidden or 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,26 @@ | ||
body { | ||
margin: 0; | ||
color: #fff; | ||
font-family: Inter, Avenir, Helvetica, Arial, sans-serif; | ||
background-image: linear-gradient(to bottom, #020917, #101725); | ||
} | ||
|
||
.content { | ||
display: flex; | ||
min-height: 100vh; | ||
line-height: 1.1; | ||
text-align: center; | ||
flex-direction: column; | ||
justify-content: center; | ||
} | ||
|
||
.content h1 { | ||
font-size: 3.6rem; | ||
font-weight: 700; | ||
} | ||
|
||
.content p { | ||
font-size: 1.2rem; | ||
font-weight: 400; | ||
opacity: 0.5; | ||
} |
This file contains hidden or 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,14 @@ | ||
import { Counter } from 'rslib'; | ||
import './App.css'; | ||
|
||
const App = () => { | ||
return ( | ||
<div className="content"> | ||
<Counter /> | ||
<h1>Rsbuild with React</h1> | ||
<p>Start building amazing things with Rsbuild.</p> | ||
</div> | ||
); | ||
}; | ||
|
||
export default App; |
This file contains hidden or 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,13 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom/client'; | ||
import App from './App'; | ||
|
||
const rootEl = document.getElementById('root'); | ||
if (rootEl) { | ||
const root = ReactDOM.createRoot(rootEl); | ||
root.render( | ||
<React.StrictMode> | ||
<App /> | ||
</React.StrictMode>, | ||
); | ||
} |
This file contains hidden or 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 @@ | ||
/// <reference types="@rsbuild/core/types" /> |
This file contains hidden or 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 @@ | ||
import('./bootstrap'); |
This file contains hidden or 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,20 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES2020", | ||
"lib": ["DOM", "ES2020"], | ||
"module": "ESNext", | ||
"jsx": "react-jsx", | ||
"noEmit": true, | ||
"strict": true, | ||
"skipLibCheck": true, | ||
"isolatedModules": true, | ||
"resolveJsonModule": true, | ||
"moduleResolution": "bundler", | ||
"useDefineForClassFields": true, | ||
"allowImportingTsExtensions": true, | ||
"paths": { | ||
"*": ["./@mf-types/*"] | ||
} | ||
}, | ||
"include": ["src"] | ||
} |
This file contains hidden or 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,3 @@ | ||
# @examples/mf-react-component | ||
|
||
This example demonstrates how to use Rslib to build a simple Module Federation React component. |
31 changes: 31 additions & 0 deletions
31
examples/module-federation/mf-react-component/package.json
This file contains hidden or 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,31 @@ | ||
{ | ||
"name": "@examples/mf-react-component", | ||
"private": true, | ||
"exports": { | ||
".": { | ||
"import": "./dist/esm/index.mjs", | ||
"require": "./dist/cjs/index.js", | ||
"types": "./dist/cjs/index.d.ts" | ||
} | ||
}, | ||
"main": "./dist/cjs/index.js", | ||
"module": "./dist/esm/index.mjs", | ||
"types": "./dist/cjs/index.d.ts", | ||
"scripts": { | ||
"build": "rslib build", | ||
"serve": "pnpm build && http-server -p 3001 ./dist/ --cors" | ||
}, | ||
"devDependencies": { | ||
"@module-federation/enhanced": "^0.6.11", | ||
"@module-federation/rsbuild-plugin": "^0.0.2", | ||
"@rsbuild/plugin-react": "^1.0.4", | ||
"@rslib/core": "workspace:*", | ||
"@types/react": "^18.3.11", | ||
"http-server": "^14.1.1", | ||
"react": "^18.3.1", | ||
"react-dom": "^18.3.1" | ||
}, | ||
"peerDependencies": { | ||
"react": "*" | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
examples/module-federation/mf-react-component/rslib.config.ts
This file contains hidden or 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,59 @@ | ||
import { pluginModuleFederation } from '@module-federation/rsbuild-plugin'; | ||
import { pluginReact } from '@rsbuild/plugin-react'; | ||
import { defineConfig } from '@rslib/core'; | ||
|
||
const shared = { | ||
dts: { | ||
bundle: false, | ||
}, | ||
}; | ||
|
||
export default defineConfig({ | ||
lib: [ | ||
{ | ||
...shared, | ||
format: 'esm', | ||
output: { | ||
distPath: { | ||
root: './dist/esm', | ||
}, | ||
}, | ||
}, | ||
{ | ||
...shared, | ||
format: 'cjs', | ||
output: { | ||
distPath: { | ||
root: './dist/cjs', | ||
}, | ||
}, | ||
}, | ||
{ | ||
...shared, | ||
format: 'mf', | ||
output: { | ||
distPath: { | ||
root: './dist/mf', | ||
}, | ||
assetPrefix: 'http://localhost:3001/mf', | ||
}, | ||
plugins: [ | ||
pluginModuleFederation({ | ||
name: 'rslib_provider', | ||
exposes: { | ||
'.': './src/index.tsx', | ||
}, | ||
shared: { | ||
react: { | ||
singleton: true, | ||
}, | ||
'react-dom': { | ||
singleton: true, | ||
}, | ||
}, | ||
}), | ||
], | ||
}, | ||
], | ||
plugins: [pluginReact()], | ||
}); |
15 changes: 15 additions & 0 deletions
15
examples/module-federation/mf-react-component/src/CounterButton.tsx
This file contains hidden or 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,15 @@ | ||
interface CounterButtonProps { | ||
onClick: () => void; | ||
label: string; | ||
[key: string]: any; | ||
} | ||
|
||
export const CounterButton: React.FC<CounterButtonProps> = ({ | ||
onClick, | ||
label, | ||
...props | ||
}) => ( | ||
<button type="button" onClick={onClick} {...props}> | ||
{label} | ||
</button> | ||
); |
39 changes: 39 additions & 0 deletions
39
examples/module-federation/mf-react-component/src/index.tsx
This file contains hidden or 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,39 @@ | ||
import { init, loadRemote } from '@module-federation/enhanced/runtime'; | ||
import { Suspense, createElement, lazy } from 'react'; | ||
import { CounterButton } from './CounterButton'; | ||
import { useCounter } from './useCounter'; | ||
|
||
init({ | ||
name: 'rslib_provider', | ||
remotes: [ | ||
{ | ||
name: 'mf_remote', | ||
entry: 'http://localhost:3002/mf-manifest.json', | ||
}, | ||
], | ||
}); | ||
|
||
export const Counter: React.FC = () => { | ||
const { count, increment, decrement } = useCounter(); | ||
|
||
return ( | ||
<div> | ||
<Suspense fallback={<div>loading</div>}> | ||
{createElement( | ||
lazy( | ||
() => | ||
loadRemote('mf_remote') as Promise<{ | ||
default: React.FC; | ||
}>, | ||
), | ||
)} | ||
</Suspense> | ||
<h2> | ||
<span id="mf-e2e-lib-title">Counter From Rslib MF Format: </span> | ||
<span id="mf-e2e-lib-content">{count}</span> | ||
</h2> | ||
<CounterButton id="mf-e2e-lib-decrease" onClick={decrement} label="-" /> | ||
<CounterButton id="mf-e2e-lib-increase" onClick={increment} label="+" /> | ||
</div> | ||
); | ||
}; |
10 changes: 10 additions & 0 deletions
10
examples/module-federation/mf-react-component/src/useCounter.tsx
This file contains hidden or 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,10 @@ | ||
import { useState } from 'react'; | ||
|
||
export const useCounter = (initialValue = 0) => { | ||
const [count, setCount] = useState(initialValue); | ||
|
||
const increment = () => setCount((prev) => prev + 1); | ||
const decrement = () => setCount((prev) => prev - 1); | ||
|
||
return { count, increment, decrement }; | ||
}; |
This file contains hidden or 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,8 @@ | ||
{ | ||
"compilerOptions": { | ||
"jsx": "react-jsx", | ||
"strict": true, | ||
"skipLibCheck": true | ||
}, | ||
"include": ["src/**/*"] | ||
} |
This file contains hidden or 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,31 @@ | ||
# Rsbuild Project | ||
|
||
nyqykk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Module Federation Remote Example | ||
|
||
## Setup | ||
|
||
Install the dependencies: | ||
|
||
```bash | ||
pnpm install | ||
``` | ||
|
||
## Get Started | ||
|
||
Start the dev server: | ||
|
||
```bash | ||
pnpm dev | ||
``` | ||
|
||
Build the app for production: | ||
|
||
```bash | ||
pnpm build | ||
``` | ||
|
||
Preview the production build locally: | ||
|
||
```bash | ||
pnpm preview | ||
``` |
This file contains hidden or 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,22 @@ | ||
{ | ||
"name": "@example/mf-remote", | ||
"version": "1.0.0", | ||
"private": true, | ||
"scripts": { | ||
"build": "rsbuild build", | ||
"dev": "rsbuild dev", | ||
"preview": "rsbuild preview" | ||
}, | ||
"dependencies": { | ||
"react": "^18.3.1", | ||
"react-dom": "^18.3.1" | ||
}, | ||
"devDependencies": { | ||
"@module-federation/rsbuild-plugin": "^0.0.2", | ||
"@rsbuild/core": "~1.0.14", | ||
"@rsbuild/plugin-react": "^1.0.4", | ||
"@types/react": "^18.3.11", | ||
"@types/react-dom": "^18.3.1", | ||
"typescript": "^5.6.3" | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.