Skip to content

Commit

Permalink
refactor: move @babel/types to dev deps, reduce install size
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jul 21, 2020
1 parent 54727f9 commit be4df12
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 45 deletions.
4 changes: 3 additions & 1 deletion packages/compiler-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
"dependencies": {
"@vue/shared": "3.0.0-rc.2",
"@babel/parser": "^7.10.4",
"@babel/types": "^7.10.4",
"estree-walker": "^2.0.1",
"source-map": "^0.6.1"
},
"devDependencies": {
"@babel/types": "^7.10.4"
}
}
19 changes: 8 additions & 11 deletions packages/compiler-core/src/transforms/transformExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ import {
CompoundExpressionNode,
createCompoundExpression
} from '../ast'
import {
advancePositionWithClone,
isSimpleIdentifier,
parseJS,
walkJS
} from '../utils'
import { advancePositionWithClone, isSimpleIdentifier } from '../utils'
import {
isGloballyWhitelisted,
makeMap,
Expand All @@ -31,6 +26,8 @@ import {
import { createCompilerError, ErrorCodes } from '../errors'
import { Node, Function, Identifier, ObjectProperty } from '@babel/types'
import { validateBrowserExpression } from '../validateExpression'
import { parse } from '@babel/parser'
import { walk } from 'estree-walker'

const isLiteralWhitelisted = /*#__PURE__*/ makeMap('true,false,null,this')

Expand Down Expand Up @@ -137,7 +134,7 @@ export function processExpression(
? ` ${rawExp} `
: `(${rawExp})${asParams ? `=>{}` : ``}`
try {
ast = parseJS(source, {
ast = parse(source, {
plugins: [...context.expressionPlugins, ...babelParserDefautPlugins]
}).program
} catch (e) {
Expand All @@ -158,8 +155,8 @@ export function processExpression(
ids.some(id => id.start === node.start)

// walk the AST and look for identifiers that need to be prefixed.
walkJS(ast, {
enter(node: Node & PrefixMeta, parent) {
;(walk as any)(ast, {
enter(node: Node & PrefixMeta, parent: Node) {
if (node.type === 'Identifier') {
if (!isDuplicate(node)) {
const needPrefix = shouldPrefix(node, parent)
Expand All @@ -186,8 +183,8 @@ export function processExpression(
// walk function expressions and add its arguments to known identifiers
// so that we don't prefix them
node.params.forEach(p =>
walkJS(p, {
enter(child, parent) {
(walk as any)(p, {
enter(child: Node, parent: Node) {
if (
child.type === 'Identifier' &&
// do not record as scope variable if is a destructured key
Expand Down
32 changes: 0 additions & 32 deletions packages/compiler-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ import {
BASE_TRANSITION
} from './runtimeHelpers'
import { isString, isObject, hyphenate, extend } from '@vue/shared'
import { parse } from '@babel/parser'
import { walk } from 'estree-walker'
import { Node } from '@babel/types'

export const isStaticExp = (p: JSChildNode): p is SimpleExpressionNode =>
p.type === NodeTypes.SIMPLE_EXPRESSION && p.isStatic
Expand All @@ -54,35 +51,6 @@ export function isCoreComponent(tag: string): symbol | void {
}
}

export const parseJS: typeof parse = (code, options) => {
if (__BROWSER__) {
assert(
!__BROWSER__,
`Expression AST analysis can only be performed in non-browser builds.`
)
return null as any
} else {
return parse(code, options)
}
}

interface Walker {
enter?(node: Node, parent: Node): void
leave?(node: Node): void
}

export const walkJS = (ast: Node, walker: Walker) => {
if (__BROWSER__) {
assert(
!__BROWSER__,
`Expression AST analysis can only be performed in non-browser builds.`
)
return null as any
} else {
return (walk as any)(ast, walker)
}
}

const nonIdentifierRE = /^\d|[^\$\w]/
export const isSimpleIdentifier = (name: string): boolean =>
!nonIdentifierRE.test(name)
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler-sfc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
},
"dependencies": {
"@babel/parser": "^7.10.4",
"@babel/types": "^7.10.4",
"@vue/compiler-core": "3.0.0-rc.2",
"@vue/compiler-dom": "3.0.0-rc.2",
"@vue/compiler-ssr": "3.0.0-rc.2",
Expand All @@ -52,6 +51,7 @@
"source-map": "^0.6.1"
},
"devDependencies": {
"@babel/types": "^7.10.4",
"@types/consolidate": "^0.14.0",
"@types/lru-cache": "^5.1.0",
"pug": "^2.0.4",
Expand Down

0 comments on commit be4df12

Please sign in to comment.