Skip to content

Commit 8cec846

Browse files
feat: Add @originjs/vite-plugin-federation for module federation support
* Updated package.json to include @originjs/vite-plugin-federation as a development dependency. * Configured Vite to use the federation plugin, enabling module federation with shared React components. * Enhanced build configuration for improved output handling and compatibility.
1 parent 5427ce4 commit 8cec846

File tree

6 files changed

+180
-0
lines changed

6 files changed

+180
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# Module Federation - Remote Configuration
2+
3+
Этот проект настроен как **remote** в системе Module Federation, что позволяет другим приложениям (host) импортировать и использовать основное приложение Draftly.
4+
5+
## Что экспортируется
6+
7+
### Основной компонент
8+
- `./App` - Главный компонент приложения Draftly
9+
10+
## Как использовать в host приложении
11+
12+
### 1. Установка зависимостей
13+
```bash
14+
npm install @originjs/vite-plugin-federation
15+
```
16+
17+
### 2. Конфигурация Vite в host приложении
18+
```typescript
19+
import federation from '@originjs/vite-plugin-federation';
20+
21+
export default defineConfig({
22+
plugins: [
23+
federation({
24+
name: 'host',
25+
remotes: {
26+
draftly: 'http://localhost:5173/assets/remoteEntry.js',
27+
},
28+
shared: {
29+
react: { requiredVersion: '^19.1.0' },
30+
'react-dom': { requiredVersion: '^19.1.0' },
31+
},
32+
}),
33+
],
34+
});
35+
```
36+
37+
### 3. Импорт основного компонента
38+
```typescript
39+
import { lazy } from 'react';
40+
41+
const DraftlyApp = lazy(() => import('draftly/App'));
42+
```
43+
44+
### 4. Использование в компонентах
45+
```tsx
46+
import React, { Suspense } from 'react';
47+
48+
function App() {
49+
return (
50+
<div>
51+
<Suspense fallback={<div>Loading Draftly...</div>}>
52+
<DraftlyApp />
53+
</Suspense>
54+
</div>
55+
);
56+
}
57+
```
58+
59+
## Сборка и развертывание
60+
61+
### Разработка
62+
```bash
63+
pnpm dev
64+
```
65+
66+
### Продакшн сборка
67+
```bash
68+
pnpm build
69+
```
70+
71+
После сборки в папке `dist` появится файл `remoteEntry.js`, который нужно разместить на сервере.
72+
73+
## Важные моменты
74+
75+
1. **Версии зависимостей**: Убедитесь, что версии React и React-DOM совпадают между host и remote приложениями.
76+
77+
2. **CORS**: При развертывании на разных доменах настройте CORS для доступа к `remoteEntry.js`.
78+
79+
3. **Типы TypeScript**: Для корректной работы типов в host приложении скопируйте файл `src/types/federation.d.ts`.
80+
81+
## Структура файлов
82+
83+
```
84+
frontend/
85+
├── src/
86+
│ └── types/
87+
│ └── federation.d.ts # Декларации типов для Module Federation
88+
├── vite.config.ts # Конфигурация с Module Federation
89+
└── README-MODULE-FEDERATION.md
90+
```
91+
92+
## Примеры использования
93+
94+
### Простой импорт
95+
```tsx
96+
import { CanvasWrapper } from 'draftly/App';
97+
98+
function MyApp() {
99+
return (
100+
<div>
101+
<h1>Мое приложение</h1>
102+
<DraftlyApp />
103+
</div>
104+
);
105+
}
106+
```
107+
108+
### Динамический импорт
109+
```tsx
110+
import { lazy, Suspense } from 'react';
111+
112+
const DraftlyApp = lazy(() => import('draftly/App'));
113+
114+
function App() {
115+
return (
116+
<Suspense fallback={<div>Загрузка Draftly...</div>}>
117+
<DraftlyApp />
118+
</Suspense>
119+
);
120+
}
121+
```

frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
},
2828
"devDependencies": {
2929
"@eslint/js": "^9.29.0",
30+
"@originjs/vite-plugin-federation": "^1.4.1",
3031
"@stylistic/eslint-plugin": "^5.1.0",
3132
"@types/jest": "^30.0.0",
3233
"@types/react": "^19.1.8",

frontend/pnpm-lock.yaml

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/src/shared/types/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Re-export all types for Module Federation
2+
export * from './canvas';
3+
export * from './colors';

frontend/src/types/federation.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare module 'draftly/App' {
2+
import { ComponentType } from 'react';
3+
const App: ComponentType;
4+
export default App;
5+
}

frontend/vite.config.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { defineConfig } from 'vite';
22
import react from '@vitejs/plugin-react';
33
import { VitePWA } from 'vite-plugin-pwa';
4+
import federation from '@originjs/vite-plugin-federation';
45

56
// https://vite.dev/config/
67
export default defineConfig({
@@ -29,7 +30,28 @@ export default defineConfig({
2930
start_url: '.',
3031
},
3132
}),
33+
federation({
34+
name: 'draftly',
35+
filename: 'remoteEntry.js',
36+
exposes: {
37+
'./App': './src/app/App.tsx',
38+
},
39+
shared: {
40+
react: { requiredVersion: '^19.1.0' },
41+
'react-dom': { requiredVersion: '^19.1.0' },
42+
},
43+
}),
3244
],
45+
build: {
46+
target: 'esnext',
47+
minify: false,
48+
cssCodeSplit: false,
49+
rollupOptions: {
50+
output: {
51+
minifyInternalExports: false,
52+
},
53+
},
54+
},
3355
resolve: {
3456
alias: {
3557
shared: '/src/shared',

0 commit comments

Comments
 (0)