@blocklet/pages-kit 0.4.16-beta.20250306-1 → 0.4.16-beta.20250309-3

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.
@@ -2,9 +2,16 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.injectESModulesShimsOptions = injectESModulesShimsOptions;
4
4
  function injectESModulesShimsOptions() {
5
+ // if already initialized, return
6
+ if (window.esmsInitOptions) {
7
+ return;
8
+ }
5
9
  window.esmsInitOptions = {
6
10
  shimMode: true,
7
11
  polyfillEnable: ['css-modules', 'json-modules', 'wasm-modules', 'source-phase'],
12
+ skip: ['crypto-browserify', 'crypto'],
13
+ mapOverrides: true,
8
14
  };
9
15
  }
16
+ // enable es-module-shims
10
17
  injectESModulesShimsOptions();
@@ -31,7 +31,6 @@ require("es-module-shims");
31
31
  const react_1 = __importDefault(require("react"));
32
32
  const ufo_1 = require("ufo");
33
33
  const arcblockUx = __importStar(require("../builtin/arcblock/ux"));
34
- const aiRuntime = __importStar(require("../builtin/async/ai-runtime"));
35
34
  const imagePreview = __importStar(require("../builtin/async/image-preview"));
36
35
  const reactMarkdown = __importStar(require("../builtin/async/react-markdown"));
37
36
  const reactScrollToBottom = __importStar(require("../builtin/async/react-scroll-to-bottom"));
