@blocklet/pages-kit 0.4.118 → 0.4.120

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.
@@ -8,6 +8,7 @@ export function mergeComponent({ componentId, getComponent, locale, defaultLocal
8
8
  if (!component)
9
9
  return null;
10
10
  let script;
11
+ let editComponent;
11
12
  const props = Object.fromEntries(Object.entries(parameters ?? {}).map(([id, { value }]) => {
12
13
  const property = component?.properties?.[id]?.data;
13
14
  if (!property)
@@ -23,6 +24,7 @@ export function mergeComponent({ componentId, getComponent, locale, defaultLocal
23
24
  ])));
24
25
  if (component.renderer?.type === 'react-component') {
25
26
  script = component.renderer?.script;
27
+ editComponent = component.renderer?.editComponent;
26
28
  break;
27
29
  }
28
30
  if (component.renderer?.type === 'web-component') {
@@ -51,7 +53,7 @@ export function mergeComponent({ componentId, getComponent, locale, defaultLocal
51
53
  }
52
54
  if (!script)
53
55
  return null;
54
- return { script, props };
56
+ return { script, props, editComponent };
55
57
  }
56
58
  export const RenderNestedComponent = '__RENDER_NESTED_COMPONENT__';
57
59
  export function safeJSONParse(value) {
@@ -1,3 +1,4 @@
1
+ import { basename } from 'path';
1
2
  import { BuiltinModulesGlobalVariableName } from '../../types/builtin';
2
3
  export const isRelativeModule = (moduleSpecifier) => {
3
4
  if (!moduleSpecifier) {
@@ -11,22 +12,22 @@ export const isAsyncModule = (moduleSpecifier) => {
11
12
  }
12
13
  return ['@blocklet/pages-kit/builtin/async/ai-runtime'].includes(moduleSpecifier);
13
14
  };
15
+ // check if the module is a target module
16
+ export const isBuiltinModule = (moduleSpecifier) => {
17
+ if (!moduleSpecifier) {
18
+ return false;
19
+ }
20
+ return moduleSpecifier.startsWith('@blocklet/pages-kit/builtin/');
21
+ };
14
22
  export const createBuiltinModuleTransformer = (ts) => (context) => (file) => {
15
23
  const { factory } = context;
16
24
  // 统一的模块导入收集器
17
25
  const imports = [];
18
- // check if the module is a target module
19
- const isTargetModule = (moduleSpecifier) => {
20
- if (!moduleSpecifier) {
21
- return false;
22
- }
23
- return moduleSpecifier.startsWith('@blocklet/pages-kit/builtin/') || isRelativeModule(moduleSpecifier);
24
- };
25
26
  // filter and collect the import statements that need to be processed
26
27
  const statements = file.statements.filter((s) => {
27
28
  if (ts.isImportDeclaration(s) &&
28
29
  ts.isStringLiteral(s.moduleSpecifier) &&
29
- isTargetModule(s.moduleSpecifier.text)) {
30
+ (isBuiltinModule(s.moduleSpecifier.text) || isRelativeModule(s.moduleSpecifier.text))) {
30
31
  const importInfo = {
31
32
  moduleName: s.moduleSpecifier.text,
32
33
  name: s.importClause?.name?.text,
@@ -55,11 +56,11 @@ export const createBuiltinModuleTransformer = (ts) => (context) => (file) => {
55
56
  statements.unshift(...imports.flatMap((importInfo) => {
56
57
  // call inject-global-components require method
57
58
  const requireCall = factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier(BuiltinModulesGlobalVariableName), 'require'), undefined, [factory.createStringLiteral(importInfo.moduleName)]);
59
+ const isAsync = isRelativeModule(importInfo.moduleName) || isAsyncModule(importInfo.moduleName);
58
60
  // create await expression if the module is ../ or ./
59
- const mod = isRelativeModule(importInfo.moduleName) || isAsyncModule(importInfo.moduleName)
60
- ? factory.createAwaitExpression(requireCall)
61
- : requireCall;
61
+ const mod = isAsync ? factory.createAwaitExpression(requireCall) : requireCall;
62
62
  return [
63
+ // handle default import
63
64
  importInfo.name
64
65
  ? factory.createVariableStatement([], factory.createVariableDeclarationList([
65
66
  factory.createVariableDeclaration(factory.createIdentifier(importInfo.name), undefined, undefined, factory.createPropertyAccessExpression(mod, 'default')),
@@ -72,12 +73,31 @@ export const createBuiltinModuleTransformer = (ts) => (context) => (file) => {
72
73
  ], ts.NodeFlags.Const))
73
74
  : undefined,
74
75
  // handle named import
75
- ...importInfo.members.map((member) => {
76
- return factory.createVariableStatement([], factory.createVariableDeclarationList([
77
- factory.createVariableDeclaration(factory.createIdentifier(member.name), undefined, undefined, factory.createPropertyAccessExpression(mod, member.propertyName ?? member.name)),
78
- ], ts.NodeFlags.Const));
79
- }),
76
+ importInfo.members.length > 0
77
+ ? factory.createVariableStatement([], factory.createVariableDeclarationList([
78
+ factory.createVariableDeclaration(factory.createObjectBindingPattern(importInfo.members.map((member) => {
79
+ return factory.createBindingElement(undefined, member.propertyName ? factory.createIdentifier(member.propertyName) : undefined, factory.createIdentifier(member.name), undefined);
80
+ })), undefined, undefined, mod),
81
+ ], ts.NodeFlags.Const))
82
+ : undefined,
80
83
  ].filter((i) => !!i);
81
84
  }));
82
85
  return factory.updateSourceFile(file, statements);
83
86
  };
87
+ export const createWellKnownChunksTransformer = (ts) => (context) => (file) => {
88
+ const { factory } = context;
89
+ // 转换所有导入声明
90
+ const transformedStatements = file.statements.map((node) => {
91
+ if (ts.isImportDeclaration(node) &&
92
+ ts.isStringLiteral(node.moduleSpecifier) &&
93
+ isRelativeModule(node.moduleSpecifier.text)) {
94
+ const moduleSpecifier = node.moduleSpecifier.text;
95
+ const fileName = basename(moduleSpecifier);
96
+ const newPath = `/.well-known/chunks/${fileName}`;
97
+ // 创建新的导入声明
98
+ return factory.createImportDeclaration(node.modifiers, node.importClause, factory.createStringLiteral(newPath));
99
+ }
100
+ return node;
101
+ });
102
+ return factory.updateSourceFile(file, transformedStatements);
103
+ };