@cqsjjb/jjb-cloud-component 0.0.6 → 0.0.7-experimental
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +211 -67
- package/cloud-component.d.ts +8 -9
- package/cloud-component.js +31 -20
- package/import-cloud-component.d.ts +46 -49
- package/import-cloud-component.js +55 -81
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/package.json +2 -4
package/README.md
CHANGED
|
@@ -1,78 +1,222 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @cqsjjb/jjb-cloud-component
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
前端云组件库,支持动态加载远程React组件。
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## 特性
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
- 🚀 动态加载远程云组件
|
|
8
|
+
- 🔧 支持组件依赖管理
|
|
9
|
+
- 🎨 样式隔离和自动清理
|
|
10
|
+
- 📦 CommonJS模块支持
|
|
11
|
+
- 🔄 完整的生命周期钩子
|
|
12
|
+
- 💪 TypeScript支持
|
|
8
13
|
|
|
9
|
-
##
|
|
14
|
+
## 安装
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @cqsjjb/jjb-cloud-component
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## 快速开始
|
|
21
|
+
|
|
22
|
+
### 基础用法
|
|
10
23
|
|
|
11
24
|
```jsx
|
|
12
|
-
import
|
|
13
|
-
import {
|
|
25
|
+
import React from 'react';
|
|
26
|
+
import { CloudComponent } from '@cqsjjb/jjb-cloud-component';
|
|
14
27
|
|
|
15
28
|
function App() {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
29
|
+
return (
|
|
30
|
+
<CloudComponent
|
|
31
|
+
from="https://example.com/my-component.js"
|
|
32
|
+
componentKey="my-component"
|
|
33
|
+
componentProps={{
|
|
34
|
+
title: "Hello World",
|
|
35
|
+
count: 42
|
|
36
|
+
}}
|
|
37
|
+
onLoadStart={() => console.log('开始加载')}
|
|
38
|
+
onLoadEnd={() => console.log('加载完成')}
|
|
39
|
+
onMounted={(key, ref) => {
|
|
40
|
+
console.log('组件已挂载', key);
|
|
41
|
+
// 可以调用组件方法
|
|
42
|
+
ref.updateData?.({ theme: 'dark' });
|
|
43
|
+
}}
|
|
44
|
+
/>
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### 高级用法
|
|
50
|
+
|
|
51
|
+
```jsx
|
|
52
|
+
import React from 'react';
|
|
53
|
+
import { CloudComponent } from '@cqsjjb/jjb-cloud-component';
|
|
54
|
+
|
|
55
|
+
function App() {
|
|
56
|
+
return (
|
|
57
|
+
<CloudComponent
|
|
58
|
+
from="https://cdn.example.com/advanced-component.js"
|
|
59
|
+
componentKey="advanced-component"
|
|
60
|
+
dependencies={{
|
|
61
|
+
'react': React,
|
|
62
|
+
'lodash': require('lodash'),
|
|
63
|
+
'moment': require('moment')
|
|
64
|
+
}}
|
|
65
|
+
cache="force-cache"
|
|
66
|
+
headers={{
|
|
67
|
+
'Authorization': 'Bearer token'
|
|
68
|
+
}}
|
|
69
|
+
initialize={true}
|
|
70
|
+
componentProps={{
|
|
71
|
+
config: { theme: 'dark' },
|
|
72
|
+
data: { items: [] }
|
|
73
|
+
}}
|
|
74
|
+
onLoadStart={() => console.log('开始加载云组件')}
|
|
75
|
+
onLoadEnd={() => console.log('云组件加载完成')}
|
|
76
|
+
onMounted={(key, ref) => {
|
|
77
|
+
console.log(`组件 ${key} 已挂载`);
|
|
78
|
+
// 获取组件配置
|
|
79
|
+
console.log('表单配置:', ref.formItems);
|
|
80
|
+
// 更新组件数据
|
|
81
|
+
ref.updateData?.({
|
|
82
|
+
settings: { theme: 'light' },
|
|
83
|
+
dataSource: { items: [1, 2, 3] }
|
|
84
|
+
});
|
|
85
|
+
}}
|
|
86
|
+
onUpdated={(key, ref) => {
|
|
87
|
+
console.log(`组件 ${key} 已更新`, ref);
|
|
88
|
+
}}
|
|
89
|
+
onDestroy={() => console.log('组件已销毁')}
|
|
90
|
+
/>
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## API 文档
|
|
96
|
+
|
|
97
|
+
### CloudComponent Props
|
|
98
|
+
|
|
99
|
+
| 属性 | 类型 | 必需 | 描述 |
|
|
100
|
+
|------|------|------|------|
|
|
101
|
+
| `from` | `string` | ✅ | 云组件资源地址 |
|
|
102
|
+
| `componentKey` | `string` | ✅ | 组件唯一标识 |
|
|
103
|
+
| `dependencies` | `Record<string, unknown>` | ❌ | 组件依赖 |
|
|
104
|
+
| `cache` | `CacheStrategy` | ❌ | 缓存策略 |
|
|
105
|
+
| `headers` | `Record<string, string>` | ❌ | 请求头 |
|
|
106
|
+
| `initialize` | `boolean` | ❌ | 是否需要初始化 |
|
|
107
|
+
| `componentProps` | `Record<string, unknown>` | ❌ | 组件额外属性 |
|
|
108
|
+
| `onLoadStart` | `() => void` | ❌ | 开始加载回调 |
|
|
109
|
+
| `onLoadEnd` | `() => void` | ❌ | 加载完成回调 |
|
|
110
|
+
| `onMounted` | `(key: string, ref: ComponentRef) => void` | ❌ | 组件挂载回调 |
|
|
111
|
+
| `onUpdated` | `(key: string, ref: UpdateRef) => void` | ❌ | 组件更新回调 |
|
|
112
|
+
| `onDestroy` | `() => void` | ❌ | 组件销毁回调 |
|
|
113
|
+
|
|
114
|
+
### 类型定义
|
|
115
|
+
|
|
116
|
+
```typescript
|
|
117
|
+
type CacheStrategy = 'default' | 'force-cache' | 'no-cache' | 'no-store' | 'only-if-cached' | 'reload';
|
|
118
|
+
|
|
119
|
+
interface ComponentRef {
|
|
120
|
+
formItems?: Record<string, unknown>;
|
|
121
|
+
updateData?: (settings?: Record<string, unknown>, dataSource?: unknown) => void;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
interface UpdateRef {
|
|
125
|
+
settings?: Record<string, unknown>;
|
|
126
|
+
dataSource?: unknown;
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### 工具函数
|
|
131
|
+
|
|
132
|
+
#### useUnMountCloudComponentStyle
|
|
133
|
+
|
|
134
|
+
手动卸载云组件样式。
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
import { useUnMountCloudComponentStyle } from '@cqsjjb/jjb-cloud-component';
|
|
138
|
+
|
|
139
|
+
// 卸载指定样式的云组件
|
|
140
|
+
useUnMountCloudComponentStyle('style-id-123');
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
#### ImportCloudComponent
|
|
144
|
+
|
|
145
|
+
手动导入云组件模块。
|
|
146
|
+
|
|
147
|
+
```typescript
|
|
148
|
+
import { ImportCloudComponent } from '@cqsjjb/jjb-cloud-component';
|
|
149
|
+
|
|
150
|
+
const { module, styleId, isNewVersion } = await ImportCloudComponent({
|
|
151
|
+
from: 'https://example.com/component.js',
|
|
152
|
+
dependencies: { react: React },
|
|
153
|
+
cache: 'force-cache'
|
|
154
|
+
});
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## 云组件开发
|
|
158
|
+
|
|
159
|
+
### 组件结构
|
|
160
|
+
|
|
161
|
+
云组件需要导出以下结构:
|
|
162
|
+
|
|
163
|
+
```javascript
|
|
164
|
+
// 组件信息(可选,用于版本检测)
|
|
165
|
+
const info = {
|
|
166
|
+
name: 'MyComponent',
|
|
167
|
+
version: '1.0.0',
|
|
168
|
+
description: '我的云组件',
|
|
169
|
+
remark: '这是一个示例组件',
|
|
170
|
+
environment: {},
|
|
171
|
+
dependencies: {}
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
// 组件样式(可选)
|
|
175
|
+
const style = `
|
|
176
|
+
.my-component {
|
|
177
|
+
color: blue;
|
|
178
|
+
}
|
|
179
|
+
`;
|
|
180
|
+
|
|
181
|
+
// 组件实现
|
|
182
|
+
function MyComponent(props) {
|
|
183
|
+
return React.createElement('div', {
|
|
184
|
+
className: 'my-component',
|
|
185
|
+
'data-cloud-component-style-id': '{cloudComponentStyleId}'
|
|
186
|
+
}, props.title);
|
|
24
187
|
}
|
|
188
|
+
|
|
189
|
+
// 导出
|
|
190
|
+
module.exports = {
|
|
191
|
+
info,
|
|
192
|
+
default: MyComponent
|
|
193
|
+
};
|
|
25
194
|
```
|
|
26
195
|
|
|
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
|
-
| default | 组件本体 | `React.Component` | - |
|
|
55
|
-
|
|
56
|
-
## FAQ
|
|
57
|
-
* 在底座中加载云组件报:Error: Minified React error #321
|
|
58
|
-
这个错误通常是应用ReactDOM渲染了分别来自不同React构建的DOM树,简单来说就是存在两个React库。由于云组件默认使用__coreLib依赖变量,当你的云组件加载时可能用到其他应用的__coreLib,解决办法是把应用main.js中的__coreLib改成唯一变量名,例如:__coreUserLib,然后修改云组件的lib属性即可。
|
|
59
|
-
```jsx
|
|
60
|
-
<CloudComponent lib="__coreUserLib" />
|
|
61
|
-
|
|
62
|
-
* 组件"XX"不存在"info"配置,疑似旧版云组件或配置错误
|
|
63
|
-
这个警告通常是使用了旧版云组件或链接不是云组件导致。
|
|
64
|
-
* 需要"XX"依赖,请在"window.XX"中提供此依赖!
|
|
65
|
-
这个错误只有新版云组件才会有,通常是应用依赖变量(__coreLib)不匹配云组件的依赖清单,请请检查!注意应用依赖变量的键名必须与云组件的依赖清单键名一致,否则会报错!
|
|
66
|
-
云组件
|
|
67
|
-
```text
|
|
68
|
-
// src/jjb.config.json
|
|
69
|
-
{ dependencies: { 'react': 'react', 'react-dom': 'react-dom' } }
|
|
70
|
-
```
|
|
71
|
-
应用
|
|
72
|
-
```js
|
|
73
|
-
// src/main.js
|
|
74
|
-
window.__coreLib = {
|
|
75
|
-
'react': require('react'),
|
|
76
|
-
'react-dom': require('react-dom')
|
|
77
|
-
}
|
|
78
|
-
```
|
|
196
|
+
### 样式处理
|
|
197
|
+
|
|
198
|
+
云组件中的样式会自动注入到页面中,使用 `{cloudComponentStyleId}` 占位符会被替换为实际的样式ID。
|
|
199
|
+
|
|
200
|
+
```javascript
|
|
201
|
+
const style = `
|
|
202
|
+
.my-component-{cloudComponentStyleId} {
|
|
203
|
+
color: red;
|
|
204
|
+
}
|
|
205
|
+
`;
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
## 注意事项
|
|
209
|
+
|
|
210
|
+
1. **网络请求**: 确保云组件地址可访问
|
|
211
|
+
2. **依赖管理**: 正确配置组件所需的依赖
|
|
212
|
+
3. **样式隔离**: 使用样式ID避免样式冲突
|
|
213
|
+
4. **错误处理**: 实现适当的错误处理机制
|
|
214
|
+
5. **版本兼容**: 注意新旧版本云组件的兼容性
|
|
215
|
+
|
|
216
|
+
## 许可证
|
|
217
|
+
|
|
218
|
+
MIT
|
|
219
|
+
|
|
220
|
+
## 作者
|
|
221
|
+
|
|
222
|
+
jiaoxiwei
|
package/cloud-component.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
// @ts-ignore
|
|
2
1
|
import * as React from 'react';
|
|
3
2
|
|
|
4
3
|
interface ComponentProps {
|
|
5
|
-
ref?: React.Ref<
|
|
4
|
+
ref?: React.Ref<Record<string, unknown>>;
|
|
6
5
|
children?: React.ReactNode;
|
|
7
6
|
}
|
|
8
7
|
|
|
@@ -13,8 +12,8 @@ interface ComponentProps {
|
|
|
13
12
|
export function useUnMountCloudComponentStyle (styleId: string): void;
|
|
14
13
|
|
|
15
14
|
interface CloudComponentProps extends ComponentProps {
|
|
16
|
-
//
|
|
17
|
-
|
|
15
|
+
// 组件依赖
|
|
16
|
+
dependencies?: { [ p: string ]: string }
|
|
18
17
|
// 组件资源地址
|
|
19
18
|
from: string;
|
|
20
19
|
// 缓存
|
|
@@ -26,7 +25,7 @@ interface CloudComponentProps extends ComponentProps {
|
|
|
26
25
|
// 组件唯一key
|
|
27
26
|
componentKey: string;
|
|
28
27
|
// 组件的额外Props
|
|
29
|
-
componentProps?:
|
|
28
|
+
componentProps?: Record<string, unknown>;
|
|
30
29
|
// 组件开始加载
|
|
31
30
|
onLoadStart?: () => void;
|
|
32
31
|
// 组件结束加载
|
|
@@ -36,14 +35,14 @@ interface CloudComponentProps extends ComponentProps {
|
|
|
36
35
|
// 组件已挂载
|
|
37
36
|
onMounted?: (key: string, ref: {
|
|
38
37
|
// 组件输出的表单配置
|
|
39
|
-
formItems?:
|
|
38
|
+
formItems?: Record<string, unknown>;
|
|
40
39
|
// 组件更新
|
|
41
|
-
updateData?: (settings?:
|
|
40
|
+
updateData?: (settings?: Record<string, unknown>, dataSource?: unknown) => void;
|
|
42
41
|
}) => void;
|
|
43
42
|
// 组件已更新
|
|
44
43
|
onUpdated?: (key: string, ref: {
|
|
45
|
-
settings?:
|
|
46
|
-
dataSource?:
|
|
44
|
+
settings?: Record<string, unknown>;
|
|
45
|
+
dataSource?: unknown;
|
|
47
46
|
}) => void;
|
|
48
47
|
}
|
|
49
48
|
|
package/cloud-component.js
CHANGED
|
@@ -24,10 +24,10 @@ export default function CloudComponent(props) {
|
|
|
24
24
|
let styleId;
|
|
25
25
|
const [ Component, setComponent ] = React.useState(null);
|
|
26
26
|
const {
|
|
27
|
-
lib,
|
|
28
27
|
from,
|
|
29
28
|
cache,
|
|
30
29
|
headers,
|
|
30
|
+
dependencies = [],
|
|
31
31
|
initialize,
|
|
32
32
|
componentKey,
|
|
33
33
|
componentProps,
|
|
@@ -40,35 +40,46 @@ export default function CloudComponent(props) {
|
|
|
40
40
|
|
|
41
41
|
React.useEffect(() => {
|
|
42
42
|
async function load() {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
43
|
+
try {
|
|
44
|
+
onLoadStart && onLoadStart();
|
|
45
|
+
const {
|
|
46
|
+
module,
|
|
47
|
+
styleId: _styleId,
|
|
48
|
+
isNewVersion
|
|
49
|
+
} = await ImportCloudComponent({
|
|
50
|
+
from,
|
|
51
|
+
cache,
|
|
52
|
+
headers,
|
|
53
|
+
dependencies
|
|
54
|
+
});
|
|
54
55
|
|
|
55
|
-
|
|
56
|
+
styleId = _styleId;
|
|
56
57
|
|
|
57
|
-
|
|
58
|
+
setComponent(() => {
|
|
59
|
+
onLoadEnd && onLoadEnd();
|
|
60
|
+
return isNewVersion
|
|
61
|
+
? module.default()
|
|
62
|
+
: module.default;
|
|
63
|
+
});
|
|
64
|
+
} catch (error) {
|
|
65
|
+
console.error('云组件加载失败:', error);
|
|
58
66
|
onLoadEnd && onLoadEnd();
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
:
|
|
62
|
-
|
|
67
|
+
// 可以设置一个错误组件或显示错误信息
|
|
68
|
+
setComponent(() => () => React.createElement('div', {
|
|
69
|
+
style: { color: 'red', padding: '10px' }
|
|
70
|
+
}, `组件加载失败: ${error.message}`));
|
|
71
|
+
}
|
|
63
72
|
}
|
|
64
73
|
|
|
65
74
|
load().then(() => null);
|
|
66
75
|
|
|
67
76
|
return () => {
|
|
68
|
-
|
|
77
|
+
if (styleId) {
|
|
78
|
+
useUnMountCloudComponentStyle(styleId);
|
|
79
|
+
}
|
|
69
80
|
onDestroy && onDestroy();
|
|
70
81
|
};
|
|
71
|
-
}, []);
|
|
82
|
+
}, [from, cache, headers, dependencies]);
|
|
72
83
|
|
|
73
84
|
return Component && (
|
|
74
85
|
<Component
|
|
@@ -1,10 +1,27 @@
|
|
|
1
|
-
// @ts-ignore
|
|
2
1
|
import React from 'react';
|
|
3
2
|
|
|
3
|
+
/**
|
|
4
|
+
* @description 云组件信息
|
|
5
|
+
*/
|
|
6
|
+
interface CloudComponentInfo {
|
|
7
|
+
// 组件名称
|
|
8
|
+
name: string;
|
|
9
|
+
// 组件说明
|
|
10
|
+
remark: string;
|
|
11
|
+
// 组件版本
|
|
12
|
+
version: string;
|
|
13
|
+
// 组件描述
|
|
14
|
+
description: string;
|
|
15
|
+
// 组件环境
|
|
16
|
+
environment: Record<string, unknown>;
|
|
17
|
+
// 组件依赖
|
|
18
|
+
dependencies?: Record<string, string>;
|
|
19
|
+
}
|
|
20
|
+
|
|
4
21
|
/**
|
|
5
22
|
* @description 云组件模块
|
|
6
23
|
*/
|
|
7
|
-
|
|
24
|
+
interface CloudModule {
|
|
8
25
|
// 样式id
|
|
9
26
|
styleId: string;
|
|
10
27
|
// 是否是新版云组件
|
|
@@ -12,69 +29,49 @@ type CloudModule = {
|
|
|
12
29
|
// 组件模块
|
|
13
30
|
module: {
|
|
14
31
|
// 组件信息
|
|
15
|
-
info
|
|
16
|
-
// 组件名称
|
|
17
|
-
name: string;
|
|
18
|
-
// 组件说明
|
|
19
|
-
remark: string;
|
|
20
|
-
// 组件版本
|
|
21
|
-
version: string;
|
|
22
|
-
// 组件描述
|
|
23
|
-
description: string;
|
|
24
|
-
// 组件环境
|
|
25
|
-
environment: {};
|
|
26
|
-
// 组件依赖
|
|
27
|
-
dependencies?: { [ p: string ]: string }
|
|
28
|
-
},
|
|
32
|
+
info?: CloudComponentInfo;
|
|
29
33
|
// 组件本体
|
|
30
|
-
default: () => React.
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* @description 依赖检测
|
|
36
|
-
* @param lib {string} 依赖变量
|
|
37
|
-
* @param name {string} 依赖名称
|
|
38
|
-
* @return {boolean}
|
|
39
|
-
*/
|
|
40
|
-
declare function checkDependence (lib: string, name: string): boolean;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* @description 依赖检测
|
|
44
|
-
* @param lib {string} 依赖变量
|
|
45
|
-
* @param dependencies {string[]} 依赖名称
|
|
46
|
-
* @return {Array<{ name: string, check: boolean }>}
|
|
47
|
-
*/
|
|
48
|
-
declare function checkDependencies (lib: string, dependencies: string[]): Array<{ name: string, check: boolean }>
|
|
34
|
+
default: React.ComponentType | (() => React.ComponentType);
|
|
35
|
+
};
|
|
36
|
+
}
|
|
49
37
|
|
|
50
38
|
/**
|
|
51
39
|
* @description 生成CJS运行环境
|
|
52
|
-
* @param options
|
|
53
|
-
* @param code
|
|
54
|
-
* @return
|
|
40
|
+
* @param options 云组件地址配置
|
|
41
|
+
* @param code 云组件代码
|
|
42
|
+
* @return CJS运行环境
|
|
55
43
|
*/
|
|
56
|
-
declare function genCommonJSRuntime
|
|
44
|
+
declare function genCommonJSRuntime(
|
|
45
|
+
options: { from: string },
|
|
46
|
+
code: string
|
|
47
|
+
): {
|
|
48
|
+
styleId: string;
|
|
49
|
+
useModule: (dependencies: Record<string, unknown>) => CloudModule['module'];
|
|
50
|
+
};
|
|
57
51
|
|
|
58
52
|
/**
|
|
59
|
-
* @description
|
|
60
|
-
* @param options {string} 依赖变量
|
|
61
|
-
* @return {Promise<CloudModule>}
|
|
53
|
+
* @description 导入云组件选项
|
|
62
54
|
*/
|
|
63
|
-
|
|
64
|
-
//
|
|
65
|
-
|
|
55
|
+
interface ImportCloudComponentOptions {
|
|
56
|
+
// 组件依赖
|
|
57
|
+
dependencies?: Record<string, unknown>;
|
|
66
58
|
// 组件地址
|
|
67
59
|
from: string;
|
|
68
60
|
// 缓存
|
|
69
61
|
cache?: 'default' | 'force-cache' | 'no-cache' | 'no-store' | 'only-if-cached' | 'reload';
|
|
70
62
|
// 请求头
|
|
71
|
-
headers?:
|
|
72
|
-
}
|
|
63
|
+
headers?: Record<string, string>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @description 导入云组件
|
|
68
|
+
* @param options 导入选项
|
|
69
|
+
* @return Promise<CloudModule>
|
|
70
|
+
*/
|
|
71
|
+
declare function ImportCloudComponent(options: ImportCloudComponentOptions): Promise<CloudModule>;
|
|
73
72
|
|
|
74
73
|
export default ImportCloudComponent;
|
|
75
74
|
|
|
76
75
|
export {
|
|
77
|
-
checkDependence,
|
|
78
|
-
checkDependencies,
|
|
79
76
|
genCommonJSRuntime
|
|
80
77
|
};
|
|
@@ -1,28 +1,11 @@
|
|
|
1
1
|
import { tools } from '@cqsjjb/jjb-common-lib';
|
|
2
2
|
|
|
3
|
-
function resolveLib(lib) {
|
|
4
|
-
if (typeof window.proxy !== 'undefined' && lib.indexOf('base') === -1) {
|
|
5
|
-
return genFieldPath(`window.proxy.${lib}`);
|
|
6
|
-
} else {
|
|
7
|
-
return genFieldPath(`window.${lib}`);
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
function genFieldPath(field) {
|
|
12
|
-
return field
|
|
13
|
-
.split('.')
|
|
14
|
-
.filter(Boolean)
|
|
15
|
-
.join('.');
|
|
16
|
-
}
|
|
17
|
-
|
|
18
3
|
/**
|
|
19
4
|
* @description 生成CJS运行环境
|
|
20
|
-
* @param {string} lib 云组件依赖变量
|
|
21
5
|
* @param {string} from 云组件地址
|
|
22
6
|
* @param {string} code 注入云组件代码
|
|
23
7
|
*/
|
|
24
8
|
export function genCommonJSRuntime({
|
|
25
|
-
lib,
|
|
26
9
|
from
|
|
27
10
|
}, code) {
|
|
28
11
|
const styleId = tools.createOnlyKey();
|
|
@@ -32,12 +15,11 @@ export function genCommonJSRuntime({
|
|
|
32
15
|
/* cloud-component-assets: ${from} */
|
|
33
16
|
const module = { exports: null };
|
|
34
17
|
const exports = {};
|
|
35
|
-
const require =
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
throw Error('云组件使用依赖失败,请确认"['+ id +']"是否在${resolveLib(lib)}中?');
|
|
18
|
+
const require = id => {
|
|
19
|
+
try {
|
|
20
|
+
return arguments[0][id];
|
|
21
|
+
} catch (error) {
|
|
22
|
+
throw new Error("云组件依赖[" + id + "]不存在!请检查dependencies配置!");
|
|
41
23
|
}
|
|
42
24
|
};
|
|
43
25
|
/* cloud-component-code */
|
|
@@ -48,35 +30,9 @@ export function genCommonJSRuntime({
|
|
|
48
30
|
};
|
|
49
31
|
}
|
|
50
32
|
|
|
51
|
-
/**
|
|
52
|
-
* @description 宿主环境依赖检测
|
|
53
|
-
* @param lib {string} 依赖变量
|
|
54
|
-
* @param name {string} 依赖名称
|
|
55
|
-
* @return {boolean}
|
|
56
|
-
*/
|
|
57
|
-
export function checkDependence(lib, name) {
|
|
58
|
-
const dependencies = new Function(`return ${resolveLib(lib)}`)();
|
|
59
|
-
return tools.isUndefined(dependencies[ name ]);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* @description 依赖检测
|
|
64
|
-
* @param lib {string}
|
|
65
|
-
* @param dependencies {string[]}
|
|
66
|
-
* @return {Array<{ name: string, check: boolean }>}
|
|
67
|
-
*/
|
|
68
|
-
export function checkDependencies(lib, dependencies) {
|
|
69
|
-
return Object.keys(dependencies).map(name => {
|
|
70
|
-
return {
|
|
71
|
-
name,
|
|
72
|
-
check: checkDependence(lib, name)
|
|
73
|
-
};
|
|
74
|
-
}).filter(i => i.check);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
33
|
/**
|
|
78
34
|
* @description 手动加载模块
|
|
79
|
-
* @param options {{ from: string,
|
|
35
|
+
* @param options {{ from: string, cache: string, headers: {} }}
|
|
80
36
|
* @example
|
|
81
37
|
* ImportCloudComponent({
|
|
82
38
|
* from: 'https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.min.js'
|
|
@@ -86,9 +42,8 @@ export function checkDependencies(lib, dependencies) {
|
|
|
86
42
|
* @return {Promise<{ module: { info: {}, default:() => React.Component }, styleId: string }>}
|
|
87
43
|
*/
|
|
88
44
|
export default async function ImportCloudComponent(options) {
|
|
89
|
-
const lib = options.lib || '__coreLib';
|
|
90
45
|
const from = options.from;
|
|
91
|
-
|
|
46
|
+
const dependencies = options.dependencies || {};
|
|
92
47
|
const cache = options.cache || 'force-cache';
|
|
93
48
|
const headers = tools.toObject(options.headers);
|
|
94
49
|
|
|
@@ -98,44 +53,63 @@ export default async function ImportCloudComponent(options) {
|
|
|
98
53
|
throw Error('云组件资源访问地址不能为空!');
|
|
99
54
|
}
|
|
100
55
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
56
|
+
let response;
|
|
57
|
+
let responseText;
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
response = await fetch(from, {
|
|
61
|
+
cache,
|
|
62
|
+
headers
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
if (!response.ok) {
|
|
66
|
+
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
responseText = await response.text();
|
|
70
|
+
} catch (error) {
|
|
71
|
+
throw new Error(`云组件加载失败: ${error.message}`);
|
|
72
|
+
}
|
|
73
|
+
const __require = {};
|
|
74
|
+
|
|
75
|
+
Object.entries(dependencies).forEach(([key, value]) => {
|
|
76
|
+
__require[key] = value;
|
|
104
77
|
});
|
|
105
|
-
const responseText = await response.text();
|
|
106
78
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
79
|
+
let module;
|
|
80
|
+
let styleId;
|
|
81
|
+
let useModule;
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
const runtime = genCommonJSRuntime({
|
|
85
|
+
from
|
|
86
|
+
}, responseText);
|
|
87
|
+
|
|
88
|
+
styleId = runtime.styleId;
|
|
89
|
+
useModule = runtime.useModule;
|
|
90
|
+
|
|
91
|
+
// 获取云组件导出CJS模块
|
|
92
|
+
module = useModule(__require);
|
|
93
|
+
|
|
94
|
+
if (!module || typeof module !== 'object') {
|
|
95
|
+
throw new Error('云组件模块格式错误');
|
|
96
|
+
}
|
|
97
|
+
} catch (error) {
|
|
98
|
+
throw new Error(`云组件解析失败: ${error.message}`);
|
|
99
|
+
}
|
|
100
|
+
|
|
116
101
|
// 获取云组件info变量
|
|
117
102
|
const { info } = module;
|
|
118
103
|
// 判断当前云组件是否导出了info变量
|
|
119
104
|
if (tools.isUndefined(info)) {
|
|
120
|
-
|
|
105
|
+
if (process.env.NODE_ENV === 'development') {
|
|
106
|
+
console.warn(`云组件 "${from}" 不存在"info"配置,疑似旧版云组件或配置错误`);
|
|
107
|
+
}
|
|
121
108
|
isNewVersion = false;
|
|
122
109
|
} else {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
name,
|
|
126
|
-
dependencies
|
|
127
|
-
} = info;
|
|
128
|
-
// 判断云组件是否有依赖配置
|
|
129
|
-
if (!tools.isEmptyObject(dependencies)) {
|
|
130
|
-
// 是否存在未安装的依赖项
|
|
131
|
-
const noInstallDependencies = checkDependencies(lib, dependencies);
|
|
132
|
-
// 判断是否有
|
|
133
|
-
if (tools.isValidArray(noInstallDependencies)) {
|
|
134
|
-
throw Error(`"${name}" 需要"${noInstallDependencies.at(0).name}"依赖,请在"${resolveLib(lib)}"中提供此依赖!`);
|
|
135
|
-
}
|
|
110
|
+
if (process.env.NODE_ENV === 'development') {
|
|
111
|
+
console.log(`云组件 [${info.name}] 加载完成!`);
|
|
136
112
|
}
|
|
137
|
-
|
|
138
|
-
window[ 'console' ][ 'log' ](`云组件 [${info.name}] 加载完成!`);
|
|
139
113
|
}
|
|
140
114
|
|
|
141
115
|
// 返回组件CSJ模块
|
package/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { default as CloudComponent, useUnMountCloudComponentStyle } from './cloud-component';
|
|
2
|
-
export { default as ImportCloudComponent,
|
|
2
|
+
export { default as ImportCloudComponent, genCommonJSRuntime } from './import-cloud-component';
|
package/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { default as CloudComponent, useUnMountCloudComponentStyle } from './cloud-component';
|
|
2
|
-
export { default as ImportCloudComponent,
|
|
2
|
+
export { default as ImportCloudComponent, genCommonJSRuntime } from './import-cloud-component';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cqsjjb/jjb-cloud-component",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7-experimental",
|
|
4
4
|
"description": "前端-云组件",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -11,7 +11,5 @@
|
|
|
11
11
|
"typings": "index.d.ts",
|
|
12
12
|
"author": "jiaoxiwei",
|
|
13
13
|
"license": "MIT",
|
|
14
|
-
"dependencies": {
|
|
15
|
-
|
|
16
|
-
}
|
|
14
|
+
"dependencies": {}
|
|
17
15
|
}
|