Skip to content

Commit dc4fc13

Browse files
committed
refactor(compiler-core): make ast.helpers a Set
relate #6664
1 parent 24f4c47 commit dc4fc13

File tree

4 files changed

+22
-18
lines changed

4 files changed

+22
-18
lines changed

packages/compiler-core/src/ast.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export type TemplateChildNode =
100100
export interface RootNode extends Node {
101101
type: NodeTypes.ROOT
102102
children: TemplateChildNode[]
103-
helpers: symbol[]
103+
helpers: Set<symbol>
104104
components: string[]
105105
directives: string[]
106106
hoists: (JSChildNode | null)[]

packages/compiler-core/src/codegen.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ export function generate(
208208
ssr
209209
} = context
210210

211-
const hasHelpers = ast.helpers.length > 0
211+
const helpers = Array.from(ast.helpers)
212+
const hasHelpers = helpers.length > 0
212213
const useWithBlock = !prefixIdentifiers && mode !== 'module'
213214
const genScopeId = !__BROWSER__ && scopeId != null && mode === 'module'
214215
const isSetupInlined = !__BROWSER__ && !!options.inline
@@ -249,7 +250,7 @@ export function generate(
249250
// function mode const declarations should be inside with block
250251
// also they should be renamed to avoid collision with user properties
251252
if (hasHelpers) {
252-
push(`const { ${ast.helpers.map(aliasHelper).join(', ')} } = _Vue`)
253+
push(`const { ${helpers.map(aliasHelper).join(', ')} } = _Vue`)
253254
push(`\n`)
254255
newline()
255256
}
@@ -330,11 +331,10 @@ function genFunctionPreamble(ast: RootNode, context: CodegenContext) {
330331
// In prefix mode, we place the const declaration at top so it's done
331332
// only once; But if we not prefixing, we place the declaration inside the
332333
// with block so it doesn't incur the `in` check cost for every helper access.
333-
if (ast.helpers.length > 0) {
334+
const helpers = Array.from(ast.helpers)
335+
if (helpers.length > 0) {
334336
if (!__BROWSER__ && prefixIdentifiers) {
335-
push(
336-
`const { ${ast.helpers.map(aliasHelper).join(', ')} } = ${VueBinding}\n`
337-
)
337+
push(`const { ${helpers.map(aliasHelper).join(', ')} } = ${VueBinding}\n`)
338338
} else {
339339
// "with" mode.
340340
// save Vue in a separate variable to avoid collision
@@ -350,7 +350,7 @@ function genFunctionPreamble(ast: RootNode, context: CodegenContext) {
350350
CREATE_TEXT,
351351
CREATE_STATIC
352352
]
353-
.filter(helper => ast.helpers.includes(helper))
353+
.filter(helper => helpers.includes(helper))
354354
.map(aliasHelper)
355355
.join(', ')
356356
push(`const { ${staticHelpers} } = _Vue\n`)
@@ -386,30 +386,32 @@ function genModulePreamble(
386386
} = context
387387

388388
if (genScopeId && ast.hoists.length) {
389-
ast.helpers.push(PUSH_SCOPE_ID, POP_SCOPE_ID)
389+
ast.helpers.add(PUSH_SCOPE_ID)
390+
ast.helpers.add(POP_SCOPE_ID)
390391
}
391392

392393
// generate import statements for helpers
393-
if (ast.helpers.length) {
394+
if (ast.helpers.size) {
395+
const helpers = Array.from(ast.helpers)
394396
if (optimizeImports) {
395397
// when bundled with webpack with code-split, calling an import binding
396398
// as a function leads to it being wrapped with `Object(a.b)` or `(0,a.b)`,
397399
// incurring both payload size increase and potential perf overhead.
398400
// therefore we assign the imports to variables (which is a constant ~50b
399401
// cost per-component instead of scaling with template size)
400402
push(
401-
`import { ${ast.helpers
403+
`import { ${helpers
402404
.map(s => helperNameMap[s])
403405
.join(', ')} } from ${JSON.stringify(runtimeModuleName)}\n`
404406
)
405407
push(
406-
`\n// Binding optimization for webpack code-split\nconst ${ast.helpers
408+
`\n// Binding optimization for webpack code-split\nconst ${helpers
407409
.map(s => `_${helperNameMap[s]} = ${helperNameMap[s]}`)
408410
.join(', ')}\n`
409411
)
410412
} else {
411413
push(
412-
`import { ${ast.helpers
414+
`import { ${helpers
413415
.map(s => `${helperNameMap[s]} as _${helperNameMap[s]}`)
414416
.join(', ')} } from ${JSON.stringify(runtimeModuleName)}\n`
415417
)

packages/compiler-sfc/src/compileScript.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1440,7 +1440,7 @@ export function compileScript(
14401440
// avoid duplicated unref import
14411441
// as this may get injected by the render function preamble OR the
14421442
// css vars codegen
1443-
if (ast && ast.helpers.includes(UNREF)) {
1443+
if (ast && ast.helpers.has(UNREF)) {
14441444
helperImports.delete('unref')
14451445
}
14461446
returned = code

packages/compiler-ssr/src/ssrCodegenTransform.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ export function ssrCodegenTransform(ast: RootNode, options: CompilerOptions) {
4949
createCompoundExpression([`const _cssVars = { style: `, varsExp, `}`])
5050
)
5151
Array.from(cssContext.helpers.keys()).forEach(helper => {
52-
if (!ast.helpers.includes(helper))
53-
ast.helpers.push(helper)
52+
ast.helpers.add(helper)
5453
})
5554
}
5655

@@ -62,10 +61,13 @@ export function ssrCodegenTransform(ast: RootNode, options: CompilerOptions) {
6261
// Finalize helpers.
6362
// We need to separate helpers imported from 'vue' vs. '@vue/server-renderer'
6463
ast.ssrHelpers = Array.from(
65-
new Set([...ast.helpers.filter(h => h in ssrHelpers), ...context.helpers])
64+
new Set([
65+
...Array.from(ast.helpers).filter(h => h in ssrHelpers),
66+
...context.helpers
67+
])
6668
)
6769

68-
ast.helpers = ast.helpers.filter(h => !(h in ssrHelpers))
70+
ast.helpers = new Set(Array.from(ast.helpers).filter(h => !(h in ssrHelpers)))
6971
}
7072

7173
export type SSRTransformContext = ReturnType<typeof createSSRTransformContext>

0 commit comments

Comments
 (0)