Skip to content

feat: unocss-plugin 支持 wx:class 对象写法中的类名扫描#2519

Open
mackwang112 wants to merge 3 commits into
masterfrom
feat-unocss-dynamic-class
Open

feat: unocss-plugin 支持 wx:class 对象写法中的类名扫描#2519
mackwang112 wants to merge 3 commits into
masterfrom
feat-unocss-dynamic-class

Conversation

@mackwang112

Copy link
Copy Markdown
Collaborator

新支持的写法包括:

  • wx:class="{{ { 'ml-17rpx': flag } }}" — 普通类名
  • wx:class="{{ { 'h-100%': flag } }}" — 含特殊字符的类名
  • wx:class="{{ { 'ml-17rpx h-100%': flag } }}" — 对象 key 中空格分隔的多个类名
  • wx:class="{{ { '*card': flag } }}" — alias 展开

mackwang112 and others added 3 commits June 10, 2026 14:20
修复 unocss-plugin 无法处理 wx:class="{{ { 'ml-17rpx': flag } }}" 对象写法的问题。

模板编译阶段 transDynamicClassExpr 会将 wx:class 对象 key 中的特殊字符
编码为合法 identifier(如 ml-17rpx → ml_da_17rpxMpxEscape),导致 unocss-plugin
扫描编译产物时 parseStrings 无法识别,对应 CSS 不会被生成。

新增 parseMpxEscapeKeys,通过正则扫描 MpxEscape 结尾的 identifier key,
用 unescapeKey 还原原始类名后交给 transformClasses 处理,再重新编码写回 wxml。

支持的写法:
- wx:class="{{ { 'ml-17rpx': flag } }}" — 普通类名
- wx:class="{{ { 'h-100%': true } }}" — 含特殊字符的类名
- wx:class="{{ { 'ml-17rpx h-100%': flag } }}" — key 中空格分隔的多个类名
- wx:class="{{ { '*card': flag } }}" — alias 展开

同步重构 trans-dynamic-class-expr.js:
- mpEscape → escapeClassName,keyEscape → escapeKey,新增对应的 unescape 函数
- 硬编码字符串提取为具名常量(KEY_ESCAPE_SUFFIX/DASH/SPACE)
- escapeReg 改为从 classNameEscapeMap 动态生成,保持 map 和 reg 同步
- classNameDecodeReg 按 token 长度降序排列,避免短 token 优先匹配

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
当前 classNameDecodeMap 的所有 key 互不重叠,无需按长度降序排列。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
const isValidIdentifierStr = require('../utils/is-valid-identifier-str')
const escapeReg = /[()[\]{}#!.:,%'"+$]/g
const escapeMap = {
function escapeRegExp (str) {

@hiyuki hiyuki Jun 17, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个文件不需要改动

})
parseMpxEscapeKeys(exp).forEach(({ result, start, end }) => {
const expanded = transformClasses(result, classNameHandler)
expSource.replace(start, end, escapeKey(escapeClassName(expanded)))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对于objectkey可以只记录不替换吧

@@ -1,4 +1,5 @@
const { parseMustache, stringifyAttr } = require('@mpxjs/webpack-plugin/lib/template-compiler/compiler')
const { unescapeKey } = require('@mpxjs/webpack-plugin/lib/template-compiler/trans-dynamic-class-expr')

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个unescapeKey维护在当前包内吧,webpack-plugin中本身不需要这个

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants