@wevu/compiler 6.11.1 → 6.11.4

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
@@ -231,6 +231,7 @@ interface TemplateCompileResult {
231
231
  classStyleBindings?: ClassStyleBinding[];
232
232
  classStyleWxs?: boolean;
233
233
  templateRefs?: TemplateRefBinding[];
234
+ layoutHosts?: LayoutHostBinding[];
234
235
  inlineExpressions?: InlineExpressionAsset[];
235
236
  }
236
237
  /**
@@ -295,6 +296,15 @@ interface TemplateRefBinding {
295
296
  expAst?: Expression;
296
297
  kind?: 'component' | 'element';
297
298
  }
299
+ /**
300
+ * layout 宿主绑定信息。
301
+ */
302
+ interface LayoutHostBinding {
303
+ key: string;
304
+ refName?: string;
305
+ selector: string;
306
+ kind?: 'component';
307
+ }
298
308
  //#endregion
299
309
  //#region src/types/platform.d.ts
300
310
  /**
@@ -621,6 +631,10 @@ interface TransformScriptOptions {
621
631
  * template ref 元数据(用于运行时绑定)
622
632
  */
623
633
  templateRefs?: TemplateRefBinding[];
634
+ /**
635
+ * layout host 元数据(用于运行时绑定)
636
+ */
637
+ layoutHosts?: LayoutHostBinding[];
624
638
  /**
625
639
  * 内联表达式元数据(用于事件处理)
626
640
  */
package/dist/index.mjs CHANGED
@@ -385,8 +385,8 @@ function getWevuConfigCacheRoot() {
385
385
  const env = process.env.WEAPP_VITE_WEVU_CONFIG_DIR?.trim();
386
386
  if (env) return path.resolve(env);
387
387
  const cwd = process.cwd();
388
- const nodeModulesDir = path.join(cwd, "node_modules");
389
- if (fs$1.existsSync(nodeModulesDir)) return path.join(nodeModulesDir, ".cache", "weapp-vite", "wevu-config");
388
+ const projectCacheDir = path.join(cwd, ".wevu-config");
389
+ if (fs$1.existsSync(cwd)) return projectCacheDir;
390
390
  return path.join(os.tmpdir(), "weapp-vite", "wevu-config");
391
391
  }
