-
Notifications
You must be signed in to change notification settings - Fork 390
feat: unocss-plugin 支持 wx:class 对象写法中的类名扫描 #2519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mackwang112
wants to merge
3
commits into
master
Choose a base branch
from
feat-unocss-dynamic-class
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+87
−11
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个unescapeKey维护在当前包内吧,webpack-plugin中本身不需要这个 |
||
|
|
||
| function parseClasses (content) { | ||
| const output = [] | ||
|
|
@@ -78,9 +79,32 @@ function parseStrings (content) { | |
| return output | ||
| } | ||
|
|
||
| // 匹配对象字面量中标识符形式的 key,如 { ml_da_17rpxMpxEscape: flag, a: true } | ||
| // key 前面必须是 { 或 ,(加可选空格),后面是 : | ||
| const objKeyReg = /(?:[{,]\s*)([\w-]+?)(?=\s*:)/gm | ||
|
|
||
| function parseMpxEscapeKeys (content) { | ||
| const output = [] | ||
| if (!content) { return output } | ||
| let match | ||
| objKeyReg.lastIndex = 0 | ||
| while (match = objKeyReg.exec(content)) { | ||
| const raw = match[1] | ||
| const end = match.index + match[0].length - 1 | ||
| const start = end - raw.length + 1 | ||
| output.push({ | ||
| result: unescapeKey(raw), | ||
| start, | ||
| end | ||
| }) | ||
| } | ||
| return output | ||
| } | ||
|
|
||
| module.exports = { | ||
| parseClasses, | ||
| parseStrings, | ||
| parseMpxEscapeKeys, | ||
| parseComments, | ||
| parseCommentConfig, | ||
| parseMustache, | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,8 +3,10 @@ const t = require('@babel/types') | |
| const traverse = require('@babel/traverse').default | ||
| const generate = require('@babel/generator').default | ||
| const isValidIdentifierStr = require('../utils/is-valid-identifier-str') | ||
| const escapeReg = /[()[\]{}#!.:,%'"+$]/g | ||
| const escapeMap = { | ||
| function escapeRegExp (str) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个文件不需要改动 |
||
| return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') | ||
| } | ||
| const classNameEscapeMap = { | ||
| '(': '_pl_', | ||
| ')': '_pr_', | ||
| '[': '_bl_', | ||
|
|
@@ -23,22 +25,66 @@ const escapeMap = { | |
| '+': '_a_', | ||
| $: '_si_' | ||
| } | ||
| const classNameEscapeReg = new RegExp('[' + Object.keys(classNameEscapeMap).map(escapeRegExp).join('') + ']', 'g') | ||
|
|
||
| // classNameEscapeMap 的反向映射,用于还原 escapeClassName 编码 | ||
| const classNameDecodeMap = Object.keys(classNameEscapeMap).reduce((acc, key) => { | ||
| acc[classNameEscapeMap[key]] = key | ||
| return acc | ||
| }, {}) | ||
| const classNameDecodeReg = new RegExp(Object.keys(classNameDecodeMap).map(escapeRegExp).join('|'), 'g') | ||
|
|
||
| function mpEscape (str) { | ||
| return str.replace(escapeReg, function (match) { | ||
| if (escapeMap[match]) return escapeMap[match] | ||
| function escapeClassName (str) { | ||
| return str.replace(classNameEscapeReg, function (match) { | ||
| if (classNameEscapeMap[match]) return classNameEscapeMap[match] | ||
| // unknown escaped | ||
| return '_u_' | ||
| }) | ||
| } | ||
|
|
||
| function keyEscape (str) { | ||
| let result = str.replace(/-/g, '_da_').replace(/\s+/g, '_sp_') | ||
| if (result !== str) result += 'MpxEscape' | ||
| return result | ||
| function unescapeClassName (str) { | ||
| return str.replace(classNameDecodeReg, m => classNameDecodeMap[m] || m) | ||
| } | ||
|
|
||
| const KEY_ESCAPE_SUFFIX = 'MpxEscape' | ||
| const KEY_ESCAPE_DASH = '_da_' | ||
| const KEY_ESCAPE_SPACE = '_sp_' | ||
|
|
||
| const keyEscapeMap = { | ||
| '-': KEY_ESCAPE_DASH, | ||
| ' ': KEY_ESCAPE_SPACE, | ||
| '*': '_st_' | ||
| } | ||
| const keyDecodeMap = Object.keys(keyEscapeMap).reduce((acc, key) => { | ||
| acc[keyEscapeMap[key]] = key | ||
| return acc | ||
| }, {}) | ||
| const keyDecodeReg = new RegExp(Object.keys(keyDecodeMap).map(escapeRegExp).join('|'), 'g') | ||
|
|
||
| function escapeKey (str) { | ||
| const result = str.replace(/-/g, KEY_ESCAPE_DASH).replace(/\s+/g, KEY_ESCAPE_SPACE).replace(/\*/g, '_st_') | ||
| if (result !== str) return result + KEY_ESCAPE_SUFFIX | ||
| return str | ||
| } | ||
|
|
||
| function unescapeKey (str) { | ||
| if (str.endsWith(KEY_ESCAPE_SUFFIX)) { | ||
| return unescapeClassName( | ||
| str.slice(0, -KEY_ESCAPE_SUFFIX.length).replace(keyDecodeReg, m => keyDecodeMap[m]) | ||
| ) | ||
| } | ||
| return str | ||
| } | ||
|
|
||
| module.exports = transDynamicClassExpr | ||
| module.exports.KEY_ESCAPE_SUFFIX = KEY_ESCAPE_SUFFIX | ||
| module.exports.KEY_ESCAPE_DASH = KEY_ESCAPE_DASH | ||
| module.exports.KEY_ESCAPE_SPACE = KEY_ESCAPE_SPACE | ||
| module.exports.unescapeKey = unescapeKey | ||
| module.exports.escapeKey = escapeKey | ||
| module.exports.escapeClassName = escapeClassName | ||
|
|
||
| module.exports = function transDynamicClassExpr (expr, { error } = {}) { | ||
| function transDynamicClassExpr (expr, { error } = {}) { | ||
| try { | ||
| const ast = babylon.parse(expr, { | ||
| plugins: [ | ||
|
|
@@ -50,7 +96,7 @@ module.exports = function transDynamicClassExpr (expr, { error } = {}) { | |
| path.node.properties.forEach((property) => { | ||
| if (t.isObjectProperty(property) && !property.computed) { | ||
| const rawPropertyName = property.key.name || property.key.value | ||
| const propertyName = keyEscape(mpEscape(rawPropertyName)) | ||
| const propertyName = escapeKey(escapeClassName(rawPropertyName)) | ||
| if (!isValidIdentifierStr(propertyName)) { | ||
| error && error(`Dynamic classname [${rawPropertyName}] can not be escaped as a valid identifier, which is not supported.`) | ||
| } else { | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
对于objectkey可以只记录不替换吧