@dimina/compiler 1.0.10 → 1.0.12-beta.0

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.10";
8
+ const version = "1.0.12-beta.0";
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.10";
7
+ const version = "1.0.12-beta.0";
8
8
  const pack = {
9
9
  version
10
10
  };
@@ -49,10 +49,7 @@ ${error.stack}`);
49
49
  error: {
50
50
  message: error.message,
51
51
  stack: error.stack,
52
- name: error.name,
53
- file: error.file || null,
54
- line: error.line || null,
55
- code: error.code || null
52
+ name: error.name
56
53
  }
57
54
  });
58
55
  }
@@ -98,22 +95,17 @@ async function compileJS(pages, root, mainCompileRes, progress) {
98
95
  return compileRes;
99
96
  }
100
97
  function buildJSByPath(packageName, module2, compileRes, mainCompileRes, addExtra, depthChain = [], putMain = false) {
101
- const currentPath = module2.path;
102
- if (depthChain.includes(currentPath)) {
103
- console.warn("[logic]", `检测到循环依赖: ${[...depthChain, currentPath].join(" -> ")}`);
104
- return;
105
- }
106
- if (depthChain.length > 20) {
107
- console.warn("[logic]", `检测到深度依赖: ${[...depthChain, currentPath].join(" -> ")}`);
98
+ if (!module2.path) {
108
99
  return;
109
100
  }
110
- depthChain = [...depthChain, currentPath];
111
- if (!module2.path) {
101
+ const moduleKey = packageName + module2.path;
102
+ if (processedModules.has(moduleKey)) {
112
103
  return;
113
104
  }
114
105
  if (env.hasCompileInfo(module2.path, compileRes, mainCompileRes)) {
115
106
  return;
116
107
  }
108
+ processedModules.add(moduleKey);
117
109
  const compileInfo = {
118
110
  path: module2.path,
119
111
  code: ""
@@ -175,7 +167,7 @@ function buildJSByPath(packageName, module2, compileRes, mainCompileRes, addExtr
175
167
  if (!componentModule) {
176
168
  continue;
177
169
  }
178
- buildJSByPath(packageName, componentModule, compileRes, mainCompileRes, true, depthChain, toMainSubPackage);
170
+ buildJSByPath(packageName, componentModule, compileRes, mainCompileRes, true, toMainSubPackage);
179
171
  const props = types.objectProperty(types.identifier(`'${name}'`), types.stringLiteral(path2));
180
172
  components.value.properties.push(props);
181
173
  }
@@ -201,9 +193,7 @@ function buildJSByPath(packageName, module2, compileRes, mainCompileRes, addExtr
201
193
  id = "/" + id;
202
194
  }
203
195
  ap.node.arguments[0] = types.stringLiteral(id);
204
- if (!processedModules.has(packageName + id)) {
205
- buildJSByPath(packageName, { path: id }, compileRes, mainCompileRes, false, depthChain);
206
- }
196
+ buildJSByPath(packageName, { path: id }, compileRes, mainCompileRes, false);
207
197
  }
208
198
  }
209
199
  },
@@ -233,9 +223,7 @@ function buildJSByPath(packageName, module2, compileRes, mainCompileRes, addExtr
233
223
  }
234
224
  if (shouldProcess) {
235
225
  ap.node.source = types.stringLiteral(id);
236
- if (!processedModules.has(packageName + id)) {
237
- buildJSByPath(packageName, { path: id }, compileRes, mainCompileRes, false, depthChain);
238
- }
226
+ buildJSByPath(packageName, { path: id }, compileRes, mainCompileRes, false);
239
227
  }
240
228
  }
241
229
  }
@@ -248,7 +236,6 @@ function buildJSByPath(packageName, module2, compileRes, mainCompileRes, addExtr
248
236
  ]
249
237
  });
250
238
  compileInfo.code = code;
251
- processedModules.add(packageName + currentPath);
252
239
  }
253
240
  function getExtraInfoStatement(type, addedArgs) {
254
241
  const propertyAssignment = types.objectProperty(types.identifier("__extraInfo"), addedArgs);
@@ -47,10 +47,7 @@ ${error.stack}`);
47
47
  error: {
48
48
  message: error.message,
49
49
  stack: error.stack,
50
- name: error.name,
51
- file: error.file || null,
52
- line: error.line || null,
53
- code: error.code || null
50
+ name: error.name
54
51
  }
55
52
  });
