@wevu/compiler 6.7.0 → 6.7.2

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.
Files changed (2) hide show
  1. package/dist/index.mjs +94 -11
  2. package/package.json +3 -3
package/dist/index.mjs CHANGED
@@ -528,6 +528,9 @@ function parseEventBinding$2(eventName) {
528
528
  name: eventName
529
529
  };
530
530
  }
531
+ function shouldUseColonEventBinding$2(name) {
532
+ return name.includes(":") || name.includes("-");
533
+ }
531
534
  /**
532
535
  * 百度智能小程序平台适配器。
533
536
  */
@@ -548,11 +551,11 @@ const swanPlatform = {
548
551
  eventBindingAttr: (eventName) => {
549
552
  const { prefix, name } = parseEventBinding$2(eventName);
550
553
  switch (prefix) {
551
- case "catch": return name.includes(":") ? `catch:${name}` : `catch${name}`;
554
+ case "catch": return shouldUseColonEventBinding$2(name) ? `catch:${name}` : `catch${name}`;
552
555
  case "capture-bind": return `capture-bind:${name}`;
553
556
  case "capture-catch": return `capture-catch:${name}`;
554
557
  case "mut-bind": return `mut-bind:${name}`;
555
- default: return name.includes(":") ? `bind:${name}` : `bind${name}`;
558
+ default: return shouldUseColonEventBinding$2(name) ? `bind:${name}` : `bind${name}`;
556
559
  }
557
560
  }
558
561
  };
@@ -592,6 +595,9 @@ function parseEventBinding$1(eventName) {
592
595
  name: eventName
593
596
  };
594
597
  }
598
+ function shouldUseColonEventBinding$1(name) {
599
+ return name.includes(":") || name.includes("-");
600
+ }
595
601
  /**
596
602
  * 抖音小程序平台适配器。
597
603
  */
@@ -612,11 +618,11 @@ const ttPlatform = {
612
618
  eventBindingAttr: (eventName) => {
613
619
  const { prefix, name } = parseEventBinding$1(eventName);
614
620
  switch (prefix) {
615
- case "catch": return name.includes(":") ? `catch:${name}` : `catch${name}`;
621
+ case "catch": return shouldUseColonEventBinding$1(name) ? `catch:${name}` : `catch${name}`;
616
622
  case "capture-bind": return `capture-bind:${name}`;
617
623
  case "capture-catch": return `capture-catch:${name}`;
618
624
  case "mut-bind": return `mut-bind:${name}`;
619
- default: return name.includes(":") ? `bind:${name}` : `bind${name}`;
625
+ default: return shouldUseColonEventBinding$1(name) ? `bind:${name}` : `bind${name}`;
620
626
  }
621
627
  }
622
628
  };
@@ -656,6 +662,9 @@ function parseEventBinding(eventName) {
656
662
  name: eventName
657
663
  };
658
664
  }
665
+ function shouldUseColonEventBinding(name) {
666
+ return name.includes(":") || name.includes("-");
667
+ }
659
668
  /**
660
669
  * 微信小程序平台适配器。
661
670
  */
@@ -676,11 +685,11 @@ const wechatPlatform = {
676
685
  eventBindingAttr: (eventName) => {
677
686
  const { prefix, name } = parseEventBinding(eventName);
678
687
  switch (prefix) {
679
- case "catch": return name.includes(":") ? `catch:${name}` : `catch${name}`;
688
+ case "catch": return shouldUseColonEventBinding(name) ? `catch:${name}` : `catch${name}`;
680
689
  case "capture-bind": return `capture-bind:${name}`;
681
690
  case "capture-catch": return `capture-catch:${name}`;
682
691
  case "mut-bind": return `mut-bind:${name}`;
683
- default: return name.includes(":") ? `bind:${name}` : `bind${name}`;
692
+ default: return shouldUseColonEventBinding(name) ? `bind:${name}` : `bind${name}`;
684
693
  }
685
694
  }
686
695
  };