392
392
  function resolveWevuConfigTempDir(fromDir) {
@@ -414,7 +414,10 @@ async function evaluateScriptSetupJsonMacro(params) {
414
414
  const { originalContent, filename, lang, macroName, macroStatements, bodyPaths, keptStatementPaths, options } = params;
415
415
  const ms = new MagicString(originalContent);
416
416
  const dir = path.dirname(filename);
417
- const tempDir = resolveWevuConfigTempDir(dir);
417
+ const tempRoot = resolveWevuConfigTempDir(dir);
418
+ const basename = path.basename(filename, path.extname(filename));
419
+ const unique = `${Date.now()}-${Math.random().toString(16).slice(2)}`;
420
+ const tempDir = path.join(tempRoot, `${basename}.json-macro.${unique}`);
418
421
  for (const statementPath of keptStatementPaths) {
419
422
  if (!statementPath.isImportDeclaration()) continue;
420
423
  const sourceNode = statementPath.node.source;
@@ -444,10 +447,8 @@ const __weapp_defineSitemapJson = (config) => (__weapp_json_macro_values.push(co
444
447
  const __weapp_defineThemeJson = (config) => (__weapp_json_macro_values.push(config), config)
445
448
  `.trimStart() + ms.toString() + `\nexport default __weapp_json_macro_values\n`;
446
449
  const extension = resolveScriptSetupExtension$1(lang);
447
- return await withTempDirLock(tempDir, async () => {
450
+ return await withTempDirLock(tempRoot, async () => {
448
451
  await fs.ensureDir(tempDir);
449
- const basename = path.basename(filename, path.extname(filename));
450
- const unique = `${Date.now()}-${Math.random().toString(16).slice(2)}`;
451
452
  const tempFile = path.join(tempDir, `${basename}.json-macro.${unique}.${extension}`);
452
453
  await fs.writeFile(tempFile, evalSource, "utf8");
453
454
  try {
@@ -477,10 +478,7 @@ const __weapp_defineThemeJson = (config) => (__weapp_json_macro_values.push(conf
477
478
  };
478
479
  } finally {
479
480
  try {
480
- await fs.remove(tempFile);
481
- } catch {}
482
- try {
483
- if ((await fs.readdir(tempDir)).length === 0) await fs.remove(tempDir);
481
+ await fs.remove(tempDir);
484
482
  } catch {}
485
483
  }
486
484
  });
@@ -2555,6 +2553,37 @@ function injectInlineExpressions(componentExpr, inlineExpressions) {
2555
2553
  return false;
2556
2554
  }
2557
2555
  //#endregion
2556
+ //#region src/plugins/vue/transform/transformScript/rewrite/layoutHosts.ts
2557
+ function buildLayoutHostEntry(binding) {
2558
+ const props = [t.objectProperty(t.identifier("key"), t.stringLiteral(binding.key))];
2559
+ if (binding.refName) props.push(t.objectProperty(t.identifier("refName"), t.stringLiteral(binding.refName)));
2560
+ props.push(t.objectProperty(t.identifier("selector"), t.stringLiteral(binding.selector)));
2561
+ if (binding.kind) props.push(t.objectProperty(t.identifier("kind"), t.stringLiteral(binding.kind)));
2562
+ return t.objectExpression(props);
2563
+ }
2564
+ function injectLayoutHosts(optionsObject, bindings, warn) {
2565
+ if (!bindings.length) return false;
2566
+ const warnHandler = resolveWarnHandler(warn);
2567
+ const entries = bindings.map((binding) => buildLayoutHostEntry(binding));
2568
+ const hostsArray = t.arrayExpression(entries);
2569
+ const key = createStaticObjectKey$1("__wevuLayoutHosts");
2570
+ const existing = getObjectPropertyByKey$1(optionsObject, "__wevuLayoutHosts");
2571
+ if (!existing) {
2572
+ optionsObject.properties.push(t.objectProperty(key, hostsArray));
2573
+ return true;
2574
+ }
2575
+ if (t.isArrayExpression(existing.value)) {
2576
+ existing.value.elements.push(...entries);
2577
+ return true;
2578
+ }
2579
+ if (t.isIdentifier(existing.value) || t.isMemberExpression(existing.value)) {
2580
+ existing.value = t.arrayExpression([...entries, t.spreadElement(t.cloneNode(existing.value, true))]);
2581
+ return true;
2582
+ }
2583
+ warnHandler("无法自动注入 layout host 元数据,请手动合并 __wevuLayoutHosts。");
2584
+ return false;
2585
+ }
2586
+ //#endregion
2558
2587
  //#region src/plugins/vue/transform/transformScript/rewrite/setupInitialData.ts
2559
2588
  function unwrapExpression(node) {
2560
2589
  if (t.isTSAsExpression(node) || t.isTSSatisfiesExpression(node) || t.isTSNonNullExpression(node) || t.isTypeCastExpression(node) || t.isParenthesizedExpression(node)) return unwrapExpression(node.expression);
@@ -2817,6 +2846,9 @@ function rewriteDefaultExport(ast, state, options, enabledPageFeatures, serializ
2817
2846
  const templateRefs = options?.templateRefs ?? [];
2818
2847
  if (templateRefs.length) if (componentOptionsObject) transformed = injectTemplateRefs(componentOptionsObject, templateRefs, warn) || transformed;
2819
2848
  else warn("无法自动注入 template ref 元数据:组件选项不是对象字面量。");
2849
+ const layoutHosts = options?.layoutHosts ?? [];
2850
+ if (layoutHosts.length) if (componentOptionsObject) transformed = injectLayoutHosts(componentOptionsObject, layoutHosts, warn) || transformed;
2851
+ else warn("无法自动注入 layout host 元数据:组件选项不是对象字面量。");
2820
2852
  const inlineExpressions = options?.inlineExpressions ?? [];
2821
2853
  if (inlineExpressions.length) if (componentOptionsObject) {
2822
2854
  const injected = injectInlineExpressions(componentOptionsObject, inlineExpressions);
@@ -5811,15 +5843,25 @@ function collectElementAttributes(node, context, options) {
5811
5843
  const isComponentElement = options?.isComponent ?? !isBuiltinTag(node.tag);
5812
5844
  const attrs = options?.extraAttrs ? [...options.extraAttrs] : [];
5813
5845
  let staticClass;
5846
+ let staticId;
5814
5847
  let dynamicClassExp;
5815
5848
  let staticStyle;
5816
5849
  let dynamicStyleExp;
5817
5850
  let vShowExp;
5818
5851
  let vTextExp;
5819
5852
  let templateRef;
5853
+ let layoutHostKey;
5854
+ let hasDynamicIdBinding = false;
5820
5855
  const inFor = Boolean(options?.forInfo || context.forStack.length);
5821
5856
  for (const prop of props) {
5822
5857
  if (prop.type === NodeTypes.ATTRIBUTE) {
5858
+ if (prop.name === "layout-host") {
5859
+ const rawKey = prop.value?.type === NodeTypes.TEXT ? prop.value.content.trim() : "";
5860
+ if (!isComponentElement) context.warnings.push("layout-host 仅支持声明在组件节点上,当前节点已忽略。");
5861
+ else if (!rawKey) context.warnings.push("layout-host 需要提供非空字符串 key。");
5862
+ else layoutHostKey = rawKey;
5863
+ continue;
5864
+ }
5823
5865
  if (prop.name === "ref") {
5824
5866
  if (prop.value?.type === NodeTypes.TEXT) {
5825
5867
  const name = prop.value.content.trim();
@@ -5831,6 +5873,10 @@ function collectElementAttributes(node, context, options) {
5831
5873
  staticClass = prop.value.content;
5832
5874
  continue;
5833
5875
  }
5876
+ if (prop.name === "id" && prop.value?.type === NodeTypes.TEXT) {
5877
+ staticId = prop.value.content.trim();
5878
+ continue;
5879
+ }
5834
5880
  if (prop.name === "style" && prop.value?.type === NodeTypes.TEXT) {
5835
5881
  staticStyle = prop.value.content;
5836
5882
  continue;
@@ -5849,6 +5895,11 @@ function collectElementAttributes(node, context, options) {
5849
5895
  }
5850
5896
  continue;
5851
5897
  }
5898
+ if (prop.name === "bind" && prop.arg?.type === NodeTypes.SIMPLE_EXPRESSION && prop.arg.content === "id") hasDynamicIdBinding = true;
5899
+ if (prop.name === "bind" && prop.arg?.type === NodeTypes.SIMPLE_EXPRESSION && prop.arg.content === "layout-host") {
5900
+ context.warnings.push("暂不支持动态 layout-host,已忽略该绑定。");
5901
+ continue;
5902
+ }
5852
5903
  if (prop.name === "bind" && prop.arg?.type === NodeTypes.SIMPLE_EXPRESSION && prop.arg.content === "class" && prop.exp?.type === NodeTypes.SIMPLE_EXPRESSION) {
5853
5904
  dynamicClassExp = prop.exp.content;
5854
5905
  continue;
@@ -5881,6 +5932,26 @@ function collectElementAttributes(node, context, options) {
5881
5932
  kind: isComponentElement ? "component" : "element"
5882
5933
  });
5883
5934
  }
5935
+ if (layoutHostKey) if (!staticId && hasDynamicIdBinding) context.warnings.push("layout-host 暂不支持与动态 id 同时使用,当前节点已忽略。");
5936
+ else {
5937
+ const hostIndex = context.layoutHostIndexSeed++;
5938
+ const hostId = staticId || `__wv-layout-host-${hostIndex}`;
5939
+ const hostRefName = `__wevu_layout_host_${hostIndex}`;
5940
+ staticId = hostId;
5941
+ context.templateRefs.push({
5942
+ selector: `#${hostId}`,
5943
+ inFor: false,
5944
+ name: hostRefName,
5945
+ kind: "component"
5946
+ });
5947
+ context.layoutHosts.push({
5948
+ key: layoutHostKey,
5949
+ refName: hostRefName,
5950
+ selector: `#${hostId}`,
5951
+ kind: "component"
5952
+ });
5953
+ }
5954
+ if (staticId) attrs.unshift(`id="${staticId}"`);
5884
5955
  const classAttr = renderClassAttribute(staticClass, dynamicClassExp, context);
5885
5956
  if (classAttr) attrs.unshift(classAttr);
5886
5957
  const styleAttr = renderStyleAttribute(staticStyle, dynamicStyleExp, vShowExp, context);
@@ -6547,6 +6618,8 @@ function compileVueTemplateToWxml(template, filename, options) {
6547
6618
  forIndexSeed: 0,
6548
6619
  templateRefs: [],
6549
6620
  templateRefIndexSeed: 0,
6621
+ layoutHosts: [],
6622
+ layoutHostIndexSeed: 0,
6550
6623
  inlineExpressions: [],
6551
6624
  inlineExpressionSeed: 0
6552
6625
  };
@@ -6564,6 +6637,7 @@ function compileVueTemplateToWxml(template, filename, options) {
6564
6637
  result.classStyleRuntime = context.classStyleRuntime;
6565
6638
  }
6566
6639
  if (context.templateRefs.length) result.templateRefs = context.templateRefs;
6640
+ if (context.layoutHosts.length) result.layoutHosts = context.layoutHosts;
6567
6641
  if (context.inlineExpressions.length) result.inlineExpressions = context.inlineExpressions;
6568
6642
  return result;
6569
6643
  } catch (error) {
@@ -6604,11 +6678,12 @@ function resolveJsLikeLang(lang) {
6604
6678
  async function evaluateJsLikeConfig(source, filename, lang) {
6605
6679
  const dir = path.dirname(filename);
6606
6680
  const extension = resolveJsLikeLang(lang) === "ts" ? "ts" : "js";
6607
- const tempDir = resolveWevuConfigTempDir(dir);
6608
- return await withTempDirLock(tempDir, async () => {
6681
+ const tempRoot = resolveWevuConfigTempDir(dir);
6682
+ const basename = path.basename(filename, path.extname(filename));
6683
+ const unique = `${Date.now()}-${Math.random().toString(16).slice(2)}`;
6684
+ const tempDir = path.join(tempRoot, `${basename}.config.${unique}`);
6685
+ return await withTempDirLock(tempRoot, async () => {
6609
6686
  await fs.ensureDir(tempDir);
6610
- const basename = path.basename(filename, path.extname(filename));
6611
- const unique = `${Date.now()}-${Math.random().toString(16).slice(2)}`;
6612
6687
  const tempFile = path.join(tempDir, `${basename}.${unique}.${extension}`);
6613
6688
  const rewritten = rewriteJsLikeImportsForTempDir(source, dir, tempDir);
6614
6689
  await fs.writeFile(tempFile, rewritten, "utf8");
@@ -6624,10 +6699,7 @@ async function evaluateJsLikeConfig(source, filename, lang) {
6624
6699
  throw new Error("配置块必须导出对象或返回对象的函数。");
6625
6700
  } finally {
6626
6701
  try {
6627
- await fs.remove(tempFile);
6628
- } catch {}
6629
- try {
6630
- if ((await fs.readdir(tempDir)).length === 0) await fs.remove(tempDir);
6702
+ await fs.remove(tempDir);
6631
6703
  } catch {}
6632
6704
  }
6633
6705
  });
@@ -6929,7 +7001,10 @@ async function evaluateDefineOptionsValues(params) {
6929
7001
  const { content, filename, lang, statements } = params;
6930
7002
  const ms = new MagicString(content);
6931
7003
  const dir = path.dirname(filename);
6932
- const tempDir = resolveWevuConfigTempDir(dir);
7004
+ const tempRoot = resolveWevuConfigTempDir(dir);
7005
+ const basename = path.basename(filename, path.extname(filename));
7006
+ const unique = `${Date.now()}-${Math.random().toString(16).slice(2)}`;
7007
+ const tempDir = path.join(tempRoot, `${basename}.define-options.${unique}`);
6933
7008
  const programPath = statements[0]?.statementPath.parentPath;
6934
7009
  if (!programPath) return {
6935
7010
  values: [],
@@ -6969,10 +7044,8 @@ const __weapp_defineOptions = (value) => (__weapp_define_options_values.push(val
6969
7044
  const evalSource = `${header + ms.toString() + (scopeFooter ? `\n${scopeFooter}\n` : "\n")}export const __weapp_define_scope = __weapp_define_scope_values\n
6970
7045
  export const __weapp_define_options = __weapp_define_options_values
6971
7046
  `;
6972
- return await withTempDirLock(tempDir, async () => {
7047
+ return await withTempDirLock(tempRoot, async () => {
6973
7048
  await fs.ensureDir(tempDir);
6974
- const basename = path.basename(filename, path.extname(filename));
6975
- const unique = `${Date.now()}-${Math.random().toString(16).slice(2)}`;
6976
7049
  const tempFile = path.join(tempDir, `${basename}.define-options.${unique}.${extension}`);
6977
7050
  await fs.writeFile(tempFile, evalSource, "utf8");
6978
7051
  try {
@@ -6988,10 +7061,7 @@ export const __weapp_define_options = __weapp_define_options_values
6988
7061
  };
6989
7062
  } finally {
6990
7063
  try {
6991
- await fs.remove(tempFile);
6992
- } catch {}
6993
- try {
6994
- if ((await fs.readdir(tempDir)).length === 0) await fs.remove(tempDir);
7064
+ await fs.remove(tempDir);
6995
7065
  } catch {}
6996
7066
  }
6997
7067
  });
@@ -7278,6 +7348,7 @@ async function compileScriptPhase(descriptor, descriptorForCompile, filename, op
7278
7348
  classStyleRuntime: templateCompiled?.classStyleRuntime,
7279
7349
  classStyleBindings: templateCompiled?.classStyleBindings,
7280
7350
  templateRefs: templateCompiled?.templateRefs,
7351
+ layoutHosts: templateCompiled?.layoutHosts,
7281
7352
  inlineExpressions: templateCompiled?.inlineExpressions,
7282
7353
  relaxStructuredTypeOnlyProps
7283
7354
  }).code,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@wevu/compiler",
3
3
  "type": "module",
4
- "version": "6.11.1",
4
+ "version": "6.11.4",
5
5
  "description": "wevu 编译器基础包,面向小程序模板的编译与转换",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",
@@ -50,8 +50,8 @@
50
50
  "pathe": "^2.0.3",
51
51
  "vue": "^3.5.30",
52
52
  "@weapp-core/shared": "3.0.2",
53
- "@weapp-vite/ast": "6.11.1",
54
- "rolldown-require": "2.0.8"
53
+ "@weapp-vite/ast": "6.11.4",
54
+ "rolldown-require": "2.0.9"
55
55
  },
56
56
  "publishConfig": {
57
57
  "access": "public",