56
53
  }
@@ -96,22 +93,17 @@ async function compileJS(pages, root, mainCompileRes, progress) {
96
93
  return compileRes;
97
94
  }
98
95
  function buildJSByPath(packageName, module, compileRes, mainCompileRes, addExtra, depthChain = [], putMain = false) {
99
- const currentPath = module.path;
100
- if (depthChain.includes(currentPath)) {
101
- console.warn("[logic]", `检测到循环依赖: ${[...depthChain, currentPath].join(" -> ")}`);
102
- return;
103
- }
104
- if (depthChain.length > 20) {
105
- console.warn("[logic]", `检测到深度依赖: ${[...depthChain, currentPath].join(" -> ")}`);
96
+ if (!module.path) {
106
97
  return;
107
98
  }
108
- depthChain = [...depthChain, currentPath];
109
- if (!module.path) {
99
+ const moduleKey = packageName + module.path;
100
+ if (processedModules.has(moduleKey)) {
110
101
  return;
111
102
  }
112
103
  if (hasCompileInfo(module.path, compileRes, mainCompileRes)) {
113
104
  return;
114
105
  }
106
+ processedModules.add(moduleKey);
115
107
  const compileInfo = {
116
108
  path: module.path,
117
109
  code: ""
@@ -173,7 +165,7 @@ function buildJSByPath(packageName, module, compileRes, mainCompileRes, addExtra
173
165
  if (!componentModule) {
174
166
  continue;
175
167
  }
176
- buildJSByPath(packageName, componentModule, compileRes, mainCompileRes, true, depthChain, toMainSubPackage);
168
+ buildJSByPath(packageName, componentModule, compileRes, mainCompileRes, true, toMainSubPackage);
177
169
  const props = types.objectProperty(types.identifier(`'${name}'`), types.stringLiteral(path));
178
170
  components.value.properties.push(props);
179
171
  }
@@ -199,9 +191,7 @@ function buildJSByPath(packageName, module, compileRes, mainCompileRes, addExtra
199
191
  id = "/" + id;
200
192
  }
201
193
  ap.node.arguments[0] = types.stringLiteral(id);
202
- if (!processedModules.has(packageName + id)) {
203
- buildJSByPath(packageName, { path: id }, compileRes, mainCompileRes, false, depthChain);
204
- }
194
+ buildJSByPath(packageName, { path: id }, compileRes, mainCompileRes, false);
205
195
  }
206
196
  }
207
197
  },
@@ -231,9 +221,7 @@ function buildJSByPath(packageName, module, compileRes, mainCompileRes, addExtra
231
221
  }
232
222
  if (shouldProcess) {
233
223
  ap.node.source = types.stringLiteral(id);
234
- if (!processedModules.has(packageName + id)) {
235
- buildJSByPath(packageName, { path: id }, compileRes, mainCompileRes, false, depthChain);
236
- }
224
+ buildJSByPath(packageName, { path: id }, compileRes, mainCompileRes, false);
237
225
  }
238
226
  }
239
227
  }
@@ -246,7 +234,6 @@ function buildJSByPath(packageName, module, compileRes, mainCompileRes, addExtra
246
234
  ]
247
235
  });
248
236
  compileInfo.code = code;
249
- processedModules.add(packageName + currentPath);
250
237
  }
