Skip to content

Commit bc30809

Browse files
committed
feat:重命名插件为 @winner-fed/plugin-qiankun,更新版本为 1.0.0,完善 README 文档,添加子应用支持的配置和功能特性,修改相关依赖和配置文件以适应新结构。
1 parent c739452 commit bc30809

File tree

10 files changed

+1285
-204
lines changed

10 files changed

+1285
-204
lines changed

README.md

Lines changed: 279 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,305 @@
1-
# winjs-plugin-example
1+
# winjs-plugin-qiankun
22

3-
Example plugin for WinJS.
3+
适配 WinJS 的微前端 qiankun 子应用插件。
44

55
<p>
6-
<a href="https://npmjs.com/package/winjs-plugin-example">
7-
<img src="https://img.shields.io/npm/v/winjs-plugin-example?style=flat-square&colorA=564341&colorB=EDED91" alt="npm version" />
6+
<a href="https://npmjs.com/package/@winner-fed/plugin-qiankun">
7+
<img src="https://img.shields.io/npm/v/@winner-fed/plugin-qiankun?style=flat-square&colorA=564341&colorB=EDED91" alt="npm version" />
88
</a>
99
<img src="https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="license" />
10-
<a href="https://npmcharts.com/compare/winjs-plugin-example?minimal=true"><img src="https://img.shields.io/npm/dm/winjs-plugin-example.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="downloads" /></a>
10+
<a href="https://npmcharts.com/compare/@winner-fed/plugin-qiankun?minimal=true"><img src="https://img.shields.io/npm/dm/@winner-fed/plugin-qiankun.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="downloads" /></a>
1111
</p>
1212

13-
## Usage
1413

15-
Install:
14+
## 功能特性
15+
16+
- 🚀 **自动化子应用改造**: 自动将 WinJS 应用改造为 qiankun 子应用
17+
- 📦 **完整生命周期支持**: 提供 bootstrap、mount、unmount、update 生命周期钩子
18+
- 🔧 **智能路由配置**: 自动处理路由前缀和公共路径,支持 hash 和 browser 模式
19+
- 🎯 **运行时检测**: 提供运行时检测函数,判断是否在 qiankun 环境中运行
20+
- 📊 **缓存机制**: 内置应用实例缓存,优化性能
21+
- 🔄 **热重载支持**: 开发环境支持热重载调试
22+
23+
## 安装
1624

1725
```bash
18-
npm add winjs-plugin-example -D
26+
npm install @winner-fed/plugin-qiankun
27+
#
28+
yarn add @winner-fed/plugin-qiankun
29+
#
30+
pnpm add @winner-fed/plugin-qiankun
31+
```
32+
33+
## 基础使用
34+
35+
### 1. 在 WinJS 项目中启用插件
36+
37+
`.winrc.ts` 配置文件中添加插件:
38+
39+
```typescript
40+
import { defineConfig } from 'win';
41+
42+
export default defineConfig({
43+
plugins: [
44+
require.resolve('@winner-fed/plugin-qiankun')
45+
],
46+
qiankun: {
47+
child: {}
48+
}
49+
});
1950
```
2051

21-
Add plugin to your `.winrc.ts`:
52+
### 2. 基础配置
2253

