@blocklet/pages-kit 0.4.83 → 0.4.84
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/tsconfig.tsbuildinfo +1 -1
- package/lib/cjs/utils/property.js +35 -0
- package/lib/cjs/utils/typescript/chunks-analyzer-transformer.js +56 -0
- package/lib/esm/tsconfig.tsbuildinfo +1 -1
- package/lib/esm/utils/property.js +34 -0
- package/lib/esm/utils/typescript/chunks-analyzer-transformer.js +52 -0
- package/lib/types/tsconfig.tsbuildinfo +1 -1
- package/lib/types/types/core.d.ts +1 -0
- package/lib/types/types/state.d.ts +84 -0
- package/lib/types/utils/property.d.ts +6 -0
- package/lib/types/utils/typescript/chunks-analyzer-transformer.d.ts +10 -0
- package/package.json +3 -3
|
@@ -93,6 +93,7 @@ export function parsePropertyValue(property, value, { locale, defaultLocale }) {
|
|
|
93
93
|
return undefined;
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
|
+
// type: component 存在 componentId 的时候,会复写为 RenderNestedComponent
|
|
96
97
|
if (property.type === 'component') {
|
|
97
98
|
const componentId = value?.componentId;
|
|
98
99
|
if (typeof componentId !== 'string')
|
|
@@ -117,3 +118,36 @@ export function assignNullableFields(properties, newProperties) {
|
|
|
117
118
|
}
|
|
118
119
|
return properties;
|
|
119
120
|
}
|
|
121
|
+
export function getComponentDependencies({ state, pageIds = [], componentIds = [], }) {
|
|
122
|
+
const pickComponentId = (obj, componentIds = new Set()) => {
|
|
123
|
+
if (Array.isArray(obj)) {
|
|
124
|
+
obj.forEach((i) => pickComponentId(i, componentIds));
|
|
125
|
+
}
|
|
126
|
+
if (obj && typeof obj === 'object') {
|
|
127
|
+
if (typeof obj.componentId === 'string') {
|
|
128
|
+
componentIds.add(obj.componentId);
|
|
129
|
+
}
|
|
130
|
+
Object.values(obj).forEach((i) => pickComponentId(i, componentIds));
|
|
131
|
+
}
|
|
132
|
+
return componentIds;
|
|
133
|
+
};
|
|
134
|
+
const result = new Set(componentIds);
|
|
135
|
+
for (const pageId of pageIds) {
|
|
136
|
+
const page = state.pages[pageId];
|
|
137
|
+
pickComponentId(page, result);
|
|
138
|
+
}
|
|
139
|
+
const tmp = [...result];
|
|
140
|
+
while (tmp.length) {
|
|
141
|
+
const componentId = tmp.shift();
|
|
142
|
+
if (!result.has(componentId))
|
|
143
|
+
result.add(componentId);
|
|
144
|
+
const component = state.components[componentId]?.data;
|
|
145
|
+
if (component) {
|
|
146
|
+
for (const i of pickComponentId(component)) {
|
|
147
|
+
if (!result.has(i))
|
|
148
|
+
tmp.push(i);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return [...result];
|
|
153
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { basename } from 'path';
|
|
2
|
+
import { SyntaxKind } from 'typescript';
|
|
3
|
+
import { isRelativeModule } from './builtin-module-transformer';
|
|
4
|
+
/**
|
|
5
|
+
* 直接分析文件内容,查找chunks引用
|
|
6
|
+
* @param ts TypeScript命名空间
|
|
7
|
+
* @param fileContent 文件内容
|
|
8
|
+
* @returns 找到的chunks名称数组(只包含basename)
|
|
9
|
+
*/
|
|
10
|
+
export const analyzeFileChunks = (ts, fileContent) => {
|
|
11
|
+
// 创建源文件
|
|
12
|
+
const sourceFile = ts.createSourceFile('temp.js', // 文件名不重要,只是用于创建源文件
|
|
13
|
+
fileContent, ts.ScriptTarget.Latest, true);
|
|
14
|
+
// 查找引用的chunks
|
|
15
|
+
const referencedChunks = new Set();
|
|
16
|
+
// 递归访问AST
|
|
17
|
+
const visitNode = (node) => {
|
|
18
|
+
// 检查动态导入: import('./xxx')
|
|
19
|
+
if (ts.isCallExpression(node) &&
|
|
20
|
+
node.expression.kind === SyntaxKind.ImportKeyword &&
|
|
21
|
+
node.arguments.length > 0 &&
|
|
22
|
+
ts.isStringLiteral(node.arguments[0])) {
|
|
23
|
+
const importPath = node.arguments[0].text;
|
|
24
|
+
checkAndAddChunk(importPath, referencedChunks);
|
|
25
|
+
}
|
|
26
|
+
// 检查其他调用模式: load('./xxx') 或 __vitePreload('./xxx')
|
|
27
|
+
if (ts.isCallExpression(node) &&
|
|
28
|
+
ts.isIdentifier(node.expression) &&
|
|
29
|
+
(node.expression.text.includes('load') || node.expression.text.includes('Preload')) &&
|
|
30
|
+
node.arguments.length > 0 &&
|
|
31
|
+
ts.isStringLiteral(node.arguments[0])) {
|
|
32
|
+
const chunkPath = node.arguments[0].text;
|
|
33
|
+
checkAndAddChunk(chunkPath, referencedChunks);
|
|
34
|
+
}
|
|
35
|
+
// 检查字符串字面量
|
|
36
|
+
if (ts.isStringLiteral(node)) {
|
|
37
|
+
checkAndAddChunk(node.text, referencedChunks);
|
|
38
|
+
}
|
|
39
|
+
// 继续遍历子节点
|
|
40
|
+
ts.forEachChild(node, visitNode);
|
|
41
|
+
};
|
|
42
|
+
// 检查路径是否引用了chunk并添加到结果集
|
|
43
|
+
const checkAndAddChunk = (path, results) => {
|
|
44
|
+
if (isRelativeModule(path)) {
|
|
45
|
+
results.add(basename(path));
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
// 开始遍历AST
|
|
49
|
+
visitNode(sourceFile);
|
|
50
|
+
// 只返回chunk名称(不含路径和扩展名)
|
|
51
|
+
return Array.from(referencedChunks);
|
|
52
|
+
};
|