@blocklet/pages-kit 0.4.16-beta.20250305-2 → 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/ErrorComponent.js +40 -0
- package/lib/cjs/components/CustomComponentRenderer/index.js +3 -14
- package/lib/cjs/components/CustomComponentRenderer/state.js +117 -9
- package/lib/cjs/tsconfig.tsbuildinfo +1 -1
- package/lib/cjs/utils/inject-es-module-shims-options.js +16 -6
- package/lib/cjs/utils/inject-global-components.js +101 -53
- package/lib/cjs/utils/typescript/builtin-module-transformer.js +9 -9
- package/lib/esm/components/CustomComponentRenderer/ErrorComponent.js +36 -0
- package/lib/esm/components/CustomComponentRenderer/index.js +4 -15
- package/lib/esm/components/CustomComponentRenderer/state.js +118 -10
- package/lib/esm/tsconfig.tsbuildinfo +1 -1
- package/lib/esm/utils/inject-es-module-shims-options.js +14 -7
- package/lib/esm/utils/inject-global-components.js +101 -53
- package/lib/esm/utils/typescript/builtin-module-transformer.js +7 -8
- package/lib/types/components/CustomComponentRenderer/ErrorComponent.d.ts +14 -0
- package/lib/types/tsconfig.tsbuildinfo +1 -1
- package/lib/types/types/preload.d.ts +1 -1
- package/lib/types/utils/inject-es-module-shims-options.d.ts +1 -0
- package/lib/types/utils/typescript/builtin-module-transformer.d.ts +1 -0
- package/package.json +4 -4
|
@@ -1,7 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
export function injectESModulesShimsOptions() {
|
|
2
|
+
// if already initialized, return
|
|
3
|
+
if (window.esmsInitOptions) {
|
|
4
|
+
return;
|
|
5
|
+
}
|
|
6
|
+
window.esmsInitOptions = {
|
|
7
|
+
shimMode: true,
|
|
8
|
+
polyfillEnable: ['css-modules', 'json-modules', 'wasm-modules', 'source-phase'],
|
|
9
|
+
skip: ['crypto-browserify', 'crypto'],
|
|
10
|
+
mapOverrides: true,
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
// enable es-module-shims
|
|
14
|
+
injectESModulesShimsOptions();
|
|
@@ -1,17 +1,7 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
1
|
import 'es-module-shims';
|
|
11
2
|
import React from 'react';
|
|
12
3
|
import { joinURL } from 'ufo';
|
|
13
4
|
import * as arcblockUx from '../builtin/arcblock/ux';
|
|
14
|
-
import * as aiRuntime from '../builtin/async/ai-runtime';
|
|
15
5
|
import * as imagePreview from '../builtin/async/image-preview';
|
|
16
6
|
import * as reactMarkdown from '../builtin/async/react-markdown';
|
|
17
7
|
import * as reactScrollToBottom from '../builtin/async/react-scroll-to-bottom';
|
|
@@ -36,14 +26,20 @@ import * as zustand from '../builtin/zustand';
|
|
|
36
26
|
import * as zustandMiddlewareImmer from '../builtin/zustand/middleware/immer';
|
|
37
27
|
import CustomComponentRenderer from '../components/CustomComponentRenderer';
|
|
38
28
|
import { BuiltinModulesGlobalVariableName } from '../types/builtin';
|
|
29
|
+
import { isRelativeModule } from './typescript/builtin-module-transformer';
|
|
39
30
|
// Initialize ES Module Shims before any imports
|
|
40
31
|
export function injectGlobalComponents() {
|
|
32
|
+
var _a;
|
|
41
33
|
const win = window || {};
|
|
42
|
-
|
|
34
|
+
// if already initialized, return
|
|
35
|
+
if (win[BuiltinModulesGlobalVariableName]) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const enableShim = !!((_a = window === null || window === void 0 ? void 0 : window.esmsInitOptions) === null || _a === void 0 ? void 0 : _a.shimMode);
|
|
43
39
|
win.React = React;
|
|
44
|
-
//
|
|
40
|
+
// create module map
|
|
45
41
|
const modules = {
|
|
46
|
-
react,
|
|
42
|
+
React: react,
|
|
47
43
|
'@blocklet/pages-kit/builtin/pages-kit': { CustomComponentRenderer },
|
|
48
44
|
'@blocklet/pages-kit/builtin/dayjs': dayjs,
|
|
49
45
|
'@blocklet/pages-kit/builtin/utils': utils,
|
|
@@ -68,55 +64,107 @@ export function injectGlobalComponents() {
|
|
|
68
64
|
'@blocklet/pages-kit/builtin/async/react-markdown': reactMarkdown,
|
|
69
65
|
'@blocklet/pages-kit/builtin/async/react-syntax-highlighter': reactSyntaxHighlighter,
|
|
70
66
|
'@blocklet/pages-kit/builtin/async/image-preview': imagePreview,
|
|
71
|
-
'@blocklet/pages-kit/builtin/async/ai-runtime':
|
|
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
|
+
},
|
|
72
73
|
};
|
|
73
|
-
//
|
|
74
|
+
// set global variable
|
|
74
75
|
win[BuiltinModulesGlobalVariableName] = {
|
|
75
76
|
modules,
|
|
76
77
|
require(module) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
}
|
|
91
|
-
return yield import(/* @vite-ignore */ fullUrl);
|
|
78
|
+
var _a;
|
|
79
|
+
// handle builtin module
|
|
80
|
+
const builtinModule = this.modules[module];
|
|
81
|
+
if (builtinModule) {
|
|
82
|
+
return builtinModule;
|
|
83
|
+
}
|
|
84
|
+
// handle relative path import
|
|
85
|
+
if (isRelativeModule(module)) {
|
|
86
|
+
const fileName = module.split('/').pop();
|
|
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);
|
|
88
|
+
if (enableShim) {
|
|
89
|
+
const mod = window.importShim(fullUrl);
|
|
90
|
+
return mod;
|
|
92
91
|
}
|
|
93
|
-
|
|
94
|
-
|
|
92
|
+
const mod = import(/* @vite-ignore */ fullUrl);
|
|
93
|
+
return mod;
|
|
94
|
+
}
|
|
95
|
+
return null;
|
|
95
96
|
},
|
|
96
97
|
};
|
|
97
|
-
//
|
|
98
|
+
// create importmap
|
|
98
99
|
const setupImportMap = () => {
|
|
99
|
-
//
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
// 添加 resolve 配置
|
|
115
|
-
const importMap = {
|
|
116
|
-
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);
|
|
117
115
|
};
|
|
118
|
-
|
|
119
|
-
|
|
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 };
|
|
166
|
+
if (enableShim) {
|
|
167
|
+
window.importShim.addImportMap(importMap);
|
|
120
168
|
}
|
|
121
169
|
else {
|
|
122
170
|
// fallback to create script tag
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { BuiltinModulesGlobalVariableName } from '../../types/builtin';
|
|
2
|
+
export const isRelativeModule = (moduleSpecifier) => {
|
|
3
|
+
return moduleSpecifier.startsWith('./') || moduleSpecifier.startsWith('../');
|
|
4
|
+
};
|
|
2
5
|
export const createBuiltinModuleTransformer = (ts) => (context) => (file) => {
|
|
3
6
|
const { factory } = context;
|
|
4
7
|
// 统一的模块导入收集器
|
|
5
8
|
const imports = [];
|
|
6
9
|
// check if the module is a target module
|
|
7
10
|
const isTargetModule = (moduleSpecifier) => {
|
|
8
|
-
return
|
|
9
|
-
moduleSpecifier.startsWith('./') ||
|
|
10
|
-
moduleSpecifier.startsWith('../'));
|
|
11
|
+
return moduleSpecifier.startsWith('@blocklet/pages-kit/builtin/') || isRelativeModule(moduleSpecifier);
|
|
11
12
|
};
|
|
12
13
|
// filter and collect the import statements that need to be processed
|
|
13
14
|
const statements = file.statements.filter((s) => {
|
|
@@ -43,11 +44,9 @@ export const createBuiltinModuleTransformer = (ts) => (context) => (file) => {
|
|
|
43
44
|
});
|
|
44
45
|
statements.unshift(...imports.flatMap((importInfo) => {
|
|
45
46
|
// call inject-global-components require method
|
|
46
|
-
const requireCall = factory.createCallExpression(factory.createPropertyAccessExpression(factory.
|
|
47
|
-
//
|
|
48
|
-
|
|
49
|
-
// create await expression to wait for the async result
|
|
50
|
-
const mod = factory.createAwaitExpression(requireCall);
|
|
47
|
+
const requireCall = factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier(BuiltinModulesGlobalVariableName), 'require'), undefined, [factory.createStringLiteral(importInfo.moduleName)]);
|
|
48
|
+
// create await expression if the module is ../ or ./
|
|
49
|
+
const mod = isRelativeModule(importInfo.moduleName) ? factory.createAwaitExpression(requireCall) : requireCall;
|
|
51
50
|
return [
|
|
52
51
|
importInfo.name
|
|
53
52
|
? factory.createVariableStatement([], factory.createVariableDeclarationList([
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
interface ComponentErrorProps {
|
|
3
|
+
title?: string;
|
|
4
|
+
message: string;
|
|
5
|
+
componentId?: string;
|
|
6
|
+
componentName?: string;
|
|
7
|
+
blockletId?: string;
|
|
8
|
+
blockletTitle?: string;
|
|
9
|
+
showHints?: boolean;
|
|
10
|
+
error?: Error;
|
|
11
|
+
resetErrorBoundary?: () => void;
|
|
12
|
+
}
|
|
13
|
+
export declare const ComponentError: React.FC<ComponentErrorProps>;
|
|
14
|
+
export {};
|