@wevu/compiler 6.6.9 → 6.6.11

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
@@ -239,6 +239,7 @@ interface ForParseResult {
239
239
  item?: string;
240
240
  index?: string;
241
241
  key?: string;
242
+ itemAliases?: Record<string, string>;
242
243
  }
243
244
  /**
244
245
  * 模板编译选项。
package/dist/index.mjs CHANGED
@@ -4125,6 +4125,8 @@ function getClassStyleWxsSource(options = {}) {
4125
4125
 
4126
4126
  //#endregion
4127
4127
  //#region src/plugins/vue/compiler/template/elements/helpers.ts
4128
+ const IDENTIFIER_RE$1 = /^[A-Z_$][\w$]*$/i;
4129
+ const FOR_ITEM_ALIAS_PLACEHOLDER = "__wv_for_item__";
4128
4130
  function isStructuralDirective(node) {
4129
4131
  for (const prop of node.props) if (prop.type === NodeTypes.DIRECTIVE) {
4130
4132
  if (prop.name === "if" || prop.name === "else-if" || prop.name === "else") return {
@@ -4218,35 +4220,217 @@ function isScopedSlotsDisabled(context) {
4218
4220
  function findSlotDirective(node) {
4219
4221
  return node.props.find((prop) => prop.type === NodeTypes.DIRECTIVE && prop.name === "slot");
4220
4222
  }
4221
- function parseForExpression(exp) {
4222
- const match = exp.match(/^\(([^,]+),\s*([^,]+),\s*([^)]+)\)\s+in\s+(.+)$/);
4223
- if (match) {
4224
- const [, item, _key, index, list] = match;
4225
- return {
4226
- listExp: list,
4227
- item,
4228
- index,
4229
- key: _key
4230
- };
4223
+ function isIdentifier(value) {
4224
+ return IDENTIFIER_RE$1.test(value);
4225
+ }
4226
+ function splitTopLevelByComma(input) {
4227
+ const out = [];
4228
+ let start = 0;
4229
+ let parenDepth = 0;
4230
+ let bracketDepth = 0;
4231
+ let braceDepth = 0;
4232
+ let quote = "";
4233
+ let escaped = false;
4234
+ for (let i = 0; i < input.length; i += 1) {
4235
+ const ch = input[i];
4236
+ if (quote) {
4237
+ if (escaped) {
4238
+ escaped = false;
4239
+ continue;
4240
+ }
4241
+ if (ch === "\\") {
4242
+ escaped = true;
4243
+ continue;
4244
+ }
4245
+ if (ch === quote) quote = "";
4246
+ continue;
4247
+ }
4248
+ if (ch === "'" || ch === "\"" || ch === "`") {
4249
+ quote = ch;
4250
+ continue;
4251
+ }
4252
+ if (ch === "(") {
4253
+ parenDepth += 1;
4254
+ continue;
4255
+ }
4256
+ if (ch === ")") {
4257
+ parenDepth = Math.max(0, parenDepth - 1);
4258
+ continue;
4259
+ }
4260
+ if (ch === "[") {
4261
+ bracketDepth += 1;
4262
+ continue;
4263
+ }
4264
+ if (ch === "]") {
4265
+ bracketDepth = Math.max(0, bracketDepth - 1);
4266
+ continue;
4267
+ }
4268
+ if (ch === "{") {
4269
+ braceDepth += 1;
4270
+ continue;
4271
+ }
4272
+ if (ch === "}") {
4273
+ braceDepth = Math.max(0, braceDepth - 1);
4274
+ continue;
4275
+ }
4276
+ if (ch === "," && parenDepth === 0 && bracketDepth === 0 && braceDepth === 0) {
4277
+ out.push(input.slice(start, i).trim());
4278
+ start = i + 1;
4279
+ }
4231
4280
  }
4232
- const match2 = exp.match(/^\(([^,]+),\s*([^)]+)\)\s+in\s+(.+)$/);
4233
- if (match2) {
4234
- const [, item, index, list] = match2;
4235
- return {
4236
- listExp: list,
4237
- item,
4238
- index
4281
+ out.push(input.slice(start).trim());
4282
+ return out.filter(Boolean);
4283
+ }
4284
+ function splitForExpression(exp) {
4285
+ let parenDepth = 0;
4286
+ let bracketDepth = 0;
4287
+ let braceDepth = 0;
4288
+ let quote = "";
4289
+ let escaped = false;
4290
+ for (let i = 0; i < exp.length; i += 1) {
4291
+ const ch = exp[i];
4292
+ if (quote) {
4293
+ if (escaped) {
4294
+ escaped = false;
4295
+ continue;
4296
+ }
4297
+ if (ch === "\\") {
4298
+ escaped = true;
4299
+ continue;
4300
+ }
4301
+ if (ch === quote) quote = "";
4302
+ continue;
4303
+ }
4304
+ if (ch === "'" || ch === "\"" || ch === "`") {
4305
+ quote = ch;
4306
+ continue;
4307
+ }
4308
+ if (ch === "(") {
4309
+ parenDepth += 1;
4310
+ continue;
4311
+ }
4312
+ if (ch === ")") {
4313
+ parenDepth = Math.max(0, parenDepth - 1);
4314
+ continue;
4315
+ }
4316
+ if (ch === "[") {
4317
+ bracketDepth += 1;
4318
+ continue;
4319
+ }
4320
+ if (ch === "]") {
4321
+ bracketDepth = Math.max(0, bracketDepth - 1);
4322
+ continue;
4323
+ }
4324
+ if (ch === "{") {
4325
+ braceDepth += 1;
4326
+ continue;
4327
+ }
4328
+ if (ch === "}") {
4329
+ braceDepth = Math.max(0, braceDepth - 1);
4330
+ continue;
4331
+ }
4332
+ if (parenDepth !== 0 || bracketDepth !== 0 || braceDepth !== 0) continue;
4333
+ if (exp.startsWith(" in ", i) || exp.startsWith(" of ", i)) return {
4334
+ source: exp.slice(0, i).trim(),
4335
+ list: exp.slice(i + 4).trim()
4239
4336
  };
4240
4337
  }
4241
- const match3 = exp.match(/^(\w+)\s+in\s+(.+)$/);
4242
- if (match3) {
4243
- const [, item, list] = match3;
4244
- return {
4245
- listExp: list,
4246
- item
4247
- };
4338
+ return null;
4339
+ }
4340
+ function stripOuterParentheses(value) {
4341
+ const trimmed = value.trim();
4342
+ if (!trimmed.startsWith("(") || !trimmed.endsWith(")")) return trimmed;
4343
+ let depth = 0;
4344
+ for (let i = 0; i < trimmed.length; i += 1) {
4345
+ const ch = trimmed[i];
4346
+ if (ch === "(") depth += 1;
4347
+ else if (ch === ")") {
4348
+ depth -= 1;
4349
+ if (depth === 0 && i !== trimmed.length - 1) return trimmed;
4350
+ }
4248
4351
  }
4249
- return {};
4352
+ return trimmed.slice(1, -1).trim();
4353
+ }
4354
+ function toMemberAccess(base, property, computed) {
4355
+ if (!computed) {
4356
+ if (t.isIdentifier(property) && isIdentifier(property.name)) return `${base}.${property.name}`;
4357
+ if (t.isStringLiteral(property)) return `${base}[${JSON.stringify(property.value)}]`;
4358
+ }
4359
+ if (t.isExpression(property)) return `${base}[${generateExpression(property)}]`;
4360
+ return base;
4361
+ }
4362
+ function collectPatternAliases(node, base, aliases) {
4363
+ if (t.isIdentifier(node)) {
4364
+ aliases[node.name] = base;
4365
+ return;
4366
+ }
4367
+ if (t.isAssignmentPattern(node)) {
4368
+ collectPatternAliases(node.left, base, aliases);
4369
+ return;
4370
+ }
4371
+ if (t.isRestElement(node)) {
4372
+ if (t.isIdentifier(node.argument)) aliases[node.argument.name] = base;
4373
+ return;
4374
+ }
4375
+ if (t.isArrayPattern(node)) {
4376
+ node.elements.forEach((element, index) => {
4377
+ if (!element) return;
4378
+ if (t.isRestElement(element)) {
4379
+ collectPatternAliases(element, `${base}.slice(${index})`, aliases);
4380
+ return;
4381
+ }
4382
+ collectPatternAliases(element, `${base}[${index}]`, aliases);
4383
+ });
4384
+ return;
4385
+ }
4386
+ if (!t.isObjectPattern(node)) return;
4387
+ node.properties.forEach((property) => {
4388
+ if (t.isRestElement(property)) {
4389
+ if (t.isIdentifier(property.argument)) aliases[property.argument.name] = base;
4390
+ return;
4391
+ }
4392
+ const nextBase = toMemberAccess(base, property.key, property.computed);
4393
+ collectPatternAliases(property.value, nextBase, aliases);
4394
+ });
4395
+ }
4396
+ function parseItemAliases(pattern) {
4397
+ try {
4398
+ const stmt = parseJsLike(`(${pattern}) => {}`).program.body[0];
4399
+ if (!stmt || stmt.type !== "ExpressionStatement") return {};
4400
+ const exp = stmt.expression;
4401
+ if (!t.isArrowFunctionExpression(exp) || exp.params.length !== 1) return {};
4402
+ const aliases = {};
4403
+ collectPatternAliases(exp.params[0], FOR_ITEM_ALIAS_PLACEHOLDER, aliases);
4404
+ return aliases;
4405
+ } catch {
4406
+ return {};
4407
+ }
4408
+ }
4409
+ function parseForExpression(exp) {
4410
+ const split = splitForExpression(exp.trim());
4411
+ if (!split) return {};
4412
+ const segments = splitTopLevelByComma(stripOuterParentheses(split.source));
4413
+ if (!segments.length || segments.length > 3) return { listExp: split.list };
4414
+ const result = { listExp: split.list };
4415
+ const rawItem = segments[0]?.trim();
4416
+ if (rawItem) if (isIdentifier(rawItem)) result.item = rawItem;
4417
+ else {
4418
+ const aliases = parseItemAliases(rawItem);
4419
+ if (Object.keys(aliases).length) {
4420
+ result.item = FOR_ITEM_ALIAS_PLACEHOLDER;
4421
+ result.itemAliases = aliases;
4422
+ }
4423
+ }
4424
+ if (segments.length === 2) {
4425
+ const rawIndex = segments[1]?.trim();
4426
+ if (rawIndex && isIdentifier(rawIndex)) result.index = rawIndex;
4427
+ } else if (segments.length === 3) {
4428
+ const rawKey = segments[1]?.trim();
4429
+ const rawIndex = segments[2]?.trim();
4430
+ if (rawKey && isIdentifier(rawKey)) result.key = rawKey;
4431
+ if (rawIndex && isIdentifier(rawIndex)) result.index = rawIndex;
4432
+ }
4433
+ return result;
4250
4434
  }
4251
4435
 
4252
4436
  //#endregion
@@ -4293,6 +4477,23 @@ function collectSlotPropMapping(context) {
4293
4477
  for (const entry of context.slotPropStack) Object.assign(mapping, entry);
4294
4478
  return mapping;
4295
4479
  }
4480
+ function collectForAliasMapping$2(context) {
4481
+ const mapping = {};
4482
+ for (const forInfo of context.forStack) {
4483
+ if (!forInfo.itemAliases) continue;
4484
+ Object.assign(mapping, forInfo.itemAliases);
4485
+ }
4486
+ return mapping;
4487
+ }
4488
+ function replaceIdentifierWithExpression(path, replacement) {
4489
+ const parent = path.parentPath;
4490
+ if (parent.isObjectProperty() && parent.node.shorthand && parent.node.key === path.node) {
4491
+ parent.node.shorthand = false;
4492
+ parent.node.value = replacement;
4493
+ return;
4494
+ }
4495
+ path.replaceWith(replacement);
4496
+ }
4296
4497
  function rewriteScopedSlotExpression(exp, context) {
4297
4498
  const normalized = normalizeWxmlExpression(exp);
4298
4499
  const parsed = parseBabelExpressionFile(normalized);
@@ -4300,6 +4501,7 @@ function rewriteScopedSlotExpression(exp, context) {
4300
4501
  const { ast } = parsed;
4301
4502
  const locals = collectScopedSlotLocals(context);
4302
4503
  const slotProps = collectSlotPropMapping(context);
4504
+ const forAliases = collectForAliasMapping$2(context);
4303
4505
  const createMemberAccess = (target, prop) => {
4304
4506
  if (!prop) return t.identifier(target);
4305
4507
  if (/^[A-Z_$][\w$]*$/i.test(prop)) return t.memberExpression(t.identifier(target), t.identifier(prop));
@@ -4309,33 +4511,48 @@ function rewriteScopedSlotExpression(exp, context) {
4309
4511
  if (!path.isReferencedIdentifier()) return;
4310
4512
  const name = path.node.name;
4311
4513
  if (SCOPED_SLOT_GLOBALS.has(name)) return;
4312
- if (locals.has(name)) return;
4313
- if (Object.prototype.hasOwnProperty.call(slotProps, name)) {
4314
- const member = createMemberAccess("__wvSlotPropsData", slotProps[name]);
4315
- const parent = path.parentPath;
4316
- if (parent.isObjectProperty() && parent.node.shorthand && parent.node.key === path.node) {
4317
- parent.node.shorthand = false;
4318
- parent.node.value = member;
4514
+ if (path.scope.hasBinding(name)) return;
4515
+ if (Object.prototype.hasOwnProperty.call(forAliases, name)) {
4516
+ const aliasExp = parseBabelExpression(forAliases[name]);
4517
+ if (aliasExp) {
4518
+ replaceIdentifierWithExpression(path, t.cloneNode(aliasExp, true));
4319
4519
  return;
4320
4520
  }
4321
- path.replaceWith(member);
4322
- return;
4323
4521
  }
4324
- const member = createMemberAccess("__wvOwner", name);
4325
- const parent = path.parentPath;
4326
- if (parent.isObjectProperty() && parent.node.shorthand && parent.node.key === path.node) {
4327
- parent.node.shorthand = false;
4328
- parent.node.value = member;
4522
+ if (locals.has(name)) return;
4523
+ if (Object.prototype.hasOwnProperty.call(slotProps, name)) {
4524
+ replaceIdentifierWithExpression(path, createMemberAccess("__wvSlotPropsData", slotProps[name]));
4329
4525
  return;
4330
4526
  }
4331
- path.replaceWith(member);
4527
+ replaceIdentifierWithExpression(path, createMemberAccess("__wvOwner", name));
4528
+ } });
4529
+ const stmt = ast.program.body[0];
4530
+ const updatedExpression = stmt && "expression" in stmt ? stmt.expression : null;
4531
+ return updatedExpression ? generateExpression(updatedExpression) : normalized;
4532
+ }
4533
+ function rewriteForAliasExpression(exp, context) {
4534
+ const normalized = normalizeWxmlExpression(exp);
4535
+ const forAliases = collectForAliasMapping$2(context);
4536
+ if (!Object.keys(forAliases).length) return normalized;
4537
+ const parsed = parseBabelExpressionFile(normalized);
4538
+ if (!parsed) return normalized;
4539
+ const { ast } = parsed;
4540
+ traverse(ast, { Identifier(path) {
4541
+ if (!path.isReferencedIdentifier()) return;
4542
+ const name = path.node.name;
4543
+ if (path.scope.hasBinding(name)) return;
4544
+ if (!Object.prototype.hasOwnProperty.call(forAliases, name)) return;
4545
+ const aliasExp = parseBabelExpression(forAliases[name]);
4546
+ if (!aliasExp) return;
4547
+ replaceIdentifierWithExpression(path, t.cloneNode(aliasExp, true));
4332
4548
  } });
4333
4549
  const stmt = ast.program.body[0];
4334
4550
  const updatedExpression = stmt && "expression" in stmt ? stmt.expression : null;
4335
4551
  return updatedExpression ? generateExpression(updatedExpression) : normalized;
4336
4552
  }
4337
4553
  function normalizeWxmlExpressionWithContext(exp, context) {
4338
- if (!context?.rewriteScopedSlot) return normalizeWxmlExpression(exp);
4554
+ if (!context) return normalizeWxmlExpression(exp);
4555
+ if (!context.rewriteScopedSlot) return rewriteForAliasExpression(exp, context);
4339
4556
  return rewriteScopedSlotExpression(exp, context);
4340
4557
  }
4341
4558
 
@@ -4561,6 +4778,14 @@ function buildScopeResolvers(usedLocals, context, slotProps, indexBindings) {
4561
4778
  }
4562
4779
  return resolvers;
4563
4780
  }
4781
+ function collectForAliasMapping$1(context) {
4782
+ const mapping = {};
4783
+ for (const forInfo of context.forStack) {
4784
+ if (!forInfo.itemAliases) continue;
4785
+ Object.assign(mapping, forInfo.itemAliases);
4786
+ }
4787
+ return mapping;
4788
+ }
4564
4789
  function registerInlineExpression(exp, context) {
4565
4790
  const parsed = parseBabelExpressionFile(exp);
4566
4791
  if (!parsed) return null;
@@ -4576,10 +4801,13 @@ function registerInlineExpression(exp, context) {
4576
4801
  usedLocals.push(name);
4577
4802
  };
4578
4803
  rewriteExpressionAst(ast, locals, { markLocal });
4804
+ const forAliases = collectForAliasMapping$1(context);
4579
4805
  const updatedStmt = ast.program.body[0];
4580
4806
  const updatedExpressionNode = updatedStmt && "expression" in updatedStmt ? updatedStmt.expression : null;
4581
4807
  const updatedExpression = updatedExpressionNode ? generateExpression(updatedExpressionNode) : exp;
4582
4808
  const scopeBindings = usedLocals.map((name) => {
4809
+ const forAlias = forAliases[name];
4810
+ if (forAlias) return forAlias;
4583
4811
  return resolveSlotPropBinding(slotProps, name) ?? name;
4584
4812
  });
4585
4813
  const indexBindings = buildInlineIndexBindings(context);
@@ -4688,6 +4916,14 @@ function createIdentifierAccessWithPropsFallback(name) {
4688
4916
  const shouldUsePropsAccess = t.logicalExpression("&&", hasUsablePropsValue, t.unaryExpression("!", hasThisMember));
4689
4917
  return t.conditionalExpression(shouldUseStateAccess, thisAccess, t.conditionalExpression(shouldUsePropsAccess, propsAccess, thisAccess));
4690
4918
  }
4919
+ function collectForAliasMapping(context) {
4920
+ const mapping = {};
4921
+ for (const forInfo of context.forStack) {
4922
+ if (!forInfo.itemAliases) continue;
4923
+ Object.assign(mapping, forInfo.itemAliases);
4924
+ }
4925
+ return mapping;
4926
+ }
4691
4927
  function normalizeJsExpressionWithContext(exp, context, options) {
4692
4928
  const trimmed = exp.trim();
4693
4929
  if (!trimmed) return null;
@@ -4700,11 +4936,26 @@ function normalizeJsExpressionWithContext(exp, context, options) {
4700
4936
  const { ast } = parsed;
4701
4937
  const locals = collectScopedSlotLocals(context);
4702
4938
  const slotProps = context.rewriteScopedSlot ? collectSlotPropMapping(context) : {};
4939
+ const forAliases = collectForAliasMapping(context);
4703
4940
  traverse(ast, { Identifier(path) {
4704
4941
  if (!path.isReferencedIdentifier()) return;
4705
4942
  const name = path.node.name;
4706
4943
  if (JS_RUNTIME_GLOBALS.has(name)) return;
4707
4944
  if (path.scope.hasBinding(name)) return;
4945
+ if (Object.prototype.hasOwnProperty.call(forAliases, name)) {
4946
+ const aliasExp = parseBabelExpression(forAliases[name]);
4947
+ if (aliasExp) {
4948
+ const replacement = t.cloneNode(aliasExp, true);
4949
+ const parent = path.parentPath;
4950
+ if (parent.isObjectProperty() && parent.node.shorthand && parent.node.key === path.node) {
4951
+ parent.node.shorthand = false;
4952
+ parent.node.value = replacement;
4953
+ return;
4954
+ }
4955
+ path.replaceWith(replacement);
4956
+ return;
4957
+ }
4958
+ }
4708
4959
  if (locals.has(name)) return;
4709
4960
  let replacement;
4710
4961
  if (context.rewriteScopedSlot) if (Object.prototype.hasOwnProperty.call(slotProps, name)) {
@@ -5038,6 +5289,12 @@ function transformModelDirective(node, context, elementNode) {
5038
5289
  //#endregion
5039
5290
  //#region src/plugins/vue/compiler/template/directives/on.ts
5040
5291
  const isSimpleHandler = (value) => /^[A-Z_$][\w$]*$/i.test(value);
5292
+ function shouldUseDetailPayload(options) {
5293
+ return options?.isComponent === true;
5294
+ }
5295
+ function normalizeEventDatasetSuffix(eventName) {
5296
+ return eventName.trim().replace(/[^a-z0-9]+/gi, "-").replace(/^-+|-+$/g, "").toLowerCase() || "event";
5297
+ }
5041
5298
  function buildInlineScopeAttrs(scopeBindings, context) {
5042
5299
  return scopeBindings.map((binding, index) => {
5043
5300
  return `data-wv-s${index}="${renderMustache(binding.replace(/"/g, "&quot;"), context)}"`;
@@ -5059,32 +5316,41 @@ function resolveEventPrefix(modifiers) {
5059
5316
  if (hasCapture) return "capture-bind";
5060
5317
  return "bind";
5061
5318
  }
5062
- function transformOnDirective(node, context) {
5319
+ function transformOnDirective(node, context, options) {
5063
5320
  const { exp, arg } = node;
5064
5321
  if (!arg) return null;
5065
5322
  const argValue = arg.type === NodeTypes.SIMPLE_EXPRESSION ? arg.content : "";
5066
5323
  if (!exp) return null;
5067
- const rawExpValue = exp.type === NodeTypes.SIMPLE_EXPRESSION ? exp.content : "";
5068
- const isInlineExpression = rawExpValue && !isSimpleHandler(rawExpValue);
5069
- const inlineExpression = isInlineExpression ? registerInlineExpression(rawExpValue, context) : null;
5324
+ const rawExpValue = exp.type === NodeTypes.SIMPLE_EXPRESSION ? exp.content.trim() : "";
5325
+ const useDetailPayload = shouldUseDetailPayload(options);
5326
+ const inlineSource = useDetailPayload && isSimpleHandler(rawExpValue) ? `${rawExpValue}($event)` : rawExpValue;
5327
+ const isInlineExpression = inlineSource && !isSimpleHandler(inlineSource);
5328
+ const inlineExpression = isInlineExpression ? registerInlineExpression(inlineSource, context) : null;
5070
5329
  const mappedEvent = context.platform.mapEventName(argValue);
5330
+ const eventSuffix = normalizeEventDatasetSuffix(mappedEvent);
5071
5331
  const eventPrefix = resolveEventPrefix(node.modifiers);
5072
5332
  const bindAttr = context.platform.eventBindingAttr(`${eventPrefix}:${mappedEvent}`);
5333
+ const detailAttr = useDetailPayload ? `data-wv-event-detail-${eventSuffix}="1"` : "";
5073
5334
  if (context.rewriteScopedSlot) {
5074
5335
  if (inlineExpression) {
5075
5336
  const scopeAttrs = buildInlineScopeAttrs(inlineExpression.scopeBindings, context);
5076
5337
  const indexAttrs = buildInlineIndexAttrs(inlineExpression.indexBindings, context);
5077
5338
  return [
5078
- `data-wv-inline-id="${inlineExpression.id}"`,
5339
+ detailAttr,
5340
+ `data-wv-inline-id-${eventSuffix}="${inlineExpression.id}"`,
5079
5341
  ...scopeAttrs,
5080
5342
  ...indexAttrs,
5081
5343
  `${bindAttr}="__weapp_vite_owner"`
5082
5344
  ].filter(Boolean).join(" ");
5083
5345
  }
5084
- if (!isInlineExpression && rawExpValue) return `data-wv-handler="${rawExpValue}" ${bindAttr}="__weapp_vite_owner"`;
5346
+ if (!isInlineExpression && rawExpValue) return [
5347
+ detailAttr,
5348
+ `data-wv-handler-${eventSuffix}="${rawExpValue}"`,
5349
+ `${bindAttr}="__weapp_vite_owner"`
5350
+ ].filter(Boolean).join(" ");
5085
5351
  if (isInlineExpression) {
5086
5352
  context.warnings.push("作用域插槽的事件处理解析失败,请使用简单的方法引用。");
5087
- return `${bindAttr}="__weapp_vite_owner"`;
5353
+ return [detailAttr, `${bindAttr}="__weapp_vite_owner"`].filter(Boolean).join(" ");
5088
5354
  }
5089
5355
  }
5090
5356
  const expValue = normalizeWxmlExpressionWithContext(rawExpValue, context);
@@ -5092,14 +5358,19 @@ function transformOnDirective(node, context) {
5092
5358
  const scopeAttrs = buildInlineScopeAttrs(inlineExpression.scopeBindings, context);
5093
5359
  const indexAttrs = buildInlineIndexAttrs(inlineExpression.indexBindings, context);
5094
5360
  return [
5095
- `data-wv-inline-id="${inlineExpression.id}"`,
5361
+ detailAttr,
5362
+ `data-wv-inline-id-${eventSuffix}="${inlineExpression.id}"`,
5096
5363
  ...scopeAttrs,
5097
5364
  ...indexAttrs,
5098
5365
  `${bindAttr}="__weapp_vite_inline"`
5099
5366
  ].filter(Boolean).join(" ");
5100
5367
  }
5101
- if (isInlineExpression) return `data-wv-inline="${rawExpValue.replace(/"/g, "&quot;")}" ${bindAttr}="__weapp_vite_inline"`;
5102
- return `${bindAttr}="${expValue}"`;
5368
+ if (isInlineExpression) return [
5369
+ detailAttr,
5370
+ `data-wv-inline-${eventSuffix}="${inlineSource.replace(/"/g, "&quot;")}"`,
5371
+ `${bindAttr}="__weapp_vite_inline"`
5372
+ ].filter(Boolean).join(" ");
5373
+ return [detailAttr, `${bindAttr}="${expValue}"`].filter(Boolean).join(" ");
5103
5374
  }
5104
5375
 
5105
5376
  //#endregion
@@ -5112,10 +5383,10 @@ function transformShowDirective(node, context) {
5112
5383
 
5113
5384
  //#endregion
5114
5385
  //#region src/plugins/vue/compiler/template/directives/index.ts
5115
- function transformDirective(node, context, elementNode, forInfo) {
5386
+ function transformDirective(node, context, elementNode, forInfo, options) {
5116
5387
  const { name, exp, arg } = node;
5117
5388
  if (name === "bind") return transformBindDirective(node, context, forInfo);
5118
- if (name === "on") return transformOnDirective(node, context);
5389
+ if (name === "on") return transformOnDirective(node, context, options);
5119
5390
  if (name === "model") return transformModelDirective(node, context, elementNode);
5120
5391
  if (name === "show") return transformShowDirective(node, context);
5121
5392
  if (name === "html") {
@@ -5138,6 +5409,7 @@ function isBuiltinTag(tag) {
5138
5409
  }
5139
5410
  function collectElementAttributes(node, context, options) {
5140
5411
  const { props } = node;
5412
+ const isComponentElement = options?.isComponent ?? !isBuiltinTag(node.tag);
5141
5413
  const attrs = options?.extraAttrs ? [...options.extraAttrs] : [];
5142
5414
  let staticClass;
5143
5415
  let dynamicClassExp;
@@ -5195,20 +5467,19 @@ function collectElementAttributes(node, context, options) {
5195
5467
  vTextExp = (shouldFallbackToRuntimeBinding(rawExp) ? registerRuntimeBindingExpression(rawExp, context, { hint: "v-text" }) : null) ?? normalizeWxmlExpressionWithContext(rawExp, context);
5196
5468
  continue;
5197
5469
  }
5198
- const dir = transformDirective(prop, context, node, options?.forInfo);
5470
+ const dir = transformDirective(prop, context, node, options?.forInfo, { isComponent: isComponentElement });
5199
5471
  if (dir) attrs.push(dir);
5200
5472
  }
5201
5473
  }
5202
5474
  if (templateRef) {
5203
5475
  const className = `__wv-ref-${context.templateRefIndexSeed++}`;
5204
5476
  staticClass = staticClass ? `${staticClass} ${className}` : className;
5205
- const isComponentRef = options?.isComponent ?? !isBuiltinTag(node.tag);
5206
5477
  context.templateRefs.push({
5207
5478
  selector: `.${className}`,
5208
5479
  inFor,
5209
5480
  name: templateRef.name,
5210
5481
  expAst: templateRef.expAst,
5211
- kind: isComponentRef ? "component" : "element"
5482
+ kind: isComponentElement ? "component" : "element"
5212
5483
  });
5213
5484
  }
5214
5485
  const classAttr = renderClassAttribute(staticClass, dynamicClassExp, context);
@@ -5586,7 +5857,7 @@ function transformComponentElement(node, context, transformNode) {
5586
5857
  const attr = transformAttribute(prop, context);
5587
5858
  if (attr) attrs.push(attr);
5588
5859
  } else if (prop.type === NodeTypes.DIRECTIVE) {
5589
- const dir = transformDirective(prop, context, node);
5860
+ const dir = transformDirective(prop, context, node, void 0, { isComponent: true });
5590
5861
  if (dir) attrs.push(dir);
5591
5862
  }
5592
5863
  const children = node.children.map((child) => transformNode(child, context)).join("");
@@ -5644,6 +5915,17 @@ function transformForElement(node, context, transformNode) {
5644
5915
  const forDirective = node.props.find((prop) => prop.type === NodeTypes.DIRECTIVE && prop.name === "for");
5645
5916
  if (!forDirective || !forDirective.exp) return transformNormalElement(node, context, transformNode);
5646
5917
  const forInfo = parseForExpression(forDirective.exp.type === NodeTypes.SIMPLE_EXPRESSION ? forDirective.exp.content : "");
5918
+ if (forInfo.item === FOR_ITEM_ALIAS_PLACEHOLDER) {
5919
+ const generatedItem = `__wv_item_${context.forIndexSeed++}`;
5920
+ forInfo.item = generatedItem;
5921
+ if (forInfo.itemAliases) {
5922
+ const escaped = FOR_ITEM_ALIAS_PLACEHOLDER.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
5923
+ const placeholderRE = new RegExp(`\\b${escaped}\\b`, "g");
5924
+ forInfo.itemAliases = Object.fromEntries(Object.entries(forInfo.itemAliases).map(([alias, expression]) => {
5925
+ return [alias, expression.replace(placeholderRE, generatedItem)];
5926
+ }));
5927
+ }
5928
+ }
5647
5929
  if (context.classStyleRuntime === "js" && !forInfo.index) forInfo.index = `__wv_index_${context.forIndexSeed++}`;
5648
5930
  const listExp = forInfo.listExp ? resolveConditionExpression$1(forInfo.listExp, context, "v-for 列表") : void 0;
5649
5931
  const listExpAst = forInfo.listExp ? normalizeJsExpressionWithContext(forInfo.listExp, context, { hint: "v-for 列表" }) : void 0;
@@ -5658,7 +5940,8 @@ function transformForElement(node, context, transformNode) {
5658
5940
  const scopeNames = [
5659
5941
  forInfo.item,
5660
5942
  forInfo.index,
5661
- forInfo.key
5943
+ forInfo.key,
5944
+ ...Object.keys(forInfo.itemAliases ?? {})
5662
5945
  ].filter(Boolean);
5663
5946
  return withForScope(context, scopedForInfo, () => withScope(context, scopeNames, () => {
5664
5947
  const otherProps = node.props.filter((prop) => prop !== forDirective);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@wevu/compiler",
3
3
  "type": "module",
4
- "version": "6.6.9",
4
+ "version": "6.6.11",
5
5
  "description": "wevu 编译器基础包,面向小程序模板的编译与转换",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",
@@ -45,14 +45,14 @@
45
45
  "@babel/parser": "^7.29.0",
46
46
  "@babel/traverse": "^7.29.0",
47
47
  "@babel/types": "^7.29.0",
48
- "@vue/compiler-core": "^3.5.28",
48
+ "@vue/compiler-core": "^3.5.29",
49
49
  "comment-json": "^4.5.1",
50
50
  "fs-extra": "^11.3.3",
51
51
  "lru-cache": "^11.2.6",
52
52
  "magic-string": "^0.30.21",
53
53
  "merge": "^2.1.1",
54
54
  "pathe": "^2.0.3",
55
- "vue": "^3.5.28",
55
+ "vue": "^3.5.29",
56
56
  "@weapp-core/shared": "3.0.1",
57
57
  "rolldown-require": "2.0.6"
58
58
  },