251
238
  function getExtraInfoStatement(type, addedArgs) {
252
239
  const propertyAssignment = types.objectProperty(types.identifier("__extraInfo"), addedArgs);
@@ -30,6 +30,7 @@ function _interopNamespaceDefault(e) {
30
30
  const sass__namespace = /* @__PURE__ */ _interopNamespaceDefault(sass);
31
31
  const fileType = [".wxss", ".ddss", ".less", ".scss", ".sass"];
32
32
  const compileRes = /* @__PURE__ */ new Map();
33
+ const processedPaths = /* @__PURE__ */ new Set();
33
34
  if (!node_worker_threads.isMainThread) {
34
35
  node_worker_threads.parentPort.on("message", async ({ pages, storeInfo }) => {
35
36
  try {
@@ -50,7 +51,14 @@ if (!node_worker_threads.isMainThread) {
50
51
  }
51
52
  node_worker_threads.parentPort.postMessage({ success: true });
52
53
  } catch (error) {
53
- node_worker_threads.parentPort.postMessage({ success: false, error: error.message });
54
+ node_worker_threads.parentPort.postMessage({
55
+ success: false,
56
+ error: {
57
+ message: error.message,
58
+ stack: error.stack,
59
+ name: error.name
60
+ }
61
+ });
54
62
  }
55
63
  });
56
64
  }
@@ -76,15 +84,10 @@ async function compileSS(pages, root, progress) {
76
84
  }
77
85
  async function buildCompileCss(module2, depthChain = []) {
78
86
  const currentPath = module2.path;
79
- if (depthChain.includes(currentPath)) {
80
- console.warn("[style]", `检测到循环依赖: ${[...depthChain, currentPath].join(" -> ")}`);
81
- return;
82
- }
83
- if (depthChain.length > 20) {
84
- console.warn("[style]", `检测到深度依赖: ${[...depthChain, currentPath].join(" -> ")}`);
87
+ if (processedPaths.has(currentPath)) {
85
88
  return;
86
89
  }
87
- depthChain = [...depthChain, currentPath];
90
+ processedPaths.add(currentPath);
88
91
  let result = await enhanceCSS(module2) || "";
89
92
  if (module2.usingComponents) {
90
93
  for (const componentInfo of Object.values(module2.usingComponents)) {
@@ -92,7 +95,7 @@ async function buildCompileCss(module2, depthChain = []) {
92
95
  if (!componentModule) {
93
96
  continue;
94
97
  }
95
- result += await buildCompileCss(componentModule, depthChain);
98
+ result += await buildCompileCss(componentModule);
96
99
  }
97
100
  }
98
101
  return result;
@@ -11,6 +11,7 @@ import * as sass from "sass";
11
11
  import { r as resetStoreInfo, g as getTargetPath, a as getComponent, b as getContentByPath, h as tagWhiteList, e as collectAssets, f as getAppId, d as getWorkPath, t as transformRpx } from "../env-Csj3AHY4.js";
12
12
  const fileType = [".wxss", ".ddss", ".less", ".scss", ".sass"];
13
13
  const compileRes = /* @__PURE__ */ new Map();
14
+ const processedPaths = /* @__PURE__ */ new Set();
14
15
  if (!isMainThread) {
15
16
  parentPort.on("message", async ({ pages, storeInfo }) => {
16
17
  try {
@@ -31,7 +32,14 @@ if (!isMainThread) {
31
32
  }
32
33
  parentPort.postMessage({ success: true });
33
34
  } catch (error) {
34
- parentPort.postMessage({ success: false, error: error.message });
35
+ parentPort.postMessage({
36
+ success: false,
37
+ error: {
38
+ message: error.message,
39
+ stack: error.stack,
40
+ name: error.name
41
+ }
42
+ });
35
43
  }
36
44
  });
37
45
  }
@@ -57,15 +65,10 @@ async function compileSS(pages, root, progress) {
57
65
  }
58
66
  async function buildCompileCss(module, depthChain = []) {
59
67
  const currentPath = module.path;
60
- if (depthChain.includes(currentPath)) {
61
- console.warn("[style]", `检测到循环依赖: ${[...depthChain, currentPath].join(" -> ")}`);
62
- return;
63
- }
64
- if (depthChain.length > 20) {
65
- console.warn("[style]", `检测到深度依赖: ${[...depthChain, currentPath].join(" -> ")}`);
68
+ if (processedPaths.has(currentPath)) {
66
69
  return;
67
70
  }
68
- depthChain = [...depthChain, currentPath];
71
+ processedPaths.add(currentPath);
69
72
  let result = await enhanceCSS(module) || "";
70
73
  if (module.usingComponents) {
71
74
  for (const componentInfo of Object.values(module.usingComponents)) {
@@ -73,7 +76,7 @@ async function buildCompileCss(module, depthChain = []) {
73
76
  if (!componentModule) {
74
77
  continue;
75
78
  }
76
- result += await buildCompileCss(componentModule, depthChain);
79
+ result += await buildCompileCss(componentModule);
77
80
  }
78
81
  }
79
82
  return result;
@@ -29,11 +29,118 @@ 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();
143
+ const processedViewPaths = /* @__PURE__ */ new Set();
37
144
  if (!node_worker_threads.isMainThread) {
38
145
  node_worker_threads.parentPort.on("message", async ({ pages, storeInfo }) => {
39
146
  try {
@@ -54,7 +161,14 @@ if (!node_worker_threads.isMainThread) {
54
161
  }
55
162
  node_worker_threads.parentPort.postMessage({ success: true });
56
163
  } catch (error) {
57
- node_worker_threads.parentPort.postMessage({ success: false, error: error.message });
164
+ node_worker_threads.parentPort.postMessage({
165
+ success: false,
166
+ error: {
167
+ message: error.message,
168
+ stack: error.stack,
169
+ name: error.name
170
+ }
171
+ });
58
172
  }
59
173
  });
60
174
  }
@@ -125,15 +239,10 @@ function isRegisteredWxsModule(modulePath) {
125
239
  }
126
240
  function buildCompileView(module2, isComponent = false, scriptRes, depthChain = []) {
127
241
  const currentPath = module2.path;
128
- if (depthChain.includes(currentPath)) {
129
- console.warn("[view]", `检测到循环依赖: ${[...depthChain, currentPath].join(" -> ")}`);
130
- return;
131
- }
132
- if (depthChain.length > 20) {
133
- console.warn("[view]", `检测到深度依赖: ${[...depthChain, currentPath].join(" -> ")}`);
134
- return;
242
+ if (processedViewPaths.has(currentPath)) {
243
+ return null;
135
244
  }
136
- depthChain = [...depthChain, currentPath];
245
+ processedViewPaths.add(currentPath);
137
246
  const allScriptModules = [];
138
247
  const currentInstruction = compileModule(module2, isComponent, scriptRes);
139
248
  if (currentInstruction && currentInstruction.scriptModule) {
@@ -146,10 +255,10 @@ function buildCompileView(module2, isComponent = false, scriptRes, depthChain =
146
255
  continue;
147
256
  }
148
257
  if (componentModule.path === module2.path) {
149
- console.warn("[view]", `检测到循环依赖,跳过处理: ${module2.path}`);
258
+ console.warn("[view]", `检测到自依赖,跳过处理: ${module2.path}`);
150
259
  continue;
151
260
  }
152
- const componentInstruction = buildCompileView(componentModule, true, scriptRes, depthChain);
261
+ const componentInstruction = buildCompileView(componentModule, true, scriptRes);
153
262
  if (componentInstruction && componentInstruction.scriptModule) {
154
263
  for (const sm of componentInstruction.scriptModule) {
155
264
  if (!allScriptModules.find((existing) => existing.path === sm.path)) {
@@ -488,6 +597,31 @@ function compileModuleWithAllWxs(module2, scriptRes, allScriptModules) {
488
597
  compileResCache.set(module2.path, cacheData);
489
598
  scriptRes.set(module2.path, code);
490
599
  }
600
+ function processIncludeConditionalAttrs($, elem, includeContent) {
601
+ const allAttrs = $(elem).attr();
602
+ const conditionAttrs = {};
603
+ let hasCondition = false;
604
+ for (const attrName in allAttrs) {
605
+ if (attrName.endsWith(":if") || attrName.endsWith(":elif") || attrName.endsWith(":else")) {
606
+ conditionAttrs[attrName] = allAttrs[attrName];
607
+ hasCondition = true;
608
+ }
609
+ }
610
+ if (hasCondition) {
611
+ let blockAttrs = "";
612
+ for (const attrName in conditionAttrs) {
613
+ const attrValue = conditionAttrs[attrName];
614
+ if (attrValue !== void 0 && attrValue !== "") {
615
+ blockAttrs += ` ${attrName}="${attrValue}"`;
616
+ } else {
617
+ blockAttrs += ` ${attrName}`;
618
+ }
619
+ }
620
+ return `<block${blockAttrs}>${includeContent}</block>`;
621
+ } else {
622
+ return includeContent;
623
+ }
624
+ }
491
625
  function processIncludedFileWxsDependencies(content, includePath, scriptModule, components, processedPaths = /* @__PURE__ */ new Set()) {
492
626
  if (processedPaths.has(includePath)) {
493
627
  return;
@@ -581,7 +715,8 @@ function toCompileTemplate(isComponent, path2, components, componentPlaceholder,
581
715
  $includeContent("template").remove();
582
716
  $includeContent("wxs").remove();
583
717
  $includeContent("dds").remove();
584
- $(elem).replaceWith($includeContent.html());
718
+ const processedContent = processIncludeConditionalAttrs($, elem, $includeContent.html());
719
+ $(elem).replaceWith(processedContent);
585
720
  } else {
586
721
  $(elem).remove();
587
722
  }
@@ -698,7 +833,7 @@ function transTag(opts) {
698
833
  res = `dd-${tag}`;
699
834
  }
700
835
  let tagRes;
701
- const propsAry = isStart ? getProps(attrs, tag) : [];
836
+ const propsAry = isStart ? getProps(attrs, tag, components) : [];
702
837
  const multipleSlots = attrs?.slot;
703
838
  if (attrs?.slot) {
704
839
  const isDynamicSlot = isWrappedByBraces(multipleSlots);
@@ -746,8 +881,9 @@ function generateSlotDirective(slotValue) {
746
881
  return `#${slotValue}`;
747
882
  }
748
883
  }
749
- function getProps(attrs, tag) {
884
+ function getProps(attrs, tag, components) {
750
885
  const attrsList = [];
886
+ const propBindings = {};
751
887
  Object.entries(attrs).forEach(([name, value]) => {
752
888
  if (name.endsWith(":if")) {
753
889
  attrsList.push({
@@ -846,6 +982,11 @@ function getProps(attrs, tag) {
846
982
  if (tag === "template" && name === "data") {
847
983
  pVal = `{${pVal}}`;
848
984
  }
985
+ if (components && components[tag]) {
986
+ if (pVal && typeof pVal === "string") {
987
+ propBindings[name] = pVal;
988
+ }
989
+ }
849
990
  attrsList.push({
850
991
  name: `:${name}`,
851
992
  value: pVal
@@ -868,6 +1009,24 @@ function getProps(attrs, tag) {
868
1009
  propsRes.push(`${name}="${escapeQuotes(value)}"`);
869
1010
  }
870
1011
  });
1012
+ if (components && components[tag] && Object.keys(propBindings).length > 0) {
1013
+ try {
1014
+ const validBindings = {};
1015
+ for (const [key, value] of Object.entries(propBindings)) {
1016
+ if (value !== void 0 && value !== null && typeof value === "string") {
1017
+ validBindings[key] = value;
1018
+ }
1019
+ }
1020
+ if (Object.keys(validBindings).length > 0) {
1021
+ const parsedBindings = parseBindings(validBindings);
1022
+ const bindingsJson = JSON.stringify(parsedBindings);
1023
+ const escapedJson = bindingsJson.replace(/"/g, "&quot;");
1024
+ propsRes.push(`v-c-prop-bindings="${escapedJson}"`);
1025
+ }
1026
+ } catch (error) {
1027
+ console.warn("[compiler] 序列化 propBindings 失败:", error.message, "标签:", tag, "绑定数据:", propBindings);
1028
+ }
1029
+ }
871
1030
  return propsRes;
872
1031
  }
873
1032
  function generateVModelTemplate(expression) {
@@ -1192,5 +1351,6 @@ exports.generateVModelTemplate = generateVModelTemplate;
1192
1351
  exports.parseBraceExp = parseBraceExp;
1193
1352
  exports.parseClassRules = parseClassRules;
1194
1353
  exports.parseKeyExpression = parseKeyExpression;
1354
+ exports.processIncludeConditionalAttrs = processIncludeConditionalAttrs;
1195
1355
  exports.processWxsContent = processWxsContent;
1196
1356
  exports.splitWithBraces = splitWithBraces;
@@ -9,11 +9,118 @@ 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();
123
+ const processedViewPaths = /* @__PURE__ */ new Set();
17
124
  if (!isMainThread) {
18
125
  parentPort.on("message", async ({ pages, storeInfo }) => {
19
126
  try {
@@ -34,7 +141,14 @@ if (!isMainThread) {
34
141
  }
35
142
  parentPort.postMessage({ success: true });
36
143
  } catch (error) {
37
- parentPort.postMessage({ success: false, error: error.message });
144
+ parentPort.postMessage({
145
+ success: false,
146
+ error: {
147
+ message: error.message,
148
+ stack: error.stack,
149
+ name: error.name
150
+ }
151
+ });
38
152
  }
39
153
  });
40
154
  }
@@ -105,15 +219,10 @@ function isRegisteredWxsModule(modulePath) {
105
219
  }
106
220
  function buildCompileView(module, isComponent = false, scriptRes, depthChain = []) {
107
221
  const currentPath = module.path;
108
- if (depthChain.includes(currentPath)) {
109
- console.warn("[view]", `检测到循环依赖: ${[...depthChain, currentPath].join(" -> ")}`);
110
- return;
111
- }
112
- if (depthChain.length > 20) {
113
- console.warn("[view]", `检测到深度依赖: ${[...depthChain, currentPath].join(" -> ")}`);
114
- return;
222
+ if (processedViewPaths.has(currentPath)) {
223
+ return null;
115
224
  }
116
- depthChain = [...depthChain, currentPath];
225
+ processedViewPaths.add(currentPath);
117
226
  const allScriptModules = [];
118
227
  const currentInstruction = compileModule(module, isComponent, scriptRes);
119
228
  if (currentInstruction && currentInstruction.scriptModule) {
@@ -126,10 +235,10 @@ function buildCompileView(module, isComponent = false, scriptRes, depthChain = [
126
235
  continue;
127
236
  }
128
237
  if (componentModule.path === module.path) {
129
- console.warn("[view]", `检测到循环依赖,跳过处理: ${module.path}`);
238
+ console.warn("[view]", `检测到自依赖,跳过处理: ${module.path}`);
130
239
  continue;
131
240
  }
132
- const componentInstruction = buildCompileView(componentModule, true, scriptRes, depthChain);
241
+ const componentInstruction = buildCompileView(componentModule, true, scriptRes);
133
242
  if (componentInstruction && componentInstruction.scriptModule) {
134
243
  for (const sm of componentInstruction.scriptModule) {
135
244
  if (!allScriptModules.find((existing) => existing.path === sm.path)) {
@@ -468,6 +577,31 @@ function compileModuleWithAllWxs(module, scriptRes, allScriptModules) {
468
577
  compileResCache.set(module.path, cacheData);
469
578
  scriptRes.set(module.path, code);
470
579
  }
580
+ function processIncludeConditionalAttrs($, elem, includeContent) {
581
+ const allAttrs = $(elem).attr();
582
+ const conditionAttrs = {};
583
+ let hasCondition = false;
584
+ for (const attrName in allAttrs) {
585
+ if (attrName.endsWith(":if") || attrName.endsWith(":elif") || attrName.endsWith(":else")) {
586
+ conditionAttrs[attrName] = allAttrs[attrName];
587
+ hasCondition = true;
588
+ }
589
+ }
590
+ if (hasCondition) {
591
+ let blockAttrs = "";
592
+ for (const attrName in conditionAttrs) {
593
+ const attrValue = conditionAttrs[attrName];
594
+ if (attrValue !== void 0 && attrValue !== "") {
595
+ blockAttrs += ` ${attrName}="${attrValue}"`;
596
+ } else {
597
+ blockAttrs += ` ${attrName}`;
598
+ }
599
+ }
600
+ return `<block${blockAttrs}>${includeContent}</block>`;
601
+ } else {
602
+ return includeContent;
603
+ }
604
+ }
471
605
  function processIncludedFileWxsDependencies(content, includePath, scriptModule, components, processedPaths = /* @__PURE__ */ new Set()) {
472
606
  if (processedPaths.has(includePath)) {
473
607
  return;
@@ -561,7 +695,8 @@ function toCompileTemplate(isComponent, path2, components, componentPlaceholder,
561
695
  $includeContent("template").remove();
562
696
  $includeContent("wxs").remove();
563
697
  $includeContent("dds").remove();
564
- $(elem).replaceWith($includeContent.html());
698
+ const processedContent = processIncludeConditionalAttrs($, elem, $includeContent.html());
699
+ $(elem).replaceWith(processedContent);
565
700
  } else {
566
701
  $(elem).remove();
567
702
  }
@@ -678,7 +813,7 @@ function transTag(opts) {
678
813
  res = `dd-${tag}`;
679
814
  }
680
815
  let tagRes;
681
- const propsAry = isStart ? getProps(attrs, tag) : [];
816
+ const propsAry = isStart ? getProps(attrs, tag, components) : [];
682
817
  const multipleSlots = attrs?.slot;
683
818
  if (attrs?.slot) {
684
819
  const isDynamicSlot = isWrappedByBraces(multipleSlots);
@@ -726,8 +861,9 @@ function generateSlotDirective(slotValue) {
726
861
  return `#${slotValue}`;
727
862
  }
728
863
  }
729
- function getProps(attrs, tag) {
864
+ function getProps(attrs, tag, components) {
730
865
  const attrsList = [];
866
+ const propBindings = {};
731
867
  Object.entries(attrs).forEach(([name, value]) => {
732
868
  if (name.endsWith(":if")) {
733
869
  attrsList.push({
@@ -826,6 +962,11 @@ function getProps(attrs, tag) {
826
962
  if (tag === "template" && name === "data") {
827
963
  pVal = `{${pVal}}`;
828
964
  }
965
+ if (components && components[tag]) {
966
+ if (pVal && typeof pVal === "string") {
967
+ propBindings[name] = pVal;
968
+ }
969
+ }
829
970
  attrsList.push({
830
971
  name: `:${name}`,
831
972
  value: pVal
@@ -848,6 +989,24 @@ function getProps(attrs, tag) {
848
989
  propsRes.push(`${name}="${escapeQuotes(value)}"`);
849
990
  }
850
991
  });
992
+ if (components && components[tag] && Object.keys(propBindings).length > 0) {
993
+ try {
994
+ const validBindings = {};
995
+ for (const [key, value] of Object.entries(propBindings)) {
996
+ if (value !== void 0 && value !== null && typeof value === "string") {
997
+ validBindings[key] = value;
998
+ }
999
+ }
1000
+ if (Object.keys(validBindings).length > 0) {
1001
+ const parsedBindings = parseBindings(validBindings);
1002
+ const bindingsJson = JSON.stringify(parsedBindings);
1003
+ const escapedJson = bindingsJson.replace(/"/g, "&quot;");
1004
+ propsRes.push(`v-c-prop-bindings="${escapedJson}"`);
1005
+ }
1006
+ } catch (error) {
1007
+ console.warn("[compiler] 序列化 propBindings 失败:", error.message, "标签:", tag, "绑定数据:", propBindings);
1008
+ }
1009
+ }
851
1010
  return propsRes;
852
1011
  }
853
1012
  function generateVModelTemplate(expression) {
@@ -1173,6 +1332,7 @@ export {
1173
1332
  parseBraceExp,
1174
1333
  parseClassRules,
1175
1334
  parseKeyExpression,
1335
+ processIncludeConditionalAttrs,
1176
1336
  processWxsContent,
1177
1337
  splitWithBraces
1178
1338
  };
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$1() {
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$1();
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.10",
3
+ "version": "1.0.12-beta.0",
4
4
  "description": "星河编译工具",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -33,7 +33,9 @@
33
33
  "build": "vite build",
34
34
  "test": "vitest run",
35
35
  "test:dev": "vitest",
36
- "coverage": "vitest run --coverage"
36
+ "coverage": "vitest run --coverage",
37
+ "version:beta": "npm version prerelease --preid=beta",
38
+ "publish:beta": "npm publish --tag beta"
37
39
  },
38
40
  "author": "doslin",
39
41
  "license": "Apache-2.0",
@@ -45,24 +47,24 @@
45
47
  "星河"
46
48
  ],
47
49
  "dependencies": {
48
- "@babel/core": "^7.28.4",
50
+ "@babel/core": "^7.28.5",
49
51
  "@babel/plugin-transform-modules-commonjs": "^7.27.1",
50
- "@babel/traverse": "^7.28.4",
51
- "@babel/types": "^7.28.4",
52
- "@vue/compiler-sfc": "^3.5.21",
53
- "autoprefixer": "^10.4.21",
52
+ "@babel/traverse": "^7.28.5",
53
+ "@babel/types": "^7.28.5",
54
+ "@vue/compiler-sfc": "^3.5.25",
55
+ "autoprefixer": "^10.4.22",
54
56
  "cheerio": "^1.1.2",
55
57
  "chokidar": "^4.0.3",
56
- "commander": "^14.0.0",
57
- "cssnano": "^7.1.1",
58
- "esbuild": "^0.25.9",
58
+ "commander": "^14.0.2",
59
+ "cssnano": "^7.1.2",
60
+ "esbuild": "^0.25.12",
59
61
  "htmlparser2": "^10.0.0",
60
- "less": "^4.4.1",
61
- "listr2": "^9.0.3",
62
+ "less": "^4.4.2",
63
+ "listr2": "^9.0.5",
62
64
  "postcss": "^8.5.6",
63
- "postcss-selector-parser": "^7.1.0",
64
- "sass": "^1.92.1",
65
- "typescript": "^5.9.2"
65
+ "postcss-selector-parser": "^7.1.1",
66
+ "sass": "^1.95.1",
67
+ "typescript": "^5.9.3"
66
68
  },
67
69
  "publishConfig": {
68
70
  "registry": "https://registry.npmjs.org/"