-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
executable file
·350 lines (298 loc) · 7.46 KB
/
setup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/bin/bash
# Create project structure
mkdir -p microfrontend-project/{main-app/src,plugin-a/src,config-server}
cd microfrontend-project
# Create main-app files
cat << EOF > main-app/src/types.ts
import React from 'react';
export interface PluginProps {
name: string;
}
export interface Plugin {
name: string;
component: React.ComponentType<PluginProps>;
routes?: Array<{
path: string;
component: React.ComponentType<any>;
}>;
}
export interface PluginConfig {
name: string;
url: string;
}
EOF
cat << EOF > main-app/src/ConfigLoader.ts
export async function loadConfig(): Promise<PluginConfig[]> {
const response = await fetch('http://localhost:3001/config');
if (!response.ok) {
throw new Error('Failed to load config');
}
return response.json();
}
EOF
cat << EOF > main-app/src/PluginRegistry.ts
import { Plugin, PluginConfig } from './types';
class PluginRegistry {
private plugins: Map<string, Plugin> = new Map();
async loadPlugins(configs: PluginConfig[]) {
for (const config of configs) {
try {
const module = await import(/* @vite-ignore */ config.url);
const plugin: Plugin = module.default;
this.register(plugin);
} catch (error) {
console.error(\`Failed to load plugin \${config.name}:\`, error);
}
}
}
register(plugin: Plugin) {
this.plugins.set(plugin.name, plugin);
}
get(name: string): Plugin | undefined {
return this.plugins.get(name);
}
getAll(): Plugin[] {
return Array.from(this.plugins.values());
}
}
export const pluginRegistry = new PluginRegistry();
EOF
cat << EOF > main-app/src/PluginLoader.tsx
import React, { useState, useEffect } from 'react';
import { Plugin, PluginProps } from './types';
import { pluginRegistry } from './PluginRegistry';
interface PluginLoaderProps {
pluginName: string;
}
export const PluginLoader: React.FC<PluginLoaderProps> = ({ pluginName }) => {
const [PluginComponent, setPluginComponent] = useState<React.ComponentType<PluginProps> | null>(null);
useEffect(() => {
const plugin = pluginRegistry.get(pluginName);
if (plugin) {
setPluginComponent(() => plugin.component);
} else {
console.error(\`Plugin \${pluginName} not found\`);
}
}, [pluginName]);
if (!PluginComponent) {
return <div>Loading plugin...</div>;
}
return <PluginComponent name={pluginName} />;
};
EOF
cat << EOF > main-app/src/Home.tsx
import React from 'react';
const Home: React.FC = () => (
<div>
<h1>Home Page</h1>
<p>Welcome to the main application.</p>
</div>
);
export default Home;
EOF
cat << EOF > main-app/src/About.tsx
import React from 'react';
const About: React.FC = () => (
<div>
<h1>About Page</h1>
<p>This is the about page of the main application.</p>
</div>
);
export default About;
EOF
cat << EOF > main-app/src/App.tsx
import React, { useEffect, useState } from 'react';
import { BrowserRouter as Router, Route, Link, Routes } from 'react-router-dom';
import Home from './Home';
import About from './About';
import { PluginLoader } from './PluginLoader';
import { pluginRegistry } from './PluginRegistry';
import { loadConfig } from './ConfigLoader';
import { Plugin } from './types';
const App: React.FC = () => {
const [plugins, setPlugins] = useState<Plugin[]>([]);
useEffect(() => {
const initPlugins = async () => {
const configs = await loadConfig();
await pluginRegistry.loadPlugins(configs);
setPlugins(pluginRegistry.getAll());
};
initPlugins();
}, []);
return (
<Router>
<div>
<nav>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/about">About</Link></li>
{plugins.map(plugin => (
plugin.routes?.map(route => (
<li key={route.path}>
<Link to={route.path}>{plugin.name}</Link>
</li>
))
))}
</ul>
</nav>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
{plugins.map(plugin => (
plugin.routes?.map(route => (
<Route
key={route.path}
path={route.path}
element={<route.component />}
/>
))
))}
</Routes>
</div>
</Router>
);
};
export default App;
EOF
cat << EOF > main-app/vite.config.ts
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [react()],
server: {
port: 3000,
},
});
EOF
cat << EOF > main-app/package.json
{
"name": "main-app",
"version": "1.0.0",
"main": "src/App.tsx",
"scripts": {
"start": "vite",
"build": "tsc && vite build",
"serve": "vite preview"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.10.0"
},
"devDependencies": {
"@types/react": "^18.0.28",
"@types/react-dom": "^18.0.11",
"@vitejs/plugin-react": "^3.1.0",
"typescript": "^4.9.3",
"vite": "^4.2.0"
}
}
EOF
# Create plugin-a files
cat << EOF > plugin-a/src/PluginAPage.tsx
import React from 'react';
const PluginAPage: React.FC = () => (
<div>
<h1>Plugin A Page</h1>
<p>This is a page added by Plugin A.</p>
</div>
);
export default PluginAPage;
EOF
cat << EOF > plugin-a/src/index.ts
import React from 'react';
import PluginAPage from './PluginAPage';
const PluginA = {
name: 'Plugin A',
component: () => React.createElement('div', null, 'Plugin A Component'),
routes: [
{
path: '/plugin-a',
component: PluginAPage,
},
],
};
export default PluginA;
EOF
cat << EOF > plugin-a/vite.config.ts
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [react()],
build: {
lib: {
entry: 'src/index.ts',
name: 'PluginA',
fileName: 'plugin-a',
},
rollupOptions: {
external: ['react', 'react-dom'],
},
},
});
EOF
cat << EOF > plugin-a/package.json
{
"name": "plugin-a",
"version": "1.0.0",
"main": "src/index.ts",
"scripts": {
"build": "tsc && vite build",
"serve": "vite preview --port 5000"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/react": "^18.0.28",
"@types/react-dom": "^18.0.11",
"@vitejs/plugin-react": "^3.1.0",
"typescript": "^4.9.3",
"vite": "^4.2.0"
}
}
EOF
# Create config-server files
cat << EOF > config-server/server.js
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors());
app.get('/config', (req, res) => {
res.json([
{
name: 'Plugin A',
url: 'http://localhost:5000/plugin-a.js',
},
]);
});
const PORT = 3001;
app.listen(PORT, () => {
console.log(\`Config server running on port \${PORT}\`);
});
EOF
cat << EOF > config-server/package.json
{
"name": "config-server",
"version": "1.0.0",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.17.1",
"cors": "^2.8.5"
}
}
EOF
# Install dependencies
cd main-app && npm install && cd ..
cd plugin-a && npm install && cd ..
cd config-server && npm install && cd ..
echo "Microfrontend project setup complete!"
echo "To start the services:"
echo "1. In main-app directory: npm run start"
echo "2. In plugin-a directory: npm run build && npm run serve"
echo "3. In config-server directory: npm start"
cd ..