@wevu/compiler 6.7.0 → 6.7.1

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 +79 -5
  2. package/package.json +2 -2
package/dist/index.mjs CHANGED
@@ -756,9 +756,17 @@ function collectTopLevelReferencedNames(path) {
756
756
  if (binding.scope?.block?.type === "Program") names.add(name);
757
757
  };
758
758
  addIfTopLevelReferenced(path);
759
- path.traverse({ Identifier(p) {
760
- addIfTopLevelReferenced(p);
761
- } });
759
+ path.traverse({
760
+ Function(p) {
761
+ if (p.node !== path.node) p.skip();
762
+ },
763
+ Class(p) {
764
+ if (p.node !== path.node) p.skip();
765
+ },
766
+ Identifier(p) {
767
+ addIfTopLevelReferenced(p);
768
+ }
769
+ });
762
770
  return names;
763
771
  }
764
772
  function collectKeptStatementPaths(programPath, macroStatements) {
@@ -2658,11 +2666,30 @@ function buildInlineMapExpression(inlineExpressions) {
2658
2666
  });
2659
2667
  return t.objectExpression(entries);
2660
2668
  }
2669
+ function buildMethodsMergeFromSpreadSources(componentExpr, inlineMapExpr) {
2670
+ const spreadSources = [];
2671
+ for (const prop of componentExpr.properties) {
2672
+ if (!t.isSpreadElement(prop) || !t.isExpression(prop.argument)) continue;
2673
+ const methodsAccess = t.optionalMemberExpression(t.cloneNode(prop.argument), t.identifier("methods"), false, true);
2674
+ spreadSources.push(t.logicalExpression("||", methodsAccess, t.objectExpression([])));
2675
+ }
2676
+ if (!spreadSources.length) return null;
2677
+ return t.callExpression(t.memberExpression(t.identifier("Object"), t.identifier("assign")), [
2678
+ t.objectExpression([]),
2679
+ ...spreadSources,
2680
+ t.objectExpression([t.objectProperty(createStaticObjectKey("__weapp_vite_inline_map"), inlineMapExpr)])
2681
+ ]);
2682
+ }
2661
2683
  function injectInlineExpressions(componentExpr, inlineExpressions) {
2662
2684
  if (!inlineExpressions.length) return false;
2663
2685
  const inlineMapExpr = buildInlineMapExpression(inlineExpressions);
2664
2686
  const methodsProp = getObjectPropertyByKey$1(componentExpr, "methods");
2665
2687
  if (!methodsProp) {
2688
+ const mergedMethods = buildMethodsMergeFromSpreadSources(componentExpr, inlineMapExpr);
2689
+ if (mergedMethods) {
2690
+ componentExpr.properties.push(t.objectProperty(createStaticObjectKey("methods"), mergedMethods));
2691
+ return true;
2692
+ }
2666
2693
  componentExpr.properties.push(t.objectProperty(createStaticObjectKey("methods"), t.objectExpression([t.objectProperty(createStaticObjectKey("__weapp_vite_inline_map"), inlineMapExpr)])));
2667
2694
  return true;
2668
2695
  }
@@ -6410,6 +6437,49 @@ function resolveScriptSetupExtension(lang) {
6410
6437
  function isIdentifierLikeKey(key) {
6411
6438
  return /^[A-Z_$][\w$]*$/i.test(key);
6412
6439
  }
6440
+ const SERIALIZABLE_NATIVE_FUNCTIONS = new Map([
6441
+ [String, "String"],
6442
+ [Number, "Number"],
6443
+ [Boolean, "Boolean"],
6444
+ [Object, "Object"],
6445
+ [Array, "Array"],
6446
+ [Function, "Function"],
6447
+ [Date, "Date"],
6448
+ [RegExp, "RegExp"],
6449
+ [Map, "Map"],
6450
+ [Set, "Set"],
6451
+ [WeakMap, "WeakMap"],
6452
+ [WeakSet, "WeakSet"],
6453
+ [Promise, "Promise"]
6454
+ ]);
6455
+ function isParsableFunctionExpressionSource(source) {
6456
+ try {
6457
+ parse$2(`(${source})`, BABEL_TS_MODULE_PARSER_OPTIONS);
6458
+ return true;
6459
+ } catch {
6460
+ return false;
6461
+ }
6462
+ }
6463
+ function tryConvertObjectMethodSourceToFunctionExpression(source) {
6464
+ try {
6465
+ const statement = parse$2(`({ ${source} })`, BABEL_TS_MODULE_PARSER_OPTIONS).program.body[0];
6466
+ if (!statement || !t.isExpressionStatement(statement) || !t.isObjectExpression(statement.expression)) return null;
6467
+ const firstProperty = statement.expression.properties[0];
6468
+ if (!firstProperty) return null;
6469
+ if (t.isObjectMethod(firstProperty)) {
6470
+ const functionName = t.isIdentifier(firstProperty.key) ? t.identifier(firstProperty.key.name) : null;
6471
+ return generate(t.functionExpression(functionName, firstProperty.params, firstProperty.body, firstProperty.generator, firstProperty.async)).code;
6472
+ }
6473
+ if (t.isObjectProperty(firstProperty) && (t.isFunctionExpression(firstProperty.value) || t.isArrowFunctionExpression(firstProperty.value))) return generate(firstProperty.value).code;
6474
+ } catch {}
6475
+ return null;
6476
+ }
6477
+ function normalizeFunctionSourceToExpression(source) {
6478
+ if (isParsableFunctionExpressionSource(source)) return source;
6479
+ const converted = tryConvertObjectMethodSourceToFunctionExpression(source);
6480
+ if (converted && isParsableFunctionExpressionSource(converted)) return converted;
6481
+ throw new Error("defineOptions 的参数中包含无法序列化的函数值。");
6482
+ }
6413
6483
  function serializeStaticValueToExpression(value, seen = /* @__PURE__ */ new WeakSet()) {
6414
6484
  if (value === null) return "null";
6415
6485
  if (value === void 0) return "undefined";
@@ -6427,8 +6497,12 @@ function serializeStaticValueToExpression(value, seen = /* @__PURE__ */ new Weak
6427
6497
  if (valueType === "symbol") throw new Error("defineOptions 的参数中不支持 Symbol 值。");
6428
6498
  if (valueType === "function") {
6429
6499
  const source = Function.prototype.toString.call(value);
6430
- if (source.includes("[native code]")) throw new Error("defineOptions 的参数中不支持原生函数值。");
6431
- return `(${source})`;
6500
+ if (source.includes("[native code]")) {
6501
+ const nativeLiteral = SERIALIZABLE_NATIVE_FUNCTIONS.get(value);
6502
+ if (nativeLiteral) return nativeLiteral;
6503
+ throw new Error("defineOptions 的参数中不支持原生函数值。");
6504
+ }
6505
+ return `(${normalizeFunctionSourceToExpression(source)})`;
6432
6506
  }
6433
6507
  if (value instanceof Date) return `new Date(${JSON.stringify(value.toISOString())})`;
6434
6508
  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.1",
5
5
  "description": "wevu 编译器基础包,面向小程序模板的编译与转换",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",
@@ -46,7 +46,7 @@
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",
49
+ "comment-json": "^4.6.2",
50
50
  "fs-extra": "^11.3.3",
51
51
  "lru-cache": "^11.2.6",
52
52
  "magic-string": "^0.30.21",