@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.
- 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 +81 -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 +81 -34
- package/lib/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
export function injectESModulesShimsOptions() {
|
|
2
|
+
// if already initialized, return
|
|
3
|
+
if (window.esmsInitOptions) {
|
|
4
|
+
return;
|
|
5
|
+
}
|
|
2
6
|
window.esmsInitOptions = {
|
|
3
7
|
shimMode: true,
|
|
4
8
|
polyfillEnable: ['css-modules', 'json-modules', 'wasm-modules', 'source-phase'],
|
|
9
|
+
skip: ['crypto-browserify', 'crypto'],
|
|
10
|
+
mapOverrides: true,
|
|
5
11
|
};
|
|
6
12
|
}
|
|
13
|
+
// enable es-module-shims
|
|
7
14
|
injectESModulesShimsOptions();
|
|
@@ -2,7 +2,6 @@ import 'es-module-shims';
|
|
|
2
2
|
import React from 'react';
|
|
3
3
|
import { joinURL } from 'ufo';
|
|
4
4
|
import * as arcblockUx from '../builtin/arcblock/ux';
|
|
5
|
-
import * as aiRuntime from '../builtin/async/ai-runtime';
|
|
6
5
|
import * as imagePreview from '../builtin/async/image-preview';
|
|
7
6
|
import * as reactMarkdown from '../builtin/async/react-markdown';
|
|
8
7
|
import * as reactScrollToBottom from '../builtin/async/react-scroll-to-bottom';
|
|
@@ -32,11 +31,15 @@ import { isRelativeModule } from './typescript/builtin-module-transformer';
|
|
|
32
31
|
export function injectGlobalComponents() {
|
|
33
32
|
var _a;
|
|
34
33
|
const win = window || {};
|
|
34
|
+
// if already initialized, return
|
|
35
|
+
if (win[BuiltinModulesGlobalVariableName]) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
35
38
|
const enableShim = !!((_a = window === null || window === void 0 ? void 0 : window.esmsInitOptions) === null || _a === void 0 ? void 0 : _a.shimMode);
|
|
36
39
|
win.React = React;
|
|
37
|
-
//
|
|
40
|
+
// create module map
|
|
38
41
|
const modules = {
|
|
39
|
-
react,
|
|
42
|
+
React: react,
|
|
40
43
|
'@blocklet/pages-kit/builtin/pages-kit': { CustomComponentRenderer },
|
|
41
44
|
'@blocklet/pages-kit/builtin/dayjs': dayjs,
|
|
42
45
|
'@blocklet/pages-kit/builtin/utils': utils,
|
|
@@ -61,27 +64,24 @@ export function injectGlobalComponents() {
|
|
|
61
64
|
'@blocklet/pages-kit/builtin/async/react-markdown': reactMarkdown,
|
|
62
65
|
'@blocklet/pages-kit/builtin/async/react-syntax-highlighter': reactSyntaxHighlighter,
|
|
63
66
|
'@blocklet/pages-kit/builtin/async/image-preview': imagePreview,
|
|
64
|
-
'@blocklet/pages-kit/builtin/async/ai-runtime':
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
// // 如果需要作为默认导出
|
|
71
|
-
// default: win.crypto,
|
|
72
|
-
// },
|
|
67
|
+
'@blocklet/pages-kit/builtin/async/ai-runtime': {
|
|
68
|
+
get: () => import('../builtin/async/ai-runtime').catch((err) => {
|
|
69
|
+
console.error('Failed to load AI runtime', err);
|
|
70
|
+
return {};
|
|
71
|
+
}),
|
|
72
|
+
},
|
|
73
73
|
};
|
|
74
|
-
//
|
|
74
|
+
// set global variable
|
|
75
75
|
win[BuiltinModulesGlobalVariableName] = {
|
|
76
76
|
modules,
|
|
77
77
|
require(module) {
|
|
78
78
|
var _a;
|
|
79
|
-
//
|
|
79
|
+
// handle builtin module
|
|
80
80
|
const builtinModule = this.modules[module];
|
|
81
81
|
if (builtinModule) {
|
|
82
82
|
return builtinModule;
|
|
83
83
|
}
|
|
84
|
-
//
|
|
84
|
+
// handle relative path import
|
|
85
85
|
if (isRelativeModule(module)) {
|
|
86
86
|
const fileName = module.split('/').pop();
|
|
87
87
|
const fullUrl = joinURL(window.location.origin, ((_a = window === null || window === void 0 ? void 0 : window.blocklet) === null || _a === void 0 ? void 0 : _a.prefix) || '/', 'chunks', fileName);
|
|
@@ -95,27 +95,74 @@ export function injectGlobalComponents() {
|
|
|
95
95
|
return null;
|
|
96
96
|
},
|
|
97
97
|
};
|
|
98
|
-
//
|
|
98
|
+
// create importmap
|
|
99
99
|
const setupImportMap = () => {
|
|
100
|
-
//
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
// 添加 resolve 配置
|
|
116
|
-
const importMap = {
|
|
117
|
-
imports,
|
|
100
|
+
// 计算模块的 hash 值作为缓存版本
|
|
101
|
+
const calculateModulesHash = () => {
|
|
102
|
+
// 只提取模块路径和导出的键名用于 hash 计算
|
|
103
|
+
const moduleStructure = Object.entries(modules).map(([path, mod]) => ({
|
|
104
|
+
path,
|
|
105
|
+
exports: Object.keys(mod).sort(),
|
|
106
|
+
}));
|
|
107
|
+
const str = JSON.stringify(moduleStructure);
|
|
108
|
+
// 简单的 hash 计算方法
|
|
109
|
+
let hash = 0;
|
|
110
|
+
for (let i = 0; i < str.length; i++) {
|
|
111
|
+
const char = str.charCodeAt(i);
|
|
112
|
+
hash = hash * 2 ** 5 - hash + char;
|
|
113
|
+
}
|
|
114
|
+
return hash.toString(16);
|
|
118
115
|
};
|
|
116
|
+
const cacheKey = 'pages-kit-import-map';
|
|
117
|
+
const moduleHash = calculateModulesHash();
|
|
118
|
+
// 获取 imports - 优先从缓存读取
|
|
119
|
+
const getImportsFromCache = () => {
|
|
120
|
+
try {
|
|
121
|
+
const cachedData = localStorage.getItem(cacheKey);
|
|
122
|
+
if (cachedData) {
|
|
123
|
+
const { hash, imports } = JSON.parse(cachedData);
|
|
124
|
+
if (hash === moduleHash) {
|
|
125
|
+
// eslint-disable-next-line no-console
|
|
126
|
+
console.log(`getImportsFromCache: ${hash}`, imports);
|
|
127
|
+
return imports; // 返回缓存的 imports
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
catch (e) {
|
|
132
|
+
console.warn('Failed to load import map from cache', e);
|
|
133
|
+
}
|
|
134
|
+
return null; // 缓存无效
|
|
135
|
+
};
|
|
136
|
+
// 尝试从缓存获取或重新计算
|
|
137
|
+
const imports = getImportsFromCache() ||
|
|
138
|
+
Object.entries(modules).reduce((acc, [modulePath, moduleContent]) => {
|
|
139
|
+
const namedExports = Object.keys(moduleContent).filter((key) => key !== 'default');
|
|
140
|
+
const hasDefaultExport = 'default' in moduleContent;
|
|
141
|
+
// create module code
|
|
142
|
+
const moduleCode = `// GENERATED FILE. DO NOT EDIT.
|
|
143
|
+
const moduleSource = window['${BuiltinModulesGlobalVariableName}'].modules['${modulePath}'];
|
|
144
|
+
${namedExports.map((name) => `export const ${name} = moduleSource['${name}'];`).join('\n')}
|
|
145
|
+
export default ${hasDefaultExport ? 'moduleSource.default' : 'moduleSource'};
|
|
146
|
+
`;
|
|
147
|
+
// create base64 code
|
|
148
|
+
const base64Code = btoa(moduleCode);
|
|
149
|
+
acc[modulePath] = `data:application/javascript;base64,${base64Code}`;
|
|
150
|
+
return acc;
|
|
151
|
+
}, {});
|
|
152
|
+
// 如果是新计算的 imports,保存到缓存
|
|
153
|
+
if (!getImportsFromCache()) {
|
|
154
|
+
try {
|
|
155
|
+
localStorage.setItem(cacheKey, JSON.stringify({
|
|
156
|
+
hash: moduleHash,
|
|
157
|
+
imports,
|
|
158
|
+
}));
|
|
159
|
+
}
|
|
160
|
+
catch (e) {
|
|
161
|
+
console.warn('Failed to cache import map', e);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
// 添加 resolve 配置
|
|
165
|
+
const importMap = { imports };
|
|
119
166
|
if (enableShim) {
|
|
120
167
|
window.importShim.addImportMap(importMap);
|
|
121
168
|
}
|