Skip to content

Commit a55a1ee

Browse files
committed
perf(weapp-tailwindcss): 复用默认忽略路径集合
1 parent 2003973 commit a55a1ee

2 files changed

Lines changed: 27 additions & 13 deletions

File tree

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

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

1717
const EXPRESSION_WRAPPER_PREFIX = '(\n'
1818
const EXPRESSION_WRAPPER_SUFFIX = '\n)'
19+
const EMPTY_IGNORED_PATHS = new WeakSet<NodePath<StringLiteral | TemplateElement>>()
1920
const ignoredTaggedTemplateMatcherCache = new WeakMap<IJsHandlerOptions, ReturnType<typeof createNameMatcher>>()
2021

2122
function getIgnoredTaggedTemplateMatcher(options: IJsHandlerOptions) {
@@ -35,17 +36,19 @@ export function analyzeSource(
3536
handler?: EvalHandler,
3637
): SourceAnalysis {
3738
const jsTokenUpdater = new JsTokenUpdater()
38-
const ignoredPaths = new WeakSet<NodePath<StringLiteral | TemplateElement>>()
39-
const walker = new NodePathWalker(
40-
{
41-
ignoreCallExpressionIdentifiers: options.ignoreCallExpressionIdentifiers,
42-
callback(path) {
43-
if (path.isStringLiteral() || path.isTemplateElement()) {
39+
// 仅在需要忽略特定调用参数时记录路径,默认路径复用共享空集合。
40+
const needScope = Boolean(options.ignoreCallExpressionIdentifiers && options.ignoreCallExpressionIdentifiers.length > 0)
41+
const ignoredPaths = needScope
42+
? new WeakSet<NodePath<StringLiteral | TemplateElement>>()
43+
: EMPTY_IGNORED_PATHS
44+
const walker = needScope
45+
? new NodePathWalker({
46+
ignoreCallExpressionIdentifiers: options.ignoreCallExpressionIdentifiers,
47+
callback(path) {
4448
ignoredPaths.add(path)
45-
}
46-
},
47-
},
48-
)
49+
},
50+
})
51+
: new NodePathWalker()
4952

5053
let taggedTemplateIgnore: ReturnType<typeof createTaggedTemplateIgnore> | undefined
5154

@@ -59,9 +62,6 @@ export function analyzeSource(
5962
return taggedTemplateIgnore
6063
}
6164

62-
// 仅在需要时才构建作用域信息(例如需要遍历调用表达式的实参)。
63-
const needScope = Boolean(options.ignoreCallExpressionIdentifiers && options.ignoreCallExpressionIdentifiers.length > 0)
64-
6565
const targetPaths: NodePath<StringLiteral | TemplateElement>[] = []
6666
const importDeclarations = new Set<NodePath<ImportDeclaration>>()
6767
const exportDeclarations = new Set<NodePath<ExportDeclaration>>()

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ describe('babel helpers additional coverage', () => {
6464
expect(ms.toString()).toBe(code)
6565
})
6666

67+
it('reuses an empty ignored path set when call ignore identifiers are disabled', () => {
68+
const firstCode = 'const first = "w-[100px]"'
69+
const secondCode = 'const second = "w-[200px]"'
70+
const firstAnalysis = babel.analyzeSource(parse(firstCode, { sourceType: 'module' as const }), {})
71+
const secondAnalysis = babel.analyzeSource(parse(secondCode, { sourceType: 'module' as const }), {})
72+
73+
const [firstTarget] = firstAnalysis.targetPaths
74+
const [secondTarget] = secondAnalysis.targetPaths
75+
76+
expect(firstAnalysis.ignoredPaths).toBe(secondAnalysis.ignoredPaths)
77+
expect(firstAnalysis.ignoredPaths.has(firstTarget)).toBe(false)
78+
expect(secondAnalysis.ignoredPaths.has(secondTarget)).toBe(false)
79+
})
80+
6781
it('returns the original source when parsing fails', () => {
6882
const raw = 'const = 1'
6983
const result = babel.jsHandler(raw, {})

0 commit comments

Comments
 (0)