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

feat: support Module Federation format #240

Merged
merged 48 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
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
Sep 23, 2024
b452af3
chore: remove useless change
Sep 23, 2024
d4ffe70
feat: add mf rsbuild plugin for rslib and add both host and remote ex…
Sep 27, 2024
07b0db0
chore: resolve conflict
Sep 27, 2024
43f5a14
chore: add changeset
Sep 27, 2024
0cd4b3c
chore: remove judgement
Sep 27, 2024
023444f
chore: modify readme
Sep 27, 2024
e0e6fa3
chore: use mf rsbuild plugin to remove rslib repo plugin
Oct 10, 2024
c0569a4
chore: sync main code
Oct 10, 2024
adbe668
chore: resolve conversation in issue
Oct 11, 2024
1296f93
chore: remove useless
Oct 11, 2024
04cd00e
chore: remove empty folder
Oct 11, 2024
a16c82e
chore: sync code
Oct 11, 2024
c0f84a4
chore: revert change
Oct 11, 2024
49f834f
chore: rename some folder
Timeless0911 Oct 11, 2024
3711099
Merge branch 'main' into feat/support-mf
nyqykk Oct 12, 2024
2a4f895
test: add e2e test for mf example
Oct 15, 2024
b94f1df
chore: resolve conflict
Oct 15, 2024
c73ac3d
chore: rename example
Oct 15, 2024
5a288b3
chore: update lock file
Oct 15, 2024
7c63d69
chore: sync code
Oct 15, 2024
5a2de62
feat: support hint when users enable mf format but not regist mf rsbu…
Oct 15, 2024
3a7c07b
chore: sync code
Oct 15, 2024
cbce384
fix: add shim enum
Oct 15, 2024
e171f96
docs: add doc for rsbuild plugin
Oct 16, 2024
e533134
fix: add nodeEnv for mf format
Oct 16, 2024
686350b
Merge branch 'main' into feat/support-mf
nyqykk Oct 16, 2024
279d597
docs: add en doc
Oct 16, 2024
4312475
test: add timeout for e2e
Oct 16, 2024
f16e3a9
test: add timeout for e2e
Oct 16, 2024
01d3e4d
chore: add dep
Oct 16, 2024
d5d5056
chore: add comment
Oct 16, 2024
0ff4265
chore: add todo
Oct 16, 2024
5ce3013
chore: solve conversation issue
Oct 16, 2024
fc538af
chore: modify issues
Oct 18, 2024
e470fc4
chore: solve conversation issue
Oct 18, 2024
82511cc
chore: change rsbuild plugin to dev dep
Oct 18, 2024
51cd7ed
chore: revert modify semver version
Oct 18, 2024
89b5a1e
chore: use 1.x to remove specific version
Oct 18, 2024
784fc3b
chore: fix pnpm lint check-dependency-version error
Oct 18, 2024
30571f1
chore: revert version change
Oct 18, 2024
454ac08
chore: update lock
Oct 18, 2024
696ed9d
chore: revert change dts plugin package.json
Oct 18, 2024
c052d93
chore: do not specify rsbuild plugin version
Oct 18, 2024
4720213
chore: add introduction for mf example
Oct 18, 2024
5cbadc4
chore: use external type map to simplify code
Oct 18, 2024
ea26ce3
chore: add benchmark
Oct 18, 2024
f28da36
chore: just keep mf format in benchmark
Oct 18, 2024
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ test-results
!.vscode/settings.json
!.vscode/extensions.json
.idea/
.nx/
.nx/
**/@mf-types
**/@mf-types/**
31 changes: 31 additions & 0 deletions examples/module-federation/mf-host/README.md
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
```
22 changes: 22 additions & 0 deletions examples/module-federation/mf-host/package.json
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"
}
}
24 changes: 24 additions & 0 deletions examples/module-federation/mf-host/rsbuild.config.ts
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',
}),
],
});
26 changes: 26 additions & 0 deletions examples/module-federation/mf-host/src/App.css
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;
}
14 changes: 14 additions & 0 deletions examples/module-federation/mf-host/src/App.tsx
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;
13 changes: 13 additions & 0 deletions examples/module-federation/mf-host/src/bootstrap.tsx
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>,
);
}
1 change: 1 addition & 0 deletions examples/module-federation/mf-host/src/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="@rsbuild/core/types" />
1 change: 1 addition & 0 deletions examples/module-federation/mf-host/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import('./bootstrap');
20 changes: 20 additions & 0 deletions examples/module-federation/mf-host/tsconfig.json
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"]
}
3 changes: 3 additions & 0 deletions examples/module-federation/mf-react-component/README.md
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 examples/module-federation/mf-react-component/package.json
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 examples/module-federation/mf-react-component/rslib.config.ts
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,
},
},
}),
],
nyqykk marked this conversation as resolved.
Show resolved Hide resolved
},
],
plugins: [pluginReact()],
});
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 examples/module-federation/mf-react-component/src/index.tsx
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 examples/module-federation/mf-react-component/src/useCounter.tsx
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 };
};
8 changes: 8 additions & 0 deletions examples/module-federation/mf-react-component/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"jsx": "react-jsx",
"strict": true,
"skipLibCheck": true
},
"include": ["src/**/*"]
}
31 changes: 31 additions & 0 deletions examples/module-federation/mf-remote/README.md
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
```
22 changes: 22 additions & 0 deletions examples/module-federation/mf-remote/package.json
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"
}
}
Loading
Loading