23-
```ts
24-
// .winrc.ts
25-
export default {
26-
plugins: ['winjs-plugin-example'],
27-
// 开启配置
28-
example: {}
54+
```typescript
55+
export default defineConfig({
56+
plugins: [
57+
require.resolve('@winner-fed/plugin-qiankun')
58+
],
59+
qiankun: {
60+
child: {
61+
enable: true,
62+
devSourceMap: true,
63+
shouldNotModifyDefaultBase: false,
64+
shouldNotModifyRuntimePublicPath: false,
65+
shouldNotAddLibraryChunkName: false
66+
}
67+
}
68+
});
69+
```
70+
71+
## 配置选项
72+
73+
### child 配置
74+
75+
| 参数 | 类型 | 默认值 | 说明 |
76+
|------|------|--------|------|
77+
| `enable` | `boolean` | `true` | 是否启用子应用功能 |
78+
| `devSourceMap` | `boolean` | `true` | 开发环境是否启用 source map |
79+
| `shouldNotModifyDefaultBase` | `boolean` | `false` | 是否不修改默认的 base 路径 |
80+
| `shouldNotModifyRuntimePublicPath` | `boolean` | `false` | 是否不修改运行时公共路径 |
81+
| `shouldNotAddLibraryChunkName` | `boolean` | `false` | 是否不添加库 chunk 名称 |
82+
83+
### 生命周期钩子
84+
85+
```typescript
86+
export default defineConfig({
87+
qiankun: {
88+
child: {
89+
bootstrap: async (props) => {
90+
console.log('子应用启动', props);
91+
},
92+
mount: async (props) => {
93+
console.log('子应用挂载', props);
94+
},
95+
unmount: async (props) => {
96+
console.log('子应用卸载', props);
97+
},
98+
update: async (props) => {
99+
console.log('子应用更新', props);
100+
}
101+
}
102+
}
103+
});
104+
```
105+
106+
## 运行时 API
107+
108+
### isQiankun()
109+
110+
判断当前应用是否运行在 qiankun 环境中:
111+
112+
```javascript
113+
import { isQiankun } from '@@/plugin-qiankun-child';
114+
115+
if (isQiankun()) {
116+
console.log('当前运行在 qiankun 环境中');
117+
} else {
118+
console.log('当前独立运行');
119+
}
120+
```
121+
122+
### 运行时配置
123+
124+
`src/app.js` 中进行运行时配置:
125+
126+
```javascript
127+
export const qiankun = {
128+
child: {
129+
bootstrap: async (props) => {
130+
console.log('子应用启动', props);
131+
},
132+
mount: async (props) => {
133+
console.log('子应用挂载', props);
134+
// 处理主应用传递的 props
135+
if (props.token) {
136+
// 设置认证信息
137+
localStorage.setItem('token', props.token);
138+
}
139+
},
140+
unmount: async (props) => {
141+
console.log('子应用卸载', props);
142+
// 清理资源
143+
localStorage.removeItem('token');
144+
},
145+
update: async (props) => {
146+
console.log('子应用更新', props);
147+
}
148+
}
29149
};
30150
```
31151

32-
## Options
152+
## 高级功能
153+
154+
### 路由配置
155+
156+
插件会自动处理路由前缀,支持以下场景:
157+
158+
1. **Browser 模式**: 自动添加 `/${packageName}` 前缀
159+
2. **Hash 模式**: 自动重写路由,添加路由前缀
160+
3. **自定义前缀**: 通过主应用传递的 `routerPrefix` 配置
161+
162+
```javascript
163+
// 路由配置示例
164+
export const router = {
165+
scrollBehavior(to, from) {
166+
// 自定义滚动行为
167+
return { top: 0 };
168+
}
169+
};
170+
```
171+
172+
### 历史模式支持
173+
174+
```javascript
175+
export const qiankun = {
176+
child: {
177+
mount: async (props) => {
178+
// 主应用可以传递历史模式配置
179+
const { history } = props;
180+
if (history) {
181+
console.log('使用主应用指定的历史模式:', history);
182+
}
183+
}
184+
}
185+
};
186+
```
187+
188+
### 加载状态管理
189+
190+
```javascript
191+
export const qiankun = {
192+
child: {
193+
mount: async (props) => {
194+
// 手动控制加载状态
195+
if (props.setLoading) {
196+
props.setLoading(true);
197+
198+
// 执行初始化逻辑
199+
await initializeApp();
200+
201+
// 完成后关闭加载状态
202+
props.setLoading(false);
203+
}
204+
}
205+
}
206+
};
207+
```
208+
209+
## 最佳实践
210+
211+
### 1. 应用隔离
212+
213+
确保子应用的样式和全局变量不会影响主应用:
214+
215+
```css
216+
/* 在子应用中使用 scoped 样式 */
217+
.my-app {
218+
/* 子应用样式 */
219+
}
220+
```
221+
222+
### 2. 状态管理
223+
224+
```javascript
225+
// 使用 Vuex 或 Pinia 进行状态管理
226+
export const qiankun = {
227+
child: {
228+
mount: async (props) => {
229+
// 从主应用获取初始状态
230+
if (props.initialState) {
231+
store.commit('setInitialState', props.initialState);
232+
}
233+
},
234+
unmount: async (props) => {
235+
// 清理状态
236+
store.commit('reset');
237+
}
238+
}
239+
};
240+
```
241+
242+
### 3. 通信机制
243+
244+
```javascript
245+
export const qiankun = {
246+
child: {
247+
mount: async (props) => {
248+
// 监听主应用消息
249+
if (props.onGlobalStateChange) {
250+
props.onGlobalStateChange((state, prev) => {
251+
console.log('全局状态变化:', state, prev);
252+
});
253+
}
254+
255+
// 向主应用发送消息
256+
if (props.setGlobalState) {
257+
props.setGlobalState({
258+
childAppLoaded: true
259+
});
260+
}
261+
}
262+
}
263+
};
264+
```
265+
266+
## 注意事项
267+
268+
1. **包名配置**: 确保 `package.json` 中有正确的 `name` 字段
269+
2. **public-path**: 插件会自动处理 `publicPath`,一般不需要手动配置
270+
3. **样式隔离**: 建议使用 CSS Modules 或 styled-components 避免样式冲突
271+
4. **资源加载**: 确保静态资源路径正确,避免在子应用中使用绝对路径
272+
273+
## 故障排除
274+
275+
### 常见问题
276+
277+
1. **子应用无法正常加载**
278+
- 检查 `package.json` 中的 `name` 字段
279+
- 确认主应用的 `entry` 配置正确
33280

34-
### foo
281+
2. **路由跳转异常**
282+
- 检查路由配置是否正确
283+
- 确认 `base` 配置是否符合预期
35284

36-
Some description.
285+
3. **样式冲突**
286+
- 使用 CSS Modules 或 scoped 样式
287+
- 避免全局样式污染
37288

38-
- Type: `string`
39-
- Default: `undefined`
40-
- Example:
289+
### 调试技巧
41290

42-
```js
43-
export default {
44-
plugins: ['winjs-plugin-example'],
45-
// 开启配置
46-
example: {
47-
foo: 'bar'
291+
```javascript
292+
// 开启调试模式
293+
export const qiankun = {
294+
child: {
295+
mount: async (props) => {
296+
console.log('子应用 props:', props);
297+
console.log('是否在 qiankun 环境:', window.__POWERED_BY_QIANKUN__);
298+
}
48299
}
49300
};
50301
```
51302

52-
## License
303+
## 许可证
53304

54305
[MIT](./LICENSE).

libs/childRuntimePlugin.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
/**
2-
* runtime
3-
* @Author: liwb ([email protected])
4-
* @Date: 2024-05-30 15:36
5-
* @LastEditTime: 2024-05-30 15:36
6-
* @Description: runtime
7-
*/
81
// @ts-nocheck
92
import { createHistory } from '@@/core/history';
103
import qiankunRender, { clientRenderOptsStack } from './lifecycles';

libs/lifecycles.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
/**
2-
* lifecycle
3-
* @Author: liwb ([email protected])
4-
* @Date: 2024-05-30 15:35
5-
* @LastEditTime: 2024-05-30 15:35
6-
* @Description: lifecycle
7-
*/
81
// @ts-nocheck
92
import { getPluginManager } from '@@/core/plugin';
103
import { ApplyPluginsType } from '@@/exports';

package.json

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
2-
"name": "winjs-plugin-template",
2+
"name": "@winner-fed/plugin-qiankun",
3+
"description": "适配 WinJS 的微前端 qiankun 子应用插件。",
34
"keywords": [
45
"winjs",
56
"plugin",
@@ -11,10 +12,10 @@
1112
"typescript",
1213
"biome"
1314
],
14-
"version": "0.0.0",
15+
"version": "1.0.0",
1516
"repository": {
1617
"type": "git",
17-
"url": "git+https://github.com/winjs-dev/winjs-plugin-template.git"
18+
"url": "git+https://github.com/winjs-dev/winjs-plugin-qiankun.git"
1819
},
1920
"license": "MIT",
2021
"type": "module",
@@ -23,13 +24,19 @@
2324
"types": "./dist/index.d.ts",
2425
"import": "./dist/index.js",
2526
"require": "./dist/index.cjs"
27+
},
28+
"./child": {
29+
"types": "./dist/child.d.ts",
30+
"import": "./dist/child.js",
31+
"require": "./dist/child.cjs"
2632
}
2733
},
2834
"main": "./dist/index.cjs",
2935
"module": "./dist/index.js",
3036
"types": "./dist/index.d.ts",
3137
"files": [
32-
"dist"
38+
"dist",
39+
"libs"
3340
],
3441
"scripts": {
3542
"build": "rslib build",

0 commit comments

Comments
 (0)