feat: unocss-plugin 支持 wx:class 对象写法中的类名扫描#2519
Open
mackwang112 wants to merge 3 commits into
Open
Conversation
修复 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>
hiyuki
reviewed
Jun 17, 2026
| const isValidIdentifierStr = require('../utils/is-valid-identifier-str') | ||
| const escapeReg = /[()[\]{}#!.:,%'"+$]/g | ||
| const escapeMap = { | ||
| function escapeRegExp (str) { |
hiyuki
reviewed
Jun 17, 2026
| }) | ||
| parseMpxEscapeKeys(exp).forEach(({ result, start, end }) => { | ||
| const expanded = transformClasses(result, classNameHandler) | ||
| expSource.replace(start, end, escapeKey(escapeClassName(expanded))) |
hiyuki
reviewed
Jun 17, 2026
| @@ -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') | |||
Collaborator
There was a problem hiding this comment.
这个unescapeKey维护在当前包内吧,webpack-plugin中本身不需要这个
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
新支持的写法包括: