@cqsjjb/jjb-cloud-component 0.0.5 → 0.0.6
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 +78 -0
- package/import-cloud-component.js +18 -20
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# ImportCloudComponent
|
|
2
|
+
|
|
3
|
+
手动导入云组件
|
|
4
|
+
|
|
5
|
+
## 何时使用
|
|
6
|
+
|
|
7
|
+
需要提前预加载、批量加载云组件等场景时使用
|
|
8
|
+
|
|
9
|
+
## 代码演示
|
|
10
|
+
|
|
11
|
+
```jsx
|
|
12
|
+
import { useEffect } from 'react';
|
|
13
|
+
import { ImportCloudComponent } from '@cqsjjb/jjb-cloud-component';
|
|
14
|
+
|
|
15
|
+
function App() {
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
ImportCloudComponent({
|
|
18
|
+
lib: '__coreLib',
|
|
19
|
+
from: 'https://xxx.xxx.xx/index.js'
|
|
20
|
+
}).then(m => {
|
|
21
|
+
// m
|
|
22
|
+
});
|
|
23
|
+
}, []);
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## 参数
|
|
28
|
+
|
|
29
|
+
| 属性 | 说明 | 类型 | 默认值 |
|
|
30
|
+
|---------|:----------|:-----------------------------------------------------------------------:|----------:|
|
|
31
|
+
| from | 云组件资源链接 | `string` | - |
|
|
32
|
+
| lib | 云组件依赖库变量 | `string` | `coreLib` |
|
|
33
|
+
| cache | 云组件资源缓存策略 | `default` `force-cache` `no-cache` `no-store` `only-if-cached` `reload` | `default` |
|
|
34
|
+
| headers | 请求头 | `Record<string, any>` | - |
|
|
35
|
+
|
|
36
|
+
## 返回值
|
|
37
|
+
|
|
38
|
+
| 属性 | 说明 | 类型 | 默认值 |
|
|
39
|
+
|--------------|:------------------------------------------------------|:---------:|----:|
|
|
40
|
+
| styleId | 样式id,云组件加载时,自动判断内部是否有自定义样式,如果有内部自定义添加到head元素上,并返回样式ID | `string` | - |
|
|
41
|
+
| isNewVersion | 是否是新版云组件模板开发的云组件 | `boolean` | - |
|
|
42
|
+
| module | 加载成功返回的组件模块 | `IModule` | - |
|
|
43
|
+
|
|
44
|
+
## IModule
|
|
45
|
+
|
|
46
|
+
| 属性 | 说明 | 类型 | 默认值 |
|
|
47
|
+
|-------------------|:-------|:-----------------:|----:|
|
|
48
|
+
| info.name | 组件的名称 | `string` | - |
|
|
49
|
+
| info.remark | 组件说明 | `string` | - |
|
|
50
|
+
| info.version | 组件版本 | `string` | - |
|
|
51
|
+
| info.description | 组件描述 | `string` | - |
|
|
52
|
+
| info.environment | 组件环境 | `string` | - |
|
|
53
|
+
| info.dependencies | 组件依赖清单 | `string` | - |
|
|
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
|
+
```
|
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
import { tools } from '@cqsjjb/jjb-common-lib';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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('.');
|
|
12
16
|
}
|
|
13
17
|
|
|
14
18
|
/**
|
|
@@ -29,11 +33,11 @@ export function genCommonJSRuntime({
|
|
|
29
33
|
const module = { exports: null };
|
|
30
34
|
const exports = {};
|
|
31
35
|
const require = function (id) {
|
|
32
|
-
const find =
|
|
36
|
+
const find = ${resolveLib(lib)}[ id ];
|
|
33
37
|
if (find) {
|
|
34
38
|
return find;
|
|
35
39
|
} else {
|
|
36
|
-
throw Error('云组件使用依赖失败,请确认"['+ id +']"
|
|
40
|
+
throw Error('云组件使用依赖失败,请确认"['+ id +']"是否在${resolveLib(lib)}中?');
|
|
37
41
|
}
|
|
38
42
|
};
|
|
39
43
|
/* cloud-component-code */
|
|
@@ -44,10 +48,6 @@ export function genCommonJSRuntime({
|
|
|
44
48
|
};
|
|
45
49
|
}
|
|
46
50
|
|
|
47
|
-
function print(msg) {
|
|
48
|
-
return `[ImportCloudComponent]: ${msg}`;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
51
|
/**
|
|
52
52
|
* @description 宿主环境依赖检测
|
|
53
53
|
* @param lib {string} 依赖变量
|
|
@@ -55,7 +55,7 @@ function print(msg) {
|
|
|
55
55
|
* @return {boolean}
|
|
56
56
|
*/
|
|
57
57
|
export function checkDependence(lib, name) {
|
|
58
|
-
const dependencies = new Function(`return
|
|
58
|
+
const dependencies = new Function(`return ${resolveLib(lib)}`)();
|
|
59
59
|
return tools.isUndefined(dependencies[ name ]);
|
|
60
60
|
}
|
|
61
61
|
|
|
@@ -86,9 +86,7 @@ export function checkDependencies(lib, dependencies) {
|
|
|
86
86
|
* @return {Promise<{ module: { info: {}, default:() => React.Component }, styleId: string }>}
|
|
87
87
|
*/
|
|
88
88
|
export default async function ImportCloudComponent(options) {
|
|
89
|
-
const lib =
|
|
90
|
-
? `${options.lib || '__coreLib'}`
|
|
91
|
-
: `.proxy.${options.lib || '__coreLib'}`;
|
|
89
|
+
const lib = options.lib || '__coreLib';
|
|
92
90
|
const from = options.from;
|
|
93
91
|
|
|
94
92
|
const cache = options.cache || 'force-cache';
|
|
@@ -133,7 +131,7 @@ export default async function ImportCloudComponent(options) {
|
|
|
133
131
|
const noInstallDependencies = checkDependencies(lib, dependencies);
|
|
134
132
|
// 判断是否有
|
|
135
133
|
if (tools.isValidArray(noInstallDependencies)) {
|
|
136
|
-
throw Error(`"${name}" 需要"${noInstallDependencies.at(0).name}"依赖,请在"
|
|
134
|
+
throw Error(`"${name}" 需要"${noInstallDependencies.at(0).name}"依赖,请在"${resolveLib(lib)}"中提供此依赖!`);
|
|
137
135
|
}
|
|
138
136
|
}
|
|
139
137
|
|