@@ -61,11 +60,15 @@ const builtin_module_transformer_1 = require("./typescript/builtin-module-transf
61
60
  function injectGlobalComponents() {
62
61
  var _a;
63
62
  const win = window || {};
63
+ // if already initialized, return
64
+ if (win[builtin_1.BuiltinModulesGlobalVariableName]) {
65
+ return;
66
+ }
64
67
  const enableShim = !!((_a = window === null || window === void 0 ? void 0 : window.esmsInitOptions) === null || _a === void 0 ? void 0 : _a.shimMode);
65
68
  win.React = react_1.default;
66
- // 创建模块映射
69
+ // create module map
67
70
  const modules = {
68
- react,
71
+ React: react,
69
72
  '@blocklet/pages-kit/builtin/pages-kit': { CustomComponentRenderer: CustomComponentRenderer_1.default },
70
73
  '@blocklet/pages-kit/builtin/dayjs': dayjs,
71
74
  '@blocklet/pages-kit/builtin/utils': utils,
@@ -90,27 +93,24 @@ function injectGlobalComponents() {
90
93
  '@blocklet/pages-kit/builtin/async/react-markdown': reactMarkdown,
91
94
  '@blocklet/pages-kit/builtin/async/react-syntax-highlighter': reactSyntaxHighlighter,
92
95
  '@blocklet/pages-kit/builtin/async/image-preview': imagePreview,
93
- '@blocklet/pages-kit/builtin/async/ai-runtime': aiRuntime,
94
- // crypto: {
95
- // // 手动暴露 crypto 的属性和方法
96
- // subtle: win.crypto.subtle,
97
- // getRandomValues: win.crypto.getRandomValues,
98
- // randomUUID: win.crypto.randomUUID,
99
- // // 如果需要作为默认导出
100
- // default: win.crypto,
101
- // },
96
+ '@blocklet/pages-kit/builtin/async/ai-runtime': {
97
+ get: () => Promise.resolve().then(() => __importStar(require('../builtin/async/ai-runtime'))).catch((err) => {
98
+ console.error('Failed to load AI runtime', err);
99
+ return {};
100
+ }),
101
+ },
102
102
  };
103
- // 设置全局变量
103
+ // set global variable
104
104
  win[builtin_1.BuiltinModulesGlobalVariableName] = {
105
105
  modules,
106
106
  require(module) {
107
107
  var _a;
108
- // 处理内置模块
108
+ // handle builtin module
109
109
  const builtinModule = this.modules[module];
110
110
  if (builtinModule) {
111
111
  return builtinModule;
112
112
  }
113
- // 处理相对路径导入
113
+ // handle relative path import
114
114
  if ((0, builtin_module_transformer_1.isRelativeModule)(module)) {
115
115
  const fileName = module.split('/').pop();
116
116
  const fullUrl = (0, ufo_1.joinURL)(window.location.origin, ((_a = window === null || window === void 0 ? void 0 : window.blocklet) === null || _a === void 0 ? void 0 : _a.prefix) || '/', 'chunks', fileName);
@@ -124,27 +124,74 @@ function injectGlobalComponents() {
124
124
  return null;
125
125
  },
126
126
  };
127
- // 创建 importmap
127
+ // create importmap
128
128
  const setupImportMap = () => {
129
- // 设置 importmap 内容
130
- const imports = Object.entries(modules).reduce((acc, [modulePath, moduleContent]) => {
131
- const namedExports = Object.keys(moduleContent).filter((key) => key !== 'default');
132
- const hasDefaultExport = 'default' in moduleContent;
133
- // 创建模块代码
134
- const moduleCode = `// GENERATED FILE. DO NOT EDIT.
135
- const moduleSource = window['${builtin_1.BuiltinModulesGlobalVariableName}'].modules['${modulePath}'];
136
- ${namedExports.map((name) => `export const ${name} = moduleSource['${name}'];`).join('\n')}
137
- export default ${hasDefaultExport ? 'moduleSource.default' : 'moduleSource'};
138
- `;
139
- // 使用 base64 编码模块代码
140
- const base64Code = btoa(moduleCode);
141
- acc[modulePath] = `data:application/javascript;base64,${base64Code}`;
142
- return acc;
143
- }, {});
144
- // 添加 resolve 配置
145
- const importMap = {
146
- imports,
129
+ // 计算模块的 hash 值作为缓存版本
130
+ const calculateModulesHash = () => {
131
+ // 只提取模块路径和导出的键名用于 hash 计算
132
+ const moduleStructure = Object.entries(modules).map(([path, mod]) => ({
133
+ path,
134
+ exports: Object.keys(mod).sort(),
135
+ }));
136
+ const str = JSON.stringify(moduleStructure);
137
+ // 简单的 hash 计算方法
138
+ let hash = 0;
139
+ for (let i = 0; i < str.length; i++) {
140
+ const char = str.charCodeAt(i);
141
+ hash = hash * 2 ** 5 - hash + char;
142
+ }
143
+ return hash.toString(16);
144
+ };
145
+ const cacheKey = 'pages-kit-import-map';
146
+ const moduleHash = calculateModulesHash();
147
+ // 获取 imports - 优先从缓存读取
148
+ const getImportsFromCache = () => {
149
+ try {
150
+ const cachedData = localStorage.getItem(cacheKey);
151
+ if (cachedData) {
152
+ const { hash, imports } = JSON.parse(cachedData);
153
+ if (hash === moduleHash) {
154
+ // eslint-disable-next-line no-console
155
+ console.log(`getImportsFromCache: ${hash}`, imports);
156
+ return imports; // 返回缓存的 imports
157
+ }
158
+ }
159
+ }
160
+ catch (e) {
161
+ console.warn('Failed to load import map from cache', e);
162
+ }
163
+ return null; // 缓存无效
147
164
  };
165
+ // 尝试从缓存获取或重新计算
166
+ const imports = getImportsFromCache() ||
167
+ Object.entries(modules).reduce((acc, [modulePath, moduleContent]) => {
168
+ const namedExports = Object.keys(moduleContent).filter((key) => key !== 'default');
169
+ const hasDefaultExport = 'default' in moduleContent;
170
+ // create module code
171
+ const moduleCode = `// GENERATED FILE. DO NOT EDIT.
172
+ const moduleSource = window['${builtin_1.BuiltinModulesGlobalVariableName}'].modules['${modulePath}'];
173
+ ${namedExports.map((name) => `export const ${name} = moduleSource['${name}'];`).join('\n')}
174
+ export default ${hasDefaultExport ? 'moduleSource.default' : 'moduleSource'};
175
+ `;
176
+ // create base64 code
177
+ const base64Code = btoa(moduleCode);
178
+ acc[modulePath] = `data:application/javascript;base64,${base64Code}`;
179
+ return acc;
180
+ }, {});
181
+ // 如果是新计算的 imports,保存到缓存
182
+ if (!getImportsFromCache()) {
183
+ try {
184
+ localStorage.setItem(cacheKey, JSON.stringify({
185
+ hash: moduleHash,
186
+ imports,
187
+ }));
188
+ }
189
+ catch (e) {
190
+ console.warn('Failed to cache import map', e);
191
+ }
192
+ }
193
+ // 添加 resolve 配置
194
+ const importMap = { imports };
148
195
  if (enableShim) {
149
196
  window.importShim.addImportMap(importMap);
150
197
  }
@@ -18,7 +18,7 @@ var __rest = (this && this.__rest) || function (s, e) {
18
18
  }
19
19
  return t;
20
20
  };
21
- import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
21
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
22
22
  import { cx } from '@emotion/css';
23
23
  import { Skeleton, Fade, Box } from '@mui/material';
24
24
  import { isEmpty } from 'lodash';
@@ -85,7 +85,7 @@ function importCustomComponent(m, { componentId }) {
85
85
  // 使用useMemo缓存渲染内容,避免不必要的重计算
86
86
  const content = useMemo(() => {
87
87
  if (loading) {
88
- return (_jsxs(Box, { sx: { width: '100%', p: 1 }, children: [_jsx(Skeleton, { variant: "rectangular", width: "100%", height: 60, animation: "wave" }), _jsxs(Box, { sx: { pt: 1 }, children: [_jsx(Skeleton, { width: "80%" }), _jsx(Skeleton, { width: "60%" }), _jsx(Skeleton, { width: "40%" })] })] }));
88
+ return (_jsx(Box, { sx: { width: '100%', p: 1 }, children: _jsx(Skeleton, { variant: "rectangular", width: "100%", height: 40, animation: "wave" }) }));
89
89
  }
90
90
  if (error) {
91
91
  return _jsx(ComponentError, { componentId: componentId, message: error.message });