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