@cqsjjb/jjb-cloud-component 0.0.3 → 0.0.5

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.
@@ -1,17 +1,24 @@
1
1
  // @ts-ignore
2
2
  import * as React from 'react';
3
- import type I_ImportCloudComponent from './import-cloud-component';
4
3
 
5
4
  interface ComponentProps {
6
5
  ref?: React.Ref<{ [ p: string ]: any }>;
7
6
  children?: React.ReactNode;
8
7
  }
9
8
 
9
+ /**
10
+ * @description 卸载云组件样式
11
+ * @param styleId {string} 样式id
12
+ */
13
+ export function useUnMountCloudComponentStyle (styleId: string): void;
14
+
10
15
  interface CloudComponentProps extends ComponentProps {
16
+ // 云组件依赖库变量
17
+ lib?: string;
11
18
  // 组件资源地址
12
19
  from: string;
13
20
  // 缓存
14
- cache?: string;
21
+ cache?: 'default' | 'force-cache' | 'no-cache' | 'no-store' | 'only-if-cached' | 'reload';
15
22
  // 请求头
16
23
  headers?: {};
17
24
  // 是否需要初始化更新settings和dataSource
@@ -44,4 +51,4 @@ interface CloudComponentFc extends React.FC<CloudComponentProps> {
44
51
  }
45
52
 
46
53
  declare const CloudComponent: CloudComponentFc;
47
- declare const ImportCloudComponent: typeof I_ImportCloudComponent;
54
+ export default CloudComponent;
@@ -4,7 +4,11 @@ import { tools } from '@cqsjjb/jjb-common-lib';
4
4
 
5
5
  import ImportCloudComponent from './import-cloud-component';
6
6
 
7
- function unMountCloudComponentStyle(styleId) {
7
+ /**
8
+ * @description 卸载云组件样式
9
+ * @param styleId {string} 样式id
10
+ */
11
+ export function useUnMountCloudComponentStyle(styleId) {
8
12
  const root = document.querySelector('head');
9
13
  const styled = root.querySelectorAll(`[data-cloud-component-style-id="${styleId}"]`);
10
14
  for (const style of styled) {
@@ -12,6 +16,10 @@ function unMountCloudComponentStyle(styleId) {
12
16
  }
13
17
  }
14
18
 
19
+ /**
20
+ * @description 云组件
21
+ * @param props {object} 组件props
22
+ */
15
23
  export default function CloudComponent(props) {
16
24
  let styleId;
17
25
  const [ Component, setComponent ] = React.useState(null);
@@ -35,7 +43,8 @@ export default function CloudComponent(props) {
35
43
  onLoadStart && onLoadStart();
36
44
  const {
37
45
  module,
38
- styleId: _styleId
46
+ styleId: _styleId,
47
+ isNewVersion
39
48
  } = await ImportCloudComponent({
40
49
  lib,
41
50
  from,
@@ -47,14 +56,16 @@ export default function CloudComponent(props) {
47
56
 
48
57
  setComponent(() => {
49
58
  onLoadEnd && onLoadEnd();
50
- return module.default();
59
+ return isNewVersion
60
+ ? module.default()
61
+ : module.default;
51
62
  });
52
63
  }
53
64
 
54
65
  load().then(() => null);
55
66
 
56
67
  return () => {
57
- unMountCloudComponentStyle(styleId);
68
+ useUnMountCloudComponentStyle(styleId);
58
69
  onDestroy && onDestroy();
59
70
  };
60
71
  }, []);
@@ -1,17 +1,15 @@
1
1
  // @ts-ignore
2
2
  import React from 'react';
3
3
 
4
- declare function ImportCloudComponent (options: {
5
- // 组件依赖库变量
6
- lib?: string;
7
- // 组件地址
8
- from: string;
9
- // 缓存
10
- cache?: string;
11
- // 请求头
12
- headers?: {}
13
- }): {
4
+ /**
5
+ * @description 云组件模块
6
+ */
7
+ type CloudModule = {
8
+ // 样式id
14
9
  styleId: string;
10
+ // 是否是新版云组件
11
+ isNewVersion: boolean;
12
+ // 组件模块
15
13
  module: {
16
14
  // 组件信息
17
15
  info: {
@@ -31,6 +29,52 @@ declare function ImportCloudComponent (options: {
31
29
  // 组件本体
32
30
  default: () => React.Component
33
31
  }
34
- }
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 }>
49
+
50
+ /**
51
+ * @description 生成CJS运行环境
52
+ * @param options {string} 依赖变量
53
+ * @param code {string} 依赖名称
54
+ * @return {CloudModule}
55
+ */
56
+ declare function genCommonJSRuntime (options: { lib: string, from: string }, code: string): CloudModule;
57
+
58
+ /**
59
+ * @description 导入云组件
60
+ * @param options {string} 依赖变量
61
+ * @return {Promise<CloudModule>}
62
+ */
63
+ declare function ImportCloudComponent (options: {
64
+ // 组件依赖库变量
65
+ lib?: string;
66
+ // 组件地址
67
+ from: string;
68
+ // 缓存
69
+ cache?: 'default' | 'force-cache' | 'no-cache' | 'no-store' | 'only-if-cached' | 'reload';
70
+ // 请求头
71
+ headers?: {}
72
+ }): Promise<CloudModule>
35
73
 
36
74
  export default ImportCloudComponent;
75
+
76
+ export {
77
+ checkDependence,
78
+ checkDependencies,
79
+ genCommonJSRuntime
80
+ };
@@ -11,24 +11,13 @@ function ansLibField(name) {
11
11
  : `.${name}`;
12
12
  }
13
13
 
14
- /**
15
- * @description 给地址加随机字符串
16
- * @param url {string} 地址
17
- * @return {string}
18
- */
19
- function fromURL(url) {
20
- return `${url}${url.indexOf('?') !== -1
21
- ? '&'
22
- : '?'}v=${new Date().getTime()}`;
23
- }
24
-
25
14
  /**
26
15
  * @description 生成CJS运行环境
27
16
  * @param {string} lib 云组件依赖变量
28
17
  * @param {string} from 云组件地址
29
18
  * @param {string} code 注入云组件代码
30
19
  */
31
- function genCommonJSRuntime({
20
+ export function genCommonJSRuntime({
32
21
  lib,
33
22
  from
34
23
  }, code) {
@@ -40,7 +29,12 @@ function genCommonJSRuntime({
40
29
  const module = { exports: null };
41
30
  const exports = {};
42
31
  const require = function (id) {
43
- return window${ansLibField(lib)}[ id ];
32
+ const find = window${ansLibField(lib)}[ id ];
33
+ if (find) {
34
+ return find;
35
+ } else {
36
+ throw Error('云组件使用依赖失败,请确认"['+ id +']"是否在window${ansLibField(lib)}中?');
37
+ }
44
38
  };
45
39
  /* cloud-component-code */
46
40
  ${code.replace(/\{cloudComponentStyleId\}/g, styleId)}
@@ -60,7 +54,7 @@ function print(msg) {
60
54
  * @param name {string} 依赖名称
61
55
  * @return {boolean}
62
56
  */
63
- function checkDependence(lib, name) {
57
+ export function checkDependence(lib, name) {
64
58
  const dependencies = new Function(`return window${ansLibField(lib)}`)();
65
59
  return tools.isUndefined(dependencies[ name ]);
66
60
  }
@@ -69,14 +63,13 @@ function checkDependence(lib, name) {
69
63
  * @description 依赖检测
70
64
  * @param lib {string}
71
65
  * @param dependencies {string[]}
72
- * @param isProxy {boolean}
73
66
  * @return {Array<{ name: string, check: boolean }>}
74
67
  */
75
- function checkDependencies(lib, dependencies, isProxy) {
68
+ export function checkDependencies(lib, dependencies) {
76
69
  return Object.keys(dependencies).map(name => {
77
70
  return {
78
71
  name,
79
- check: checkDependence(lib, name, isProxy)
72
+ check: checkDependence(lib, name)
80
73
  };
81
74
  }).filter(i => i.check);
82
75
  }
@@ -85,67 +78,73 @@ function checkDependencies(lib, dependencies, isProxy) {
85
78
  * @description 手动加载模块
86
79
  * @param options {{ from: string, lib: string, cache: string, headers: {} }}
87
80
  * @example
88
- * Import({
81
+ * ImportCloudComponent({
89
82
  * from: 'https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.min.js'
90
83
  * }).then(res => {
91
84
  * console.log(res);
92
85
  * });
93
86
  * @return {Promise<{ module: { info: {}, default:() => React.Component }, styleId: string }>}
94
87
  */
95
- export default function ImportCloudComponent(options) {
96
- const lib = typeof window.proxy === 'undefined' ? `${options.lib || '__coreLib'}` : `.proxy.${options.lib || '__coreLib'}`;
88
+ export default async function ImportCloudComponent(options) {
89
+ const lib = typeof window.proxy === 'undefined'
90
+ ? `${options.lib || '__coreLib'}`
91
+ : `.proxy.${options.lib || '__coreLib'}`;
97
92
  const from = options.from;
98
93
 
99
94
  const cache = options.cache || 'force-cache';
100
95
  const headers = tools.toObject(options.headers);
101
96
 
97
+ let isNewVersion = true;
98
+
102
99
  if (!from) {
103
- throw Error(print('云组件资源访问地址不能为空!'));
100
+ throw Error('云组件资源访问地址不能为空!');
104
101
  }
105
102
 
106
- return fetch(from, {
103
+ const response = await fetch(from, {
107
104
  cache,
108
105
  headers
109
- })
110
- .then(response => response.text())
111
- .then(response => {
112
- const {
113
- styleId,
114
- useModule
115
- } = genCommonJSRuntime({
116
- lib,
117
- from
118
- }, response);
119
- // 获取云组件导出CJS模块
120
- const module = useModule();
121
- // 获取云组件info变量
122
- const { info } = module;
123
- // 判断当前云组件是否导出了info变量
124
- if (tools.isUndefined(info)) {
125
- throw Error(print(`${from} 缺少info变量导出, 无法获知此组件的基本信息, 请在组件包入口处导出组件的info变量!`));
126
- }
127
- // 获取云组件的基本信息
128
- const {
129
- name,
130
- dependencies
131
- } = info;
132
- // 判断云组件是否有依赖配置
133
- if (!tools.isEmptyObject(dependencies)) {
134
- // 是否存在未安装的依赖项
135
- const noInstallDependencies = checkDependencies(lib, dependencies);
136
- // 判断是否有
137
- if (tools.isValidArray(noInstallDependencies)) {
138
- throw Error(print(`${name} 需要${noInstallDependencies.at(0).name}依赖, 请在window.${lib}中提供此依赖!`));
139
- }
106
+ });
107
+ const responseText = await response.text();
108
+
109
+ const {
110
+ styleId,
111
+ useModule
112
+ } = genCommonJSRuntime({
113
+ lib,
114
+ from
115
+ }, responseText);
116
+ // 获取云组件导出CJS模块
117
+ const module = useModule();
118
+ // 获取云组件info变量
119
+ const { info } = module;
120
+ // 判断当前云组件是否导出了info变量
121
+ if (tools.isUndefined(info)) {
122
+ window[ 'console' ][ 'warn' ](`组件 "${from}" 不存在"info"配置,疑似旧版云组件或配置错误`);
123
+ isNewVersion = false;
124
+ } else {
125
+ // 获取云组件的基本信息
126
+ const {
127
+ name,
128
+ dependencies
129
+ } = info;
130
+ // 判断云组件是否有依赖配置
131
+ if (!tools.isEmptyObject(dependencies)) {
132
+ // 是否存在未安装的依赖项
133
+ const noInstallDependencies = checkDependencies(lib, dependencies);
134
+ // 判断是否有
135
+ if (tools.isValidArray(noInstallDependencies)) {
136
+ throw Error(`"${name}" 需要"${noInstallDependencies.at(0).name}"依赖,请在"window.${lib}"中提供此依赖!`);
140
137
  }
138
+ }
141
139
 
142
- console.log(print(`${module.info.name} 加载完成!`));
140
+ window[ 'console' ][ 'log' ](`云组件 [${info.name}] 加载完成!`);
141
+ }
143
142
 
144
- // 返回组件CSJ模块
145
- return {
146
- module,
147
- styleId
148
- };
149
- });
143
+ // 返回组件CSJ模块
144
+ return {
145
+ module,
146
+ styleId,
147
+ isNewVersion
148
+ };
150
149
  }
151
150
 
package/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { default as CloudComponent, useUnMountCloudComponentStyle } from './cloud-component';
2
+ export { default as ImportCloudComponent, checkDependence, checkDependencies, genCommonJSRuntime } from './import-cloud-component';
package/index.js CHANGED
@@ -1,2 +1,2 @@
1
- export CloudComponent from './cloud-component';
2
- export ImportCloudComponent from './import-cloud-component';
1
+ export { default as CloudComponent, useUnMountCloudComponentStyle } from './cloud-component';
2
+ export { default as ImportCloudComponent, checkDependence, checkDependencies, 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",
3
+ "version": "0.0.5",
4
4
  "description": "前端-云组件",
5
5
  "main": "index.js",
6
6
  "scripts": {