@blocklet/pages-kit 0.4.16-beta.20250306-1 → 0.4.16
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/lib/cjs/components/CustomComponentRenderer/state.js +1 -1
- package/lib/cjs/tsconfig.tsbuildinfo +1 -1
- package/lib/cjs/utils/inject-es-module-shims-options.js +7 -0
- package/lib/cjs/utils/inject-global-components.js +82 -34
- package/lib/esm/components/CustomComponentRenderer/state.js +2 -2
- package/lib/esm/tsconfig.tsbuildinfo +1 -1
- package/lib/esm/utils/inject-es-module-shims-options.js +7 -0
- package/lib/esm/utils/inject-global-components.js +82 -34
- package/lib/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
|
@@ -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':
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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,75 @@ function injectGlobalComponents() {
|
|
|
124
124
|
return null;
|
|
125
125
|
},
|
|
126
126
|
};
|
|
127
|
-
//
|
|
127
|
+
// create importmap
|
|
128
128
|
const setupImportMap = () => {
|
|
129
|
-
//
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
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
|
+
// eslint-disable-next-line no-bitwise
|
|
142
|
+
hash = (hash << 5) - hash + char;
|
|
143
|
+
// eslint-disable-next-line no-bitwise
|
|
144
|
+
hash &= hash;
|
|
145
|
+
}
|
|
146
|
+
return hash.toString(16);
|
|
147
|
+
};
|
|
148
|
+
const cacheKey = 'pages-kit-import-map';
|
|
149
|
+
const moduleHash = calculateModulesHash();
|
|
150
|
+
// 获取 imports - 优先从缓存读取
|
|
151
|
+
const getImportsFromCache = () => {
|
|
152
|
+
try {
|
|
153
|
+
const cachedData = localStorage.getItem(cacheKey);
|
|
154
|
+
if (cachedData) {
|
|
155
|
+
const { hash, imports } = JSON.parse(cachedData);
|
|
156
|
+
if (hash === moduleHash) {
|
|
157
|
+
return imports; // 返回缓存的 imports
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
catch (e) {
|
|
162
|
+
console.warn('Failed to load import map from cache', e);
|
|
163
|
+
}
|
|
164
|
+
return null; // 缓存无效
|
|
147
165
|
};
|
|
166
|
+
// 尝试从缓存获取或重新计算
|
|
167
|
+
const imports = getImportsFromCache() ||
|
|
168
|
+
Object.entries(modules).reduce((acc, [modulePath, moduleContent]) => {
|
|
169
|
+
const namedExports = Object.keys(moduleContent).filter((key) => key !== 'default');
|
|
170
|
+
const hasDefaultExport = 'default' in moduleContent;
|
|
171
|
+
// create module code
|
|
172
|
+
const moduleCode = `// GENERATED FILE. DO NOT EDIT.
|
|
173
|
+
const moduleSource = window['${builtin_1.BuiltinModulesGlobalVariableName}'].modules['${modulePath}'];
|
|
174
|
+
${namedExports.map((name) => `export const ${name} = moduleSource['${name}'];`).join('\n')}
|
|
175
|
+
export default ${hasDefaultExport ? 'moduleSource.default' : 'moduleSource'};
|
|
176
|
+
`;
|
|
177
|
+
// create base64 code
|
|
178
|
+
const base64Code = btoa(moduleCode);
|
|
179
|
+
acc[modulePath] = `data:application/javascript;base64,${base64Code}`;
|
|
180
|
+
return acc;
|
|
181
|
+
}, {});
|
|
182
|
+
// 如果是新计算的 imports,保存到缓存
|
|
183
|
+
if (!getImportsFromCache()) {
|
|
184
|
+
try {
|
|
185
|
+
localStorage.setItem(cacheKey, JSON.stringify({
|
|
186
|
+
hash: moduleHash,
|
|
187
|
+
imports,
|
|
188
|
+
}));
|
|
189
|
+
}
|
|
190
|
+
catch (e) {
|
|
191
|
+
console.warn('Failed to cache import map', e);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
// 添加 resolve 配置
|
|
195
|
+
const importMap = { imports };
|
|
148
196
|
if (enableShim) {
|
|
149
197
|
window.importShim.addImportMap(importMap);
|
|
150
198
|
}
|
|
@@ -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,
|
|
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 (
|
|
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 });
|