@blocklet/pages-kit 0.4.16-beta.20250305 → 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/components/index.js +2 -1
- package/lib/cjs/tsconfig.tsbuildinfo +1 -1
- package/lib/cjs/utils/inject-es-module-shims-options.js +4 -4
- package/lib/cjs/utils/inject-global-components.js +38 -25
- 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/components/index.js +2 -1
- package/lib/esm/tsconfig.tsbuildinfo +1 -1
- package/lib/esm/utils/inject-es-module-shims-options.js +2 -5
- package/lib/esm/utils/inject-global-components.js +38 -25
- package/lib/esm/utils/typescript/builtin-module-transformer.js +7 -8
- package/lib/types/components/CustomComponentRenderer/ErrorComponent.d.ts +14 -0
- package/lib/types/components/index.d.ts +0 -1
- 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 -1
- package/lib/types/utils/typescript/builtin-module-transformer.d.ts +1 -0
- package/package.json +4 -4
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
function initShimsOptions() {
|
|
1
|
+
export function injectESModulesShimsOptions() {
|
|
3
2
|
window.esmsInitOptions = {
|
|
4
|
-
// Enable Shim Mode
|
|
5
3
|
shimMode: true,
|
|
6
|
-
// Enable polyfill features.
|
|
7
4
|
polyfillEnable: ['css-modules', 'json-modules', 'wasm-modules', 'source-phase'],
|
|
8
5
|
};
|
|
9
6
|
}
|
|
10
|
-
|
|
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,9 +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 || {};
|
|
35
|
+
const enableShim = !!((_a = window === null || window === void 0 ? void 0 : window.esmsInitOptions) === null || _a === void 0 ? void 0 : _a.shimMode);
|
|
42
36
|
win.React = React;
|
|
43
37
|
// 创建模块映射
|
|
44
38
|
const modules = {
|
|
@@ -68,26 +62,37 @@ export function injectGlobalComponents() {
|
|
|
68
62
|
'@blocklet/pages-kit/builtin/async/react-syntax-highlighter': reactSyntaxHighlighter,
|
|
69
63
|
'@blocklet/pages-kit/builtin/async/image-preview': imagePreview,
|
|
70
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
|
+
// },
|
|
71
73
|
};
|
|
72
74
|
// 设置全局变量
|
|
73
75
|
win[BuiltinModulesGlobalVariableName] = {
|
|
74
76
|
modules,
|
|
75
77
|
require(module) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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;
|
|
82
91
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
return yield importShim(fullUrl);
|
|
88
|
-
}
|
|
89
|
-
return null;
|
|
90
|
-
});
|
|
92
|
+
const mod = import(/* @vite-ignore */ fullUrl);
|
|
93
|
+
return mod;
|
|
94
|
+
}
|
|
95
|
+
return null;
|
|
91
96
|
},
|
|
92
97
|
};
|
|
93
98
|
// 创建 importmap
|
|
@@ -111,8 +116,16 @@ export function injectGlobalComponents() {
|
|
|
111
116
|
const importMap = {
|
|
112
117
|
imports,
|
|
113
118
|
};
|
|
114
|
-
|
|
119
|
+
if (enableShim) {
|
|
120
|
+
window.importShim.addImportMap(importMap);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
// fallback to create script tag
|
|
124
|
+
const script = document.createElement('script');
|
|
125
|
+
script.type = 'importmap';
|
|
126
|
+
script.textContent = JSON.stringify(importMap);
|
|
127
|
+
document.head.appendChild(script);
|
|
128
|
+
}
|
|
115
129
|
};
|
|
116
130
|
setupImportMap();
|
|
117
131
|
}
|
|
118
|
-
injectGlobalComponents();
|
|
@@ -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 {};
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import '../utils/inject-es-module-shims-options';
|
|
2
|
-
import '../utils/inject-global-components';
|
|
3
2
|
export { default as CustomComponentRenderer } from './CustomComponentRenderer';
|
|
4
3
|
export * from './CustomComponentRenderer';
|
|
5
4
|
export { default as ResponsiveImage } from './ResponsiveImage';
|