@blocklet/pages-kit 0.4.15 → 0.4.16-beta.20250305

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.
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ function initShimsOptions() {
3
+ window.esmsInitOptions = {
4
+ // Enable Shim Mode
5
+ shimMode: true,
6
+ // Enable polyfill features.
7
+ polyfillEnable: ['css-modules', 'json-modules', 'wasm-modules', 'source-phase'],
8
+ };
9
+ }
10
+ initShimsOptions();
@@ -22,11 +22,23 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
24
  };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
25
34
  var __importDefault = (this && this.__importDefault) || function (mod) {
26
35
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
36
  };
28
37
  Object.defineProperty(exports, "__esModule", { value: true });
38
+ exports.injectGlobalComponents = injectGlobalComponents;
39
+ require("es-module-shims");
29
40
  const react_1 = __importDefault(require("react"));
41
+ const ufo_1 = require("ufo");
30
42
  const arcblockUx = __importStar(require("../builtin/arcblock/ux"));
31
43
  const aiRuntime = __importStar(require("../builtin/async/ai-runtime"));
32
44
  const imagePreview = __importStar(require("../builtin/async/image-preview"));
@@ -53,57 +65,83 @@ const zustand = __importStar(require("../builtin/zustand"));
53
65
  const zustandMiddlewareImmer = __importStar(require("../builtin/zustand/middleware/immer"));
54
66
  const CustomComponentRenderer_1 = __importDefault(require("../components/CustomComponentRenderer"));
55
67
  const builtin_1 = require("../types/builtin");
68
+ // Initialize ES Module Shims before any imports
56
69
  function injectGlobalComponents() {
57
- const win = window;
58
- // if (win[BuiltinModulesGlobalVariableName]) return;
70
+ const win = window || {};
71
+ win.React = react_1.default;
72
+ // 创建模块映射
73
+ const modules = {
74
+ react,
75
+ '@blocklet/pages-kit/builtin/pages-kit': { CustomComponentRenderer: CustomComponentRenderer_1.default },
76
+ '@blocklet/pages-kit/builtin/dayjs': dayjs,
77
+ '@blocklet/pages-kit/builtin/utils': utils,
78
+ '@blocklet/pages-kit/builtin/stream': stream,
79
+ '@blocklet/pages-kit/builtin/react': react,
80
+ '@blocklet/pages-kit/builtin/emotion/css': emotionCss,
81
+ '@blocklet/pages-kit/builtin/mui/material': muiMaterial,
82
+ '@blocklet/pages-kit/builtin/mui/lab': muiLab,
83
+ '@blocklet/pages-kit/builtin/react-hook-form': reactHookForm,
84
+ '@blocklet/pages-kit/builtin/iconify/react': iconifyReact,
85
+ '@blocklet/pages-kit/builtin/zustand': zustand,
86
+ '@blocklet/pages-kit/builtin/zustand/middleware/immer': zustandMiddlewareImmer,
87
+ '@blocklet/pages-kit/builtin/immer': immer,
88
+ '@blocklet/pages-kit/builtin/react-wrap-balancer': reactWrapBalancer,
89
+ '@blocklet/pages-kit/builtin/react-router-dom': reactRouterDOM,
90
+ '@blocklet/pages-kit/builtin/session': session,
91
+ '@blocklet/pages-kit/builtin/locale': locale,
92
+ '@blocklet/pages-kit/builtin/arcblock/ux': arcblockUx,
93
+ '@blocklet/pages-kit/builtin/page/header': pageHeader,
94
+ '@blocklet/pages-kit/builtin/components': components,
95
+ '@blocklet/pages-kit/builtin/async/react-scroll-to-bottom': reactScrollToBottom,
96
+ '@blocklet/pages-kit/builtin/async/react-markdown': reactMarkdown,
97
+ '@blocklet/pages-kit/builtin/async/react-syntax-highlighter': reactSyntaxHighlighter,
98
+ '@blocklet/pages-kit/builtin/async/image-preview': imagePreview,
99
+ '@blocklet/pages-kit/builtin/async/ai-runtime': aiRuntime,
100
+ };
101
+ // 设置全局变量
59
102
  win[builtin_1.BuiltinModulesGlobalVariableName] = {
60
- modules: {
61
- '@blocklet/pages-kit/builtin/pages-kit': { CustomComponentRenderer: CustomComponentRenderer_1.default },
62
- '@blocklet/pages-kit/builtin/dayjs': dayjs,
63
- '@blocklet/pages-kit/builtin/utils': utils,
64
- '@blocklet/pages-kit/builtin/stream': stream,
65
- '@blocklet/pages-kit/builtin/react': react,
66
- '@blocklet/pages-kit/builtin/emotion/css': emotionCss,
67
- '@blocklet/pages-kit/builtin/mui/material': muiMaterial,
68
- '@blocklet/pages-kit/builtin/mui/lab': muiLab,
69
- '@blocklet/pages-kit/builtin/react-hook-form': reactHookForm,
70
- '@blocklet/pages-kit/builtin/iconify/react': iconifyReact,
71
- //
72
- '@blocklet/pages-kit/builtin/zustand': zustand,
73
- '@blocklet/pages-kit/builtin/zustand/middleware/immer': zustandMiddlewareImmer,
74
- '@blocklet/pages-kit/builtin/immer': immer,
75
- '@blocklet/pages-kit/builtin/react-wrap-balancer': reactWrapBalancer,
76
- '@blocklet/pages-kit/builtin/react-router-dom': reactRouterDOM,
77
- '@blocklet/pages-kit/builtin/session': session,
78
- '@blocklet/pages-kit/builtin/locale': locale,
79
- '@blocklet/pages-kit/builtin/arcblock/ux': arcblockUx,
80
- '@blocklet/pages-kit/builtin/page/header': pageHeader,
81
- '@blocklet/pages-kit/builtin/components': components,
82
- // The following packages are lazy loading
83
- '@blocklet/pages-kit/builtin/async/react-scroll-to-bottom': reactScrollToBottom,
84
- '@blocklet/pages-kit/builtin/async/react-markdown': reactMarkdown,
85
- '@blocklet/pages-kit/builtin/async/react-syntax-highlighter': reactSyntaxHighlighter,
86
- '@blocklet/pages-kit/builtin/async/image-preview': imagePreview,
87
- '@blocklet/pages-kit/builtin/async/ai-runtime': aiRuntime,
88
- },
103
+ modules,
89
104
  require(module) {
90
- var _a;
91
- if ((_a = win[builtin_1.BuiltinModulesGlobalVariableName]) === null || _a === void 0 ? void 0 : _a.modules[module]) {
92
- return win[builtin_1.BuiltinModulesGlobalVariableName].modules[module];
93
- }
94
- // Try loading from CDN or dynamic import
95
- try {
96
- // TODO: 需要支持动态加载
97
- // const module = await import(/* webpackIgnore: true */ `https://cdn.jsdelivr.net/npm/${moduleName}`);
98
- // return module;
105
+ return __awaiter(this, void 0, void 0, function* () {
106
+ var _a;
107
+ // 处理内置模块
108
+ const builtinModule = this.modules[module];
109
+ if (builtinModule) {
110
+ return builtinModule;
111
+ }
112
+ // 处理相对路径导入
113
+ if (module.startsWith('./') || module.startsWith('../')) {
114
+ const fileName = module.split('/').pop();
115
+ 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);
116
+ return yield importShim(fullUrl);
117
+ }
99
118
  return null;
100
- }
101
- catch (err) {
102
- console.warn(`Failed to load module ${module}`, err);
103
- return null;
104
- }
119
+ });
105
120
  },
106
121
  };
