@cqsjjb/jjb-cloud-component 0.0.5 → 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 ADDED
@@ -0,0 +1,222 @@
1
+ # @cqsjjb/jjb-cloud-component
2
+
3
+ 前端云组件库,支持动态加载远程React组件。
4
+
5
+ ## 特性
6
+
7
+ - 🚀 动态加载远程云组件
8
+ - 🔧 支持组件依赖管理
9
+ - 🎨 样式隔离和自动清理
10
+ - 📦 CommonJS模块支持
11
+ - 🔄 完整的生命周期钩子
12
+ - 💪 TypeScript支持
13
+
14
+ ## 安装
15
+
16
+ ```bash
17
+ npm install @cqsjjb/jjb-cloud-component
18
+ ```
19
+
20
+ ## 快速开始
21
+
22
+ ### 基础用法
23
+
24
+ ```jsx
25
+ import React from 'react';
26
+ import { CloudComponent } from '@cqsjjb/jjb-cloud-component';
27
+
28
+ function App() {
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);
187
+ }
188
+
189
+ // 导出
190
+ module.exports = {
191
+ info,
192
+ default: MyComponent
193
+ };
194
+ ```
195
+
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
@@ -1,8 +1,7 @@
1
- // @ts-ignore
2
1
  import * as React from 'react';
3
2
 
4
3
  interface ComponentProps {
5
- ref?: React.Ref<{ [ p: string ]: any }>;
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
- lib?: string;
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?: {}, dataSource?: any) => void;
40
+ updateData?: (settings?: Record<string, unknown>, dataSource?: unknown) => void;
42
41
  }) => void;
43
42
  // 组件已更新
44
43
  onUpdated?: (key: string, ref: {
45
- settings?: {},
46
- dataSource?: any
44
+ settings?: Record<string, unknown>;
45
+ dataSource?: unknown;
47
46
  }) => void;
48
47
  }
49
48
 
@@ -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
- onLoadStart && onLoadStart();
44
- const {
45
- module,
46
- styleId: _styleId,
47
- isNewVersion
48
- } = await ImportCloudComponent({
49
- lib,
50
- from,
51
- cache,
52
- headers
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
- styleId = _styleId;
56
+ styleId = _styleId;
56
57
 
57
- setComponent(() => {
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
- return isNewVersion
60
- ? module.default()
61
- : module.default;
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
- useUnMountCloudComponentStyle(styleId);
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
- type CloudModule = {
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.Component
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 {string} 依赖变量
53
- * @param code {string} 依赖名称
54
- * @return {CloudModule}
40
+ * @param options 云组件地址配置
41
+ * @param code 云组件代码
42
+ * @return CJS运行环境
55
43
  */
56
- declare function genCommonJSRuntime (options: { lib: string, from: string }, code: string): CloudModule;
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
- declare function ImportCloudComponent (options: {
64
- // 组件依赖库变量
65
- lib?: string;
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
- }): Promise<CloudModule>
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,24 +1,11 @@
1
1
  import { tools } from '@cqsjjb/jjb-common-lib';
2
2
 
3
- /**
4
- * @description 优化依赖变量
5
- * @param name {string} 依赖变量名
6
- * @return {*|string}
7
- */
8
- function ansLibField(name) {
9
- return name.indexOf('.') !== -1
10
- ? name
11
- : `.${name}`;
12
- }
13
-
14
3
  /**
15
4
  * @description 生成CJS运行环境
16
- * @param {string} lib 云组件依赖变量
17
5
  * @param {string} from 云组件地址
18
6
  * @param {string} code 注入云组件代码
19
7
  */
20
8
  export function genCommonJSRuntime({
21
- lib,
22
9
  from
23
10
  }, code) {
24
11
  const styleId = tools.createOnlyKey();
@@ -28,12 +15,11 @@ export function genCommonJSRuntime({
28
15
  /* cloud-component-assets: ${from} */
29
16
  const module = { exports: null };
30
17
  const exports = {};
31
- const require = function (id) {
32
- const find = window${ansLibField(lib)}[ id ];
33
- if (find) {
34
- return find;
35
- } else {
36
- throw Error('云组件使用依赖失败,请确认"['+ id +']"是否在window${ansLibField(lib)}中?');
18
+ const require = id => {
19
+ try {
20
+ return arguments[0][id];
21
+ } catch (error) {
22
+ throw new Error("云组件依赖[" + id + "]不存在!请检查dependencies配置!");
37
23
  }
38
24
  };
39
25
  /* cloud-component-code */
@@ -44,39 +30,9 @@ export function genCommonJSRuntime({
44
30
  };
45
31
  }
46
32
 
47
- function print(msg) {
48
- return `[ImportCloudComponent]: ${msg}`;
49
- }
50
-
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 window${ansLibField(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, lib: string, cache: string, headers: {} }}
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,11 +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 = typeof window.proxy === 'undefined'
90
- ? `${options.lib || '__coreLib'}`
91
- : `.proxy.${options.lib || '__coreLib'}`;
92
45
  const from = options.from;
93
-
46
+ const dependencies = options.dependencies || {};
94
47
  const cache = options.cache || 'force-cache';
95
48
  const headers = tools.toObject(options.headers);
96
49
 
@@ -100,44 +53,63 @@ export default async function ImportCloudComponent(options) {
100
53
  throw Error('云组件资源访问地址不能为空!');
101
54
  }
102
55
 
103
- const response = await fetch(from, {
104
- cache,
105
- headers
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;
106
77
  });
107
- const responseText = await response.text();
108
78
 
109
- const {
110
- styleId,
111
- useModule
112
- } = genCommonJSRuntime({
113
- lib,
114
- from
115
- }, responseText);
116
- // 获取云组件导出CJS模块
117
- const module = useModule();
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
+
118
101
  // 获取云组件info变量
119
102
  const { info } = module;
120
103
  // 判断当前云组件是否导出了info变量
121
104
  if (tools.isUndefined(info)) {
122
- window[ 'console' ][ 'warn' ](`组件 "${from}" 不存在"info"配置,疑似旧版云组件或配置错误`);
105
+ if (process.env.NODE_ENV === 'development') {
106
+ console.warn(`云组件 "${from}" 不存在"info"配置,疑似旧版云组件或配置错误`);
107
+ }
123
108
  isNewVersion = false;
124
109
  } 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}"中提供此依赖!`);
137
- }
110
+ if (process.env.NODE_ENV === 'development') {
111
+ console.log(`云组件 [${info.name}] 加载完成!`);
138
112
  }
139
-
140
- window[ 'console' ][ 'log' ](`云组件 [${info.name}] 加载完成!`);
141
113
  }
142
114
 
143
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, checkDependence, checkDependencies, genCommonJSRuntime } from './import-cloud-component';
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, checkDependence, checkDependencies, genCommonJSRuntime } from './import-cloud-component';
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.5",
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
  }