Skip to content

Commit 2003973

Browse files
committed
perf(weapp-tailwindcss): 延迟构建标签忽略匹配器
1 parent 88f605d commit 2003973

2 files changed

Lines changed: 51 additions & 6 deletions

File tree

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ import { createTaggedTemplateIgnore } from './taggedTemplateIgnore'
1616

1717
const EXPRESSION_WRAPPER_PREFIX = '(\n'
1818
const EXPRESSION_WRAPPER_SUFFIX = '\n)'
19+
const ignoredTaggedTemplateMatcherCache = new WeakMap<IJsHandlerOptions, ReturnType<typeof createNameMatcher>>()
20+
21+
function getIgnoredTaggedTemplateMatcher(options: IJsHandlerOptions) {
22+
const cached = ignoredTaggedTemplateMatcherCache.get(options)
23+
if (cached) {
24+
return cached
25+
}
26+
27+
const created = createNameMatcher(options.ignoreTaggedTemplateExpressionIdentifiers, { exact: true })
28+
ignoredTaggedTemplateMatcherCache.set(options, created)
29+
return created
30+
}
1931

2032
export function analyzeSource(
2133
ast: ParseResult<File>,
@@ -35,11 +47,17 @@ export function analyzeSource(
3547
},
3648
)
3749

38-
const isIgnoredTaggedTemplate = createNameMatcher(options.ignoreTaggedTemplateExpressionIdentifiers, { exact: true })
39-
const taggedTemplateIgnore = createTaggedTemplateIgnore({
40-
matcher: isIgnoredTaggedTemplate,
41-
names: options.ignoreTaggedTemplateExpressionIdentifiers,
42-
})
50+
let taggedTemplateIgnore: ReturnType<typeof createTaggedTemplateIgnore> | undefined
51+
52+
function getTaggedTemplateIgnore() {
53+
if (!taggedTemplateIgnore) {
54+
taggedTemplateIgnore = createTaggedTemplateIgnore({
55+
matcher: getIgnoredTaggedTemplateMatcher(options),
56+
names: options.ignoreTaggedTemplateExpressionIdentifiers,
57+
})
58+
}
59+
return taggedTemplateIgnore
60+
}
4361

4462
// 仅在需要时才构建作用域信息(例如需要遍历调用表达式的实参)。
4563
const needScope = Boolean(options.ignoreCallExpressionIdentifiers && options.ignoreCallExpressionIdentifiers.length > 0)
@@ -70,7 +88,7 @@ export function analyzeSource(
7088
}
7189
if (ppp.isTaggedTemplateExpression()) {
7290
const tagPath = ppp.get('tag') as NodePath<Node>
73-
if (taggedTemplateIgnore.shouldIgnore(tagPath)) {
91+
if (getTaggedTemplateIgnore().shouldIgnore(tagPath)) {
7492
return
7593
}
7694
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { MappingChars2String } from '@weapp-core/escape'
55
import { describe, expect, it, vi } from 'vitest'
66
import { parse, traverse } from '@/babel'
77
import * as babel from '@/js/babel'
8+
import * as nameMatcher from '@/utils/nameMatcher'
89

910
function prepareEval(code: string) {
1011
const ast = parse(code, { sourceType: 'module' })
@@ -153,6 +154,32 @@ describe('babel helpers branch coverage', () => {
153154
expect(analysis.targetPaths).toHaveLength(1)
154155
})
155156

157+
it('does not build ignore matchers when tagged templates are absent', () => {
158+
const spy = vi.spyOn(nameMatcher, 'createNameMatcher')
159+
const ast = parse('const cls = `w-[100px]`', { sourceType: 'module' })
160+
161+
const analysis = babel.analyzeSource(ast, {
162+
ignoreTaggedTemplateExpressionIdentifiers: ['styled'],
163+
})
164+
165+
expect(analysis.targetPaths).toHaveLength(1)
166+
expect(spy).not.toHaveBeenCalled()
167+
})
168+
169+
it('reuses ignored tag matchers for the same options object', () => {
170+
const spy = vi.spyOn(nameMatcher, 'createNameMatcher')
171+
const options = {
172+
ignoreTaggedTemplateExpressionIdentifiers: ['styled'],
173+
}
174+
175+
const firstAst = parse('styled`w-[100px]`', { sourceType: 'module' })
176+
const secondAst = parse('styled`w-[200px]`', { sourceType: 'module' })
177+
178+
expect(babel.analyzeSource(firstAst, options).targetPaths).toHaveLength(0)
179+
expect(babel.analyzeSource(secondAst, options).targetPaths).toHaveLength(0)
180+
expect(spy).toHaveBeenCalledTimes(1)
181+
})
182+
156183
it('falls back to an empty original value for unexpected eval nodes', () => {
157184
const rawSource = 'eval(`__coverage__` )'
158185
const ast = parse(rawSource, { sourceType: 'module' })

0 commit comments

Comments
 (0)