Skip to content

Commit 88f605d

Browse files
committed
perf(weapp-tailwindcss): 精简标签忽略名称匹配
1 parent c2e0c9c commit 88f605d

2 files changed

Lines changed: 52 additions & 6 deletions

File tree

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

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,46 @@ export function createTaggedTemplateIgnore(
1818
const bindingIgnoreCache = new Map<Binding, boolean>()
1919
const taggedTemplateIgnoreCache = new WeakMap<Node, boolean>()
2020
const seenBindings = new Set<Binding>()
21-
const canonicalIgnoreNames = new Set(
22-
(names ?? [])
23-
.filter((item): item is string => typeof item === 'string'),
24-
)
25-
const hasCanonicalIgnoreNames = canonicalIgnoreNames.size > 0
21+
let singleCanonicalIgnoreName: string | undefined
22+
let canonicalIgnoreNames: Set<string> | undefined
23+
24+
for (const item of names ?? []) {
25+
if (typeof item !== 'string') {
26+
continue
27+
}
28+
29+
if (singleCanonicalIgnoreName === undefined) {
30+
singleCanonicalIgnoreName = item
31+
continue
32+
}
33+
34+
if (item === singleCanonicalIgnoreName) {
35+
continue
36+
}
37+
38+
if (!canonicalIgnoreNames) {
39+
canonicalIgnoreNames = new Set([singleCanonicalIgnoreName, item])
40+
continue
41+
}
42+
43+
canonicalIgnoreNames.add(item)
44+
}
45+
46+
const hasCanonicalIgnoreNames = singleCanonicalIgnoreName !== undefined
2647

2748
const matchesIgnoreName = (value: string): boolean => {
28-
return (hasCanonicalIgnoreNames && canonicalIgnoreNames.has(value)) || matcher(value)
49+
if (hasCanonicalIgnoreNames) {
50+
if (canonicalIgnoreNames) {
51+
if (canonicalIgnoreNames.has(value)) {
52+
return true
53+
}
54+
}
55+
else if (value === singleCanonicalIgnoreName) {
56+
return true
57+
}
58+
}
59+
60+
return matcher(value)
2961
}
3062

3163
const propertyMatches = (propertyPath: NodePath<Node> | undefined): boolean => {

packages/weapp-tailwindcss/test/js/taggedTemplateIgnore.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ describe('taggedTemplateIgnore', () => {
3232
expect(helper.shouldIgnore(tagPath)).toBe(true)
3333
})
3434

35+
it('matches multiple configured identifiers without depending on matcher fallback', () => {
36+
const helper = createTaggedTemplateIgnore({
37+
matcher: value => value === 'ignoredByMatcher',
38+
names: ['ignoreMe', 'weappTwIgnore'],
39+
})
40+
const [configuredTag, matcherTag] = getTagPaths([
41+
'ignoreMe`foo`',
42+
'ignoredByMatcher`bar`',
43+
].join('\n'))
44+
45+
expect(helper.shouldIgnore(configuredTag)).toBe(true)
46+
expect(helper.shouldIgnore(matcherTag)).toBe(true)
47+
})
48+
3549
it('resolves imports of weappTwIgnore and alias chains', () => {
3650
const helper = createTaggedTemplateIgnore({ matcher: () => false, names: ['weappTwIgnore'] })
3751
const [importAlias, chainedAlias] = getTagPaths([

0 commit comments

Comments
 (0)