107
- win.React = react_1.default;
122
+ // 创建 importmap
123
+ const setupImportMap = () => {
124
+ // 设置 importmap 内容
125
+ const imports = Object.entries(modules).reduce((acc, [modulePath, moduleContent]) => {
126
+ const namedExports = Object.keys(moduleContent).filter((key) => key !== 'default');
127
+ const hasDefaultExport = 'default' in moduleContent;
128
+ // 创建模块代码
129
+ const moduleCode = `// GENERATED FILE. DO NOT EDIT.
130
+ const moduleSource = window['${builtin_1.BuiltinModulesGlobalVariableName}'].modules['${modulePath}'];
131
+ ${namedExports.map((name) => `export const ${name} = moduleSource['${name}'];`).join('\n')}
132
+ export default ${hasDefaultExport ? 'moduleSource.default' : 'moduleSource'};
133
+ `;
134
+ // 使用 base64 编码模块代码
135
+ const base64Code = btoa(moduleCode);
136
+ acc[modulePath] = `data:application/javascript;base64,${base64Code}`;
137
+ return acc;
138
+ }, {});
139
+ // 添加 resolve 配置
140
+ const importMap = {
141
+ imports,
142
+ };
143
+ importShim.addImportMap(importMap);
144
+ };
145
+ setupImportMap();
108
146
  }
109
147
  injectGlobalComponents();
@@ -4,48 +4,67 @@ exports.createBuiltinModuleTransformer = void 0;
4
4
  const builtin_1 = require("../../types/builtin");
