Skip to content

Commit

Permalink
Merge pull request #184 from hwaphon/fix/reset-regex-lastindex
Browse files Browse the repository at this point in the history
fix(plugin-compiler): css 压缩校验类名时重置正则 lastIndex,解决部分场景匹配异常问题
  • Loading branch information
hwaphon committed Apr 18, 2024
2 parents 7c9153c + 2f873ad commit b524287
Showing 1 changed file with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class CSSClassNameCompressPlugin implements Plugin {
dynamicClassRegExp: RegExp

// (用于 template,css 中动态绑定提取,颗粒度更细)template 模板动态 class 绑定检测正则
dynamicClassRegExpGrained = /\s*(\w+)?{{.*?}}(\w+)?\s*/gi
dynamicClassRegExpGrained = /\s*(\S+)?({{.*?}})+(\S+)?\s*/gi

// 自定义属性名称
customClassAttrs: string[]
Expand Down Expand Up @@ -329,7 +329,7 @@ export class CSSClassNameCompressPlugin implements Plugin {
*/
splitBySpaceAndBraces(input) {
// 正则表达式,匹配 {{}} 或者空格
const regex = this.dynamicClassRegExpGrained
const regex = new RegExp(this.dynamicClassRegExpGrained)
let match
let lastIndex = 0
const result = []
Expand All @@ -341,6 +341,7 @@ export class CSSClassNameCompressPlugin implements Plugin {
if (match.index > lastIndex) {
result.push(...splitBySpace(input.slice(lastIndex, match.index)))
}

// 如果匹配到的是 {{}},则将其加入结果数组
if (match[0].includes('{{')) {
result.push(match[0])
Expand Down Expand Up @@ -459,7 +460,8 @@ export class CSSClassNameCompressPlugin implements Plugin {

// 如果开启跳过动态 class 检测,在 template 模板中遇到遇到动态 class 直接跳过(内置,降低业务配置成本)
if (this.options.disableDynamicClassDetection) {
if (this.dynamicClassRegExpGrained.test(className)) return className
if (new RegExp(this.dynamicClassRegExpGrained).test(className))
return className
}
// 如果存在类名过滤器,则如果返回结果为 false 则不压缩
if (
Expand Down

0 comments on commit b524287

Please sign in to comment.