@cqsjjb/jjb-cloud-component 0.0.10 → 0.0.11

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.
@@ -24,7 +24,7 @@ export function useUnMountCloudComponentStyle(styleId) {
24
24
  * @note 用户自定义依赖项会覆盖同名的默认依赖项
25
25
  */
26
26
  export default function CloudComponent(props) {
27
- let styleId;
27
+ const styleId = React.useRef(null);
28
28
  const [Component, setComponent] = React.useState(null);
29
29
  const {
30
30
  from,
@@ -110,15 +110,17 @@ export default function CloudComponent(props) {
110
110
  onLoadStartRef.current && onLoadStartRef.current();
111
111
  const {
112
112
  module,
113
- styleId: _styleId,
114
- isNewVersion
113
+ isNewVersion,
114
+ styleId: _styleId
115
115
  } = await ImportCloudComponent({
116
116
  from,
117
117
  cache,
118
118
  headers,
119
119
  dependencies: stableDependencies
120
120
  });
121
- styleId = _styleId;
121
+
122
+ // 优先使用导入云组件时生成的styleId,如果导入云组件时没有生成styleId,则使用模块info中的cloudComponentStyleId
123
+ styleId.current = _styleId || module?.info?.cloudComponentStyleId;
122
124
  setComponent(() => {
123
125
  onLoadEndRef.current && onLoadEndRef.current();
124
126
  const $module = isNewVersion ? module.default({
@@ -142,13 +144,16 @@ export default function CloudComponent(props) {
142
144
  }
143
145
  }
144
146
  load().then(() => null);
147
+ }, [from, cache, headers, shouldReloadDependencies, moduleKey]);
148
+ React.useEffect(() => {
145
149
  return () => {
146
- if (styleId) {
147
- useUnMountCloudComponentStyle(styleId);
150
+ if (styleId.current) {
151
+ useUnMountCloudComponentStyle(styleId.current);
152
+ styleId.current = null;
148
153
  }
149
154
  onDestroyRef.current && onDestroyRef.current();
150
155
  };
151
- }, [from, cache, headers, shouldReloadDependencies, moduleKey]);
156
+ }, [styleId]);
152
157
 
153
158
  /**
154
159
  * 深度比较两个值是否相等,支持 ReactNode
@@ -8,7 +8,8 @@ import { tools } from '@cqsjjb/jjb-common-lib';
8
8
  export function genCommonJSRuntime({
9
9
  from
10
10
  }, code) {
11
- const styleId = tools.createOnlyKey();
11
+ const styleId = Math.random().toString(36).substring(2, 15);
12
+ const newCode = code.replace(/\{cloudComponentStyleId\}/g, styleId);
12
13
  return {
13
14
  styleId,
14
15
  useModule: new Function(`
@@ -23,7 +24,7 @@ export function genCommonJSRuntime({
23
24
  }
24
25
  };
25
26
  /* cloud-component-code */
26
- ${code.replace(/\{cloudComponentStyleId\}/g, styleId)}
27
+ ${newCode}
27
28
  /* cloud-component-code */
28
29
  return Object.assign(module.exports, exports);
29
30
  `)
@@ -69,16 +70,14 @@ export default async function ImportCloudComponent(options) {
69
70
  __dependencies[key] = value;
70
71
  });
71
72
  let module;
72
- let styleId;
73
73
  let useModule;
74
+ let styleId;
74
75
  try {
75
76
  const runtime = genCommonJSRuntime({
76
77
  from
77
78
  }, responseText);
78
- console.log('runtime', runtime);
79
- styleId = runtime.styleId;
80
79
  useModule = runtime.useModule;
81
-
80
+ styleId = runtime.styleId;
82
81
  // 获取云组件导出CJS模块
83
82
  module = useModule(__dependencies);
84
83
  if (!module || typeof module !== 'object') {
@@ -107,7 +106,7 @@ export default async function ImportCloudComponent(options) {
107
106
  // 返回组件CSJ模块
108
107
  return {
109
108
  module,
110
- styleId,
111
- isNewVersion
109
+ isNewVersion,
110
+ styleId
112
111
  };
113
112
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cqsjjb/jjb-cloud-component",
3
- "version": "0.0.10",
3
+ "version": "0.0.11",
4
4
  "description": "前端-云组件",
5
5
  "main": "index.js",
6
6
  "scripts": {