@dimina/compiler 1.0.8 → 1.0.10

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.
@@ -5,7 +5,7 @@ const process = require("node:process");
5
5
  const chokidar = require("chokidar");
6
6
  const commander = require("commander");
7
7
  const index = require("../index.cjs");
8
- const version = "1.0.7";
8
+ const version = "1.0.10";
9
9
  const pack = {
10
10
  version
11
11
  };
package/dist/bin/index.js CHANGED
@@ -4,7 +4,7 @@ import process from "node:process";
4
4
  import chokidar from "chokidar";
5
5
  import { program } from "commander";
6
6
  import build from "../index.js";
7
- const version = "1.0.7";
7
+ const version = "1.0.10";
8
8
  const pack = {
9
9
  version
10
10
  };
@@ -34,8 +34,6 @@ const fileType = [".wxml", ".ddml"];
34
34
  const compileResCache = /* @__PURE__ */ new Map();
35
35
  const wxsModuleRegistry = /* @__PURE__ */ new Set();
36
36
  const wxsFilePathMap = /* @__PURE__ */ new Map();
37
- const moduleCompileStatus = /* @__PURE__ */ new Map();
38
- const moduleCompileResults = /* @__PURE__ */ new Map();
39
37
  if (!node_worker_threads.isMainThread) {
40
38
  node_worker_threads.parentPort.on("message", async ({ pages, storeInfo }) => {
41
39
  try {
@@ -60,16 +58,8 @@ if (!node_worker_threads.isMainThread) {
60
58
  }
61
59
  });
62
60
  }
63
- function clearCompileState() {
64
- moduleCompileStatus.clear();
65
- moduleCompileResults.clear();
66
- compileResCache.clear();
67
- wxsModuleRegistry.clear();
68
- wxsFilePathMap.clear();
69
- }
70
61
  async function compileML(pages, root, progress) {
71
62
  const workPath = env.getWorkPath();
72
- clearCompileState();
73
63
  initWxsFilePathMap(workPath);
74
64
  for (const page of pages) {
75
65
  const scriptRes = /* @__PURE__ */ new Map();
@@ -135,71 +125,49 @@ function isRegisteredWxsModule(modulePath) {
135
125
  }
136
126
  function buildCompileView(module2, isComponent = false, scriptRes, depthChain = []) {
137
127
  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
128
  if (depthChain.includes(currentPath)) {
151
129
  console.warn("[view]", `检测到循环依赖: ${[...depthChain, currentPath].join(" -> ")}`);
152
- return { scriptModule: [] };
130
+ return;
153
131
  }
154
- if (depthChain.length > 5) {
155
- console.warn("[view]", `检测到深度依赖,中断编译: ${[...depthChain, currentPath].join(" -> ")}`);
156
- return { scriptModule: [] };
132
+ if (depthChain.length > 20) {
133
+ console.warn("[view]", `检测到深度依赖: ${[...depthChain, currentPath].join(" -> ")}`);
134
+ return;
157
135
  }
158
- moduleCompileStatus.set(currentPath, "pending");
159
136
  depthChain = [...depthChain, currentPath];
160
- try {
161
- const allScriptModules = [];
162
- const currentInstruction = compileModule(module2, isComponent, scriptRes);
163
- if (currentInstruction && currentInstruction.scriptModule) {
164
- allScriptModules.push(...currentInstruction.scriptModule);
165
- }
166
- if (module2.usingComponents) {
167
- for (const componentInfo of Object.values(module2.usingComponents)) {
168
- const componentModule = env.getComponent(componentInfo);
169
- if (!componentModule) {
170
- continue;
171
- }
172
- if (componentModule.path === module2.path) {
173
- console.warn("[view]", `检测到自依赖,跳过处理: ${module2.path}`);
174
- continue;
175
- }
176
- const componentInstruction = buildCompileView(componentModule, true, scriptRes, depthChain);
177
- if (componentInstruction && componentInstruction.scriptModule) {
178
- for (const sm of componentInstruction.scriptModule) {
179
- if (!allScriptModules.find((existing) => existing.path === sm.path)) {
180
- allScriptModules.push(sm);
181
- }
137
+ const allScriptModules = [];
138
+ const currentInstruction = compileModule(module2, isComponent, scriptRes);
139
+ if (currentInstruction && currentInstruction.scriptModule) {
140
+ allScriptModules.push(...currentInstruction.scriptModule);
141
+ }
142
+ if (module2.usingComponents) {
143
+ for (const componentInfo of Object.values(module2.usingComponents)) {
144
+ const componentModule = env.getComponent(componentInfo);
145
+ if (!componentModule) {
146
+ continue;
147
+ }
148
+ if (componentModule.path === module2.path) {
149
+ console.warn("[view]", `检测到循环依赖,跳过处理: ${module2.path}`);
150
+ continue;
151
+ }
152
+ const componentInstruction = buildCompileView(componentModule, true, scriptRes, depthChain);
153
+ if (componentInstruction && componentInstruction.scriptModule) {
154
+ for (const sm of componentInstruction.scriptModule) {
155
+ if (!allScriptModules.find((existing) => existing.path === sm.path)) {
156
+ allScriptModules.push(sm);
182
157
  }
183
158
  }
184
159
  }
185
160
  }
186
- if (!isComponent && allScriptModules.length > 0) {
187
- for (const sm of allScriptModules) {
188
- if (!scriptRes.has(sm.path)) {
189
- scriptRes.set(sm.path, sm.code);
190
- }
161
+ }
162
+ if (!isComponent && allScriptModules.length > 0) {
163
+ for (const sm of allScriptModules) {
164
+ if (!scriptRes.has(sm.path)) {
165
+ scriptRes.set(sm.path, sm.code);
191
166
  }
192
- compileModuleWithAllWxs(module2, scriptRes, allScriptModules);
193
167
  }
194
- const result = { scriptModule: allScriptModules };
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: [] };
168
+ compileModuleWithAllWxs(module2, scriptRes, allScriptModules);
202
169
  }
170
+ return { scriptModule: allScriptModules };
203
171
  }
204
172
  function compileModule(module2, isComponent, scriptRes) {
205
173
  const { tpl, instruction } = toCompileTemplate(isComponent, module2.path, module2.usingComponents, module2.componentPlaceholder);
@@ -277,20 +245,22 @@ function compileModule(module2, isComponent, scriptRes) {
277
245
  const ast = babel.parseSync(code2);
278
246
  insertWxsToRenderAst(ast, instruction.scriptModule, scriptRes);
279
247
  code2 = babel.transformFromAstSync(ast, "", {
280
- comments: false
248
+ comments: false,
249
+ sourceType: "script"
281
250
  }).code;
282
- tplComponents += `'${tm.path}':${code2.replace(/;$/, "").replace(/^"use strict";\s*/, "")},`;
251
+ tplComponents += `'${tm.path}':${code2.replace(/;$/, "")},`;
283
252
  }
284
253
  tplComponents += "}";
285
254
  const tplAst = babel.parseSync(tplCode.code);
286
255
  insertWxsToRenderAst(tplAst, instruction.scriptModule, scriptRes);
287
256
  const { code: transCode } = babel.transformFromAstSync(tplAst, "", {
288
- comments: false
257
+ comments: false,
258
+ sourceType: "script"
289
259
  });
290
260
  const code = `Module({
291
261
  path: '${module2.path}',
292
262
  id: '${module2.id}',
293
- render: ${transCode.replace(/;$/, "").replace(/^"use strict";\s*/, "")},
263
+ render: ${transCode.replace(/;$/, "")},
294
264
  usingComponents: ${JSON.stringify(module2.usingComponents)},
295
265
  tplComponents: ${tplComponents},
296
266
  });`;
@@ -417,7 +387,8 @@ function processWxsContent(wxsContent, wxsFilePath, scriptModule, workPath, file
417
387
  }
418
388
  });
419
389
  return babel.transformFromAstSync(wxsAst, "", {
420
- comments: false
390
+ comments: false,
391
+ sourceType: "script"
421
392
  }).code;
422
393
  }
423
394
  function isWxsModuleByContent(moduleCode, modulePath = "") {
@@ -491,7 +462,8 @@ function compileModuleWithAllWxs(module2, scriptRes, allScriptModules) {
491
462
  const ast = babel.parseSync(code2);
492
463
  insertWxsToRenderAst(ast, allScriptModules, scriptRes);
493
464
  code2 = babel.transformFromAstSync(ast, "", {
494
- comments: false
465
+ comments: false,
466
+ sourceType: "script"
495
467
  }).code;
496
468
  tplComponents += `'${tm.path}':${code2.replace(/;$/, "").replace(/^"use strict";\s*/, "")},`;
497
469
  }
@@ -499,7 +471,8 @@ function compileModuleWithAllWxs(module2, scriptRes, allScriptModules) {
499
471
  const tplAst = babel.parseSync(tplCode.code);
500
472
  insertWxsToRenderAst(tplAst, allScriptModules, scriptRes);
501
473
  const { code: transCode } = babel.transformFromAstSync(tplAst, "", {
502
- comments: false
474
+ comments: false,
475
+ sourceType: "script"
503
476
  });
504
477
  const code = `Module({
505
478
  path: '${module2.path}',
@@ -515,10 +488,41 @@ function compileModuleWithAllWxs(module2, scriptRes, allScriptModules) {
515
488
  compileResCache.set(module2.path, cacheData);
516
489
  scriptRes.set(module2.path, code);
517
490
  }
518
- function processIncludedFileWxsDependencies(content, includePath, scriptModule, components) {
519
- console.warn("[view]", `跳过被引入文件的组件 wxs 依赖处理: ${includePath}`);
491
+ function processIncludedFileWxsDependencies(content, includePath, scriptModule, components, processedPaths = /* @__PURE__ */ new Set()) {
492
+ if (processedPaths.has(includePath)) {
493
+ return;
494
+ }
495
+ processedPaths.add(includePath);
496
+ const $ = cheerio__namespace.load(content, {
497
+ xmlMode: true,
498
+ decodeEntities: false
499
+ });
500
+ const componentTags = /* @__PURE__ */ new Set();
501
+ $("*").each((_, elem) => {
502
+ const tagName = elem.tagName;
503
+ if (components && components[tagName]) {
504
+ componentTags.add(tagName);
505
+ }
506
+ });
507
+ for (const tagName of componentTags) {
508
+ const componentPath = components[tagName];
509
+ const componentModule = env.getComponent(componentPath);
510
+ if (componentModule) {
511
+ if (processedPaths.has(componentModule.path)) {
512
+ continue;
513
+ }
514
+ const componentTemplate = toCompileTemplate(true, componentModule.path, componentModule.usingComponents, componentModule.componentPlaceholder, processedPaths);
515
+ if (componentTemplate && componentTemplate.instruction && componentTemplate.instruction.scriptModule) {
516
+ for (const sm of componentTemplate.instruction.scriptModule) {
517
+ if (!scriptModule.find((existing) => existing.path === sm.path)) {
518
+ scriptModule.push(sm);
519
+ }
520
+ }
521
+ }
522
+ }
523
+ }
520
524
  }
521
- function toCompileTemplate(isComponent, path2, components, componentPlaceholder) {
525
+ function toCompileTemplate(isComponent, path2, components, componentPlaceholder, processedPaths = /* @__PURE__ */ new Set()) {
522
526
  const workPath = env.getWorkPath();
523
527
  const fullPath = getViewPath(workPath, path2);
524
528
  if (!fullPath) {
@@ -573,7 +577,7 @@ function toCompileTemplate(isComponent, path2, components, componentPlaceholder)
573
577
  scriptModule,
574
578
  includePath
575
579
  );
576
- processIncludedFileWxsDependencies(includeContent, includePath);
580
+ processIncludedFileWxsDependencies(includeContent, includePath, scriptModule, components, processedPaths);
577
581
  $includeContent("template").remove();
578
582
  $includeContent("wxs").remove();
579
583
  $includeContent("dds").remove();
@@ -613,7 +617,7 @@ function toCompileTemplate(isComponent, path2, components, componentPlaceholder)
613
617
  scriptModule,
614
618
  importPath
615
619
  );
616
- processIncludedFileWxsDependencies(importContent, importPath);
620
+ processIncludedFileWxsDependencies(importContent, importPath, scriptModule, components, processedPaths);
617
621
  }
618
622
  }
619
623
  });
@@ -697,6 +701,7 @@ function transTag(opts) {
697
701
  const propsAry = isStart ? getProps(attrs, tag) : [];
698
702
  const multipleSlots = attrs?.slot;
699
703
  if (attrs?.slot) {
704
+ const isDynamicSlot = isWrappedByBraces(multipleSlots);
700
705
  if (isStart) {
701
706
  const withVIf = [];
702
707
  const withoutVIf = [];
@@ -704,13 +709,24 @@ function transTag(opts) {
704
709
  const prop = propsAry[i];
705
710
  if (prop.includes("v-if") || prop.includes("v-else-if") || prop.includes("v-else")) {
706
711
  withVIf.push(prop);
707
- } else {
712
+ } else if (!prop.includes("slot")) {
708
713
  withoutVIf.push(prop);
709
714
  }
710
715
  }
711
- tagRes = `<template ${`${withVIf.join(" ")}`} #${multipleSlots}><${res}${` ${withoutVIf.join(" ")}`}>`;
716
+ const vIfProps = withVIf.length > 0 ? `${withVIf.join(" ")} ` : "";
717
+ const vOtherProps = withoutVIf.length > 0 ? ` ${withoutVIf.join(" ")}` : "";
718
+ const templateContent = `<template ${vIfProps}${generateSlotDirective(multipleSlots)}><${res}${vOtherProps}>`;
719
+ if (isDynamicSlot) {
720
+ tagRes = `<dd-block>${templateContent}`;
721
+ } else {
722
+ tagRes = templateContent;
723
+ }
712
724
  } else {
713
- tagRes = `</${res}></template>`;
725
+ if (isDynamicSlot) {
726
+ tagRes = `</${res}></template></dd-block>`;
727
+ } else {
728
+ tagRes = `</${res}></template>`;
729
+ }
714
730
  }
715
731
  } else {
716
732
  if (isStart) {
@@ -722,6 +738,14 @@ function transTag(opts) {
722
738
  }
723
739
  return tagRes;
724
740
  }
741
+ function generateSlotDirective(slotValue) {
742
+ if (isWrappedByBraces(slotValue)) {
743
+ const slotExpression = parseBraceExp(slotValue);
744
+ return `#[${slotExpression}]`;
745
+ } else {
746
+ return `#${slotValue}`;
747
+ }
748
+ }
725
749
  function getProps(attrs, tag) {
726
750
  const attrsList = [];
727
751
  Object.entries(attrs).forEach(([name, value]) => {
@@ -1060,11 +1084,7 @@ function transTagWxs($, scriptModule, filePath) {
1060
1084
  });
1061
1085
  wxsNodes.remove();
1062
1086
  }
1063
- function collectAllWxsModules(scriptRes, collectedPaths = /* @__PURE__ */ new Set(), scriptModule = [], maxDepth = 5) {
1064
- if (maxDepth <= 0) {
1065
- console.warn("[view]", "collectAllWxsModules 达到最大递归深度,停止处理");
1066
- return [];
1067
- }
1087
+ function collectAllWxsModules(scriptRes, collectedPaths = /* @__PURE__ */ new Set(), scriptModule = []) {
1068
1088
  const allWxsModules = [];
1069
1089
  const workPath = env.getWorkPath();
1070
1090
  for (const [modulePath, moduleCode] of scriptRes.entries()) {
@@ -1081,12 +1101,7 @@ function collectAllWxsModules(scriptRes, collectedPaths = /* @__PURE__ */ new Se
1081
1101
  for (const depPath of dependencies) {
1082
1102
  if (!collectedPaths.has(depPath)) {
1083
1103
  if (scriptRes.has(depPath)) {
1084
- const depModules = collectAllWxsModules(
1085
- /* @__PURE__ */ new Map([[depPath, scriptRes.get(depPath)]]),
1086
- collectedPaths,
1087
- scriptModule,
1088
- maxDepth - 1
1089
- );
1104
+ const depModules = collectAllWxsModules(/* @__PURE__ */ new Map([[depPath, scriptRes.get(depPath)]]), collectedPaths, scriptModule);
1090
1105
  allWxsModules.push(...depModules);
1091
1106
  } else {
1092
1107
  const loaded = loadWxsModule(depPath, workPath, scriptModule);
@@ -1094,12 +1109,7 @@ function collectAllWxsModules(scriptRes, collectedPaths = /* @__PURE__ */ new Se
1094
1109
  scriptRes.set(depPath, loaded.code);
1095
1110
  allWxsModules.push(loaded);
1096
1111
  collectedPaths.add(depPath);
1097
- const depModules = collectAllWxsModules(
1098
- /* @__PURE__ */ new Map([[depPath, loaded.code]]),
1099
- collectedPaths,
1100
- scriptModule,
1101
- maxDepth - 1
1102
- );
1112
+ const depModules = collectAllWxsModules(/* @__PURE__ */ new Map([[depPath, loaded.code]]), collectedPaths, scriptModule);
1103
1113
  allWxsModules.push(...depModules);
1104
1114
  }
1105
1115
  }
@@ -1177,6 +1187,7 @@ function insertWxsToRenderAst(ast, scriptModule, scriptRes) {
1177
1187
  }
1178
1188
  }
1179
1189
  exports.compileML = compileML;
1190
+ exports.generateSlotDirective = generateSlotDirective;
1180
1191
  exports.generateVModelTemplate = generateVModelTemplate;
1181
1192
  exports.parseBraceExp = parseBraceExp;
1182
1193
  exports.parseClassRules = parseClassRules;
@@ -14,8 +14,6 @@ const fileType = [".wxml", ".ddml"];
14
14
  const compileResCache = /* @__PURE__ */ new Map();
15
15
  const wxsModuleRegistry = /* @__PURE__ */ new Set();
16
16
  const wxsFilePathMap = /* @__PURE__ */ new Map();
17
- const moduleCompileStatus = /* @__PURE__ */ new Map();
18
- const moduleCompileResults = /* @__PURE__ */ new Map();
19
17
  if (!isMainThread) {
20
18
  parentPort.on("message", async ({ pages, storeInfo }) => {
21
19
  try {
@@ -40,16 +38,8 @@ if (!isMainThread) {
40
38
  }
41
39
  });
42
40
  }
43
- function clearCompileState() {
44
- moduleCompileStatus.clear();
45
- moduleCompileResults.clear();
46
- compileResCache.clear();
47
- wxsModuleRegistry.clear();
48
- wxsFilePathMap.clear();
49
- }
50
41
  async function compileML(pages, root, progress) {
51
42
  const workPath = getWorkPath();
52
- clearCompileState();
53
43
  initWxsFilePathMap(workPath);
54
44
  for (const page of pages) {
55
45
  const scriptRes = /* @__PURE__ */ new Map();
@@ -115,71 +105,49 @@ function isRegisteredWxsModule(modulePath) {
115
105
  }
116
106
  function buildCompileView(module, isComponent = false, scriptRes, depthChain = []) {
117
107
  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
108
  if (depthChain.includes(currentPath)) {
131
109
  console.warn("[view]", `检测到循环依赖: ${[...depthChain, currentPath].join(" -> ")}`);
132
- return { scriptModule: [] };
110
+ return;
133
111
  }
134
- if (depthChain.length > 5) {
135
- console.warn("[view]", `检测到深度依赖,中断编译: ${[...depthChain, currentPath].join(" -> ")}`);
136
- return { scriptModule: [] };
112
+ if (depthChain.length > 20) {
113
+ console.warn("[view]", `检测到深度依赖: ${[...depthChain, currentPath].join(" -> ")}`);
114
+ return;
137
115
  }
138
- moduleCompileStatus.set(currentPath, "pending");
139
116
  depthChain = [...depthChain, currentPath];
140
- try {
141
- const allScriptModules = [];
142
- const currentInstruction = compileModule(module, isComponent, scriptRes);
143
- if (currentInstruction && currentInstruction.scriptModule) {
144
- allScriptModules.push(...currentInstruction.scriptModule);
145
- }
146
- if (module.usingComponents) {
147
- for (const componentInfo of Object.values(module.usingComponents)) {
148
- const componentModule = getComponent(componentInfo);
149
- if (!componentModule) {
150
- continue;
151
- }
152
- if (componentModule.path === module.path) {
153
- console.warn("[view]", `检测到自依赖,跳过处理: ${module.path}`);
154
- continue;
155
- }
156
- const componentInstruction = buildCompileView(componentModule, true, scriptRes, depthChain);
157
- if (componentInstruction && componentInstruction.scriptModule) {
158
- for (const sm of componentInstruction.scriptModule) {
159
- if (!allScriptModules.find((existing) => existing.path === sm.path)) {
160
- allScriptModules.push(sm);
161
- }
117
+ const allScriptModules = [];
118
+ const currentInstruction = compileModule(module, isComponent, scriptRes);
119
+ if (currentInstruction && currentInstruction.scriptModule) {
120
+ allScriptModules.push(...currentInstruction.scriptModule);
121
+ }
122
+ if (module.usingComponents) {
123
+ for (const componentInfo of Object.values(module.usingComponents)) {
124
+ const componentModule = getComponent(componentInfo);
125
+ if (!componentModule) {
126
+ continue;
127
+ }
128
+ if (componentModule.path === module.path) {
129
+ console.warn("[view]", `检测到循环依赖,跳过处理: ${module.path}`);
130
+ continue;
131
+ }
132
+ const componentInstruction = buildCompileView(componentModule, true, scriptRes, depthChain);
133
+ if (componentInstruction && componentInstruction.scriptModule) {
134
+ for (const sm of componentInstruction.scriptModule) {
135
+ if (!allScriptModules.find((existing) => existing.path === sm.path)) {
136
+ allScriptModules.push(sm);
162
137
  }
163
138
  }
164
139
  }
165
140
  }
166
- if (!isComponent && allScriptModules.length > 0) {
167
- for (const sm of allScriptModules) {
168
- if (!scriptRes.has(sm.path)) {
169
- scriptRes.set(sm.path, sm.code);
170
- }
141
+ }
142
+ if (!isComponent && allScriptModules.length > 0) {
143
+ for (const sm of allScriptModules) {
144
+ if (!scriptRes.has(sm.path)) {
145
+ scriptRes.set(sm.path, sm.code);
171
146
  }
172
- compileModuleWithAllWxs(module, scriptRes, allScriptModules);
173
147
  }
174
- const result = { scriptModule: allScriptModules };
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: [] };
148
+ compileModuleWithAllWxs(module, scriptRes, allScriptModules);
182
149
  }
150
+ return { scriptModule: allScriptModules };
183
151
  }
184
152
  function compileModule(module, isComponent, scriptRes) {
185
153
  const { tpl, instruction } = toCompileTemplate(isComponent, module.path, module.usingComponents, module.componentPlaceholder);
@@ -257,20 +225,22 @@ function compileModule(module, isComponent, scriptRes) {
257
225
  const ast = babel.parseSync(code2);
258
226
  insertWxsToRenderAst(ast, instruction.scriptModule, scriptRes);
259
227
  code2 = babel.transformFromAstSync(ast, "", {
260
- comments: false
228
+ comments: false,
229
+ sourceType: "script"
261
230
  }).code;
262
- tplComponents += `'${tm.path}':${code2.replace(/;$/, "").replace(/^"use strict";\s*/, "")},`;
231
+ tplComponents += `'${tm.path}':${code2.replace(/;$/, "")},`;
263
232
  }
264
233
  tplComponents += "}";
265
234
  const tplAst = babel.parseSync(tplCode.code);
266
235
  insertWxsToRenderAst(tplAst, instruction.scriptModule, scriptRes);
267
236
  const { code: transCode } = babel.transformFromAstSync(tplAst, "", {
268
- comments: false
237
+ comments: false,
238
+ sourceType: "script"
269
239
  });
270
240
  const code = `Module({
271
241
  path: '${module.path}',
272
242
  id: '${module.id}',
273
- render: ${transCode.replace(/;$/, "").replace(/^"use strict";\s*/, "")},
243
+ render: ${transCode.replace(/;$/, "")},
274
244
  usingComponents: ${JSON.stringify(module.usingComponents)},
275
245
  tplComponents: ${tplComponents},
276
246
  });`;
@@ -397,7 +367,8 @@ function processWxsContent(wxsContent, wxsFilePath, scriptModule, workPath, file
397
367
  }
398
368
  });
399
369
  return babel.transformFromAstSync(wxsAst, "", {
400
- comments: false
370
+ comments: false,
371
+ sourceType: "script"
401
372
  }).code;
402
373
  }
403
374
  function isWxsModuleByContent(moduleCode, modulePath = "") {
@@ -471,7 +442,8 @@ function compileModuleWithAllWxs(module, scriptRes, allScriptModules) {
471
442
  const ast = babel.parseSync(code2);
472
443
  insertWxsToRenderAst(ast, allScriptModules, scriptRes);
473
444
  code2 = babel.transformFromAstSync(ast, "", {
474
- comments: false
445
+ comments: false,
446
+ sourceType: "script"
475
447
  }).code;
476
448
  tplComponents += `'${tm.path}':${code2.replace(/;$/, "").replace(/^"use strict";\s*/, "")},`;
477
449
  }
@@ -479,7 +451,8 @@ function compileModuleWithAllWxs(module, scriptRes, allScriptModules) {
479
451
  const tplAst = babel.parseSync(tplCode.code);
480
452
  insertWxsToRenderAst(tplAst, allScriptModules, scriptRes);
481
453
  const { code: transCode } = babel.transformFromAstSync(tplAst, "", {
482
- comments: false
454
+ comments: false,
455
+ sourceType: "script"
483
456
  });
484
457
  const code = `Module({
485
458
  path: '${module.path}',
@@ -495,10 +468,41 @@ function compileModuleWithAllWxs(module, scriptRes, allScriptModules) {
495
468
  compileResCache.set(module.path, cacheData);
496
469
  scriptRes.set(module.path, code);
497
470
  }
498
- function processIncludedFileWxsDependencies(content, includePath, scriptModule, components) {
499
- console.warn("[view]", `跳过被引入文件的组件 wxs 依赖处理: ${includePath}`);
471
+ function processIncludedFileWxsDependencies(content, includePath, scriptModule, components, processedPaths = /* @__PURE__ */ new Set()) {
472
+ if (processedPaths.has(includePath)) {
473
+ return;
474
+ }
475
+ processedPaths.add(includePath);
476
+ const $ = cheerio.load(content, {
477
+ xmlMode: true,
478
+ decodeEntities: false
479
+ });
480
+ const componentTags = /* @__PURE__ */ new Set();
481
+ $("*").each((_, elem) => {
482
+ const tagName = elem.tagName;
483
+ if (components && components[tagName]) {
484
+ componentTags.add(tagName);
485
+ }
486
+ });
487
+ for (const tagName of componentTags) {
488
+ const componentPath = components[tagName];
489
+ const componentModule = getComponent(componentPath);
490
+ if (componentModule) {
491
+ if (processedPaths.has(componentModule.path)) {
492
+ continue;
493
+ }
494
+ const componentTemplate = toCompileTemplate(true, componentModule.path, componentModule.usingComponents, componentModule.componentPlaceholder, processedPaths);
495
+ if (componentTemplate && componentTemplate.instruction && componentTemplate.instruction.scriptModule) {
496
+ for (const sm of componentTemplate.instruction.scriptModule) {
497
+ if (!scriptModule.find((existing) => existing.path === sm.path)) {
498
+ scriptModule.push(sm);
499
+ }
500
+ }
501
+ }
502
+ }
503
+ }
500
504
  }
501
- function toCompileTemplate(isComponent, path2, components, componentPlaceholder) {
505
+ function toCompileTemplate(isComponent, path2, components, componentPlaceholder, processedPaths = /* @__PURE__ */ new Set()) {
502
506
  const workPath = getWorkPath();
503
507
  const fullPath = getViewPath(workPath, path2);
504
508
  if (!fullPath) {
@@ -553,7 +557,7 @@ function toCompileTemplate(isComponent, path2, components, componentPlaceholder)
553
557
  scriptModule,
554
558
  includePath
555
559
  );
556
- processIncludedFileWxsDependencies(includeContent, includePath);
560
+ processIncludedFileWxsDependencies(includeContent, includePath, scriptModule, components, processedPaths);
557
561
  $includeContent("template").remove();
558
562
  $includeContent("wxs").remove();
559
563
  $includeContent("dds").remove();
@@ -593,7 +597,7 @@ function toCompileTemplate(isComponent, path2, components, componentPlaceholder)
593
597
  scriptModule,
594
598
  importPath
595
599
  );
596
- processIncludedFileWxsDependencies(importContent, importPath);
600
+ processIncludedFileWxsDependencies(importContent, importPath, scriptModule, components, processedPaths);
597
601
  }
598
602
  }
599
603
  });
@@ -677,6 +681,7 @@ function transTag(opts) {
677
681
  const propsAry = isStart ? getProps(attrs, tag) : [];
678
682
  const multipleSlots = attrs?.slot;
679
683
  if (attrs?.slot) {
684
+ const isDynamicSlot = isWrappedByBraces(multipleSlots);
680
685
  if (isStart) {
681
686
  const withVIf = [];
682
687
  const withoutVIf = [];
@@ -684,13 +689,24 @@ function transTag(opts) {
684
689
  const prop = propsAry[i];
685
690
  if (prop.includes("v-if") || prop.includes("v-else-if") || prop.includes("v-else")) {
686
691
  withVIf.push(prop);
687
- } else {
692
+ } else if (!prop.includes("slot")) {
688
693
  withoutVIf.push(prop);
689
694
  }
690
695
  }
691
- tagRes = `<template ${`${withVIf.join(" ")}`} #${multipleSlots}><${res}${` ${withoutVIf.join(" ")}`}>`;
696
+ const vIfProps = withVIf.length > 0 ? `${withVIf.join(" ")} ` : "";
697
+ const vOtherProps = withoutVIf.length > 0 ? ` ${withoutVIf.join(" ")}` : "";
698
+ const templateContent = `<template ${vIfProps}${generateSlotDirective(multipleSlots)}><${res}${vOtherProps}>`;
699
+ if (isDynamicSlot) {
700
+ tagRes = `<dd-block>${templateContent}`;
701
+ } else {
702
+ tagRes = templateContent;
703
+ }
692
704
  } else {
693
- tagRes = `</${res}></template>`;
705
+ if (isDynamicSlot) {
706
+ tagRes = `</${res}></template></dd-block>`;
707
+ } else {
708
+ tagRes = `</${res}></template>`;
709
+ }
694
710
  }
695
711
  } else {
696
712
  if (isStart) {
@@ -702,6 +718,14 @@ function transTag(opts) {
702
718
  }
703
719
  return tagRes;
704
720
  }
721
+ function generateSlotDirective(slotValue) {
722
+ if (isWrappedByBraces(slotValue)) {
723
+ const slotExpression = parseBraceExp(slotValue);
724
+ return `#[${slotExpression}]`;
725
+ } else {
726
+ return `#${slotValue}`;
727
+ }
728
+ }
705
729
  function getProps(attrs, tag) {
706
730
  const attrsList = [];
707
731
  Object.entries(attrs).forEach(([name, value]) => {
@@ -1040,11 +1064,7 @@ function transTagWxs($, scriptModule, filePath) {
1040
1064
  });
1041
1065
  wxsNodes.remove();
1042
1066
  }
1043
- function collectAllWxsModules(scriptRes, collectedPaths = /* @__PURE__ */ new Set(), scriptModule = [], maxDepth = 5) {
1044
- if (maxDepth <= 0) {
1045
- console.warn("[view]", "collectAllWxsModules 达到最大递归深度,停止处理");
1046
- return [];
1047
- }
1067
+ function collectAllWxsModules(scriptRes, collectedPaths = /* @__PURE__ */ new Set(), scriptModule = []) {
1048
1068
  const allWxsModules = [];
1049
1069
  const workPath = getWorkPath();
1050
1070
  for (const [modulePath, moduleCode] of scriptRes.entries()) {
@@ -1061,12 +1081,7 @@ function collectAllWxsModules(scriptRes, collectedPaths = /* @__PURE__ */ new Se
1061
1081
  for (const depPath of dependencies) {
1062
1082
  if (!collectedPaths.has(depPath)) {
1063
1083
  if (scriptRes.has(depPath)) {
1064
- const depModules = collectAllWxsModules(
1065
- /* @__PURE__ */ new Map([[depPath, scriptRes.get(depPath)]]),
1066
- collectedPaths,
1067
- scriptModule,
1068
- maxDepth - 1
1069
- );
1084
+ const depModules = collectAllWxsModules(/* @__PURE__ */ new Map([[depPath, scriptRes.get(depPath)]]), collectedPaths, scriptModule);
1070
1085
  allWxsModules.push(...depModules);
1071
1086
  } else {
1072
1087
  const loaded = loadWxsModule(depPath, workPath, scriptModule);
@@ -1074,12 +1089,7 @@ function collectAllWxsModules(scriptRes, collectedPaths = /* @__PURE__ */ new Se
1074
1089
  scriptRes.set(depPath, loaded.code);
1075
1090
  allWxsModules.push(loaded);
1076
1091
  collectedPaths.add(depPath);
1077
- const depModules = collectAllWxsModules(
1078
- /* @__PURE__ */ new Map([[depPath, loaded.code]]),
1079
- collectedPaths,
1080
- scriptModule,
1081
- maxDepth - 1
1082
- );
1092
+ const depModules = collectAllWxsModules(/* @__PURE__ */ new Map([[depPath, loaded.code]]), collectedPaths, scriptModule);
1083
1093
  allWxsModules.push(...depModules);
1084
1094
  }
1085
1095
  }
@@ -1158,6 +1168,7 @@ function insertWxsToRenderAst(ast, scriptModule, scriptRes) {
1158
1168
  }
1159
1169
  export {
1160
1170
  compileML,
1171
+ generateSlotDirective,
1161
1172
  generateVModelTemplate,
1162
1173
  parseBraceExp,
1163
1174
  parseClassRules,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dimina/compiler",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "description": "星河编译工具",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -45,23 +45,23 @@
45
45
  "星河"
46
46
  ],
47
47
  "dependencies": {
48
- "@babel/core": "^7.28.3",
48
+ "@babel/core": "^7.28.4",
49
49
  "@babel/plugin-transform-modules-commonjs": "^7.27.1",
50
- "@babel/traverse": "^7.28.3",
51
- "@babel/types": "^7.28.2",
52
- "@vue/compiler-sfc": "^3.5.19",
50
+ "@babel/traverse": "^7.28.4",
51
+ "@babel/types": "^7.28.4",
52
+ "@vue/compiler-sfc": "^3.5.21",
53
53
  "autoprefixer": "^10.4.21",
54
54
  "cheerio": "^1.1.2",
55
55
  "chokidar": "^4.0.3",
56
56
  "commander": "^14.0.0",
57
- "cssnano": "^7.1.0",
57
+ "cssnano": "^7.1.1",
58
58
  "esbuild": "^0.25.9",
59
59
  "htmlparser2": "^10.0.0",
60
60
  "less": "^4.4.1",
61
- "listr2": "^9.0.2",
61
+ "listr2": "^9.0.3",
62
62
  "postcss": "^8.5.6",
63
63
  "postcss-selector-parser": "^7.1.0",
64
- "sass": "^1.90.0",
64
+ "sass": "^1.92.1",
65
65
  "typescript": "^5.9.2"
66
66
  },
67
67
  "publishConfig": {