Skip to content

Commit c9acce7

Browse files
committed
perf(weapp-tailwindcss): 减少候选解析与 legacy 回退开销
1 parent 8767581 commit c9acce7

5 files changed

Lines changed: 40 additions & 12 deletions

File tree

packages/weapp-tailwindcss/src/js/handlers.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ function createCandidatePlanResolver(
105105
classContext: boolean,
106106
) {
107107
const { escapeMap } = options
108+
const transformOptions = classContext
109+
? {
110+
...options,
111+
classContext,
112+
}
113+
: options
108114
const cache = new Map<string, CandidatePlan>()
109115

110116
return (candidate: string): CandidatePlan => {
@@ -113,10 +119,7 @@ function createCandidatePlanResolver(
113119
return cached
114120
}
115121

116-
const result = resolveClassNameTransformWithResult(candidate, {
117-
...options,
118-
classContext,
119-
})
122+
const result = resolveClassNameTransformWithResult(candidate, transformOptions)
120123

121124
if (result.decision === 'skip') {
122125
const plan = { result }

packages/weapp-tailwindcss/src/wxml/utils/codegen/legacy-rewriter.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ import { JsTokenUpdater } from '../../../js/JsTokenUpdater'
55
import { createLegacyTraverseOptions } from './legacy-visitor'
66

77
export function rewriteLegacyExpression(match: string, options: ITemplateHandlerOptions) {
8-
const ms = new MagicString(match)
98
const ast = parseExpression(match)
109
const jsTokenUpdater = new JsTokenUpdater()
1110

1211
traverse(ast, createLegacyTraverseOptions(options, jsTokenUpdater))
12+
if (jsTokenUpdater.length === 0) {
13+
return match
14+
}
15+
16+
const ms = new MagicString(match)
1317
jsTokenUpdater.updateMagicString(ms)
1418

1519
return ms.toString()

packages/weapp-tailwindcss/src/wxml/utils/codegen/legacy-visitor.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ export function createLegacyTraverseOptions(
2222
options: ITemplateHandlerOptions,
2323
jsTokenUpdater: JsTokenUpdater,
2424
) {
25+
const legacyReplaceOptions = {
26+
escapeMap: options.escapeMap,
27+
classNameSet: options.runtimeSet,
28+
needEscaped: true,
29+
alwaysEscape: true,
30+
}
2531
return {
2632
StringLiteral(path) {
2733
if (shouldSkipLegacyStringLiteral(path)) {
@@ -30,12 +36,7 @@ export function createLegacyTraverseOptions(
3036
jsTokenUpdater.addToken(
3137
replaceHandleValue(
3238
path,
33-
{
34-
escapeMap: options.escapeMap,
35-
classNameSet: options.runtimeSet,
36-
needEscaped: true,
37-
alwaysEscape: true,
38-
},
39+
legacyReplaceOptions,
3940
),
4041
)
4142

packages/weapp-tailwindcss/test/js/handlers-branches.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import type { NodePath } from '@babel/traverse'
22
import type { NumericLiteral, StringLiteral, TemplateElement } from '@babel/types'
33
import { MappingChars2String } from '@weapp-core/escape'
4-
import { describe, expect, it } from 'vitest'
4+
import { describe, expect, it, vi } from 'vitest'
55
import { parse, traverse } from '@/babel'
66
import { replaceHandleValue } from '@/js/handlers'
7+
import * as classNameTransform from '@/shared/classname-transform'
78

89
type LiteralKind = 'StringLiteral' | 'TemplateElement'
910

@@ -164,6 +165,20 @@ describe('replaceHandleValue branch coverage', () => {
164165
expect(token?.value).toBe('gap-_b20px_B gap-_b20px_B')
165166
})
166167

168+
it('reuses the candidate plan for repeated candidates in one literal', () => {
169+
const literal = getLiteralPath('const repeated = \'w-[100px] w-[100px]\'', 'StringLiteral')
170+
const spy = vi.spyOn(classNameTransform, 'resolveClassNameTransformWithResult')
171+
172+
const token = replaceHandleValue(literal, {
173+
escapeMap: MappingChars2String,
174+
classNameSet: new Set(['w-[100px]']),
175+
needEscaped: true,
176+
})
177+
178+
expect(token?.value).toBe('w-_b100px_B w-_b100px_B')
179+
expect(spy).toHaveBeenCalledTimes(1)
180+
})
181+
167182
it('evaluates unicode decoding guard when no escape is present', () => {
168183
const literal = getLiteralPath('const plain = \'flex\'', 'StringLiteral')
169184
const token = replaceHandleValue(literal, {

packages/weapp-tailwindcss/test/wxml/utils.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,9 @@ describe('utils', () => {
102102
wrapExpression: true,
103103
})
104104
})
105+
106+
it('falls back to legacy expression rewriting when runtime js handler is unavailable', () => {
107+
const code = generateCode(`{'w-[100px]': flag}`)
108+
expect(code).toContain(`'w-_b100px_B'`)
109+
})
105110
})

0 commit comments

Comments
 (0)