@blocklet/pages-kit 0.4.16-beta.20250305-2 → 0.4.16-beta.20250306-1
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 +9 -6
- package/lib/cjs/utils/inject-global-components.js +30 -29
- 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 +119 -11
- package/lib/esm/tsconfig.tsbuildinfo +1 -1
- package/lib/esm/utils/inject-es-module-shims-options.js +7 -7
- package/lib/esm/utils/inject-global-components.js +30 -29
- 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,7 @@
|
|
|
1
|
-
|
|
2
|
-
window.esmsInitOptions = {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
export function injectESModulesShimsOptions() {
|
|
2
|
+
window.esmsInitOptions = {
|
|
3
|
+
shimMode: true,
|
|
4
|
+
polyfillEnable: ['css-modules', 'json-modules', 'wasm-modules', 'source-phase'],
|
|
5
|
+
};
|
|
6
|
+
}
|
|
7
|
+
injectESModulesShimsOptions();
|
|
@@ -1,12 +1,3 @@
|
|
|
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';
|
|
@@ -36,10 +27,12 @@ import * as zustand from '../builtin/zustand';
|
|
|
36
27
|
import * as zustandMiddlewareImmer from '../builtin/zustand/middleware/immer';
|
|
37
28
|
import CustomComponentRenderer from '../components/CustomComponentRenderer';
|
|
38
29
|
import { BuiltinModulesGlobalVariableName } from '../types/builtin';
|
|
30
|
+
import { isRelativeModule } from './typescript/builtin-module-transformer';
|
|
39
31
|
// Initialize ES Module Shims before any imports
|
|
40
32
|
export function injectGlobalComponents() {
|
|
33
|
+
var _a;
|
|
41
34
|
const win = window || {};
|
|
42
|
-
const
|
|
35
|
+
const enableShim = !!((_a = window === null || window === void 0 ? void 0 : window.esmsInitOptions) === null || _a === void 0 ? void 0 : _a.shimMode);
|
|
43
36
|
win.React = React;
|
|
44
37
|
// 创建模块映射
|
|
45
38
|
const modules = {
|
|
@@ -69,29 +62,37 @@ export function injectGlobalComponents() {
|
|
|
69
62
|
'@blocklet/pages-kit/builtin/async/react-syntax-highlighter': reactSyntaxHighlighter,
|
|
70
63
|
'@blocklet/pages-kit/builtin/async/image-preview': imagePreview,
|
|
71
64
|
'@blocklet/pages-kit/builtin/async/ai-runtime': aiRuntime,
|
|
65
|
+
// crypto: {
|
|
66
|
+
// // 手动暴露 crypto 的属性和方法
|
|
67
|
+
// subtle: win.crypto.subtle,
|
|
68
|
+
// getRandomValues: win.crypto.getRandomValues,
|
|
69
|
+
// randomUUID: win.crypto.randomUUID,
|
|
70
|
+
// // 如果需要作为默认导出
|
|
71
|
+
// default: win.crypto,
|
|
72
|
+
// },
|
|
72
73
|
};
|
|
73
74
|
// 设置全局变量
|
|
74
75
|
win[BuiltinModulesGlobalVariableName] = {
|
|
75
76
|
modules,
|
|
76
77
|
require(module) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
78
|
+
var _a;
|
|
79
|
+
// 处理内置模块
|
|
80
|
+
const builtinModule = this.modules[module];
|
|
81
|
+
if (builtinModule) {
|
|
82
|
+
return builtinModule;
|
|
83
|
+
}
|
|
84
|
+
// 处理相对路径导入
|
|
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;
|
|
83
91
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
if (importShim) {
|
|
89
|
-
return yield importShim(fullUrl);
|
|
90
|
-
}
|
|
91
|
-
return yield import(/* @vite-ignore */ fullUrl);
|
|
92
|
-
}
|
|
93
|
-
return null;
|
|
94
|
-
});
|
|
92
|
+
const mod = import(/* @vite-ignore */ fullUrl);
|
|
93
|
+
return mod;
|
|
94
|
+
}
|
|
95
|
+
return null;
|
|
95
96
|
},
|
|
96
97
|
};
|
|
97
98
|
// 创建 importmap
|
|
@@ -115,8 +116,8 @@ export function injectGlobalComponents() {
|
|
|
115
116
|
const importMap = {
|
|
116
117
|
imports,
|
|
117
118
|
};
|
|
118
|
-
if (
|
|
119
|
-
importShim.addImportMap(importMap);
|
|
119
|
+
if (enableShim) {
|
|
120
|
+
window.importShim.addImportMap(importMap);
|
|
120
121
|
}
|
|
121
122
|
else {
|
|
122
123
|
// 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 {};
|