Skip to content

Commit 6a99630

Browse files
committed
Fix multiple react imports during runtime
1 parent cfaa562 commit 6a99630

26 files changed

+1845
-23
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# React - Vite Federation Demo
2+
3+
This example demos a react application with shared global state using zustand.
4+
5+
## Running
6+
7+
Install `pnpm` as per instructions provided [here](https://pnpm.io/installation)
8+
9+
Run `pnpm install`, then `pnpm run build` and `pnpm run serve`. This will build and serve both `host` and `remote` on ports 5000, 5001 respectively.
10+
11+
- HOST: [localhost:5000](http://localhost:5000/)
12+
- REMOTE: [localhost:5001](http://localhost:5001/)
13+
14+
`CTRL + C` can only stop the host server. You can run `pnpm stop` to stop all services.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { browserLogs, page } from '~utils'
2+
import { expect, test } from 'vitest'
3+
4+
test('should have no 404s', () => {
5+
browserLogs.forEach((msg) => {
6+
expect(msg).not.toMatch('404')
7+
})
8+
})
9+
10+
test('shared state', async () => {
11+
expect(
12+
await page.textContent('h1')
13+
).toBe('0 around here ...')
14+
15+
await page.click('button');
16+
17+
expect(
18+
await page.textContent('h1')
19+
).toBe('1 around here ...')
20+
})
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Advanced React</title>
7+
</head>
8+
<body>
9+
<div id="root"></div>
10+
<script type="module" src="/src/main.jsx"></script>
11+
</body>
12+
</html>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "app",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "vite --port 5000 --strictPort",
8+
"build": "vite build",
9+
"preview": "vite preview --port 5000 --strictPort",
10+
"serve": "vite preview --port 5000 --strictPort"
11+
},
12+
"dependencies": {
13+
"react": "^18.2.0",
14+
"react-dom": "^18.2.0"
15+
},
16+
"devDependencies": {
17+
"@types/react": "^18.0.17",
18+
"@types/react-dom": "^18.0.6",
19+
"@vitejs/plugin-react": "^3.0.0",
20+
"vite": "^4.0.5"
21+
}
22+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#root {
2+
max-width: 1280px;
3+
margin: 0 auto;
4+
padding: 2rem;
5+
text-align: center;
6+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import './App.css'
2+
import useBearStore from 'remoteApp/useBearStore';
3+
import Button from 'remoteApp/Button';
4+
5+
function App() {
6+
const bears = useBearStore((state) => state.bears)
7+
8+
return (
9+
<div className="App">
10+
<h1>{bears} around here ...</h1>
11+
<Button/>
12+
</div>
13+
)
14+
}
15+
16+
export default App
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
:root {
2+
font-family: Inter, Avenir, Helvetica, Arial, sans-serif;
3+
font-size: 16px;
4+
line-height: 24px;
5+
font-weight: 400;
6+
7+
color-scheme: light dark;
8+
color: rgba(255, 255, 255, 0.87);
9+
background-color: #242424;
10+
11+
font-synthesis: none;
12+
text-rendering: optimizeLegibility;
13+
-webkit-font-smoothing: antialiased;
14+
-moz-osx-font-smoothing: grayscale;
15+
-webkit-text-size-adjust: 100%;
16+
}
17+
18+
a {
19+
font-weight: 500;
20+
color: #646cff;
21+
text-decoration: inherit;
22+
}
23+
a:hover {
24+
color: #535bf2;
25+
}
26+
27+
body {
28+
margin: 0;
29+
display: flex;
30+
place-items: center;
31+
min-width: 320px;
32+
min-height: 100vh;
33+
}
34+
35+
h1 {
36+
font-size: 3.2em;
37+
line-height: 1.1;
38+
}
39+
40+
button {
41+
border-radius: 8px;
42+
border: 1px solid transparent;
43+
padding: 0.6em 1.2em;
44+
font-size: 1em;
45+
font-weight: 500;
46+
font-family: inherit;
47+
background-color: #1a1a1a;
48+
cursor: pointer;
49+
transition: border-color 0.25s;
50+
}
51+
button:hover {
52+
border-color: #646cff;
53+
}
54+
button:focus,
55+
button:focus-visible {
56+
outline: 4px auto -webkit-focus-ring-color;
57+
}
58+
59+
@media (prefers-color-scheme: light) {
60+
:root {
61+
color: #213547;
62+
background-color: #ffffff;
63+
}
64+
a:hover {
65+
color: #747bff;
66+
}
67+
button {
68+
background-color: #f9f9f9;
69+
}
70+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react'
2+
import ReactDOM from 'react-dom/client'
3+
import App from './App'
4+
import './index.css'
5+
6+
ReactDOM.createRoot(document.getElementById('root')).render(
7+
<React.StrictMode>
8+
<App />
9+
</React.StrictMode>
10+
)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { defineConfig } from 'vite'
2+
import federation from '@originjs/vite-plugin-federation'
3+
import react from '@vitejs/plugin-react'
4+
5+
// https://vitejs.dev/config/
6+
export default defineConfig({
7+
plugins: [
8+
react(),
9+
federation({
10+
name: 'app',
11+
remotes: {
12+
remoteApp: 'http://localhost:5001/assets/remoteEntry.js',
13+
},
14+
shared: ['react','react-dom']
15+
})
16+
],
17+
build: {
18+
modulePreload: false,
19+
target: 'esnext',
20+
minify: false,
21+
cssCodeSplit: false
22+
}
23+
})
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "react-vite",
3+
"private": true,
4+
"version": "1.0.0",
5+
"scripts": {
6+
"build": "pnpm --parallel --filter \"./**\" build",
7+
"serve": "pnpm --parallel --filter \"./**\" preview",
8+
"build:remotes": "pnpm --parallel --filter \"./remote\" build",
9+
"serve:remotes": "pnpm --parallel --filter \"./remote\" serve",
10+
"dev:hosts": "pnpm --filter \"./host\" dev",
11+
"stop": "kill-port --port 5000,5001"
12+
},
13+
"devDependencies": {
14+
"kill-port": "^2.0.1",
15+
"@originjs/vite-plugin-federation": "^1.2.3"
16+
}
17+
}
18+

0 commit comments

Comments
 (0)