Skip to content

Commit 2570920

Browse files
committed
refactor(compiler-vapor): remove @babel/types to reduce bundle sizee
1 parent fedceb3 commit 2570920

File tree

6 files changed

+52
-37
lines changed

6 files changed

+52
-37
lines changed

packages/compiler/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@
4141
},
4242
"dependencies": {
4343
"@babel/parser": "catalog:",
44-
"@babel/types": "catalog:",
4544
"@vue/shared": "catalog:",
4645
"ast-kit": "catalog:",
4746
"source-map-js": "catalog:"
47+
},
48+
"devDependencies": {
49+
"@babel/types": "catalog:"
4850
}
4951
}

packages/compiler/src/generators/for.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
import { parseExpression } from '@babel/parser'
2-
import {
3-
isNodesEquivalent,
4-
type Expression,
5-
type Identifier,
6-
type Node,
7-
} from '@babel/types'
82
import { extend, isGloballyAllowed } from '@vue/shared'
93
import { walkAST, walkIdentifiers } from 'ast-kit'
104
import {
@@ -23,6 +17,7 @@ import type { BlockIRNode, ForIRNode, IREffect } from '../ir'
2317
import { genBlockContent } from './block'
2418
import { genExpression } from './expression'
2519
import { genOperation } from './operation'
20+
import type { Expression, Identifier, Node } from '@babel/types'
2621

2722
/**
2823
* Flags to optimize vapor `createFor` runtime behavior, shared between the
@@ -114,6 +109,7 @@ export function genFor(
114109
render,
115110
keyProp,
116111
idMap,
112+
context.ir.source,
117113
)
118114
const selectorDeclarations: CodeFragment[] = []
119115
const selectorSetup: CodeFragment[] = []
@@ -331,6 +327,7 @@ function matchPatterns(
331327
render: BlockIRNode,
332328
keyProp: SimpleExpressionNode | undefined,
333329
idMap: Record<string, string | SimpleExpressionNode | null>,
330+
source: string,
334331
) {
335332
const selectorPatterns: NonNullable<
336333
ReturnType<typeof matchSelectorPattern>
@@ -341,12 +338,21 @@ function matchPatterns(
341338

342339
render.effect = render.effect.filter((effect) => {
343340
if (keyProp !== undefined) {
344-
const selector = matchSelectorPattern(effect, keyProp.ast, idMap)
341+
const selector = matchSelectorPattern(
342+
effect,
343+
keyProp.content,
344+
idMap,
345+
source,
346+
)
345347
if (selector) {
346348
selectorPatterns.push(selector)
347349
return false
348350
}
349-
const keyOnly = matchKeyOnlyBindingPattern(effect, keyProp.ast)
351+
const keyOnly = matchKeyOnlyBindingPattern(
352+
effect,
353+
keyProp.content,
354+
source,
355+
)
350356
if (keyOnly) {
351357
keyOnlyBindingPatterns.push(keyOnly)
352358
return false
@@ -364,7 +370,8 @@ function matchPatterns(
364370

365371
function matchKeyOnlyBindingPattern(
366372
effect: IREffect,
367-
keyAst: any,
373+
key: string,
374+
source: string,
368375
):
369376
| {
370377
effect: IREffect
@@ -376,7 +383,7 @@ function matchKeyOnlyBindingPattern(
376383
if (
377384
typeof ast === 'object' &&
378385
ast !== null &&
379-
isKeyOnlyBinding(ast, keyAst)
386+
isKeyOnlyBinding(ast, key, source)
380387
) {
381388
return { effect }
382389
}
@@ -385,8 +392,9 @@ function matchKeyOnlyBindingPattern(
385392

386393
function matchSelectorPattern(
387394
effect: IREffect,
388-
keyAst: any,
395+
key: string,
389396
idMap: Record<string, string | SimpleExpressionNode | null>,
397+
source: string,
390398
):
391399
| {
392400
effect: IREffect
@@ -414,8 +422,8 @@ function matchSelectorPattern(
414422
[left, right],
415423
[right, left],
416424
]) {
417-
const aIsKey = isKeyOnlyBinding(a, keyAst)
418-
const bIsKey = isKeyOnlyBinding(b, keyAst)
425+
const aIsKey = isKeyOnlyBinding(a, key, source)
426+
const bIsKey = isKeyOnlyBinding(b, key, source)
419427
const bVars = analyzeVariableScopes(b, idMap)
420428
if (aIsKey && !bIsKey && !bVars.locals.length) {
421429
matcheds.push([a, b])
@@ -482,8 +490,8 @@ function matchSelectorPattern(
482490
[left, right],
483491
[right, left],
484492
]) {
485-
const aIsKey = isKeyOnlyBinding(a, keyAst)
486-
const bIsKey = isKeyOnlyBinding(b, keyAst)
493+
const aIsKey = isKeyOnlyBinding(a, key, source)
494+
const bIsKey = isKeyOnlyBinding(b, key, source)
487495
const bVars = analyzeVariableScopes(b, idMap)
488496
if (aIsKey && !bIsKey && !bVars.locals.length) {
489497
return {
@@ -535,11 +543,11 @@ function analyzeVariableScopes(
535543
return { globals, locals }
536544
}
537545

538-
function isKeyOnlyBinding(expr: Node, keyAst: any) {
546+
function isKeyOnlyBinding(expr: Node, key: string, source: string) {
539547
let only = true
540548
walkAST(expr, {
541549
enter(node) {
542-
if (isNodesEquivalent(node, keyAst)) {
550+
if (source.slice(node.start!, node.end!) === key) {
543551
this.skip()
544552
return
545553
}

packages/compiler/src/transforms/transformText.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ function processTextContainer(children: TextLike[], context: TransformContext) {
139139
const values = createTextLikeExpressions(children, context)
140140
const literals = values.map(getLiteralExpressionValue)
141141
if (literals.every((l) => l != null)) {
142-
context.childrenTemplate = literals.map((l) => escapeHtml(String(l)))
142+
context.childrenTemplate = literals
143143
} else {
144144
context.childrenTemplate = [' ']
145145
context.registerOperation({

packages/compiler/src/utils/expression.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export function isConstantExpression(exp: SimpleExpressionNode) {
4343

4444
export function getLiteralExpressionValue(
4545
exp: SimpleExpressionNode,
46-
): number | string | boolean | null {
46+
): string | null {
4747
if (exp.ast) {
4848
const res = getTextLikeValue(exp.ast)
4949
if (res != null) {

packages/compiler/src/utils/transform.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
import {
2-
jsxClosingFragment,
3-
jsxExpressionContainer,
4-
jsxFragment,
5-
jsxOpeningFragment,
6-
type Expression,
7-
type JSXElement,
8-
type JSXFragment,
9-
} from '@babel/types'
101
import {
112
DynamicFlag,
123
type BlockIRNode,
@@ -15,6 +6,7 @@ import {
156
} from '../ir/index'
167
import { isTemplate } from '../utils'
178
import type { TransformContext } from '../transform'
9+
import type { Expression, JSXElement, JSXFragment } from '@babel/types'
1810

1911
export function newDynamic(): IRDynamicInfo {
2012
return {
@@ -47,12 +39,24 @@ export function createBranch(
4739
return [branch, exitBlock]
4840
}
4941

50-
export function wrapFragment(node: JSXElement | JSXFragment | Expression) {
42+
export function wrapFragment(
43+
node: JSXElement | JSXFragment | Expression,
44+
): JSXFragment | JSXElement {
5145
if (node.type === 'JSXFragment' || isTemplate(node)) {
52-
return node
46+
return node as JSXFragment
5347
}
5448

55-
return jsxFragment(jsxOpeningFragment(), jsxClosingFragment(), [
56-
node.type === 'JSXElement' ? node : jsxExpressionContainer(node),
57-
])
49+
return {
50+
type: 'JSXFragment',
51+
openingFragment: { type: 'JSXOpeningFragment' },
52+
closingFragment: { type: 'JSXClosingFragment' },
53+
children: [
54+
node.type === 'JSXElement'
55+
? node
56+
: {
57+
type: 'JSXExpressionContainer',
58+
expression: node,
59+
},
60+
],
61+
}
5862
}

pnpm-lock.yaml

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)