@dimina/compiler 1.0.9 → 1.0.11
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/dist/bin/index.cjs +1 -1
- package/dist/bin/index.js +1 -1
- package/dist/core/logic-compiler.cjs +1 -4
- package/dist/core/logic-compiler.js +1 -4
- package/dist/core/style-compiler.cjs +8 -1
- package/dist/core/style-compiler.js +8 -1
- package/dist/core/view-compiler.cjs +274 -99
- package/dist/core/view-compiler.js +274 -99
- package/dist/index.cjs +4 -4
- package/package.json +14 -14
package/dist/bin/index.cjs
CHANGED
package/dist/bin/index.js
CHANGED
|
@@ -50,7 +50,14 @@ if (!node_worker_threads.isMainThread) {
|
|
|
50
50
|
}
|
|
51
51
|
node_worker_threads.parentPort.postMessage({ success: true });
|
|
52
52
|
} catch (error) {
|
|
53
|
-
node_worker_threads.parentPort.postMessage({
|
|
53
|
+
node_worker_threads.parentPort.postMessage({
|
|
54
|
+
success: false,
|
|
55
|
+
error: {
|
|
56
|
+
message: error.message,
|
|
57
|
+
stack: error.stack,
|
|
58
|
+
name: error.name
|
|
59
|
+
}
|
|
60
|
+
});
|
|
54
61
|
}
|
|
55
62
|
});
|
|
56
63
|
}
|
|
@@ -31,7 +31,14 @@ if (!isMainThread) {
|
|
|
31
31
|
}
|
|
32
32
|
parentPort.postMessage({ success: true });
|
|
33
33
|
} catch (error) {
|
|
34
|
-
parentPort.postMessage({
|
|
34
|
+
parentPort.postMessage({
|
|
35
|
+
success: false,
|
|
36
|
+
error: {
|
|
37
|
+
message: error.message,
|
|
38
|
+
stack: error.stack,
|
|
39
|
+
name: error.name
|
|
40
|
+
}
|
|
41
|
+
});
|
|
35
42
|
}
|
|
36
43
|
});
|
|
37
44
|
}
|
|
@@ -29,13 +29,117 @@ function _interopNamespaceDefault(e) {
|
|
|
29
29
|
}
|
|
30
30
|
const cheerio__namespace = /* @__PURE__ */ _interopNamespaceDefault(cheerio);
|
|
31
31
|
const htmlparser2__namespace = /* @__PURE__ */ _interopNamespaceDefault(htmlparser2);
|
|
32
|
+
const traverse$1 = _traverse.default ? _traverse.default : _traverse;
|
|
33
|
+
const KEYWORDS = /* @__PURE__ */ new Set([
|
|
34
|
+
"true",
|
|
35
|
+
"false",
|
|
36
|
+
"null",
|
|
37
|
+
"undefined",
|
|
38
|
+
"NaN",
|
|
39
|
+
"Infinity",
|
|
40
|
+
"this",
|
|
41
|
+
"arguments",
|
|
42
|
+
"Array",
|
|
43
|
+
"Object",
|
|
44
|
+
"String",
|
|
45
|
+
"Number",
|
|
46
|
+
"Boolean",
|
|
47
|
+
"Math",
|
|
48
|
+
"Date",
|
|
49
|
+
"RegExp",
|
|
50
|
+
"Error",
|
|
51
|
+
"JSON",
|
|
52
|
+
"console",
|
|
53
|
+
"window",
|
|
54
|
+
"document",
|
|
55
|
+
"parseInt",
|
|
56
|
+
"parseFloat",
|
|
57
|
+
"isNaN",
|
|
58
|
+
"isFinite",
|
|
59
|
+
"encodeURI",
|
|
60
|
+
"encodeURIComponent",
|
|
61
|
+
"decodeURI",
|
|
62
|
+
"decodeURIComponent"
|
|
63
|
+
]);
|
|
64
|
+
function extractDependencies(expression) {
|
|
65
|
+
if (!expression || typeof expression !== "string") {
|
|
66
|
+
return [];
|
|
67
|
+
}
|
|
68
|
+
const dependencies = /* @__PURE__ */ new Set();
|
|
69
|
+
try {
|
|
70
|
+
const code = `(${expression})`;
|
|
71
|
+
const ast = babel.parseSync(code, {
|
|
72
|
+
sourceType: "module",
|
|
73
|
+
plugins: []
|
|
74
|
+
});
|
|
75
|
+
traverse$1(ast, {
|
|
76
|
+
Identifier(path2) {
|
|
77
|
+
const name = path2.node.name;
|
|
78
|
+
if (KEYWORDS.has(name)) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const parent = path2.parent;
|
|
82
|
+
if (parent.type === "MemberExpression" && parent.property === path2.node && !parent.computed) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (parent.type === "CallExpression" && parent.callee === path2.node) {
|
|
86
|
+
dependencies.add(name);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (parent.type === "ObjectProperty" && parent.key === path2.node && !parent.computed) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
dependencies.add(name);
|
|
93
|
+
},
|
|
94
|
+
// 处理成员表达式,确保只提取根对象
|
|
95
|
+
MemberExpression(path2) {
|
|
96
|
+
let root = path2.node.object;
|
|
97
|
+
while (root.type === "MemberExpression") {
|
|
98
|
+
root = root.object;
|
|
99
|
+
}
|
|
100
|
+
if (root.type === "Identifier" && !KEYWORDS.has(root.name)) {
|
|
101
|
+
dependencies.add(root.name);
|
|
102
|
+
}
|
|
103
|
+
path2.skip();
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
} catch (error) {
|
|
107
|
+
console.warn("[expression-parser] AST 解析失败,表达式:", expression, "错误:", error.message);
|
|
108
|
+
return [];
|
|
109
|
+
}
|
|
110
|
+
return Array.from(dependencies);
|
|
111
|
+
}
|
|
112
|
+
function parseExpression(expression) {
|
|
113
|
+
if (!expression || typeof expression !== "string") {
|
|
114
|
+
return {
|
|
115
|
+
expression: "",
|
|
116
|
+
dependencies: [],
|
|
117
|
+
isSimple: true
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
const dependencies = extractDependencies(expression);
|
|
121
|
+
const isSimple = dependencies.length === 1 && expression.trim() === dependencies[0];
|
|
122
|
+
return {
|
|
123
|
+
expression: expression.trim(),
|
|
124
|
+
dependencies,
|
|
125
|
+
isSimple
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
function parseBindings(bindings) {
|
|
129
|
+
if (!bindings || typeof bindings !== "object") {
|
|
130
|
+
return {};
|
|
131
|
+
}
|
|
132
|
+
const parsed = {};
|
|
133
|
+
for (const [propName, expression] of Object.entries(bindings)) {
|
|
134
|
+
parsed[propName] = parseExpression(expression);
|
|
135
|
+
}
|
|
136
|
+
return parsed;
|
|
137
|
+
}
|
|
32
138
|
const traverse = _traverse.default ? _traverse.default : _traverse;
|
|
33
139
|
const fileType = [".wxml", ".ddml"];
|
|
34
140
|
const compileResCache = /* @__PURE__ */ new Map();
|
|
35
141
|
const wxsModuleRegistry = /* @__PURE__ */ new Set();
|
|
36
142
|
const wxsFilePathMap = /* @__PURE__ */ new Map();
|
|
37
|
-
const moduleCompileStatus = /* @__PURE__ */ new Map();
|
|
38
|
-
const moduleCompileResults = /* @__PURE__ */ new Map();
|
|
39
143
|
if (!node_worker_threads.isMainThread) {
|
|
40
144
|
node_worker_threads.parentPort.on("message", async ({ pages, storeInfo }) => {
|
|
41
145
|
try {
|
|
@@ -56,20 +160,19 @@ if (!node_worker_threads.isMainThread) {
|
|
|
56
160
|
}
|
|
57
161
|
node_worker_threads.parentPort.postMessage({ success: true });
|
|
58
162
|
} catch (error) {
|
|
59
|
-
node_worker_threads.parentPort.postMessage({
|
|
163
|
+
node_worker_threads.parentPort.postMessage({
|
|
164
|
+
success: false,
|
|
165
|
+
error: {
|
|
166
|
+
message: error.message,
|
|
167
|
+
stack: error.stack,
|
|
168
|
+
name: error.name
|
|
169
|
+
}
|
|
170
|
+
});
|
|
60
171
|
}
|
|
61
172
|
});
|
|
62
173
|
}
|
|
63
|
-
function clearCompileState() {
|
|
64
|
-
moduleCompileStatus.clear();
|
|
65
|
-
moduleCompileResults.clear();
|
|
66
|
-
compileResCache.clear();
|
|
67
|
-
wxsModuleRegistry.clear();
|
|
68
|
-
wxsFilePathMap.clear();
|
|
69
|
-
}
|
|
70
174
|
async function compileML(pages, root, progress) {
|
|
71
175
|
const workPath = env.getWorkPath();
|
|
72
|
-
clearCompileState();
|
|
73
176
|
initWxsFilePathMap(workPath);
|
|
74
177
|
for (const page of pages) {
|
|
75
178
|
const scriptRes = /* @__PURE__ */ new Map();
|
|
@@ -135,71 +238,49 @@ function isRegisteredWxsModule(modulePath) {
|
|
|
135
238
|
}
|
|
136
239
|
function buildCompileView(module2, isComponent = false, scriptRes, depthChain = []) {
|
|
137
240
|
const currentPath = module2.path;
|
|
138
|
-
if (moduleCompileStatus.has(currentPath)) {
|
|
139
|
-
const status = moduleCompileStatus.get(currentPath);
|
|
140
|
-
if (status === "pending") {
|
|
141
|
-
console.warn("[view]", `检测到循环依赖: ${[...depthChain, currentPath].join(" -> ")}`);
|
|
142
|
-
return { scriptModule: [] };
|
|
143
|
-
} else if (status === "completed") {
|
|
144
|
-
return moduleCompileResults.get(currentPath) || { scriptModule: [] };
|
|
145
|
-
} else if (status === "failed") {
|
|
146
|
-
console.warn("[view]", `模块编译失败,跳过: ${currentPath}`);
|
|
147
|
-
return { scriptModule: [] };
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
241
|
if (depthChain.includes(currentPath)) {
|
|
151
242
|
console.warn("[view]", `检测到循环依赖: ${[...depthChain, currentPath].join(" -> ")}`);
|
|
152
|
-
return
|
|
243
|
+
return;
|
|
153
244
|
}
|
|
154
|
-
if (depthChain.length >
|
|
155
|
-
console.warn("[view]",
|
|
156
|
-
return
|
|
245
|
+
if (depthChain.length > 20) {
|
|
246
|
+
console.warn("[view]", `检测到深度依赖: ${[...depthChain, currentPath].join(" -> ")}`);
|
|
247
|
+
return;
|
|
157
248
|
}
|
|
158
|
-
moduleCompileStatus.set(currentPath, "pending");
|
|
159
249
|
depthChain = [...depthChain, currentPath];
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
allScriptModules.push(sm);
|
|
181
|
-
}
|
|
250
|
+
const allScriptModules = [];
|
|
251
|
+
const currentInstruction = compileModule(module2, isComponent, scriptRes);
|
|
252
|
+
if (currentInstruction && currentInstruction.scriptModule) {
|
|
253
|
+
allScriptModules.push(...currentInstruction.scriptModule);
|
|
254
|
+
}
|
|
255
|
+
if (module2.usingComponents) {
|
|
256
|
+
for (const componentInfo of Object.values(module2.usingComponents)) {
|
|
257
|
+
const componentModule = env.getComponent(componentInfo);
|
|
258
|
+
if (!componentModule) {
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
if (componentModule.path === module2.path) {
|
|
262
|
+
console.warn("[view]", `检测到循环依赖,跳过处理: ${module2.path}`);
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
265
|
+
const componentInstruction = buildCompileView(componentModule, true, scriptRes, depthChain);
|
|
266
|
+
if (componentInstruction && componentInstruction.scriptModule) {
|
|
267
|
+
for (const sm of componentInstruction.scriptModule) {
|
|
268
|
+
if (!allScriptModules.find((existing) => existing.path === sm.path)) {
|
|
269
|
+
allScriptModules.push(sm);
|
|
182
270
|
}
|
|
183
271
|
}
|
|
184
272
|
}
|
|
185
273
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
274
|
+
}
|
|
275
|
+
if (!isComponent && allScriptModules.length > 0) {
|
|
276
|
+
for (const sm of allScriptModules) {
|
|
277
|
+
if (!scriptRes.has(sm.path)) {
|
|
278
|
+
scriptRes.set(sm.path, sm.code);
|
|
191
279
|
}
|
|
192
|
-
compileModuleWithAllWxs(module2, scriptRes, allScriptModules);
|
|
193
280
|
}
|
|
194
|
-
|
|
195
|
-
moduleCompileStatus.set(currentPath, "completed");
|
|
196
|
-
moduleCompileResults.set(currentPath, result);
|
|
197
|
-
return result;
|
|
198
|
-
} catch (error) {
|
|
199
|
-
moduleCompileStatus.set(currentPath, "failed");
|
|
200
|
-
console.error("[view]", `模块编译失败: ${currentPath}`, error.message);
|
|
201
|
-
return { scriptModule: [] };
|
|
281
|
+
compileModuleWithAllWxs(module2, scriptRes, allScriptModules);
|
|
202
282
|
}
|
|
283
|
+
return { scriptModule: allScriptModules };
|
|
203
284
|
}
|
|
204
285
|
function compileModule(module2, isComponent, scriptRes) {
|
|
205
286
|
const { tpl, instruction } = toCompileTemplate(isComponent, module2.path, module2.usingComponents, module2.componentPlaceholder);
|
|
@@ -277,20 +358,22 @@ function compileModule(module2, isComponent, scriptRes) {
|
|
|
277
358
|
const ast = babel.parseSync(code2);
|
|
278
359
|
insertWxsToRenderAst(ast, instruction.scriptModule, scriptRes);
|
|
279
360
|
code2 = babel.transformFromAstSync(ast, "", {
|
|
280
|
-
comments: false
|
|
361
|
+
comments: false,
|
|
362
|
+
sourceType: "script"
|
|
281
363
|
}).code;
|
|
282
|
-
tplComponents += `'${tm.path}':${code2.replace(/;$/, "")
|
|
364
|
+
tplComponents += `'${tm.path}':${code2.replace(/;$/, "")},`;
|
|
283
365
|
}
|
|
284
366
|
tplComponents += "}";
|
|
285
367
|
const tplAst = babel.parseSync(tplCode.code);
|
|
286
368
|
insertWxsToRenderAst(tplAst, instruction.scriptModule, scriptRes);
|
|
287
369
|
const { code: transCode } = babel.transformFromAstSync(tplAst, "", {
|
|
288
|
-
comments: false
|
|
370
|
+
comments: false,
|
|
371
|
+
sourceType: "script"
|
|
289
372
|
});
|
|
290
373
|
const code = `Module({
|
|
291
374
|
path: '${module2.path}',
|
|
292
375
|
id: '${module2.id}',
|
|
293
|
-
render: ${transCode.replace(/;$/, "")
|
|
376
|
+
render: ${transCode.replace(/;$/, "")},
|
|
294
377
|
usingComponents: ${JSON.stringify(module2.usingComponents)},
|
|
295
378
|
tplComponents: ${tplComponents},
|
|
296
379
|
});`;
|
|
@@ -417,7 +500,8 @@ function processWxsContent(wxsContent, wxsFilePath, scriptModule, workPath, file
|
|
|
417
500
|
}
|
|
418
501
|
});
|
|
419
502
|
return babel.transformFromAstSync(wxsAst, "", {
|
|
420
|
-
comments: false
|
|
503
|
+
comments: false,
|
|
504
|
+
sourceType: "script"
|
|
421
505
|
}).code;
|
|
422
506
|
}
|
|
423
507
|
function isWxsModuleByContent(moduleCode, modulePath = "") {
|
|
@@ -491,7 +575,8 @@ function compileModuleWithAllWxs(module2, scriptRes, allScriptModules) {
|
|
|
491
575
|
const ast = babel.parseSync(code2);
|
|
492
576
|
insertWxsToRenderAst(ast, allScriptModules, scriptRes);
|
|
493
577
|
code2 = babel.transformFromAstSync(ast, "", {
|
|
494
|
-
comments: false
|
|
578
|
+
comments: false,
|
|
579
|
+
sourceType: "script"
|
|
495
580
|
}).code;
|
|
496
581
|
tplComponents += `'${tm.path}':${code2.replace(/;$/, "").replace(/^"use strict";\s*/, "")},`;
|
|
497
582
|
}
|
|
@@ -499,7 +584,8 @@ function compileModuleWithAllWxs(module2, scriptRes, allScriptModules) {
|
|
|
499
584
|
const tplAst = babel.parseSync(tplCode.code);
|
|
500
585
|
insertWxsToRenderAst(tplAst, allScriptModules, scriptRes);
|
|
501
586
|
const { code: transCode } = babel.transformFromAstSync(tplAst, "", {
|
|
502
|
-
comments: false
|
|
587
|
+
comments: false,
|
|
588
|
+
sourceType: "script"
|
|
503
589
|
});
|
|
504
590
|
const code = `Module({
|
|
505
591
|
path: '${module2.path}',
|
|
@@ -515,10 +601,66 @@ function compileModuleWithAllWxs(module2, scriptRes, allScriptModules) {
|
|
|
515
601
|
compileResCache.set(module2.path, cacheData);
|
|
516
602
|
scriptRes.set(module2.path, code);
|
|
517
603
|
}
|
|
518
|
-
function
|
|
519
|
-
|
|
604
|
+
function processIncludeConditionalAttrs($, elem, includeContent) {
|
|
605
|
+
const allAttrs = $(elem).attr();
|
|
606
|
+
const conditionAttrs = {};
|
|
607
|
+
let hasCondition = false;
|
|
608
|
+
for (const attrName in allAttrs) {
|
|
609
|
+
if (attrName.endsWith(":if") || attrName.endsWith(":elif") || attrName.endsWith(":else")) {
|
|
610
|
+
conditionAttrs[attrName] = allAttrs[attrName];
|
|
611
|
+
hasCondition = true;
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
if (hasCondition) {
|
|
615
|
+
let blockAttrs = "";
|
|
616
|
+
for (const attrName in conditionAttrs) {
|
|
617
|
+
const attrValue = conditionAttrs[attrName];
|
|
618
|
+
if (attrValue !== void 0 && attrValue !== "") {
|
|
619
|
+
blockAttrs += ` ${attrName}="${attrValue}"`;
|
|
620
|
+
} else {
|
|
621
|
+
blockAttrs += ` ${attrName}`;
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
return `<block${blockAttrs}>${includeContent}</block>`;
|
|
625
|
+
} else {
|
|
626
|
+
return includeContent;
|
|
627
|
+
}
|
|
520
628
|
}
|
|
521
|
-
function
|
|
629
|
+
function processIncludedFileWxsDependencies(content, includePath, scriptModule, components, processedPaths = /* @__PURE__ */ new Set()) {
|
|
630
|
+
if (processedPaths.has(includePath)) {
|
|
631
|
+
return;
|
|
632
|
+
}
|
|
633
|
+
processedPaths.add(includePath);
|
|
634
|
+
const $ = cheerio__namespace.load(content, {
|
|
635
|
+
xmlMode: true,
|
|
636
|
+
decodeEntities: false
|
|
637
|
+
});
|
|
638
|
+
const componentTags = /* @__PURE__ */ new Set();
|
|
639
|
+
$("*").each((_, elem) => {
|
|
640
|
+
const tagName = elem.tagName;
|
|
641
|
+
if (components && components[tagName]) {
|
|
642
|
+
componentTags.add(tagName);
|
|
643
|
+
}
|
|
644
|
+
});
|
|
645
|
+
for (const tagName of componentTags) {
|
|
646
|
+
const componentPath = components[tagName];
|
|
647
|
+
const componentModule = env.getComponent(componentPath);
|
|
648
|
+
if (componentModule) {
|
|
649
|
+
if (processedPaths.has(componentModule.path)) {
|
|
650
|
+
continue;
|
|
651
|
+
}
|
|
652
|
+
const componentTemplate = toCompileTemplate(true, componentModule.path, componentModule.usingComponents, componentModule.componentPlaceholder, processedPaths);
|
|
653
|
+
if (componentTemplate && componentTemplate.instruction && componentTemplate.instruction.scriptModule) {
|
|
654
|
+
for (const sm of componentTemplate.instruction.scriptModule) {
|
|
655
|
+
if (!scriptModule.find((existing) => existing.path === sm.path)) {
|
|
656
|
+
scriptModule.push(sm);
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
function toCompileTemplate(isComponent, path2, components, componentPlaceholder, processedPaths = /* @__PURE__ */ new Set()) {
|
|
522
664
|
const workPath = env.getWorkPath();
|
|
523
665
|
const fullPath = getViewPath(workPath, path2);
|
|
524
666
|
if (!fullPath) {
|
|
@@ -573,11 +715,12 @@ function toCompileTemplate(isComponent, path2, components, componentPlaceholder)
|
|
|
573
715
|
scriptModule,
|
|
574
716
|
includePath
|
|
575
717
|
);
|
|
576
|
-
processIncludedFileWxsDependencies(includeContent, includePath);
|
|
718
|
+
processIncludedFileWxsDependencies(includeContent, includePath, scriptModule, components, processedPaths);
|
|
577
719
|
$includeContent("template").remove();
|
|
578
720
|
$includeContent("wxs").remove();
|
|
579
721
|
$includeContent("dds").remove();
|
|
580
|
-
|
|
722
|
+
const processedContent = processIncludeConditionalAttrs($, elem, $includeContent.html());
|
|
723
|
+
$(elem).replaceWith(processedContent);
|
|
581
724
|
} else {
|
|
582
725
|
$(elem).remove();
|
|
583
726
|
}
|
|
@@ -613,7 +756,7 @@ function toCompileTemplate(isComponent, path2, components, componentPlaceholder)
|
|
|
613
756
|
scriptModule,
|
|
614
757
|
importPath
|
|
615
758
|
);
|
|
616
|
-
processIncludedFileWxsDependencies(importContent, importPath);
|
|
759
|
+
processIncludedFileWxsDependencies(importContent, importPath, scriptModule, components, processedPaths);
|
|
617
760
|
}
|
|
618
761
|
}
|
|
619
762
|
});
|
|
@@ -694,9 +837,10 @@ function transTag(opts) {
|
|
|
694
837
|
res = `dd-${tag}`;
|
|
695
838
|
}
|
|
696
839
|
let tagRes;
|
|
697
|
-
const propsAry = isStart ? getProps(attrs, tag) : [];
|
|
840
|
+
const propsAry = isStart ? getProps(attrs, tag, components) : [];
|
|
698
841
|
const multipleSlots = attrs?.slot;
|
|
699
842
|
if (attrs?.slot) {
|
|
843
|
+
const isDynamicSlot = isWrappedByBraces(multipleSlots);
|
|
700
844
|
if (isStart) {
|
|
701
845
|
const withVIf = [];
|
|
702
846
|
const withoutVIf = [];
|
|
@@ -704,13 +848,24 @@ function transTag(opts) {
|
|
|
704
848
|
const prop = propsAry[i];
|
|
705
849
|
if (prop.includes("v-if") || prop.includes("v-else-if") || prop.includes("v-else")) {
|
|
706
850
|
withVIf.push(prop);
|
|
707
|
-
} else {
|
|
851
|
+
} else if (!prop.includes("slot")) {
|
|
708
852
|
withoutVIf.push(prop);
|
|
709
853
|
}
|
|
710
854
|
}
|
|
711
|
-
|
|
855
|
+
const vIfProps = withVIf.length > 0 ? `${withVIf.join(" ")} ` : "";
|
|
856
|
+
const vOtherProps = withoutVIf.length > 0 ? ` ${withoutVIf.join(" ")}` : "";
|
|
857
|
+
const templateContent = `<template ${vIfProps}${generateSlotDirective(multipleSlots)}><${res}${vOtherProps}>`;
|
|
858
|
+
if (isDynamicSlot) {
|
|
859
|
+
tagRes = `<dd-block>${templateContent}`;
|
|
860
|
+
} else {
|
|
861
|
+
tagRes = templateContent;
|
|
862
|
+
}
|
|
712
863
|
} else {
|
|
713
|
-
|
|
864
|
+
if (isDynamicSlot) {
|
|
865
|
+
tagRes = `</${res}></template></dd-block>`;
|
|
866
|
+
} else {
|
|
867
|
+
tagRes = `</${res}></template>`;
|
|
868
|
+
}
|
|
714
869
|
}
|
|
715
870
|
} else {
|
|
716
871
|
if (isStart) {
|
|
@@ -722,8 +877,17 @@ function transTag(opts) {
|
|
|
722
877
|
}
|
|
723
878
|
return tagRes;
|
|
724
879
|
}
|
|
725
|
-
function
|
|
880
|
+
function generateSlotDirective(slotValue) {
|
|
881
|
+
if (isWrappedByBraces(slotValue)) {
|
|
882
|
+
const slotExpression = parseBraceExp(slotValue);
|
|
883
|
+
return `#[${slotExpression}]`;
|
|
884
|
+
} else {
|
|
885
|
+
return `#${slotValue}`;
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
function getProps(attrs, tag, components) {
|
|
726
889
|
const attrsList = [];
|
|
890
|
+
const propBindings = {};
|
|
727
891
|
Object.entries(attrs).forEach(([name, value]) => {
|
|
728
892
|
if (name.endsWith(":if")) {
|
|
729
893
|
attrsList.push({
|
|
@@ -822,6 +986,11 @@ function getProps(attrs, tag) {
|
|
|
822
986
|
if (tag === "template" && name === "data") {
|
|
823
987
|
pVal = `{${pVal}}`;
|
|
824
988
|
}
|
|
989
|
+
if (components && components[tag]) {
|
|
990
|
+
if (pVal && typeof pVal === "string") {
|
|
991
|
+
propBindings[name] = pVal;
|
|
992
|
+
}
|
|
993
|
+
}
|
|
825
994
|
attrsList.push({
|
|
826
995
|
name: `:${name}`,
|
|
827
996
|
value: pVal
|
|
@@ -844,6 +1013,24 @@ function getProps(attrs, tag) {
|
|
|
844
1013
|
propsRes.push(`${name}="${escapeQuotes(value)}"`);
|
|
845
1014
|
}
|
|
846
1015
|
});
|
|
1016
|
+
if (components && components[tag] && Object.keys(propBindings).length > 0) {
|
|
1017
|
+
try {
|
|
1018
|
+
const validBindings = {};
|
|
1019
|
+
for (const [key, value] of Object.entries(propBindings)) {
|
|
1020
|
+
if (value !== void 0 && value !== null && typeof value === "string") {
|
|
1021
|
+
validBindings[key] = value;
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
if (Object.keys(validBindings).length > 0) {
|
|
1025
|
+
const parsedBindings = parseBindings(validBindings);
|
|
1026
|
+
const bindingsJson = JSON.stringify(parsedBindings);
|
|
1027
|
+
const escapedJson = bindingsJson.replace(/"/g, """);
|
|
1028
|
+
propsRes.push(`v-c-prop-bindings="${escapedJson}"`);
|
|
1029
|
+
}
|
|
1030
|
+
} catch (error) {
|
|
1031
|
+
console.warn("[compiler] 序列化 propBindings 失败:", error.message, "标签:", tag, "绑定数据:", propBindings);
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
847
1034
|
return propsRes;
|
|
848
1035
|
}
|
|
849
1036
|
function generateVModelTemplate(expression) {
|
|
@@ -1060,11 +1247,7 @@ function transTagWxs($, scriptModule, filePath) {
|
|
|
1060
1247
|
});
|
|
1061
1248
|
wxsNodes.remove();
|
|
1062
1249
|
}
|
|
1063
|
-
function collectAllWxsModules(scriptRes, collectedPaths = /* @__PURE__ */ new Set(), scriptModule = []
|
|
1064
|
-
if (maxDepth <= 0) {
|
|
1065
|
-
console.warn("[view]", "collectAllWxsModules 达到最大递归深度,停止处理");
|
|
1066
|
-
return [];
|
|
1067
|
-
}
|
|
1250
|
+
function collectAllWxsModules(scriptRes, collectedPaths = /* @__PURE__ */ new Set(), scriptModule = []) {
|
|
1068
1251
|
const allWxsModules = [];
|
|
1069
1252
|
const workPath = env.getWorkPath();
|
|
1070
1253
|
for (const [modulePath, moduleCode] of scriptRes.entries()) {
|
|
@@ -1081,12 +1264,7 @@ function collectAllWxsModules(scriptRes, collectedPaths = /* @__PURE__ */ new Se
|
|
|
1081
1264
|
for (const depPath of dependencies) {
|
|
1082
1265
|
if (!collectedPaths.has(depPath)) {
|
|
1083
1266
|
if (scriptRes.has(depPath)) {
|
|
1084
|
-
const depModules = collectAllWxsModules(
|
|
1085
|
-
/* @__PURE__ */ new Map([[depPath, scriptRes.get(depPath)]]),
|
|
1086
|
-
collectedPaths,
|
|
1087
|
-
scriptModule,
|
|
1088
|
-
maxDepth - 1
|
|
1089
|
-
);
|
|
1267
|
+
const depModules = collectAllWxsModules(/* @__PURE__ */ new Map([[depPath, scriptRes.get(depPath)]]), collectedPaths, scriptModule);
|
|
1090
1268
|
allWxsModules.push(...depModules);
|
|
1091
1269
|
} else {
|
|
1092
1270
|
const loaded = loadWxsModule(depPath, workPath, scriptModule);
|
|
@@ -1094,12 +1272,7 @@ function collectAllWxsModules(scriptRes, collectedPaths = /* @__PURE__ */ new Se
|
|
|
1094
1272
|
scriptRes.set(depPath, loaded.code);
|
|
1095
1273
|
allWxsModules.push(loaded);
|
|
1096
1274
|
collectedPaths.add(depPath);
|
|
1097
|
-
const depModules = collectAllWxsModules(
|
|
1098
|
-
/* @__PURE__ */ new Map([[depPath, loaded.code]]),
|
|
1099
|
-
collectedPaths,
|
|
1100
|
-
scriptModule,
|
|
1101
|
-
maxDepth - 1
|
|
1102
|
-
);
|
|
1275
|
+
const depModules = collectAllWxsModules(/* @__PURE__ */ new Map([[depPath, loaded.code]]), collectedPaths, scriptModule);
|
|
1103
1276
|
allWxsModules.push(...depModules);
|
|
1104
1277
|
}
|
|
1105
1278
|
}
|
|
@@ -1177,9 +1350,11 @@ function insertWxsToRenderAst(ast, scriptModule, scriptRes) {
|
|
|
1177
1350
|
}
|
|
1178
1351
|
}
|
|
1179
1352
|
exports.compileML = compileML;
|
|
1353
|
+
exports.generateSlotDirective = generateSlotDirective;
|
|
1180
1354
|
exports.generateVModelTemplate = generateVModelTemplate;
|
|
1181
1355
|
exports.parseBraceExp = parseBraceExp;
|
|
1182
1356
|
exports.parseClassRules = parseClassRules;
|
|
1183
1357
|
exports.parseKeyExpression = parseKeyExpression;
|
|
1358
|
+
exports.processIncludeConditionalAttrs = processIncludeConditionalAttrs;
|
|
1184
1359
|
exports.processWxsContent = processWxsContent;
|
|
1185
1360
|
exports.splitWithBraces = splitWithBraces;
|
|
@@ -9,13 +9,117 @@ import * as cheerio from "cheerio";
|
|
|
9
9
|
import { transform } from "esbuild";
|
|
10
10
|
import * as htmlparser2 from "htmlparser2";
|
|
11
11
|
import { r as resetStoreInfo, g as getTargetPath, a as getComponent, b as getContentByPath, c as getAbsolutePath, d as getWorkPath, e as collectAssets, f as getAppId, t as transformRpx, h as tagWhiteList } from "../env-Csj3AHY4.js";
|
|
12
|
+
const traverse$1 = _traverse.default ? _traverse.default : _traverse;
|
|
13
|
+
const KEYWORDS = /* @__PURE__ */ new Set([
|
|
14
|
+
"true",
|
|
15
|
+
"false",
|
|
16
|
+
"null",
|
|
17
|
+
"undefined",
|
|
18
|
+
"NaN",
|
|
19
|
+
"Infinity",
|
|
20
|
+
"this",
|
|
21
|
+
"arguments",
|
|
22
|
+
"Array",
|
|
23
|
+
"Object",
|
|
24
|
+
"String",
|
|
25
|
+
"Number",
|
|
26
|
+
"Boolean",
|
|
27
|
+
"Math",
|
|
28
|
+
"Date",
|
|
29
|
+
"RegExp",
|
|
30
|
+
"Error",
|
|
31
|
+
"JSON",
|
|
32
|
+
"console",
|
|
33
|
+
"window",
|
|
34
|
+
"document",
|
|
35
|
+
"parseInt",
|
|
36
|
+
"parseFloat",
|
|
37
|
+
"isNaN",
|
|
38
|
+
"isFinite",
|
|
39
|
+
"encodeURI",
|
|
40
|
+
"encodeURIComponent",
|
|
41
|
+
"decodeURI",
|
|
42
|
+
"decodeURIComponent"
|
|
43
|
+
]);
|
|
44
|
+
function extractDependencies(expression) {
|
|
45
|
+
if (!expression || typeof expression !== "string") {
|
|
46
|
+
return [];
|
|
47
|
+
}
|
|
48
|
+
const dependencies = /* @__PURE__ */ new Set();
|
|
49
|
+
try {
|
|
50
|
+
const code = `(${expression})`;
|
|
51
|
+
const ast = babel.parseSync(code, {
|
|
52
|
+
sourceType: "module",
|
|
53
|
+
plugins: []
|
|
54
|
+
});
|
|
55
|
+
traverse$1(ast, {
|
|
56
|
+
Identifier(path2) {
|
|
57
|
+
const name = path2.node.name;
|
|
58
|
+
if (KEYWORDS.has(name)) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const parent = path2.parent;
|
|
62
|
+
if (parent.type === "MemberExpression" && parent.property === path2.node && !parent.computed) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
if (parent.type === "CallExpression" && parent.callee === path2.node) {
|
|
66
|
+
dependencies.add(name);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (parent.type === "ObjectProperty" && parent.key === path2.node && !parent.computed) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
dependencies.add(name);
|
|
73
|
+
},
|
|
74
|
+
// 处理成员表达式,确保只提取根对象
|
|
75
|
+
MemberExpression(path2) {
|
|
76
|
+
let root = path2.node.object;
|
|
77
|
+
while (root.type === "MemberExpression") {
|
|
78
|
+
root = root.object;
|
|
79
|
+
}
|
|
80
|
+
if (root.type === "Identifier" && !KEYWORDS.has(root.name)) {
|
|
81
|
+
dependencies.add(root.name);
|
|
82
|
+
}
|
|
83
|
+
path2.skip();
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
} catch (error) {
|
|
87
|
+
console.warn("[expression-parser] AST 解析失败,表达式:", expression, "错误:", error.message);
|
|
88
|
+
return [];
|
|
89
|
+
}
|
|
90
|
+
return Array.from(dependencies);
|
|
91
|
+
}
|
|
92
|
+
function parseExpression(expression) {
|
|
93
|
+
if (!expression || typeof expression !== "string") {
|
|
94
|
+
return {
|
|
95
|
+
expression: "",
|
|
96
|
+
dependencies: [],
|
|
97
|
+
isSimple: true
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
const dependencies = extractDependencies(expression);
|
|
101
|
+
const isSimple = dependencies.length === 1 && expression.trim() === dependencies[0];
|
|
102
|
+
return {
|
|
103
|
+
expression: expression.trim(),
|
|
104
|
+
dependencies,
|
|
105
|
+
isSimple
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
function parseBindings(bindings) {
|
|
109
|
+
if (!bindings || typeof bindings !== "object") {
|
|
110
|
+
return {};
|
|
111
|
+
}
|
|
112
|
+
const parsed = {};
|
|
113
|
+
for (const [propName, expression] of Object.entries(bindings)) {
|
|
114
|
+
parsed[propName] = parseExpression(expression);
|
|
115
|
+
}
|
|
116
|
+
return parsed;
|
|
117
|
+
}
|
|
12
118
|
const traverse = _traverse.default ? _traverse.default : _traverse;
|
|
13
119
|
const fileType = [".wxml", ".ddml"];
|
|
14
120
|
const compileResCache = /* @__PURE__ */ new Map();
|
|
15
121
|
const wxsModuleRegistry = /* @__PURE__ */ new Set();
|
|
16
122
|
const wxsFilePathMap = /* @__PURE__ */ new Map();
|
|
17
|
-
const moduleCompileStatus = /* @__PURE__ */ new Map();
|
|
18
|
-
const moduleCompileResults = /* @__PURE__ */ new Map();
|
|
19
123
|
if (!isMainThread) {
|
|
20
124
|
parentPort.on("message", async ({ pages, storeInfo }) => {
|
|
21
125
|
try {
|
|
@@ -36,20 +140,19 @@ if (!isMainThread) {
|
|
|
36
140
|
}
|
|
37
141
|
parentPort.postMessage({ success: true });
|
|
38
142
|
} catch (error) {
|
|
39
|
-
parentPort.postMessage({
|
|
143
|
+
parentPort.postMessage({
|
|
144
|
+
success: false,
|
|
145
|
+
error: {
|
|
146
|
+
message: error.message,
|
|
147
|
+
stack: error.stack,
|
|
148
|
+
name: error.name
|
|
149
|
+
}
|
|
150
|
+
});
|
|
40
151
|
}
|
|
41
152
|
});
|
|
42
153
|
}
|
|
43
|
-
function clearCompileState() {
|
|
44
|
-
moduleCompileStatus.clear();
|
|
45
|
-
moduleCompileResults.clear();
|
|
46
|
-
compileResCache.clear();
|
|
47
|
-
wxsModuleRegistry.clear();
|
|
48
|
-
wxsFilePathMap.clear();
|
|
49
|
-
}
|
|
50
154
|
async function compileML(pages, root, progress) {
|
|
51
155
|
const workPath = getWorkPath();
|
|
52
|
-
clearCompileState();
|
|
53
156
|
initWxsFilePathMap(workPath);
|
|
54
157
|
for (const page of pages) {
|
|
55
158
|
const scriptRes = /* @__PURE__ */ new Map();
|
|
@@ -115,71 +218,49 @@ function isRegisteredWxsModule(modulePath) {
|
|
|
115
218
|
}
|
|
116
219
|
function buildCompileView(module, isComponent = false, scriptRes, depthChain = []) {
|
|
117
220
|
const currentPath = module.path;
|
|
118
|
-
if (moduleCompileStatus.has(currentPath)) {
|
|
119
|
-
const status = moduleCompileStatus.get(currentPath);
|
|
120
|
-
if (status === "pending") {
|
|
121
|
-
console.warn("[view]", `检测到循环依赖: ${[...depthChain, currentPath].join(" -> ")}`);
|
|
122
|
-
return { scriptModule: [] };
|
|
123
|
-
} else if (status === "completed") {
|
|
124
|
-
return moduleCompileResults.get(currentPath) || { scriptModule: [] };
|
|
125
|
-
} else if (status === "failed") {
|
|
126
|
-
console.warn("[view]", `模块编译失败,跳过: ${currentPath}`);
|
|
127
|
-
return { scriptModule: [] };
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
221
|
if (depthChain.includes(currentPath)) {
|
|
131
222
|
console.warn("[view]", `检测到循环依赖: ${[...depthChain, currentPath].join(" -> ")}`);
|
|
132
|
-
return
|
|
223
|
+
return;
|
|
133
224
|
}
|
|
134
|
-
if (depthChain.length >
|
|
135
|
-
console.warn("[view]",
|
|
136
|
-
return
|
|
225
|
+
if (depthChain.length > 20) {
|
|
226
|
+
console.warn("[view]", `检测到深度依赖: ${[...depthChain, currentPath].join(" -> ")}`);
|
|
227
|
+
return;
|
|
137
228
|
}
|
|
138
|
-
moduleCompileStatus.set(currentPath, "pending");
|
|
139
229
|
depthChain = [...depthChain, currentPath];
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
allScriptModules.push(sm);
|
|
161
|
-
}
|
|
230
|
+
const allScriptModules = [];
|
|
231
|
+
const currentInstruction = compileModule(module, isComponent, scriptRes);
|
|
232
|
+
if (currentInstruction && currentInstruction.scriptModule) {
|
|
233
|
+
allScriptModules.push(...currentInstruction.scriptModule);
|
|
234
|
+
}
|
|
235
|
+
if (module.usingComponents) {
|
|
236
|
+
for (const componentInfo of Object.values(module.usingComponents)) {
|
|
237
|
+
const componentModule = getComponent(componentInfo);
|
|
238
|
+
if (!componentModule) {
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
if (componentModule.path === module.path) {
|
|
242
|
+
console.warn("[view]", `检测到循环依赖,跳过处理: ${module.path}`);
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
const componentInstruction = buildCompileView(componentModule, true, scriptRes, depthChain);
|
|
246
|
+
if (componentInstruction && componentInstruction.scriptModule) {
|
|
247
|
+
for (const sm of componentInstruction.scriptModule) {
|
|
248
|
+
if (!allScriptModules.find((existing) => existing.path === sm.path)) {
|
|
249
|
+
allScriptModules.push(sm);
|
|
162
250
|
}
|
|
163
251
|
}
|
|
164
252
|
}
|
|
165
253
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
254
|
+
}
|
|
255
|
+
if (!isComponent && allScriptModules.length > 0) {
|
|
256
|
+
for (const sm of allScriptModules) {
|
|
257
|
+
if (!scriptRes.has(sm.path)) {
|
|
258
|
+
scriptRes.set(sm.path, sm.code);
|
|
171
259
|
}
|
|
172
|
-
compileModuleWithAllWxs(module, scriptRes, allScriptModules);
|
|
173
260
|
}
|
|
174
|
-
|
|
175
|
-
moduleCompileStatus.set(currentPath, "completed");
|
|
176
|
-
moduleCompileResults.set(currentPath, result);
|
|
177
|
-
return result;
|
|
178
|
-
} catch (error) {
|
|
179
|
-
moduleCompileStatus.set(currentPath, "failed");
|
|
180
|
-
console.error("[view]", `模块编译失败: ${currentPath}`, error.message);
|
|
181
|
-
return { scriptModule: [] };
|
|
261
|
+
compileModuleWithAllWxs(module, scriptRes, allScriptModules);
|
|
182
262
|
}
|
|
263
|
+
return { scriptModule: allScriptModules };
|
|
183
264
|
}
|
|
184
265
|
function compileModule(module, isComponent, scriptRes) {
|
|
185
266
|
const { tpl, instruction } = toCompileTemplate(isComponent, module.path, module.usingComponents, module.componentPlaceholder);
|
|
@@ -257,20 +338,22 @@ function compileModule(module, isComponent, scriptRes) {
|
|
|
257
338
|
const ast = babel.parseSync(code2);
|
|
258
339
|
insertWxsToRenderAst(ast, instruction.scriptModule, scriptRes);
|
|
259
340
|
code2 = babel.transformFromAstSync(ast, "", {
|
|
260
|
-
comments: false
|
|
341
|
+
comments: false,
|
|
342
|
+
sourceType: "script"
|
|
261
343
|
}).code;
|
|
262
|
-
tplComponents += `'${tm.path}':${code2.replace(/;$/, "")
|
|
344
|
+
tplComponents += `'${tm.path}':${code2.replace(/;$/, "")},`;
|
|
263
345
|
}
|
|
264
346
|
tplComponents += "}";
|
|
265
347
|
const tplAst = babel.parseSync(tplCode.code);
|
|
266
348
|
insertWxsToRenderAst(tplAst, instruction.scriptModule, scriptRes);
|
|
267
349
|
const { code: transCode } = babel.transformFromAstSync(tplAst, "", {
|
|
268
|
-
comments: false
|
|
350
|
+
comments: false,
|
|
351
|
+
sourceType: "script"
|
|
269
352
|
});
|
|
270
353
|
const code = `Module({
|
|
271
354
|
path: '${module.path}',
|
|
272
355
|
id: '${module.id}',
|
|
273
|
-
render: ${transCode.replace(/;$/, "")
|
|
356
|
+
render: ${transCode.replace(/;$/, "")},
|
|
274
357
|
usingComponents: ${JSON.stringify(module.usingComponents)},
|
|
275
358
|
tplComponents: ${tplComponents},
|
|
276
359
|
});`;
|
|
@@ -397,7 +480,8 @@ function processWxsContent(wxsContent, wxsFilePath, scriptModule, workPath, file
|
|
|
397
480
|
}
|
|
398
481
|
});
|
|
399
482
|
return babel.transformFromAstSync(wxsAst, "", {
|
|
400
|
-
comments: false
|
|
483
|
+
comments: false,
|
|
484
|
+
sourceType: "script"
|
|
401
485
|
}).code;
|
|
402
486
|
}
|
|
403
487
|
function isWxsModuleByContent(moduleCode, modulePath = "") {
|
|
@@ -471,7 +555,8 @@ function compileModuleWithAllWxs(module, scriptRes, allScriptModules) {
|
|
|
471
555
|
const ast = babel.parseSync(code2);
|
|
472
556
|
insertWxsToRenderAst(ast, allScriptModules, scriptRes);
|
|
473
557
|
code2 = babel.transformFromAstSync(ast, "", {
|
|
474
|
-
comments: false
|
|
558
|
+
comments: false,
|
|
559
|
+
sourceType: "script"
|
|
475
560
|
}).code;
|
|
476
561
|
tplComponents += `'${tm.path}':${code2.replace(/;$/, "").replace(/^"use strict";\s*/, "")},`;
|
|
477
562
|
}
|
|
@@ -479,7 +564,8 @@ function compileModuleWithAllWxs(module, scriptRes, allScriptModules) {
|
|
|
479
564
|
const tplAst = babel.parseSync(tplCode.code);
|
|
480
565
|
insertWxsToRenderAst(tplAst, allScriptModules, scriptRes);
|
|
481
566
|
const { code: transCode } = babel.transformFromAstSync(tplAst, "", {
|
|
482
|
-
comments: false
|
|
567
|
+
comments: false,
|
|
568
|
+
sourceType: "script"
|
|
483
569
|
});
|
|
484
570
|
const code = `Module({
|
|
485
571
|
path: '${module.path}',
|
|
@@ -495,10 +581,66 @@ function compileModuleWithAllWxs(module, scriptRes, allScriptModules) {
|
|
|
495
581
|
compileResCache.set(module.path, cacheData);
|
|
496
582
|
scriptRes.set(module.path, code);
|
|
497
583
|
}
|
|
498
|
-
function
|
|
499
|
-
|
|
584
|
+
function processIncludeConditionalAttrs($, elem, includeContent) {
|
|
585
|
+
const allAttrs = $(elem).attr();
|
|
586
|
+
const conditionAttrs = {};
|
|
587
|
+
let hasCondition = false;
|
|
588
|
+
for (const attrName in allAttrs) {
|
|
589
|
+
if (attrName.endsWith(":if") || attrName.endsWith(":elif") || attrName.endsWith(":else")) {
|
|
590
|
+
conditionAttrs[attrName] = allAttrs[attrName];
|
|
591
|
+
hasCondition = true;
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
if (hasCondition) {
|
|
595
|
+
let blockAttrs = "";
|
|
596
|
+
for (const attrName in conditionAttrs) {
|
|
597
|
+
const attrValue = conditionAttrs[attrName];
|
|
598
|
+
if (attrValue !== void 0 && attrValue !== "") {
|
|
599
|
+
blockAttrs += ` ${attrName}="${attrValue}"`;
|
|
600
|
+
} else {
|
|
601
|
+
blockAttrs += ` ${attrName}`;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
return `<block${blockAttrs}>${includeContent}</block>`;
|
|
605
|
+
} else {
|
|
606
|
+
return includeContent;
|
|
607
|
+
}
|
|
500
608
|
}
|
|
501
|
-
function
|
|
609
|
+
function processIncludedFileWxsDependencies(content, includePath, scriptModule, components, processedPaths = /* @__PURE__ */ new Set()) {
|
|
610
|
+
if (processedPaths.has(includePath)) {
|
|
611
|
+
return;
|
|
612
|
+
}
|
|
613
|
+
processedPaths.add(includePath);
|
|
614
|
+
const $ = cheerio.load(content, {
|
|
615
|
+
xmlMode: true,
|
|
616
|
+
decodeEntities: false
|
|
617
|
+
});
|
|
618
|
+
const componentTags = /* @__PURE__ */ new Set();
|
|
619
|
+
$("*").each((_, elem) => {
|
|
620
|
+
const tagName = elem.tagName;
|
|
621
|
+
if (components && components[tagName]) {
|
|
622
|
+
componentTags.add(tagName);
|
|
623
|
+
}
|
|
624
|
+
});
|
|
625
|
+
for (const tagName of componentTags) {
|
|
626
|
+
const componentPath = components[tagName];
|
|
627
|
+
const componentModule = getComponent(componentPath);
|
|
628
|
+
if (componentModule) {
|
|
629
|
+
if (processedPaths.has(componentModule.path)) {
|
|
630
|
+
continue;
|
|
631
|
+
}
|
|
632
|
+
const componentTemplate = toCompileTemplate(true, componentModule.path, componentModule.usingComponents, componentModule.componentPlaceholder, processedPaths);
|
|
633
|
+
if (componentTemplate && componentTemplate.instruction && componentTemplate.instruction.scriptModule) {
|
|
634
|
+
for (const sm of componentTemplate.instruction.scriptModule) {
|
|
635
|
+
if (!scriptModule.find((existing) => existing.path === sm.path)) {
|
|
636
|
+
scriptModule.push(sm);
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
function toCompileTemplate(isComponent, path2, components, componentPlaceholder, processedPaths = /* @__PURE__ */ new Set()) {
|
|
502
644
|
const workPath = getWorkPath();
|
|
503
645
|
const fullPath = getViewPath(workPath, path2);
|
|
504
646
|
if (!fullPath) {
|
|
@@ -553,11 +695,12 @@ function toCompileTemplate(isComponent, path2, components, componentPlaceholder)
|
|
|
553
695
|
scriptModule,
|
|
554
696
|
includePath
|
|
555
697
|
);
|
|
556
|
-
processIncludedFileWxsDependencies(includeContent, includePath);
|
|
698
|
+
processIncludedFileWxsDependencies(includeContent, includePath, scriptModule, components, processedPaths);
|
|
557
699
|
$includeContent("template").remove();
|
|
558
700
|
$includeContent("wxs").remove();
|
|
559
701
|
$includeContent("dds").remove();
|
|
560
|
-
|
|
702
|
+
const processedContent = processIncludeConditionalAttrs($, elem, $includeContent.html());
|
|
703
|
+
$(elem).replaceWith(processedContent);
|
|
561
704
|
} else {
|
|
562
705
|
$(elem).remove();
|
|
563
706
|
}
|
|
@@ -593,7 +736,7 @@ function toCompileTemplate(isComponent, path2, components, componentPlaceholder)
|
|
|
593
736
|
scriptModule,
|
|
594
737
|
importPath
|
|
595
738
|
);
|
|
596
|
-
processIncludedFileWxsDependencies(importContent, importPath);
|
|
739
|
+
processIncludedFileWxsDependencies(importContent, importPath, scriptModule, components, processedPaths);
|
|
597
740
|
}
|
|
598
741
|
}
|
|
599
742
|
});
|
|
@@ -674,9 +817,10 @@ function transTag(opts) {
|
|
|
674
817
|
res = `dd-${tag}`;
|
|
675
818
|
}
|
|
676
819
|
let tagRes;
|
|
677
|
-
const propsAry = isStart ? getProps(attrs, tag) : [];
|
|
820
|
+
const propsAry = isStart ? getProps(attrs, tag, components) : [];
|
|
678
821
|
const multipleSlots = attrs?.slot;
|
|
679
822
|
if (attrs?.slot) {
|
|
823
|
+
const isDynamicSlot = isWrappedByBraces(multipleSlots);
|
|
680
824
|
if (isStart) {
|
|
681
825
|
const withVIf = [];
|
|
682
826
|
const withoutVIf = [];
|
|
@@ -684,13 +828,24 @@ function transTag(opts) {
|
|
|
684
828
|
const prop = propsAry[i];
|
|
685
829
|
if (prop.includes("v-if") || prop.includes("v-else-if") || prop.includes("v-else")) {
|
|
686
830
|
withVIf.push(prop);
|
|
687
|
-
} else {
|
|
831
|
+
} else if (!prop.includes("slot")) {
|
|
688
832
|
withoutVIf.push(prop);
|
|
689
833
|
}
|
|
690
834
|
}
|
|
691
|
-
|
|
835
|
+
const vIfProps = withVIf.length > 0 ? `${withVIf.join(" ")} ` : "";
|
|
836
|
+
const vOtherProps = withoutVIf.length > 0 ? ` ${withoutVIf.join(" ")}` : "";
|
|
837
|
+
const templateContent = `<template ${vIfProps}${generateSlotDirective(multipleSlots)}><${res}${vOtherProps}>`;
|
|
838
|
+
if (isDynamicSlot) {
|
|
839
|
+
tagRes = `<dd-block>${templateContent}`;
|
|
840
|
+
} else {
|
|
841
|
+
tagRes = templateContent;
|
|
842
|
+
}
|
|
692
843
|
} else {
|
|
693
|
-
|
|
844
|
+
if (isDynamicSlot) {
|
|
845
|
+
tagRes = `</${res}></template></dd-block>`;
|
|
846
|
+
} else {
|
|
847
|
+
tagRes = `</${res}></template>`;
|
|
848
|
+
}
|
|
694
849
|
}
|
|
695
850
|
} else {
|
|
696
851
|
if (isStart) {
|
|
@@ -702,8 +857,17 @@ function transTag(opts) {
|
|
|
702
857
|
}
|
|
703
858
|
return tagRes;
|
|
704
859
|
}
|
|
705
|
-
function
|
|
860
|
+
function generateSlotDirective(slotValue) {
|
|
861
|
+
if (isWrappedByBraces(slotValue)) {
|
|
862
|
+
const slotExpression = parseBraceExp(slotValue);
|
|
863
|
+
return `#[${slotExpression}]`;
|
|
864
|
+
} else {
|
|
865
|
+
return `#${slotValue}`;
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
function getProps(attrs, tag, components) {
|
|
706
869
|
const attrsList = [];
|
|
870
|
+
const propBindings = {};
|
|
707
871
|
Object.entries(attrs).forEach(([name, value]) => {
|
|
708
872
|
if (name.endsWith(":if")) {
|
|
709
873
|
attrsList.push({
|
|
@@ -802,6 +966,11 @@ function getProps(attrs, tag) {
|
|
|
802
966
|
if (tag === "template" && name === "data") {
|
|
803
967
|
pVal = `{${pVal}}`;
|
|
804
968
|
}
|
|
969
|
+
if (components && components[tag]) {
|
|
970
|
+
if (pVal && typeof pVal === "string") {
|
|
971
|
+
propBindings[name] = pVal;
|
|
972
|
+
}
|
|
973
|
+
}
|
|
805
974
|
attrsList.push({
|
|
806
975
|
name: `:${name}`,
|
|
807
976
|
value: pVal
|
|
@@ -824,6 +993,24 @@ function getProps(attrs, tag) {
|
|
|
824
993
|
propsRes.push(`${name}="${escapeQuotes(value)}"`);
|
|
825
994
|
}
|
|
826
995
|
});
|
|
996
|
+
if (components && components[tag] && Object.keys(propBindings).length > 0) {
|
|
997
|
+
try {
|
|
998
|
+
const validBindings = {};
|
|
999
|
+
for (const [key, value] of Object.entries(propBindings)) {
|
|
1000
|
+
if (value !== void 0 && value !== null && typeof value === "string") {
|
|
1001
|
+
validBindings[key] = value;
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
if (Object.keys(validBindings).length > 0) {
|
|
1005
|
+
const parsedBindings = parseBindings(validBindings);
|
|
1006
|
+
const bindingsJson = JSON.stringify(parsedBindings);
|
|
1007
|
+
const escapedJson = bindingsJson.replace(/"/g, """);
|
|
1008
|
+
propsRes.push(`v-c-prop-bindings="${escapedJson}"`);
|
|
1009
|
+
}
|
|
1010
|
+
} catch (error) {
|
|
1011
|
+
console.warn("[compiler] 序列化 propBindings 失败:", error.message, "标签:", tag, "绑定数据:", propBindings);
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
827
1014
|
return propsRes;
|
|
828
1015
|
}
|
|
829
1016
|
function generateVModelTemplate(expression) {
|
|
@@ -1040,11 +1227,7 @@ function transTagWxs($, scriptModule, filePath) {
|
|
|
1040
1227
|
});
|
|
1041
1228
|
wxsNodes.remove();
|
|
1042
1229
|
}
|
|
1043
|
-
function collectAllWxsModules(scriptRes, collectedPaths = /* @__PURE__ */ new Set(), scriptModule = []
|
|
1044
|
-
if (maxDepth <= 0) {
|
|
1045
|
-
console.warn("[view]", "collectAllWxsModules 达到最大递归深度,停止处理");
|
|
1046
|
-
return [];
|
|
1047
|
-
}
|
|
1230
|
+
function collectAllWxsModules(scriptRes, collectedPaths = /* @__PURE__ */ new Set(), scriptModule = []) {
|
|
1048
1231
|
const allWxsModules = [];
|
|
1049
1232
|
const workPath = getWorkPath();
|
|
1050
1233
|
for (const [modulePath, moduleCode] of scriptRes.entries()) {
|
|
@@ -1061,12 +1244,7 @@ function collectAllWxsModules(scriptRes, collectedPaths = /* @__PURE__ */ new Se
|
|
|
1061
1244
|
for (const depPath of dependencies) {
|
|
1062
1245
|
if (!collectedPaths.has(depPath)) {
|
|
1063
1246
|
if (scriptRes.has(depPath)) {
|
|
1064
|
-
const depModules = collectAllWxsModules(
|
|
1065
|
-
/* @__PURE__ */ new Map([[depPath, scriptRes.get(depPath)]]),
|
|
1066
|
-
collectedPaths,
|
|
1067
|
-
scriptModule,
|
|
1068
|
-
maxDepth - 1
|
|
1069
|
-
);
|
|
1247
|
+
const depModules = collectAllWxsModules(/* @__PURE__ */ new Map([[depPath, scriptRes.get(depPath)]]), collectedPaths, scriptModule);
|
|
1070
1248
|
allWxsModules.push(...depModules);
|
|
1071
1249
|
} else {
|
|
1072
1250
|
const loaded = loadWxsModule(depPath, workPath, scriptModule);
|
|
@@ -1074,12 +1252,7 @@ function collectAllWxsModules(scriptRes, collectedPaths = /* @__PURE__ */ new Se
|
|
|
1074
1252
|
scriptRes.set(depPath, loaded.code);
|
|
1075
1253
|
allWxsModules.push(loaded);
|
|
1076
1254
|
collectedPaths.add(depPath);
|
|
1077
|
-
const depModules = collectAllWxsModules(
|
|
1078
|
-
/* @__PURE__ */ new Map([[depPath, loaded.code]]),
|
|
1079
|
-
collectedPaths,
|
|
1080
|
-
scriptModule,
|
|
1081
|
-
maxDepth - 1
|
|
1082
|
-
);
|
|
1255
|
+
const depModules = collectAllWxsModules(/* @__PURE__ */ new Map([[depPath, loaded.code]]), collectedPaths, scriptModule);
|
|
1083
1256
|
allWxsModules.push(...depModules);
|
|
1084
1257
|
}
|
|
1085
1258
|
}
|
|
@@ -1158,10 +1331,12 @@ function insertWxsToRenderAst(ast, scriptModule, scriptRes) {
|
|
|
1158
1331
|
}
|
|
1159
1332
|
export {
|
|
1160
1333
|
compileML,
|
|
1334
|
+
generateSlotDirective,
|
|
1161
1335
|
generateVModelTemplate,
|
|
1162
1336
|
parseBraceExp,
|
|
1163
1337
|
parseClassRules,
|
|
1164
1338
|
parseKeyExpression,
|
|
1339
|
+
processIncludeConditionalAttrs,
|
|
1165
1340
|
processWxsContent,
|
|
1166
1341
|
splitWithBraces
|
|
1167
1342
|
};
|
package/dist/index.cjs
CHANGED
|
@@ -8,7 +8,7 @@ const fs = require("node:fs");
|
|
|
8
8
|
const env = require("./env-Cmen1qwy.cjs");
|
|
9
9
|
const os = require("node:os");
|
|
10
10
|
var _documentCurrentScript = typeof document !== "undefined" ? document.currentScript : null;
|
|
11
|
-
const artCode = `
|
|
11
|
+
const artCode$1 = `
|
|
12
12
|
██████╗ ██╗███╗ ███╗██╗███╗ ██╗ █████╗
|
|
13
13
|
██╔══██╗██║████╗ ████║██║████╗ ██║██╔══██╗
|
|
14
14
|
██║ ██║██║██╔████╔██║██║██╔██╗ ██║███████║
|
|
@@ -16,8 +16,8 @@ const artCode = `
|
|
|
16
16
|
██████╔╝██║██║ ╚═╝ ██║██║██║ ╚████║██║ ██║
|
|
17
17
|
╚═════╝ ╚═╝╚═╝ ╚═╝╚═╝╚═╝ ╚═══╝╚═╝ ╚═╝
|
|
18
18
|
`;
|
|
19
|
-
function artCode
|
|
20
|
-
console.log(artCode);
|
|
19
|
+
function artCode() {
|
|
20
|
+
console.log(artCode$1);
|
|
21
21
|
}
|
|
22
22
|
function createDist() {
|
|
23
23
|
const distPath = env.getTargetPath();
|
|
@@ -317,7 +317,7 @@ function compileConfig() {
|
|
|
317
317
|
let isPrinted = false;
|
|
318
318
|
async function build(targetPath, workPath, useAppIdDir = true) {
|
|
319
319
|
if (!isPrinted) {
|
|
320
|
-
artCode
|
|
320
|
+
artCode();
|
|
321
321
|
isPrinted = true;
|
|
322
322
|
}
|
|
323
323
|
const tasks = new listr2.Listr(
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dimina/compiler",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.11",
|
|
4
4
|
"description": "星河编译工具",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -45,24 +45,24 @@
|
|
|
45
45
|
"星河"
|
|
46
46
|
],
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@babel/core": "^7.28.
|
|
48
|
+
"@babel/core": "^7.28.5",
|
|
49
49
|
"@babel/plugin-transform-modules-commonjs": "^7.27.1",
|
|
50
|
-
"@babel/traverse": "^7.28.
|
|
51
|
-
"@babel/types": "^7.28.
|
|
52
|
-
"@vue/compiler-sfc": "^3.5.
|
|
53
|
-
"autoprefixer": "^10.4.
|
|
50
|
+
"@babel/traverse": "^7.28.5",
|
|
51
|
+
"@babel/types": "^7.28.5",
|
|
52
|
+
"@vue/compiler-sfc": "^3.5.25",
|
|
53
|
+
"autoprefixer": "^10.4.22",
|
|
54
54
|
"cheerio": "^1.1.2",
|
|
55
55
|
"chokidar": "^4.0.3",
|
|
56
|
-
"commander": "^14.0.
|
|
57
|
-
"cssnano": "^7.1.
|
|
58
|
-
"esbuild": "^0.25.
|
|
56
|
+
"commander": "^14.0.2",
|
|
57
|
+
"cssnano": "^7.1.2",
|
|
58
|
+
"esbuild": "^0.25.12",
|
|
59
59
|
"htmlparser2": "^10.0.0",
|
|
60
|
-
"less": "^4.4.
|
|
61
|
-
"listr2": "^9.0.
|
|
60
|
+
"less": "^4.4.2",
|
|
61
|
+
"listr2": "^9.0.5",
|
|
62
62
|
"postcss": "^8.5.6",
|
|
63
|
-
"postcss-selector-parser": "^7.1.
|
|
64
|
-
"sass": "^1.
|
|
65
|
-
"typescript": "^5.9.
|
|
63
|
+
"postcss-selector-parser": "^7.1.1",
|
|
64
|
+
"sass": "^1.95.1",
|
|
65
|
+
"typescript": "^5.9.3"
|
|
66
66
|
},
|
|
67
67
|
"publishConfig": {
|
|
68
68
|
"registry": "https://registry.npmjs.org/"
|