@blocklet/pages-kit 0.4.83 → 0.4.85

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.
@@ -30,6 +30,7 @@ exports.safeJSONParse = safeJSONParse;
30
30
  exports.safeYamlParse = safeYamlParse;
31
31
  exports.parsePropertyValue = parsePropertyValue;
32
32
  exports.assignNullableFields = assignNullableFields;
33
+ exports.getComponentDependencies = getComponentDependencies;
33
34
  const yaml = __importStar(require("yaml"));
34
35
  function componentUMDName({ componentId }) {
35
36
  return `PagesCustomComponent${componentId}`;
@@ -125,6 +126,7 @@ function parsePropertyValue(property, value, { locale, defaultLocale }) {
125
126
  return undefined;
126
127
  }
127
128
  }
129
+ // type: component 存在 componentId 的时候,会复写为 RenderNestedComponent
128
130
  if (property.type === 'component') {
129
131
  const componentId = value?.componentId;
130
132
  if (typeof componentId !== 'string')
@@ -149,3 +151,36 @@ function assignNullableFields(properties, newProperties) {
149
151
  }
150
152
  return properties;
151
153
  }
154
+ function getComponentDependencies({ state, pageIds = [], componentIds = [], }) {
155
+ const pickComponentId = (obj, componentIds = new Set()) => {
156
+ if (Array.isArray(obj)) {
157
+ obj.forEach((i) => pickComponentId(i, componentIds));
158
+ }
159
+ if (obj && typeof obj === 'object') {
160
+ if (typeof obj.componentId === 'string') {
161
+ componentIds.add(obj.componentId);
162
+ }
163
+ Object.values(obj).forEach((i) => pickComponentId(i, componentIds));
164
+ }
165
+ return componentIds;
166
+ };
167
+ const result = new Set(componentIds);
168
+ for (const pageId of pageIds) {
169
+ const page = state.pages[pageId];
170
+ pickComponentId(page, result);
171
+ }
172
+ const tmp = [...result];
173
+ while (tmp.length) {
174
+ const componentId = tmp.shift();
175
+ if (!result.has(componentId))
176
+ result.add(componentId);
177
+ const component = state.components[componentId]?.data;
178
+ if (component) {
179
+ for (const i of pickComponentId(component)) {
180
+ if (!result.has(i))
181
+ tmp.push(i);
182
+ }
183
+ }
184
+ }
185
+ return [...result];
186
+ }
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.analyzeFileChunks = void 0;
4
+ const path_1 = require("path");
5
+ const typescript_1 = require("typescript");
6
+ const builtin_module_transformer_1 = require("./builtin-module-transformer");
7
+ /**
8
+ * 直接分析文件内容,查找chunks引用
9
+ * @param ts TypeScript命名空间
10
+ * @param fileContent 文件内容
11
+ * @returns 找到的chunks名称数组(只包含basename)
12
+ */
13
+ const analyzeFileChunks = (ts, fileContent) => {
14
+ // 创建源文件
15
+ const sourceFile = ts.createSourceFile('temp.js', // 文件名不重要,只是用于创建源文件
16
+ fileContent, ts.ScriptTarget.Latest, true);
17
+ // 查找引用的chunks
18
+ const referencedChunks = new Set();
19
+ // 递归访问AST
20
+ const visitNode = (node) => {
21
+ // 检查动态导入: import('./xxx')
22
+ if (ts.isCallExpression(node) &&
23
+ node.expression.kind === typescript_1.SyntaxKind.ImportKeyword &&
24
+ node.arguments.length > 0 &&
25
+ ts.isStringLiteral(node.arguments[0])) {
26
+ const importPath = node.arguments[0].text;
27
+ checkAndAddChunk(importPath, referencedChunks);
28
+ }
29
+ // 检查其他调用模式: load('./xxx') 或 __vitePreload('./xxx')
30
+ if (ts.isCallExpression(node) &&
31
+ ts.isIdentifier(node.expression) &&
32
+ (node.expression.text.includes('load') || node.expression.text.includes('Preload')) &&
33
+ node.arguments.length > 0 &&
34
+ ts.isStringLiteral(node.arguments[0])) {
35
+ const chunkPath = node.arguments[0].text;
36
+ checkAndAddChunk(chunkPath, referencedChunks);
37
+ }
38
+ // 检查字符串字面量
39
+ if (ts.isStringLiteral(node)) {
40
+ checkAndAddChunk(node.text, referencedChunks);
41
+ }
42
+ // 继续遍历子节点
43
+ ts.forEachChild(node, visitNode);
44
+ };
45
+ // 检查路径是否引用了chunk并添加到结果集
46
+ const checkAndAddChunk = (path, results) => {
47
+ if ((0, builtin_module_transformer_1.isRelativeModule)(path)) {
48
+ results.add((0, path_1.basename)(path));
49
+ }
50
+ };
51
+ // 开始遍历AST
52
+ visitNode(sourceFile);
53
+ // 只返回chunk名称(不含路径和扩展名)
54
+ return Array.from(referencedChunks);
55
+ };
56
+ exports.analyzeFileChunks = analyzeFileChunks;