@@ -756,9 +765,17 @@ function collectTopLevelReferencedNames(path) {
756
765
  if (binding.scope?.block?.type === "Program") names.add(name);
757
766
  };
758
767
  addIfTopLevelReferenced(path);
759
- path.traverse({ Identifier(p) {
760
- addIfTopLevelReferenced(p);
761
- } });
768
+ path.traverse({
769
+ Function(p) {
770
+ if (p.node !== path.node) p.skip();
771
+ },
772
+ Class(p) {
773
+ if (p.node !== path.node) p.skip();
774
+ },
775
+ Identifier(p) {
776
+ addIfTopLevelReferenced(p);
777
+ }
778
+ });
762
779
  return names;
763
780
  }
764
781
  function collectKeptStatementPaths(programPath, macroStatements) {
@@ -2658,11 +2675,30 @@ function buildInlineMapExpression(inlineExpressions) {
2658
2675
  });
2659
2676
  return t.objectExpression(entries);
2660
2677
  }
2678
+ function buildMethodsMergeFromSpreadSources(componentExpr, inlineMapExpr) {
2679
+ const spreadSources = [];
2680
+ for (const prop of componentExpr.properties) {
2681
+ if (!t.isSpreadElement(prop) || !t.isExpression(prop.argument)) continue;
2682
+ const methodsAccess = t.optionalMemberExpression(t.cloneNode(prop.argument), t.identifier("methods"), false, true);
2683
+ spreadSources.push(t.logicalExpression("||", methodsAccess, t.objectExpression([])));
2684
+ }
2685
+ if (!spreadSources.length) return null;
2686
+ return t.callExpression(t.memberExpression(t.identifier("Object"), t.identifier("assign")), [
2687
+ t.objectExpression([]),
2688
+ ...spreadSources,
2689
+ t.objectExpression([t.objectProperty(createStaticObjectKey("__weapp_vite_inline_map"), inlineMapExpr)])
2690
+ ]);
2691
+ }
2661
2692
  function injectInlineExpressions(componentExpr, inlineExpressions) {
2662
2693
  if (!inlineExpressions.length) return false;
2663
2694
  const inlineMapExpr = buildInlineMapExpression(inlineExpressions);
2664
2695
  const methodsProp = getObjectPropertyByKey$1(componentExpr, "methods");
2665
2696
  if (!methodsProp) {
2697
+ const mergedMethods = buildMethodsMergeFromSpreadSources(componentExpr, inlineMapExpr);
2698
+ if (mergedMethods) {
2699
+ componentExpr.properties.push(t.objectProperty(createStaticObjectKey("methods"), mergedMethods));
2700
+ return true;
2701
+ }
2666
2702
  componentExpr.properties.push(t.objectProperty(createStaticObjectKey("methods"), t.objectExpression([t.objectProperty(createStaticObjectKey("__weapp_vite_inline_map"), inlineMapExpr)])));
2667
2703
  return true;
2668
2704
  }
@@ -6410,6 +6446,49 @@ function resolveScriptSetupExtension(lang) {
6410
6446
  function isIdentifierLikeKey(key) {
6411
6447
  return /^[A-Z_$][\w$]*$/i.test(key);
6412
6448
  }
6449
+ const SERIALIZABLE_NATIVE_FUNCTIONS = new Map([
6450
+ [String, "String"],
6451
+ [Number, "Number"],
6452
+ [Boolean, "Boolean"],
6453
+ [Object, "Object"],
6454
+ [Array, "Array"],
6455
+ [Function, "Function"],
6456
+ [Date, "Date"],
6457
+ [RegExp, "RegExp"],
6458
+ [Map, "Map"],
6459
+ [Set, "Set"],
6460
+ [WeakMap, "WeakMap"],
6461
+ [WeakSet, "WeakSet"],
6462
+ [Promise, "Promise"]
6463
+ ]);
6464
+ function isParsableFunctionExpressionSource(source) {
6465
+ try {
6466
+ parse$2(`(${source})`, BABEL_TS_MODULE_PARSER_OPTIONS);
6467
+ return true;
6468
+ } catch {
6469
+ return false;
6470
+ }
6471
+ }
6472
+ function tryConvertObjectMethodSourceToFunctionExpression(source) {
6473
+ try {
6474
+ const statement = parse$2(`({ ${source} })`, BABEL_TS_MODULE_PARSER_OPTIONS).program.body[0];
6475
+ if (!statement || !t.isExpressionStatement(statement) || !t.isObjectExpression(statement.expression)) return null;
6476
+ const firstProperty = statement.expression.properties[0];
6477
+ if (!firstProperty) return null;
6478
+ if (t.isObjectMethod(firstProperty)) {
6479
+ const functionName = t.isIdentifier(firstProperty.key) ? t.identifier(firstProperty.key.name) : null;
6480
+ return generate(t.functionExpression(functionName, firstProperty.params, firstProperty.body, firstProperty.generator, firstProperty.async)).code;
6481
+ }
6482
+ if (t.isObjectProperty(firstProperty) && (t.isFunctionExpression(firstProperty.value) || t.isArrowFunctionExpression(firstProperty.value))) return generate(firstProperty.value).code;
6483
+ } catch {}
6484
+ return null;
6485
+ }
6486
+ function normalizeFunctionSourceToExpression(source) {
6487
+ if (isParsableFunctionExpressionSource(source)) return source;
6488
+ const converted = tryConvertObjectMethodSourceToFunctionExpression(source);
6489
+ if (converted && isParsableFunctionExpressionSource(converted)) return converted;
6490
+ throw new Error("defineOptions 的参数中包含无法序列化的函数值。");
6491
+ }
6413
6492
  function serializeStaticValueToExpression(value, seen = /* @__PURE__ */ new WeakSet()) {
6414
6493
  if (value === null) return "null";
6415
6494
  if (value === void 0) return "undefined";
@@ -6427,8 +6506,12 @@ function serializeStaticValueToExpression(value, seen = /* @__PURE__ */ new Weak
6427
6506
  if (valueType === "symbol") throw new Error("defineOptions 的参数中不支持 Symbol 值。");
6428
6507
  if (valueType === "function") {
6429
6508
  const source = Function.prototype.toString.call(value);
6430
- if (source.includes("[native code]")) throw new Error("defineOptions 的参数中不支持原生函数值。");
6431
- return `(${source})`;
6509
+ if (source.includes("[native code]")) {
6510
+ const nativeLiteral = SERIALIZABLE_NATIVE_FUNCTIONS.get(value);
6511
+ if (nativeLiteral) return nativeLiteral;
6512
+ throw new Error("defineOptions 的参数中不支持原生函数值。");
6513
+ }
6514
+ return `(${normalizeFunctionSourceToExpression(source)})`;
6432
6515
  }
6433
6516
  if (value instanceof Date) return `new Date(${JSON.stringify(value.toISOString())})`;
6434
6517
  if (value instanceof RegExp) return value.toString();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@wevu/compiler",
3
3
  "type": "module",
4
- "version": "6.7.0",
4
+ "version": "6.7.2",
5
5
  "description": "wevu 编译器基础包,面向小程序模板的编译与转换",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",
@@ -46,8 +46,8 @@
46
46
  "@babel/traverse": "^7.29.0",
47
47
  "@babel/types": "^7.29.0",
48
48
  "@vue/compiler-core": "^3.5.29",
49
- "comment-json": "^4.5.1",
50
- "fs-extra": "^11.3.3",
49
+ "comment-json": "^4.6.2",
50
+ "fs-extra": "^11.3.4",
51
51
  "lru-cache": "^11.2.6",
52
52
  "magic-string": "^0.30.21",
53
53
  "merge": "^2.1.1",