Skip to content

Commit

Permalink
perf: optimize away isBuiltInType
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Nov 25, 2023
1 parent bc170c4 commit 66c0ed0
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 24 deletions.
2 changes: 1 addition & 1 deletion packages/compiler-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ export {
CompilerDeprecationTypes
} from './compat/compatConfig'

export { baseParse as newParse } from './parser/index'
// export { baseParse as newParse } from './parser/index'
11 changes: 3 additions & 8 deletions packages/compiler-core/src/transforms/vIf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,7 @@ import { createCompilerError, ErrorCodes } from '../errors'
import { processExpression } from './transformExpression'
import { validateBrowserExpression } from '../validateExpression'
import { FRAGMENT, CREATE_COMMENT } from '../runtimeHelpers'
import {
injectProp,
findDir,
findProp,
isBuiltInType,
getMemoedVNodeCall
} from '../utils'
import { injectProp, findDir, findProp, getMemoedVNodeCall } from '../utils'
import { PatchFlags, PatchFlagNames } from '@vue/shared'

export const transformIf = createStructuralDirectiveTransform(
Expand Down Expand Up @@ -165,7 +159,8 @@ export function processIf(
!(
context.parent &&
context.parent.type === NodeTypes.ELEMENT &&
isBuiltInType(context.parent.tag, 'transition')
(context.parent.tag === 'transition' ||
context.parent.tag === 'Transition')
)
) {
branch.children = [...comments, ...branch.children]
Expand Down
5 changes: 1 addition & 4 deletions packages/compiler-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,14 @@ import {
GUARD_REACTIVE_PROPS,
WITH_MEMO
} from './runtimeHelpers'
import { isString, isObject, hyphenate, extend, NOOP } from '@vue/shared'
import { isString, isObject, extend, NOOP } from '@vue/shared'
import { PropsExpression } from './transforms/transformElement'
import { parseExpression } from '@babel/parser'
import { Expression } from '@babel/types'

export const isStaticExp = (p: JSChildNode): p is SimpleExpressionNode =>
p.type === NodeTypes.SIMPLE_EXPRESSION && p.isStatic

export const isBuiltInType = (tag: string, expected: string): boolean =>
tag === expected || tag === hyphenate(expected)

export function isCoreComponent(tag: string): symbol | void {
switch (tag) {
case 'Teleport':
Expand Down
7 changes: 3 additions & 4 deletions packages/compiler-dom/src/parserOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import {
TextModes,
ParserOptions,
ElementNode,
NodeTypes,
isBuiltInType
NodeTypes
} from '@vue/compiler-core'
import { isVoidTag, isHTMLTag, isSVGTag } from '@vue/shared'
import { TRANSITION, TRANSITION_GROUP } from './runtimeHelpers'
Expand All @@ -23,9 +22,9 @@ export const parserOptions: ParserOptions = {
decodeEntities: __BROWSER__ ? decodeHtmlBrowser : decodeHtml,

isBuiltInComponent: (tag: string): symbol | undefined => {
if (isBuiltInType(tag, `Transition`)) {
if (tag === 'Transition' || tag === 'transition') {
return TRANSITION
} else if (isBuiltInType(tag, `TransitionGroup`)) {
} else if (tag === 'TransitionGroup' || tag === 'transition-group') {
return TRANSITION_GROUP
}
},
Expand Down
5 changes: 2 additions & 3 deletions packages/compiler-ssr/src/transforms/ssrInjectCssVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {
createSimpleExpression,
RootNode,
TemplateChildNode,
findDir,
isBuiltInType
findDir
} from '@vue/compiler-dom'

export const ssrInjectCssVars: NodeTransform = (node, context) => {
Expand Down Expand Up @@ -43,7 +42,7 @@ function injectCssVars(node: RootNode | TemplateChildNode) {
node.tagType === ElementTypes.COMPONENT) &&
!findDir(node, 'for')
) {
if (isBuiltInType(node.tag, 'Suspense')) {
if (node.tag === 'suspense' || node.tag === 'Suspense') {
for (const child of node.children) {
if (
child.type === NodeTypes.ELEMENT &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {
RootNode,
TemplateChildNode,
ParentNode,
findDir,
isBuiltInType
findDir
} from '@vue/compiler-dom'

const filterChild = (node: ParentNode) =>
Expand All @@ -28,8 +27,10 @@ export const ssrInjectFallthroughAttrs: NodeTransform = (node, context) => {
if (
node.type === NodeTypes.ELEMENT &&
node.tagType === ElementTypes.COMPONENT &&
(isBuiltInType(node.tag, 'Transition') ||
isBuiltInType(node.tag, 'KeepAlive'))
(node.tag === 'transition' ||
node.tag === 'Transition' ||
node.tag === 'KeepAlive' ||
node.tag === 'keep-alive')
) {
const rootChildren = filterChild(context.root)
if (rootChildren.length === 1 && rootChildren[0] === node) {
Expand Down

0 comments on commit 66c0ed0

Please sign in to comment.