@wevu/compiler 6.16.43 → 6.16.44

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/index.d.mts CHANGED
@@ -748,7 +748,7 @@ interface TransformScriptOptions {
748
748
  */
749
749
  isPage?: boolean;
750
750
  /**
751
- * <script setup> 中引入的组件:编译时移除 import,并提供同名元信息对象占位,避免运行时访问时报错。
751
+ * <script setup> 中仅由模板使用的导入组件:编译时移除 import 与 Vue 编译器自动返回的 getter。
752
752
  * key: 组件别名(需与模板标签一致),value: usingComponents 的 from(如 `/components/foo/index`)
753
753
  */
754
754
  templateComponentMeta?: Record<string, string>;
package/dist/index.mjs CHANGED
@@ -1821,18 +1821,83 @@ function createPageEntryMatcher(source) {
1821
1821
  }
1822
1822
  //#endregion
1823
1823
  //#region src/plugins/vue/transform/scriptTemplateMeta.ts
1824
- function injectTemplateComponentMeta(ast, templateComponentMeta) {
1824
+ function isScriptSetupReturnedObject(path) {
1825
+ const parent = path.parentPath;
1826
+ if (parent?.isVariableDeclarator() && t.isIdentifier(parent.node.id, { name: "__returned__" })) return true;
1827
+ if (parent?.isReturnStatement()) return true;
1828
+ return false;
1829
+ }
1830
+ function isTemplateReturnGetterReference(path) {
1831
+ const objectMethod = path.findParent((parent) => parent.isObjectMethod());
1832
+ if (objectMethod?.isObjectMethod() && objectMethod.node.kind === "get" && !objectMethod.node.computed && t.isIdentifier(objectMethod.node.key, { name: path.node.name }) && objectMethod.parentPath?.isObjectExpression() && isScriptSetupReturnedObject(objectMethod.parentPath)) {
1833
+ const getter = objectMethod.node.body.body.find((statement) => t.isReturnStatement(statement));
1834
+ return Boolean(getter && t.isReturnStatement(getter) && t.isIdentifier(getter.argument, { name: path.node.name }));
1835
+ }
1836
+ return false;
1837
+ }
1838
+ function isTemplateReturnPropertyReference(path) {
1839
+ const property = path.parentPath;
1840
+ if (!property?.isObjectProperty()) return false;
1841
+ if (!property.parentPath?.isObjectExpression() || !isScriptSetupReturnedObject(property.parentPath) || property.node.computed) return false;
1842
+ const key = property.node.key;
1843
+ const value = property.node.value;
1844
+ if (!t.isIdentifier(value, { name: path.node.name })) return false;
1845
+ if (t.isIdentifier(key, { name: path.node.name }) || t.isStringLiteral(key, { value: path.node.name })) return true;
1846
+ return false;
1847
+ }
1848
+ function isReferencedOutsideTemplateReturn(path) {
1849
+ if (!path.isReferencedIdentifier()) return false;
1850
+ if (isTemplateReturnGetterReference(path) || isTemplateReturnPropertyReference(path)) return false;
1851
+ return true;
1852
+ }
1853
+ function collectReferencedNames(ast, candidateNames) {
1854
+ const referencedNames = /* @__PURE__ */ new Set();
1855
+ traverse(ast, { Identifier(path) {
1856
+ const name = path.node.name;
1857
+ if (!candidateNames.has(name)) return;
1858
+ if (isReferencedOutsideTemplateReturn(path)) referencedNames.add(name);
1859
+ } });
1860
+ return referencedNames;
1861
+ }
1862
+ function removeSetupReturnProperties(ast, removableNames) {
1863
+ if (!removableNames.size) return false;
1864
+ let changed = false;
1865
+ traverse(ast, {
1866
+ ObjectMethod(path) {
1867
+ if (!path.parentPath?.isObjectExpression() || !isScriptSetupReturnedObject(path.parentPath)) return;
1868
+ if (path.node.computed || !t.isIdentifier(path.node.key)) return;
1869
+ const name = path.node.key.name;
1870
+ if (!removableNames.has(name)) return;
1871
+ if (path.node.kind !== "get") return;
1872
+ const getter = path.node.body.body.find((statement) => t.isReturnStatement(statement));
1873
+ if (!getter || !t.isReturnStatement(getter) || !t.isIdentifier(getter.argument, { name })) return;
1874
+ path.remove();
1875
+ changed = true;
1876
+ },
1877
+ ObjectProperty(path) {
1878
+ if (!path.parentPath?.isObjectExpression() || !isScriptSetupReturnedObject(path.parentPath)) return;
1879
+ if (path.node.computed || !t.isIdentifier(path.node.value)) return;
1880
+ const name = path.node.value.name;
1881
+ if (!removableNames.has(name)) return;
1882
+ if (!t.isIdentifier(path.node.key, { name }) && !t.isStringLiteral(path.node.key, { value: name })) return;
1883
+ path.remove();
1884
+ changed = true;
1885
+ }
1886
+ });
1887
+ return changed;
1888
+ }
1889
+ function pruneTemplateComponentMeta(ast, templateComponentMeta) {
1825
1890
  if (!Object.keys(templateComponentMeta).length) return false;
1826
- const metaMap = templateComponentMeta;
1827
- const candidateNames = new Set(Object.keys(metaMap));
1828
- const injectedNames = /* @__PURE__ */ new Set();
1891
+ const candidateNames = new Set(Object.keys(templateComponentMeta));
1892
+ const referencedNames = collectReferencedNames(ast, candidateNames);
1893
+ const removableNames = new Set([...candidateNames].filter((name) => !referencedNames.has(name)));
1829
1894
  let changed = false;
1830
1895
  traverse(ast, { ImportDeclaration(path) {
1831
1896
  if (!path.node.specifiers.length) return;
1832
1897
  const kept = path.node.specifiers.filter((specifier) => {
1833
1898
  if (!("local" in specifier) || !t.isIdentifier(specifier.local)) return true;
1834
1899
  const localName = specifier.local.name;
1835
- return !candidateNames.has(localName);
1900
+ return !removableNames.has(localName);
1836
1901
  });
1837
1902
  if (kept.length !== path.node.specifiers.length) {
1838
1903
  changed = true;
@@ -1843,23 +1908,7 @@ function injectTemplateComponentMeta(ast, templateComponentMeta) {
1843
1908
  path.node.specifiers = kept;
1844
1909
  }
1845
1910
  } });
1846
- const decls = [];
1847
- for (const name of Object.keys(metaMap)) {
1848
- if (injectedNames.has(name)) continue;
1849
- injectedNames.add(name);
1850
- decls.push(t.variableDeclaration("const", [t.variableDeclarator(t.identifier(name), t.objectExpression([
1851
- t.objectProperty(t.identifier("__weappViteUsingComponent"), t.booleanLiteral(true)),
1852
- t.objectProperty(t.identifier("name"), t.stringLiteral(name)),
1853
- t.objectProperty(t.identifier("from"), t.stringLiteral(metaMap[name]))
1854
- ]))]));
1855
- }
1856
- if (decls.length) {
1857
- const body = ast.program.body;
1858
- let insertAt = 0;
1859
- while (insertAt < body.length && t.isImportDeclaration(body[insertAt])) insertAt += 1;
1860
- body.splice(insertAt, 0, ...decls);
1861
- changed = true;
1862
- }
1911
+ changed = removeSetupReturnProperties(ast, removableNames) || changed;
1863
1912
  return changed;
1864
1913
  }
1865
1914
  //#endregion
@@ -2867,6 +2916,7 @@ function rewriteComponentExport(params) {
2867
2916
  ensureRuntimeImport(ast.program, WE_VU_RUNTIME_APIS.createWevuComponent);
2868
2917
  exportPath.replaceWith(t.variableDeclaration("const", [t.variableDeclarator(t.identifier(DEFAULT_OPTIONS_IDENTIFIER), componentExpr)]));
2869
2918
  exportPath.insertAfter(t.expressionStatement(t.callExpression(t.identifier(WE_VU_RUNTIME_APIS.createWevuComponent), [t.identifier(DEFAULT_OPTIONS_IDENTIFIER)])));
2919
+ exportPath.insertAfter(t.exportDefaultDeclaration(t.identifier(DEFAULT_OPTIONS_IDENTIFIER)));
2870
2920
  return true;
2871
2921
  }
2872
2922
  //#endregion
@@ -3390,7 +3440,7 @@ function transformScript(source, options) {
3390
3440
  ...createImportVisitors(ast.program, state),
3391
3441
  ...createCollectVisitors(state)
3392
3442
  });
3393
- if (options?.templateComponentMeta) state.transformed = injectTemplateComponentMeta(ast, options.templateComponentMeta) || state.transformed;
3443
+ if (options?.templateComponentMeta) state.transformed = pruneTemplateComponentMeta(ast, options.templateComponentMeta) || state.transformed;
3394
3444
  state.transformed = rewriteDefaultExport(ast, state, options, enabledPageFeatures, serializedWevuDefaults, parsedWevuDefaults) || state.transformed;
3395
3445
  if (!state.transformed) return {
3396
3446
  code: source,
@@ -7763,7 +7813,7 @@ function transformComponentWithSlots(node, context, transformNode, options) {
7763
7813
  if (scopedSlotDeclarations.length) {
7764
7814
  const scopePropsExp = buildScopePropsExpression(context);
7765
7815
  if (scopePropsExp) mergedAttrs.push(`${WEVU_SLOT_SCOPE_ATTR}="${renderMustache(scopePropsExp, context)}"`);
7766
- const ownerIdExp = context.rewriteScopedSlot ? `${WEVU_SLOT_OWNER_ID_PROP} || ${WEVU_SLOT_OWNER_ID_KEY} || ''` : `${WEVU_SLOT_OWNER_ID_KEY} || ''`;
7816
+ const ownerIdExp = context.rewriteScopedSlot ? `${WEVU_SLOT_OWNER_ID_PROP} || ${WEVU_SLOT_OWNER_ID_KEY} || ''` : `${WEVU_SLOT_OWNER_ID_PROP} || ${WEVU_SLOT_OWNER_ID_KEY} || ''`;
7767
7817
  mergedAttrs.push(`${WEVU_SLOT_OWNER_ID_ATTR}="${renderMustache(ownerIdExp, context)}"`);
7768
7818
  }
7769
7819
  const attrString = mergedAttrs.length ? ` ${mergedAttrs.join(" ")}` : "";
@@ -9280,6 +9330,20 @@ function warnReservedScriptSetupProps(scriptSetupCode, warn, context = {}) {
9280
9330
  //#endregion
9281
9331
  //#region src/plugins/vue/transform/compileVueFile/script.ts
9282
9332
  const TYPE_ONLY_DEFINE_PROPS_RE = /\bdefineProps\s*</;
9333
+ const EXPORT_DEFAULT_RE = /\bexport\s+default\b/;
9334
+ function hasDefaultExport(scriptCode) {
9335
+ try {
9336
+ const ast = parseJsLike(scriptCode);
9337
+ let found = false;
9338
+ traverse(ast, { ExportDefaultDeclaration(path) {
9339
+ found = true;
9340
+ path.stop();
9341
+ } });
9342
+ return found;
9343
+ } catch {
9344
+ return EXPORT_DEFAULT_RE.test(scriptCode);
9345
+ }
9346
+ }
9283
9347
  function resolveScriptSetupPropsAliases(bindings) {
9284
9348
  const aliases = bindings?.__propsAliases;
9285
9349
  if (!aliases || typeof aliases !== "object") return;
@@ -9387,7 +9451,7 @@ async function compileScriptPhase(descriptor, descriptorForCompile, filename, op
9387
9451
  propsDerivedKeys = resolveEffectivePropsDerivedKeys(scriptCompiled.bindings, scriptCode);
9388
9452
  scriptMap = scriptCompiled.map && typeof scriptCompiled.map === "object" ? scriptCompiled.map : null;
9389
9453
  if (scriptCode.includes("defineAppJson") || scriptCode.includes("definePageJson") || scriptCode.includes("defineComponentJson")) scriptCode = stripJsonMacroCallsFromCode(scriptCode, filename);
9390
- if (!isAppFile && !scriptCode.includes("export default")) scriptCode += "\nexport default {}";
9454
+ if (!isAppFile && !hasDefaultExport(scriptCode)) scriptCode += "\nexport default {}";
9391
9455
  } else scriptCode = "export default {}";
9392
9456
  if (scriptCode) {
9393
9457
  const transformed = transformScript(scriptCode, {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@wevu/compiler",
3
3
  "type": "module",
4
- "version": "6.16.43",
4
+ "version": "6.16.44",
5
5
  "description": "wevu 编译器基础包,面向小程序模板的编译与转换",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",
@@ -42,18 +42,18 @@
42
42
  ],
43
43
  "dependencies": {
44
44
  "@jridgewell/remapping": "^2.3.5",
45
- "@vue/compiler-core": "^3.5.35",
46
- "@vue/compiler-dom": "^3.5.35",
45
+ "@vue/compiler-core": "^3.5.38",
46
+ "@vue/compiler-dom": "^3.5.38",
47
47
  "comment-json": "^5.0.0",
48
48
  "lru-cache": "^11.5.1",
49
49
  "magic-string": "^0.30.21",
50
50
  "merge": "^2.1.1",
51
51
  "pathe": "^2.0.3",
52
- "vue": "^3.5.35",
52
+ "vue": "^3.5.38",
53
53
  "@weapp-core/constants": "0.1.12",
54
54
  "@weapp-core/shared": "3.0.4",
55
- "@weapp-vite/ast": "6.16.43",
56
- "rolldown-require": "2.0.18"
55
+ "@weapp-vite/ast": "6.16.44",
56
+ "rolldown-require": "2.0.19"
57
57
  },
58
58
  "publishConfig": {
59
59
  "access": "public",