5
5
  const createBuiltinModuleTransformer = (ts) => (context) => (file) => {
6
6
  const { factory } = context;
7
- const builtins = [];
7
+ // 统一的模块导入收集器
8
+ const imports = [];
9
+ // check if the module is a target module
10
+ const isTargetModule = (moduleSpecifier) => {
11
+ return (moduleSpecifier.startsWith('@blocklet/pages-kit/builtin/') ||
12
+ moduleSpecifier.startsWith('./') ||
13
+ moduleSpecifier.startsWith('../'));
14
+ };
15
+ // filter and collect the import statements that need to be processed
8
16
  const statements = file.statements.filter((s) => {
9
17
  var _a, _b, _c;
10
18
  if (ts.isImportDeclaration(s) &&
11
19
  ts.isStringLiteral(s.moduleSpecifier) &&
12
- s.moduleSpecifier.text.startsWith('@blocklet/pages-kit/builtin/')) {
13
- const builtin = {
20
+ isTargetModule(s.moduleSpecifier.text)) {
21
+ const importInfo = {
14
22
  moduleName: s.moduleSpecifier.text,
15
23
  name: (_b = (_a = s.importClause) === null || _a === void 0 ? void 0 : _a.name) === null || _b === void 0 ? void 0 : _b.text,
16
24
  members: [],
25
+ namespace: '',
17
26
  };
18
- builtins.push(builtin);
19
27
  const namedBindings = (_c = s.importClause) === null || _c === void 0 ? void 0 : _c.namedBindings;
20
28
  if (namedBindings) {
21
29
  if (ts.isNamespaceImport(namedBindings)) {
22
- builtin.namespace = namedBindings.name.text;
30
+ importInfo.namespace = namedBindings.name.text;
23
31
  }
24
32
  namedBindings.forEachChild((node) => {
25
33
  var _a;
26
34
  if (ts.isImportSpecifier(node)) {
27
- builtin.members.push({ name: node.name.text, propertyName: (_a = node.propertyName) === null || _a === void 0 ? void 0 : _a.text });
35
+ importInfo.members.push({
36
+ name: node.name.text,
37
+ propertyName: (_a = node.propertyName) === null || _a === void 0 ? void 0 : _a.text,
38
+ });
28
39
  }
29
40
  });
30
41
  }
42
+ imports.push(importInfo);
31
43
  return false;
32
44
  }
33
45
  return true;
34
46
  });
35
- statements.unshift(...builtins.flatMap((builtin) => {
36
- const mod = factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier(builtin_1.BuiltinModulesGlobalVariableName), 'require'), undefined, [factory.createStringLiteral(builtin.moduleName)]);
47
+ statements.unshift(...imports.flatMap((importInfo) => {
48
+ // call inject-global-components require method
49
+ const requireCall = factory.createCallExpression(factory.createPropertyAccessExpression(factory.createPropertyAccessExpression(
50
+ // factory.createIdentifier('globalThis'),
51
+ factory.createIdentifier('window'), factory.createIdentifier(builtin_1.BuiltinModulesGlobalVariableName)), factory.createIdentifier('require')), undefined, [factory.createStringLiteral(importInfo.moduleName)]);
52
+ // create await expression to wait for the async result
53
+ const mod = factory.createAwaitExpression(requireCall);
37
54
  return [
38
- builtin.name
55
+ importInfo.name
39
56
  ? factory.createVariableStatement([], factory.createVariableDeclarationList([
40
- factory.createVariableDeclaration(factory.createIdentifier(builtin.name), undefined, undefined, factory.createPropertyAccessExpression(mod, 'default')),
57
+ factory.createVariableDeclaration(factory.createIdentifier(importInfo.name), undefined, undefined, factory.createPropertyAccessExpression(mod, 'default')),
41
58
  ], ts.NodeFlags.Const))
42
59
  : undefined,
43
- builtin.namespace
60
+ // handle namespace import
61
+ importInfo.namespace
44
62
  ? factory.createVariableStatement([], factory.createVariableDeclarationList([
45
- factory.createVariableDeclaration(factory.createIdentifier(builtin.namespace), undefined, undefined, mod),
63
+ factory.createVariableDeclaration(factory.createIdentifier(importInfo.namespace), undefined, undefined, mod),
46
64
  ], ts.NodeFlags.Const))
47
65
  : undefined,
48
- ...builtin.members.map((member) => {
66
+ // handle named import
67
+ ...importInfo.members.map((member) => {
49
68
  var _a;
50
69
  return factory.createVariableStatement([], factory.createVariableDeclarationList([
51
70
  factory.createVariableDeclaration(factory.createIdentifier(member.name), undefined, undefined, factory.createPropertyAccessExpression(mod, (_a = member.propertyName) !== null && _a !== void 0 ? _a : member.name)),
@@ -1,3 +1,4 @@
1
+ import '../utils/inject-es-module-shims-options';
1
2
  import '../utils/inject-global-components';
2
3
  export { default as CustomComponentRenderer } from './CustomComponentRenderer';
3
4
  export * from './CustomComponentRenderer';