build: add browser builds for @vue/compiler-sfc

This commit is contained in:
Evan You
2020-04-26 01:24:25 -04:00
parent 0bb1f67d12
commit bd0f7da2c6
13 changed files with 746 additions and 87 deletions

View File

@@ -30,8 +30,9 @@ import {
KEEP_ALIVE,
BASE_TRANSITION
} from './runtimeHelpers'
import { isString, isFunction, isObject, hyphenate } from '@vue/shared'
import { isString, isObject, hyphenate } from '@vue/shared'
import { parse } from '@babel/parser'
import { walk } from 'estree-walker'
import { Node } from '@babel/types'
export const isBuiltInType = (tag: string, expected: string): boolean =>
@@ -49,31 +50,16 @@ export function isCoreComponent(tag: string): symbol | void {
}
}
// cache node requires
// lazy require dependencies so that they don't end up in rollup's dep graph
// and thus can be tree-shaken in browser builds.
let _parse: typeof parse
let _walk: any
export function loadDep(name: string) {
if (!__BROWSER__ && typeof process !== 'undefined' && isFunction(require)) {
return require(name)
} else {
// This is only used when we are building a dev-only build of the compiler
// which runs in the browser but also uses Node deps.
return (window as any)._deps[name]
}
}
export const parseJS: typeof parse = (code, options) => {
assert(
!__BROWSER__,
`Expression AST analysis can only be performed in non-browser builds.`
)
if (!_parse) {
_parse = loadDep('@babel/parser').parse
if (__BROWSER__) {
assert(
!__BROWSER__,
`Expression AST analysis can only be performed in non-browser builds.`
)
return null as any
} else {
return parse(code, options)
}
return _parse(code, options)
}
interface Walker {
@@ -82,12 +68,15 @@ interface Walker {
}
export const walkJS = (ast: Node, walker: Walker) => {
assert(
!__BROWSER__,
`Expression AST analysis can only be performed in non-browser builds.`
)
const walk = _walk || (_walk = loadDep('estree-walker').walk)
return walk(ast, 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]/