2020-07-10 06:18:46 +08:00
|
|
|
import MagicString from 'magic-string'
|
2020-11-17 07:27:15 +08:00
|
|
|
import { BindingMetadata, BindingTypes, UNREF } from '@vue/compiler-core'
|
2020-07-07 03:56:24 +08:00
|
|
|
import { SFCDescriptor, SFCScriptBlock } from './parse'
|
2020-10-30 23:52:46 +08:00
|
|
|
import { parse as _parse, ParserOptions, ParserPlugin } from '@babel/parser'
|
2020-08-19 21:53:09 +08:00
|
|
|
import { babelParserDefaultPlugins, generateCodeFrame } from '@vue/shared'
|
2020-07-08 05:54:01 +08:00
|
|
|
import {
|
|
|
|
Node,
|
|
|
|
Declaration,
|
|
|
|
ObjectPattern,
|
2020-08-29 04:21:03 +08:00
|
|
|
ObjectExpression,
|
2020-07-08 05:54:01 +08:00
|
|
|
ArrayPattern,
|
|
|
|
Identifier,
|
2020-07-09 05:21:39 +08:00
|
|
|
ExportSpecifier,
|
2020-07-11 06:00:13 +08:00
|
|
|
Function as FunctionNode,
|
2020-07-09 23:55:04 +08:00
|
|
|
TSType,
|
2020-07-08 05:54:01 +08:00
|
|
|
TSTypeLiteral,
|
|
|
|
TSFunctionType,
|
2020-08-29 04:21:03 +08:00
|
|
|
ObjectProperty,
|
|
|
|
ArrayExpression,
|
2020-10-30 03:03:39 +08:00
|
|
|
Statement,
|
|
|
|
Expression,
|
2020-11-13 07:11:25 +08:00
|
|
|
LabeledStatement,
|
2020-11-19 08:38:18 +08:00
|
|
|
TSUnionType,
|
|
|
|
CallExpression
|
2020-07-08 05:54:01 +08:00
|
|
|
} from '@babel/types'
|
|
|
|
import { walk } from 'estree-walker'
|
2020-07-10 06:18:46 +08:00
|
|
|
import { RawSourceMap } from 'source-map'
|
2020-11-25 04:28:35 +08:00
|
|
|
import {
|
|
|
|
CSS_VARS_HELPER,
|
|
|
|
genCssVarsCode,
|
|
|
|
genNormalScriptCssVarsCode
|
|
|
|
} from './cssVars'
|
2020-11-11 05:28:34 +08:00
|
|
|
import { compileTemplate, SFCTemplateCompileOptions } from './compileTemplate'
|
2020-11-20 09:36:15 +08:00
|
|
|
import { warnExperimental, warnOnce } from './warn'
|
2020-11-25 04:28:35 +08:00
|
|
|
import { rewriteDefault } from './rewriteDefault'
|
2020-07-07 03:56:24 +08:00
|
|
|
|
2020-11-25 04:12:59 +08:00
|
|
|
const DEFINE_PROPS = 'defineProps'
|
|
|
|
const DEFINE_EMIT = 'defineEmit'
|
2020-11-12 08:40:27 +08:00
|
|
|
|
2020-07-10 06:18:46 +08:00
|
|
|
export interface SFCScriptCompileOptions {
|
2020-11-17 07:27:15 +08:00
|
|
|
/**
|
|
|
|
* Scope ID for prefixing injected CSS varialbes.
|
|
|
|
* This must be consistent with the `id` passed to `compileStyle`.
|
|
|
|
*/
|
|
|
|
id: string
|
2020-11-18 04:58:46 +08:00
|
|
|
/**
|
|
|
|
* Production mode. Used to determine whether to generate hashed CSS variables
|
|
|
|
*/
|
|
|
|
isProd?: boolean
|
2020-07-10 11:06:11 +08:00
|
|
|
/**
|
|
|
|
* https://babeljs.io/docs/en/babel-parser#plugins
|
|
|
|
*/
|
2020-07-10 06:18:46 +08:00
|
|
|
babelParserPlugins?: ParserPlugin[]
|
2020-11-11 05:28:34 +08:00
|
|
|
/**
|
|
|
|
* Enable ref: label sugar
|
|
|
|
* https://github.com/vuejs/rfcs/pull/228
|
|
|
|
* @default true
|
|
|
|
*/
|
2020-10-30 03:03:39 +08:00
|
|
|
refSugar?: boolean
|
2020-11-11 05:28:34 +08:00
|
|
|
/**
|
|
|
|
* Compile the template and inline the resulting render function
|
|
|
|
* directly inside setup().
|
|
|
|
* - Only affects <script setup>
|
|
|
|
* - This should only be used in production because it prevents the template
|
|
|
|
* from being hot-reloaded separately from component state.
|
|
|
|
*/
|
|
|
|
inlineTemplate?: boolean
|
2020-11-17 07:27:15 +08:00
|
|
|
templateOptions?: Partial<SFCTemplateCompileOptions>
|
2020-07-07 03:56:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compile `<script setup>`
|
|
|
|
* It requires the whole SFC descriptor because we need to handle and merge
|
|
|
|
* normal `<script>` + `<script setup>` if both are present.
|
|
|
|
*/
|
2020-07-10 06:18:46 +08:00
|
|
|
export function compileScript(
|
2020-07-07 03:56:24 +08:00
|
|
|
sfc: SFCDescriptor,
|
2020-11-17 07:27:15 +08:00
|
|
|
options: SFCScriptCompileOptions
|
2020-07-10 06:18:46 +08:00
|
|
|
): SFCScriptBlock {
|
2020-11-17 07:27:15 +08:00
|
|
|
const { script, scriptSetup, source, filename } = sfc
|
2020-07-15 23:09:33 +08:00
|
|
|
|
2020-11-20 09:36:15 +08:00
|
|
|
if (scriptSetup) {
|
|
|
|
warnExperimental(`<script setup>`, 227)
|
2020-07-09 23:55:04 +08:00
|
|
|
}
|
|
|
|
|
2020-11-17 07:27:15 +08:00
|
|
|
// for backwards compat
|
|
|
|
if (!options) {
|
|
|
|
options = { id: '' }
|
|
|
|
}
|
|
|
|
if (!options.id) {
|
|
|
|
warnOnce(
|
|
|
|
`compileScript now requires passing the \`id\` option.\n` +
|
|
|
|
`Upgrade your vite or vue-loader version for compatibility with ` +
|
|
|
|
`the latest experimental proposals.`
|
|
|
|
)
|
|
|
|
}
|
2020-07-11 04:30:58 +08:00
|
|
|
|
2020-11-17 07:27:15 +08:00
|
|
|
const scopeId = options.id ? options.id.replace(/^data-v-/, '') : ''
|
2020-11-19 00:07:10 +08:00
|
|
|
const cssVars = sfc.cssVars
|
2020-11-25 04:28:35 +08:00
|
|
|
const hasInheritAttrsFlag =
|
|
|
|
sfc.template && sfc.template.attrs['inherit-attrs'] === 'false'
|
2020-07-16 05:43:54 +08:00
|
|
|
const scriptLang = script && script.lang
|
|
|
|
const scriptSetupLang = scriptSetup && scriptSetup.lang
|
|
|
|
const isTS = scriptLang === 'ts' || scriptSetupLang === 'ts'
|
2020-09-15 09:51:15 +08:00
|
|
|
const plugins: ParserPlugin[] = [...babelParserDefaultPlugins, 'jsx']
|
2020-08-29 04:21:03 +08:00
|
|
|
if (options.babelParserPlugins) plugins.push(...options.babelParserPlugins)
|
2020-09-15 09:51:15 +08:00
|
|
|
if (isTS) plugins.push('typescript', 'decorators-legacy')
|
2020-07-11 04:30:58 +08:00
|
|
|
|
2020-07-07 03:56:24 +08:00
|
|
|
if (!scriptSetup) {
|
2020-07-10 06:18:46 +08:00
|
|
|
if (!script) {
|
2020-10-30 23:52:46 +08:00
|
|
|
throw new Error(`[@vue/compiler-sfc] SFC contains no <script> tags.`)
|
2020-07-10 06:18:46 +08:00
|
|
|
}
|
2020-07-16 05:43:54 +08:00
|
|
|
if (scriptLang && scriptLang !== 'ts') {
|
|
|
|
// do not process non js/ts script blocks
|
|
|
|
return script
|
|
|
|
}
|
2020-09-15 10:10:23 +08:00
|
|
|
try {
|
2020-10-30 23:52:46 +08:00
|
|
|
const scriptAst = _parse(script.content, {
|
2020-09-15 10:10:23 +08:00
|
|
|
plugins,
|
|
|
|
sourceType: 'module'
|
|
|
|
}).program.body
|
2020-11-17 07:27:15 +08:00
|
|
|
const bindings = analyzeScriptBindings(scriptAst)
|
2020-11-25 04:28:35 +08:00
|
|
|
const needRewrite = cssVars.length || hasInheritAttrsFlag
|
|
|
|
let content = script.content
|
|
|
|
if (needRewrite) {
|
|
|
|
content = rewriteDefault(content, `__default__`, plugins)
|
|
|
|
if (cssVars.length) {
|
|
|
|
content += genNormalScriptCssVarsCode(
|
|
|
|
cssVars,
|
|
|
|
bindings,
|
|
|
|
scopeId,
|
|
|
|
!!options.isProd
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if (hasInheritAttrsFlag) {
|
|
|
|
content += `__default__.inheritAttrs = false`
|
|
|
|
}
|
|
|
|
content += `\nexport default __default__`
|
|
|
|
}
|
2020-09-15 10:10:23 +08:00
|
|
|
return {
|
|
|
|
...script,
|
2020-11-25 04:28:35 +08:00
|
|
|
content,
|
2020-11-17 07:27:15 +08:00
|
|
|
bindings,
|
2020-09-15 10:10:23 +08:00
|
|
|
scriptAst
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// silently fallback if parse fails since user may be using custom
|
|
|
|
// babel syntax
|
|
|
|
return script
|
2020-07-10 06:18:46 +08:00
|
|
|
}
|
2020-07-07 03:56:24 +08:00
|
|
|
}
|
|
|
|
|
2020-07-16 05:43:54 +08:00
|
|
|
if (script && scriptLang !== scriptSetupLang) {
|
2020-07-07 03:56:24 +08:00
|
|
|
throw new Error(
|
2020-10-30 23:52:46 +08:00
|
|
|
`[@vue/compiler-sfc] <script> and <script setup> must have the same language type.`
|
2020-07-07 03:56:24 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-07-16 05:43:54 +08:00
|
|
|
if (scriptSetupLang && scriptSetupLang !== 'ts') {
|
|
|
|
// do not process non js/ts script blocks
|
|
|
|
return scriptSetup
|
|
|
|
}
|
|
|
|
|
2020-07-10 11:06:11 +08:00
|
|
|
const defaultTempVar = `__default__`
|
2020-10-30 03:03:39 +08:00
|
|
|
const bindingMetadata: BindingMetadata = {}
|
|
|
|
const helperImports: Set<string> = new Set()
|
2020-11-12 08:40:27 +08:00
|
|
|
const userImports: Record<
|
|
|
|
string,
|
|
|
|
{
|
2020-11-20 01:17:39 +08:00
|
|
|
isType: boolean
|
2020-12-02 00:52:29 +08:00
|
|
|
imported: string
|
2020-11-12 08:40:27 +08:00
|
|
|
source: string
|
|
|
|
}
|
|
|
|
> = Object.create(null)
|
2020-11-19 08:38:18 +08:00
|
|
|
const userImportAlias: Record<string, string> = Object.create(null)
|
2020-11-19 04:17:50 +08:00
|
|
|
const setupBindings: Record<string, BindingTypes> = Object.create(null)
|
|
|
|
const refBindings: Record<string, BindingTypes> = Object.create(null)
|
2020-10-30 03:03:39 +08:00
|
|
|
const refIdentifiers: Set<Identifier> = new Set()
|
|
|
|
const enableRefSugar = options.refSugar !== false
|
2020-07-08 05:54:01 +08:00
|
|
|
let defaultExport: Node | undefined
|
2020-11-25 04:12:59 +08:00
|
|
|
let hasDefinePropsCall = false
|
|
|
|
let hasDefineEmitCall = false
|
|
|
|
let propsRuntimeDecl: Node | undefined
|
|
|
|
let propsTypeDecl: TSTypeLiteral | undefined
|
|
|
|
let propsIdentifier: string | undefined
|
|
|
|
let emitRuntimeDecl: Node | undefined
|
|
|
|
let emitTypeDecl: TSFunctionType | TSUnionType | undefined
|
|
|
|
let emitIdentifier: string | undefined
|
2020-07-11 06:00:13 +08:00
|
|
|
let hasAwait = false
|
2020-11-21 03:22:51 +08:00
|
|
|
let hasInlinedSsrRenderFn = false
|
2020-11-14 06:38:28 +08:00
|
|
|
// props/emits declared via types
|
|
|
|
const typeDeclaredProps: Record<string, PropTypeData> = {}
|
|
|
|
const typeDeclaredEmits: Set<string> = new Set()
|
|
|
|
// record declared types for runtime props type generation
|
|
|
|
const declaredTypes: Record<string, string[]> = {}
|
2020-07-09 09:11:57 +08:00
|
|
|
|
2020-11-14 06:38:28 +08:00
|
|
|
// magic-string state
|
2020-07-07 03:56:24 +08:00
|
|
|
const s = new MagicString(source)
|
|
|
|
const startOffset = scriptSetup.loc.start.offset
|
|
|
|
const endOffset = scriptSetup.loc.end.offset
|
2020-07-08 05:54:01 +08:00
|
|
|
const scriptStartOffset = script && script.loc.start.offset
|
|
|
|
const scriptEndOffset = script && script.loc.end.offset
|
2020-07-07 03:56:24 +08:00
|
|
|
|
2020-11-17 00:35:30 +08:00
|
|
|
function helper(key: string): string {
|
|
|
|
helperImports.add(key)
|
|
|
|
return `_${key}`
|
|
|
|
}
|
|
|
|
|
2020-10-30 23:52:46 +08:00
|
|
|
function parse(
|
|
|
|
input: string,
|
|
|
|
options: ParserOptions,
|
|
|
|
offset: number
|
|
|
|
): Statement[] {
|
|
|
|
try {
|
|
|
|
return _parse(input, options).program.body
|
|
|
|
} catch (e) {
|
2020-11-20 01:17:39 +08:00
|
|
|
e.message = `[@vue/compiler-sfc] ${e.message}\n\n${
|
|
|
|
sfc.filename
|
|
|
|
}\n${generateCodeFrame(source, e.pos + offset, e.pos + offset + 1)}`
|
2020-10-30 23:52:46 +08:00
|
|
|
throw e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-30 03:03:39 +08:00
|
|
|
function error(
|
|
|
|
msg: string,
|
|
|
|
node: Node,
|
|
|
|
end: number = node.end! + startOffset
|
|
|
|
) {
|
|
|
|
throw new Error(
|
2020-11-20 01:17:39 +08:00
|
|
|
`[@vue/compiler-sfc] ${msg}\n\n${sfc.filename}\n${generateCodeFrame(
|
|
|
|
source,
|
|
|
|
node.start! + startOffset,
|
|
|
|
end
|
|
|
|
)}`
|
2020-10-30 03:03:39 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-11-19 08:38:18 +08:00
|
|
|
function registerUserImport(
|
|
|
|
source: string,
|
|
|
|
local: string,
|
2020-11-20 01:17:39 +08:00
|
|
|
imported: string | false,
|
|
|
|
isType: boolean
|
2020-11-19 08:38:18 +08:00
|
|
|
) {
|
|
|
|
if (source === 'vue' && imported) {
|
|
|
|
userImportAlias[imported] = local
|
|
|
|
}
|
|
|
|
userImports[local] = {
|
2020-11-20 01:17:39 +08:00
|
|
|
isType,
|
2020-12-02 00:52:29 +08:00
|
|
|
imported: imported || 'default',
|
2020-11-19 08:38:18 +08:00
|
|
|
source
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 04:12:59 +08:00
|
|
|
function processDefineProps(node: Node): boolean {
|
|
|
|
if (isCallOf(node, DEFINE_PROPS)) {
|
|
|
|
if (hasDefinePropsCall) {
|
|
|
|
error(`duplicate ${DEFINE_PROPS}() call`, node)
|
2020-11-13 07:11:25 +08:00
|
|
|
}
|
2020-11-25 04:12:59 +08:00
|
|
|
hasDefinePropsCall = true
|
|
|
|
propsRuntimeDecl = node.arguments[0]
|
|
|
|
// context call has type parameters - infer runtime types from it
|
|
|
|
if (node.typeParameters) {
|
|
|
|
if (propsRuntimeDecl) {
|
|
|
|
error(
|
|
|
|
`${DEFINE_PROPS}() cannot accept both type and non-type arguments ` +
|
|
|
|
`at the same time. Use one or the other.`,
|
|
|
|
node
|
|
|
|
)
|
|
|
|
}
|
|
|
|
const typeArg = node.typeParameters.params[0]
|
|
|
|
if (typeArg.type === 'TSTypeLiteral') {
|
|
|
|
propsTypeDecl = typeArg
|
2020-11-13 07:11:25 +08:00
|
|
|
} else {
|
2020-11-14 06:38:28 +08:00
|
|
|
error(
|
2020-11-25 04:12:59 +08:00
|
|
|
`type argument passed to ${DEFINE_PROPS}() must be a literal type.`,
|
|
|
|
typeArg
|
2020-11-14 06:38:28 +08:00
|
|
|
)
|
2020-11-13 07:11:25 +08:00
|
|
|
}
|
|
|
|
}
|
2020-11-25 04:12:59 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
function processDefineEmit(node: Node): boolean {
|
|
|
|
if (isCallOf(node, DEFINE_EMIT)) {
|
|
|
|
if (hasDefineEmitCall) {
|
|
|
|
error(`duplicate ${DEFINE_EMIT}() call`, node)
|
|
|
|
}
|
|
|
|
hasDefineEmitCall = true
|
|
|
|
emitRuntimeDecl = node.arguments[0]
|
2020-11-13 07:11:25 +08:00
|
|
|
if (node.typeParameters) {
|
2020-11-25 04:12:59 +08:00
|
|
|
if (emitRuntimeDecl) {
|
2020-11-13 07:11:25 +08:00
|
|
|
error(
|
2020-11-25 04:12:59 +08:00
|
|
|
`${DEFINE_EMIT}() cannot accept both type and non-type arguments ` +
|
2020-11-13 07:11:25 +08:00
|
|
|
`at the same time. Use one or the other.`,
|
|
|
|
node
|
|
|
|
)
|
|
|
|
}
|
|
|
|
const typeArg = node.typeParameters.params[0]
|
2020-11-25 04:12:59 +08:00
|
|
|
if (
|
|
|
|
typeArg.type === 'TSFunctionType' ||
|
|
|
|
typeArg.type === 'TSUnionType'
|
|
|
|
) {
|
|
|
|
emitTypeDecl = typeArg
|
2020-11-13 07:11:25 +08:00
|
|
|
} else {
|
|
|
|
error(
|
2020-11-25 04:12:59 +08:00
|
|
|
`type argument passed to ${DEFINE_EMIT}() must be a function type ` +
|
|
|
|
`or a union of function types.`,
|
2020-11-13 07:11:25 +08:00
|
|
|
typeArg
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-11-25 04:12:59 +08:00
|
|
|
function checkInvalidScopeReference(node: Node | undefined, method: string) {
|
|
|
|
if (!node) return
|
|
|
|
walkIdentifiers(node, id => {
|
|
|
|
if (setupBindings[id.name]) {
|
|
|
|
error(
|
|
|
|
`\`${method}()\` in <script setup> cannot reference locally ` +
|
|
|
|
`declared variables because it will be hoisted outside of the ` +
|
|
|
|
`setup() function. If your component options requires initialization ` +
|
|
|
|
`in the module scope, use a separate normal <script> to export ` +
|
|
|
|
`the options instead.`,
|
|
|
|
id
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-30 03:03:39 +08:00
|
|
|
function processRefExpression(exp: Expression, statement: LabeledStatement) {
|
|
|
|
if (exp.type === 'AssignmentExpression') {
|
|
|
|
const { left, right } = exp
|
|
|
|
if (left.type === 'Identifier') {
|
2020-10-31 03:29:38 +08:00
|
|
|
registerRefBinding(left)
|
2020-11-17 00:35:30 +08:00
|
|
|
s.prependRight(right.start! + startOffset, `${helper('ref')}(`)
|
2020-10-30 03:03:39 +08:00
|
|
|
s.appendLeft(right.end! + startOffset, ')')
|
|
|
|
} else if (left.type === 'ObjectPattern') {
|
|
|
|
// remove wrapping parens
|
|
|
|
for (let i = left.start!; i > 0; i--) {
|
|
|
|
const char = source[i + startOffset]
|
|
|
|
if (char === '(') {
|
|
|
|
s.remove(i + startOffset, i + startOffset + 1)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (let i = left.end!; i > 0; i++) {
|
|
|
|
const char = source[i + startOffset]
|
|
|
|
if (char === ')') {
|
|
|
|
s.remove(i + startOffset, i + startOffset + 1)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
processRefObjectPattern(left, statement)
|
|
|
|
} else if (left.type === 'ArrayPattern') {
|
|
|
|
processRefArrayPattern(left, statement)
|
|
|
|
}
|
|
|
|
} else if (exp.type === 'SequenceExpression') {
|
|
|
|
// possible multiple declarations
|
|
|
|
// ref: x = 1, y = 2
|
|
|
|
exp.expressions.forEach(e => processRefExpression(e, statement))
|
2020-10-31 03:29:38 +08:00
|
|
|
} else if (exp.type === 'Identifier') {
|
|
|
|
registerRefBinding(exp)
|
2020-11-17 00:35:30 +08:00
|
|
|
s.appendLeft(exp.end! + startOffset, ` = ${helper('ref')}()`)
|
2020-10-30 03:03:39 +08:00
|
|
|
} else {
|
|
|
|
error(`ref: statements can only contain assignment expressions.`, exp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-31 03:29:38 +08:00
|
|
|
function registerRefBinding(id: Identifier) {
|
|
|
|
if (id.name[0] === '$') {
|
|
|
|
error(`ref variable identifiers cannot start with $.`, id)
|
|
|
|
}
|
2020-11-19 08:38:18 +08:00
|
|
|
refBindings[id.name] = setupBindings[id.name] = BindingTypes.SETUP_REF
|
2020-10-31 03:29:38 +08:00
|
|
|
refIdentifiers.add(id)
|
|
|
|
}
|
|
|
|
|
2020-10-30 03:03:39 +08:00
|
|
|
function processRefObjectPattern(
|
|
|
|
pattern: ObjectPattern,
|
|
|
|
statement: LabeledStatement
|
|
|
|
) {
|
|
|
|
for (const p of pattern.properties) {
|
|
|
|
let nameId: Identifier | undefined
|
|
|
|
if (p.type === 'ObjectProperty') {
|
|
|
|
if (p.key.start! === p.value.start!) {
|
|
|
|
// shorthand { foo } --> { foo: __foo }
|
|
|
|
nameId = p.key as Identifier
|
|
|
|
s.appendLeft(nameId.end! + startOffset, `: __${nameId.name}`)
|
|
|
|
if (p.value.type === 'AssignmentPattern') {
|
|
|
|
// { foo = 1 }
|
|
|
|
refIdentifiers.add(p.value.left as Identifier)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (p.value.type === 'Identifier') {
|
|
|
|
// { foo: bar } --> { foo: __bar }
|
|
|
|
nameId = p.value
|
|
|
|
s.prependRight(nameId.start! + startOffset, `__`)
|
|
|
|
} else if (p.value.type === 'ObjectPattern') {
|
|
|
|
processRefObjectPattern(p.value, statement)
|
|
|
|
} else if (p.value.type === 'ArrayPattern') {
|
|
|
|
processRefArrayPattern(p.value, statement)
|
|
|
|
} else if (p.value.type === 'AssignmentPattern') {
|
|
|
|
// { foo: bar = 1 } --> { foo: __bar = 1 }
|
|
|
|
nameId = p.value.left as Identifier
|
|
|
|
s.prependRight(nameId.start! + startOffset, `__`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// rest element { ...foo } --> { ...__foo }
|
|
|
|
nameId = p.argument as Identifier
|
|
|
|
s.prependRight(nameId.start! + startOffset, `__`)
|
|
|
|
}
|
|
|
|
if (nameId) {
|
2020-10-31 03:29:38 +08:00
|
|
|
registerRefBinding(nameId)
|
2020-10-30 03:03:39 +08:00
|
|
|
// append binding declarations after the parent statement
|
|
|
|
s.appendLeft(
|
|
|
|
statement.end! + startOffset,
|
2020-11-17 00:35:30 +08:00
|
|
|
`\nconst ${nameId.name} = ${helper('ref')}(__${nameId.name});`
|
2020-10-30 03:03:39 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function processRefArrayPattern(
|
|
|
|
pattern: ArrayPattern,
|
|
|
|
statement: LabeledStatement
|
|
|
|
) {
|
|
|
|
for (const e of pattern.elements) {
|
|
|
|
if (!e) continue
|
|
|
|
let nameId: Identifier | undefined
|
|
|
|
if (e.type === 'Identifier') {
|
|
|
|
// [a] --> [__a]
|
|
|
|
nameId = e
|
|
|
|
} else if (e.type === 'AssignmentPattern') {
|
|
|
|
// [a = 1] --> [__a = 1]
|
|
|
|
nameId = e.left as Identifier
|
|
|
|
} else if (e.type === 'RestElement') {
|
|
|
|
// [...a] --> [...__a]
|
|
|
|
nameId = e.argument as Identifier
|
|
|
|
} else if (e.type === 'ObjectPattern') {
|
|
|
|
processRefObjectPattern(e, statement)
|
|
|
|
} else if (e.type === 'ArrayPattern') {
|
|
|
|
processRefArrayPattern(e, statement)
|
|
|
|
}
|
|
|
|
if (nameId) {
|
2020-10-31 03:29:38 +08:00
|
|
|
registerRefBinding(nameId)
|
|
|
|
// prefix original
|
2020-10-30 03:03:39 +08:00
|
|
|
s.prependRight(nameId.start! + startOffset, `__`)
|
|
|
|
// append binding declarations after the parent statement
|
|
|
|
s.appendLeft(
|
|
|
|
statement.end! + startOffset,
|
2020-11-17 00:35:30 +08:00
|
|
|
`\nconst ${nameId.name} = ${helper('ref')}(__${nameId.name});`
|
2020-10-30 03:03:39 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-29 04:21:03 +08:00
|
|
|
|
2020-07-08 08:23:53 +08:00
|
|
|
// 1. process normal <script> first if it exists
|
2020-10-30 03:03:39 +08:00
|
|
|
let scriptAst
|
2020-07-08 05:54:01 +08:00
|
|
|
if (script) {
|
|
|
|
// import dedupe between <script> and <script setup>
|
2020-10-30 23:52:46 +08:00
|
|
|
scriptAst = parse(
|
|
|
|
script.content,
|
|
|
|
{
|
|
|
|
plugins,
|
|
|
|
sourceType: 'module'
|
|
|
|
},
|
|
|
|
scriptStartOffset!
|
|
|
|
)
|
2020-07-08 05:54:01 +08:00
|
|
|
|
2020-08-29 04:21:03 +08:00
|
|
|
for (const node of scriptAst) {
|
2020-07-08 05:54:01 +08:00
|
|
|
if (node.type === 'ImportDeclaration') {
|
|
|
|
// record imports for dedupe
|
2020-11-12 08:40:27 +08:00
|
|
|
for (const specifier of node.specifiers) {
|
|
|
|
const imported =
|
|
|
|
specifier.type === 'ImportSpecifier' &&
|
|
|
|
specifier.imported.type === 'Identifier' &&
|
|
|
|
specifier.imported.name
|
2020-11-20 01:17:39 +08:00
|
|
|
registerUserImport(
|
|
|
|
node.source.value,
|
|
|
|
specifier.local.name,
|
|
|
|
imported,
|
|
|
|
node.importKind === 'type'
|
|
|
|
)
|
2020-07-08 05:54:01 +08:00
|
|
|
}
|
|
|
|
} else if (node.type === 'ExportDefaultDeclaration') {
|
|
|
|
// export default
|
|
|
|
defaultExport = node
|
|
|
|
const start = node.start! + scriptStartOffset!
|
|
|
|
s.overwrite(
|
|
|
|
start,
|
|
|
|
start + `export default`.length,
|
2020-07-10 11:06:11 +08:00
|
|
|
`const ${defaultTempVar} =`
|
2020-07-08 05:54:01 +08:00
|
|
|
)
|
2020-07-09 05:21:39 +08:00
|
|
|
} else if (node.type === 'ExportNamedDeclaration' && node.specifiers) {
|
|
|
|
const defaultSpecifier = node.specifiers.find(
|
2020-10-16 00:02:20 +08:00
|
|
|
s => s.exported.type === 'Identifier' && s.exported.name === 'default'
|
2020-07-09 05:21:39 +08:00
|
|
|
) as ExportSpecifier
|
|
|
|
if (defaultSpecifier) {
|
|
|
|
defaultExport = node
|
|
|
|
// 1. remove specifier
|
|
|
|
if (node.specifiers.length > 1) {
|
|
|
|
s.remove(
|
|
|
|
defaultSpecifier.start! + scriptStartOffset!,
|
|
|
|
defaultSpecifier.end! + scriptStartOffset!
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
s.remove(
|
|
|
|
node.start! + scriptStartOffset!,
|
|
|
|
node.end! + scriptStartOffset!
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if (node.source) {
|
|
|
|
// export { x as default } from './x'
|
|
|
|
// rewrite to `import { x as __default__ } from './x'` and
|
|
|
|
// add to top
|
|
|
|
s.prepend(
|
2020-07-10 11:06:11 +08:00
|
|
|
`import { ${
|
|
|
|
defaultSpecifier.local.name
|
|
|
|
} as ${defaultTempVar} } from '${node.source.value}'\n`
|
2020-07-09 05:21:39 +08:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// export { x as default }
|
|
|
|
// rewrite to `const __default__ = x` and move to end
|
2020-07-10 11:06:11 +08:00
|
|
|
s.append(
|
|
|
|
`\nconst ${defaultTempVar} = ${defaultSpecifier.local.name}\n`
|
|
|
|
)
|
2020-07-09 05:21:39 +08:00
|
|
|
}
|
2020-07-08 05:54:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-14 06:38:28 +08:00
|
|
|
// 2. parse <script setup> and walk over top level statements
|
2020-10-30 23:52:46 +08:00
|
|
|
const scriptSetupAst = parse(
|
|
|
|
scriptSetup.content,
|
|
|
|
{
|
|
|
|
plugins: [
|
|
|
|
...plugins,
|
|
|
|
// allow top level await but only inside <script setup>
|
|
|
|
'topLevelAwait'
|
|
|
|
],
|
|
|
|
sourceType: 'module'
|
|
|
|
},
|
|
|
|
startOffset
|
|
|
|
)
|
2020-08-29 04:21:03 +08:00
|
|
|
|
|
|
|
for (const node of scriptSetupAst) {
|
2020-07-07 03:56:24 +08:00
|
|
|
const start = node.start! + startOffset
|
|
|
|
let end = node.end! + startOffset
|
|
|
|
// import or type declarations: move to top
|
2020-09-15 22:39:27 +08:00
|
|
|
// locate comment
|
|
|
|
if (node.trailingComments && node.trailingComments.length > 0) {
|
|
|
|
const lastCommentNode =
|
|
|
|
node.trailingComments[node.trailingComments.length - 1]
|
|
|
|
end = lastCommentNode.end + startOffset
|
|
|
|
}
|
2020-07-07 03:56:24 +08:00
|
|
|
// locate the end of whitespace between this statement and the next
|
|
|
|
while (end <= source.length) {
|
|
|
|
if (!/\s/.test(source.charAt(end))) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
end++
|
|
|
|
}
|
2020-07-08 05:54:01 +08:00
|
|
|
|
2020-10-30 03:03:39 +08:00
|
|
|
// process `ref: x` bindings (convert to refs)
|
|
|
|
if (
|
|
|
|
node.type === 'LabeledStatement' &&
|
|
|
|
node.label.name === 'ref' &&
|
|
|
|
node.body.type === 'ExpressionStatement'
|
|
|
|
) {
|
2020-11-10 06:00:50 +08:00
|
|
|
if (enableRefSugar) {
|
2020-11-20 09:36:15 +08:00
|
|
|
warnExperimental(`ref: sugar`, 228)
|
2020-11-10 06:00:50 +08:00
|
|
|
s.overwrite(
|
|
|
|
node.label.start! + startOffset,
|
|
|
|
node.body.start! + startOffset,
|
|
|
|
'const '
|
|
|
|
)
|
|
|
|
processRefExpression(node.body.expression, node)
|
|
|
|
} else {
|
|
|
|
// TODO if we end up shipping ref: sugar as an opt-in feature,
|
|
|
|
// need to proxy the option in vite, vue-loader and rollup-plugin-vue.
|
|
|
|
error(
|
|
|
|
`ref: sugar needs to be explicitly enabled via vite or vue-loader options.`,
|
|
|
|
node
|
|
|
|
)
|
|
|
|
}
|
2020-10-30 03:03:39 +08:00
|
|
|
}
|
|
|
|
|
2020-07-07 03:56:24 +08:00
|
|
|
if (node.type === 'ImportDeclaration') {
|
2020-07-08 05:54:01 +08:00
|
|
|
// import declarations are moved to top
|
2020-07-07 03:56:24 +08:00
|
|
|
s.move(start, end, 0)
|
2020-11-17 07:27:15 +08:00
|
|
|
|
2020-07-08 05:54:01 +08:00
|
|
|
// dedupe imports
|
|
|
|
let removed = 0
|
2020-11-17 07:27:15 +08:00
|
|
|
let prev: Node | undefined, next: Node | undefined
|
|
|
|
const removeSpecifier = (node: Node) => {
|
|
|
|
removed++
|
|
|
|
s.remove(
|
|
|
|
prev ? prev.end! + startOffset : node.start! + startOffset,
|
2020-11-18 01:50:10 +08:00
|
|
|
next && !prev ? next.start! + startOffset : node.end! + startOffset
|
2020-11-17 07:27:15 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < node.specifiers.length; i++) {
|
|
|
|
const specifier = node.specifiers[i]
|
|
|
|
prev = node.specifiers[i - 1]
|
|
|
|
next = node.specifiers[i + 1]
|
2020-11-12 08:40:27 +08:00
|
|
|
const local = specifier.local.name
|
|
|
|
const imported =
|
|
|
|
specifier.type === 'ImportSpecifier' &&
|
|
|
|
specifier.imported.type === 'Identifier' &&
|
|
|
|
specifier.imported.name
|
|
|
|
const source = node.source.value
|
|
|
|
const existing = userImports[local]
|
2020-11-25 04:12:59 +08:00
|
|
|
if (
|
|
|
|
source === 'vue' &&
|
|
|
|
(imported === DEFINE_PROPS || imported === DEFINE_EMIT)
|
|
|
|
) {
|
2020-11-17 07:27:15 +08:00
|
|
|
removeSpecifier(specifier)
|
2020-11-12 08:40:27 +08:00
|
|
|
} else if (existing) {
|
|
|
|
if (existing.source === source && existing.imported === imported) {
|
|
|
|
// already imported in <script setup>, dedupe
|
2020-11-17 07:27:15 +08:00
|
|
|
removeSpecifier(specifier)
|
2020-11-12 08:40:27 +08:00
|
|
|
} else {
|
|
|
|
error(`different imports aliased to same local name.`, specifier)
|
|
|
|
}
|
2020-07-08 05:54:01 +08:00
|
|
|
} else {
|
2020-11-20 01:17:39 +08:00
|
|
|
registerUserImport(
|
|
|
|
source,
|
|
|
|
local,
|
|
|
|
imported,
|
|
|
|
node.importKind === 'type'
|
|
|
|
)
|
2020-07-08 05:54:01 +08:00
|
|
|
}
|
|
|
|
}
|
2020-11-29 02:51:32 +08:00
|
|
|
if (node.specifiers.length && removed === node.specifiers.length) {
|
2020-07-08 05:54:01 +08:00
|
|
|
s.remove(node.start! + startOffset, node.end! + startOffset)
|
|
|
|
}
|
2020-07-07 03:56:24 +08:00
|
|
|
}
|
2020-07-08 05:54:01 +08:00
|
|
|
|
2020-11-25 04:12:59 +08:00
|
|
|
// process `defineProps` and `defineEmit` calls
|
2020-11-13 07:11:25 +08:00
|
|
|
if (
|
|
|
|
node.type === 'ExpressionStatement' &&
|
2020-11-25 04:12:59 +08:00
|
|
|
(processDefineProps(node.expression) ||
|
|
|
|
processDefineEmit(node.expression))
|
2020-11-13 07:11:25 +08:00
|
|
|
) {
|
|
|
|
s.remove(node.start! + startOffset, node.end! + startOffset)
|
|
|
|
}
|
2020-11-12 08:40:27 +08:00
|
|
|
if (node.type === 'VariableDeclaration' && !node.declare) {
|
|
|
|
for (const decl of node.declarations) {
|
2020-11-25 04:12:59 +08:00
|
|
|
if (decl.init) {
|
|
|
|
const isDefineProps = processDefineProps(decl.init)
|
|
|
|
if (isDefineProps) {
|
|
|
|
propsIdentifier = scriptSetup.content.slice(
|
|
|
|
decl.id.start!,
|
|
|
|
decl.id.end!
|
|
|
|
)
|
2020-11-12 08:40:27 +08:00
|
|
|
}
|
2020-11-25 04:12:59 +08:00
|
|
|
const isDefineEmit = processDefineEmit(decl.init)
|
|
|
|
if (isDefineEmit) {
|
|
|
|
emitIdentifier = scriptSetup.content.slice(
|
|
|
|
decl.id.start!,
|
|
|
|
decl.id.end!
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if (isDefineProps || isDefineEmit)
|
|
|
|
if (node.declarations.length === 1) {
|
|
|
|
s.remove(node.start! + startOffset, node.end! + startOffset)
|
|
|
|
} else {
|
|
|
|
s.remove(decl.start! + startOffset, decl.end! + startOffset)
|
|
|
|
}
|
2020-11-12 08:40:27 +08:00
|
|
|
}
|
2020-10-30 03:03:39 +08:00
|
|
|
}
|
2020-07-08 05:54:01 +08:00
|
|
|
}
|
|
|
|
|
2020-11-12 08:40:27 +08:00
|
|
|
// walk decalrations to record declared bindings
|
2020-07-08 05:54:01 +08:00
|
|
|
if (
|
2020-07-08 07:47:16 +08:00
|
|
|
(node.type === 'VariableDeclaration' ||
|
|
|
|
node.type === 'FunctionDeclaration' ||
|
|
|
|
node.type === 'ClassDeclaration') &&
|
|
|
|
!node.declare
|
2020-07-08 05:54:01 +08:00
|
|
|
) {
|
2020-11-19 08:38:18 +08:00
|
|
|
walkDeclaration(node, setupBindings, userImportAlias)
|
2020-07-08 07:47:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Type declarations
|
|
|
|
if (node.type === 'VariableDeclaration' && node.declare) {
|
|
|
|
s.remove(start, end)
|
2020-07-08 05:54:01 +08:00
|
|
|
}
|
2020-07-08 21:45:01 +08:00
|
|
|
|
|
|
|
// move all type declarations to outer scope
|
|
|
|
if (
|
|
|
|
node.type.startsWith('TS') ||
|
|
|
|
(node.type === 'ExportNamedDeclaration' && node.exportKind === 'type')
|
|
|
|
) {
|
2020-07-09 23:55:04 +08:00
|
|
|
recordType(node, declaredTypes)
|
2020-07-08 21:45:01 +08:00
|
|
|
s.move(start, end, 0)
|
|
|
|
}
|
2020-07-11 06:00:13 +08:00
|
|
|
|
|
|
|
// walk statements & named exports / variable declarations for top level
|
|
|
|
// await
|
|
|
|
if (
|
2020-11-12 08:40:27 +08:00
|
|
|
(node.type === 'VariableDeclaration' && !node.declare) ||
|
2020-07-11 06:00:13 +08:00
|
|
|
node.type.endsWith('Statement')
|
|
|
|
) {
|
|
|
|
;(walk as any)(node, {
|
|
|
|
enter(node: Node) {
|
|
|
|
if (isFunction(node)) {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
if (node.type === 'AwaitExpression') {
|
|
|
|
hasAwait = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-11-12 08:40:27 +08:00
|
|
|
|
|
|
|
if (
|
|
|
|
(node.type === 'ExportNamedDeclaration' && node.exportKind !== 'type') ||
|
|
|
|
node.type === 'ExportAllDeclaration' ||
|
|
|
|
node.type === 'ExportDefaultDeclaration'
|
|
|
|
) {
|
|
|
|
error(
|
|
|
|
`<script setup> cannot contain ES module exports. ` +
|
|
|
|
`If you are using a previous version of <script setup>, please ` +
|
|
|
|
`consult the updated RFC at https://github.com/vuejs/rfcs/pull/227.`,
|
|
|
|
node
|
|
|
|
)
|
|
|
|
}
|
2020-07-08 05:54:01 +08:00
|
|
|
}
|
|
|
|
|
2020-11-14 06:38:28 +08:00
|
|
|
// 3. Do a full walk to rewrite identifiers referencing let exports with ref
|
2020-10-30 03:03:39 +08:00
|
|
|
// value access
|
|
|
|
if (enableRefSugar && Object.keys(refBindings).length) {
|
|
|
|
for (const node of scriptSetupAst) {
|
|
|
|
if (node.type !== 'ImportDeclaration') {
|
|
|
|
walkIdentifiers(node, (id, parent) => {
|
|
|
|
if (refBindings[id.name] && !refIdentifiers.has(id)) {
|
|
|
|
if (isStaticProperty(parent) && parent.shorthand) {
|
|
|
|
// let binding used in a property shorthand
|
|
|
|
// { foo } -> { foo: foo.value }
|
|
|
|
// skip for destructure patterns
|
2020-11-19 11:39:08 +08:00
|
|
|
if (
|
|
|
|
!(parent as any).inPattern ||
|
|
|
|
isInDestructureAssignment(parent, parentStack)
|
|
|
|
) {
|
2020-10-30 03:03:39 +08:00
|
|
|
s.appendLeft(id.end! + startOffset, `: ${id.name}.value`)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s.appendLeft(id.end! + startOffset, '.value')
|
|
|
|
}
|
|
|
|
} else if (id.name[0] === '$' && refBindings[id.name.slice(1)]) {
|
|
|
|
// $xxx raw ref access variables, remove the $ prefix
|
|
|
|
s.remove(id.start! + startOffset, id.start! + startOffset + 1)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-14 06:38:28 +08:00
|
|
|
// 4. extract runtime props/emits code from setup context type
|
2020-11-25 04:12:59 +08:00
|
|
|
if (propsTypeDecl) {
|
|
|
|
extractRuntimeProps(propsTypeDecl, typeDeclaredProps, declaredTypes)
|
|
|
|
}
|
|
|
|
if (emitTypeDecl) {
|
|
|
|
extractRuntimeEmits(emitTypeDecl, typeDeclaredEmits)
|
2020-11-12 08:40:27 +08:00
|
|
|
}
|
|
|
|
|
2020-11-13 11:51:40 +08:00
|
|
|
// 5. check useOptions args to make sure it doesn't reference setup scope
|
2020-07-08 05:54:01 +08:00
|
|
|
// variables
|
2020-11-25 04:12:59 +08:00
|
|
|
checkInvalidScopeReference(propsRuntimeDecl, DEFINE_PROPS)
|
|
|
|
checkInvalidScopeReference(emitRuntimeDecl, DEFINE_PROPS)
|
2020-07-07 03:56:24 +08:00
|
|
|
|
2020-10-30 03:03:39 +08:00
|
|
|
// 6. remove non-script content
|
2020-07-07 03:56:24 +08:00
|
|
|
if (script) {
|
2020-07-08 05:54:01 +08:00
|
|
|
if (startOffset < scriptStartOffset!) {
|
2020-07-07 03:56:24 +08:00
|
|
|
// <script setup> before <script>
|
2020-11-11 06:31:09 +08:00
|
|
|
s.remove(0, startOffset)
|
2020-07-08 05:54:01 +08:00
|
|
|
s.remove(endOffset, scriptStartOffset!)
|
|
|
|
s.remove(scriptEndOffset!, source.length)
|
2020-07-07 03:56:24 +08:00
|
|
|
} else {
|
|
|
|
// <script> before <script setup>
|
2020-07-08 05:54:01 +08:00
|
|
|
s.remove(0, scriptStartOffset!)
|
|
|
|
s.remove(scriptEndOffset!, startOffset)
|
2020-07-07 03:56:24 +08:00
|
|
|
s.remove(endOffset, source.length)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// only <script setup>
|
|
|
|
s.remove(0, startOffset)
|
|
|
|
s.remove(endOffset, source.length)
|
|
|
|
}
|
|
|
|
|
2020-11-25 04:12:59 +08:00
|
|
|
// 7. analyze binding metadata
|
2020-11-11 05:28:34 +08:00
|
|
|
if (scriptAst) {
|
|
|
|
Object.assign(bindingMetadata, analyzeScriptBindings(scriptAst))
|
|
|
|
}
|
2020-11-25 04:12:59 +08:00
|
|
|
if (propsRuntimeDecl) {
|
|
|
|
for (const key of getObjectOrArrayExpressionKeys(propsRuntimeDecl)) {
|
2020-11-13 05:11:14 +08:00
|
|
|
bindingMetadata[key] = BindingTypes.PROPS
|
2020-11-13 03:10:39 +08:00
|
|
|
}
|
|
|
|
}
|
2020-11-25 04:12:59 +08:00
|
|
|
for (const key in typeDeclaredProps) {
|
|
|
|
bindingMetadata[key] = BindingTypes.PROPS
|
2020-11-13 03:10:39 +08:00
|
|
|
}
|
2020-12-02 00:52:29 +08:00
|
|
|
for (const [key, { isType, imported, source }] of Object.entries(
|
|
|
|
userImports
|
|
|
|
)) {
|
2020-11-20 01:17:39 +08:00
|
|
|
if (isType) continue
|
2020-11-19 08:38:18 +08:00
|
|
|
bindingMetadata[key] =
|
2020-12-02 00:52:29 +08:00
|
|
|
(imported === 'default' && source.endsWith('.vue')) || source === 'vue'
|
2020-11-19 08:38:18 +08:00
|
|
|
? BindingTypes.SETUP_CONST
|
|
|
|
: BindingTypes.SETUP_MAYBE_REF
|
2020-11-13 07:11:25 +08:00
|
|
|
}
|
|
|
|
for (const key in setupBindings) {
|
|
|
|
bindingMetadata[key] = setupBindings[key]
|
2020-11-11 07:06:38 +08:00
|
|
|
}
|
2020-11-11 05:28:34 +08:00
|
|
|
|
2020-11-25 04:12:59 +08:00
|
|
|
// 8. inject `useCssVars` calls
|
2020-11-17 07:27:15 +08:00
|
|
|
if (cssVars.length) {
|
|
|
|
helperImports.add(CSS_VARS_HELPER)
|
|
|
|
helperImports.add('unref')
|
|
|
|
s.prependRight(
|
|
|
|
startOffset,
|
2020-11-18 04:58:46 +08:00
|
|
|
`\n${genCssVarsCode(
|
|
|
|
cssVars,
|
|
|
|
bindingMetadata,
|
|
|
|
scopeId,
|
|
|
|
!!options.isProd
|
|
|
|
)}\n`
|
2020-11-17 07:27:15 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-11-25 04:12:59 +08:00
|
|
|
// 9. finalize setup() argument signature
|
|
|
|
let args = `__props`
|
|
|
|
if (propsTypeDecl) {
|
|
|
|
args += `: ${scriptSetup.content.slice(
|
|
|
|
propsTypeDecl.start!,
|
|
|
|
propsTypeDecl.end!
|
|
|
|
)}`
|
|
|
|
}
|
|
|
|
// inject user assignment of props
|
|
|
|
// we use a default __props so that template expressions referencing props
|
|
|
|
// can use it directly
|
|
|
|
if (propsIdentifier) {
|
|
|
|
s.prependRight(startOffset, `\nconst ${propsIdentifier} = __props`)
|
|
|
|
}
|
|
|
|
if (emitIdentifier) {
|
|
|
|
args +=
|
|
|
|
emitIdentifier === `emit` ? `, { emit }` : `, { emit: ${emitIdentifier} }`
|
|
|
|
if (emitTypeDecl) {
|
|
|
|
args += `: {
|
|
|
|
emit: (${scriptSetup.content.slice(
|
|
|
|
emitTypeDecl.start!,
|
|
|
|
emitTypeDecl.end!
|
|
|
|
)}),
|
|
|
|
slots: any,
|
|
|
|
attrs: any
|
|
|
|
}`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-14 06:38:28 +08:00
|
|
|
// 10. generate return statement
|
2020-11-11 05:28:34 +08:00
|
|
|
let returned
|
|
|
|
if (options.inlineTemplate) {
|
2020-11-21 03:22:51 +08:00
|
|
|
if (sfc.template && !sfc.template.src) {
|
|
|
|
if (options.templateOptions && options.templateOptions.ssr) {
|
|
|
|
hasInlinedSsrRenderFn = true
|
|
|
|
}
|
2020-11-11 05:28:34 +08:00
|
|
|
// inline render function mode - we are going to compile the template and
|
|
|
|
// inline it right here
|
2020-11-17 07:27:15 +08:00
|
|
|
const { code, ast, preamble, tips, errors } = compileTemplate({
|
2020-11-11 05:28:34 +08:00
|
|
|
filename,
|
|
|
|
source: sfc.template.content,
|
2020-11-18 01:42:58 +08:00
|
|
|
inMap: sfc.template.map,
|
2020-11-21 03:22:51 +08:00
|
|
|
...options.templateOptions,
|
|
|
|
id: scopeId,
|
|
|
|
scoped: sfc.styles.some(s => s.scoped),
|
|
|
|
isProd: options.isProd,
|
|
|
|
ssrCssVars: sfc.cssVars,
|
2020-11-11 05:28:34 +08:00
|
|
|
compilerOptions: {
|
2020-11-21 03:22:51 +08:00
|
|
|
...(options.templateOptions &&
|
|
|
|
options.templateOptions.compilerOptions),
|
2020-11-11 05:28:34 +08:00
|
|
|
inline: true,
|
2020-11-18 01:42:58 +08:00
|
|
|
isTS,
|
2020-11-11 05:28:34 +08:00
|
|
|
bindingMetadata
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if (tips.length) {
|
|
|
|
tips.forEach(warnOnce)
|
|
|
|
}
|
|
|
|
const err = errors[0]
|
|
|
|
if (typeof err === 'string') {
|
|
|
|
throw new Error(err)
|
|
|
|
} else if (err) {
|
2020-11-18 01:42:58 +08:00
|
|
|
if (err.loc) {
|
|
|
|
err.message +=
|
2020-11-20 01:17:39 +08:00
|
|
|
`\n\n` +
|
|
|
|
sfc.filename +
|
|
|
|
'\n' +
|
2020-11-18 01:42:58 +08:00
|
|
|
generateCodeFrame(
|
|
|
|
source,
|
|
|
|
err.loc.start.offset,
|
|
|
|
err.loc.end.offset
|
|
|
|
) +
|
|
|
|
`\n`
|
|
|
|
}
|
2020-11-11 05:28:34 +08:00
|
|
|
throw err
|
|
|
|
}
|
|
|
|
if (preamble) {
|
|
|
|
s.prepend(preamble)
|
|
|
|
}
|
2020-11-17 07:27:15 +08:00
|
|
|
// avoid duplicated unref import
|
|
|
|
// as this may get injected by the render function preamble OR the
|
|
|
|
// css vars codegen
|
|
|
|
if (ast && ast.helpers.includes(UNREF)) {
|
|
|
|
helperImports.delete('unref')
|
|
|
|
}
|
2020-11-11 05:28:34 +08:00
|
|
|
returned = code
|
|
|
|
} else {
|
|
|
|
returned = `() => {}`
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// return bindings from setup
|
2020-11-17 07:27:15 +08:00
|
|
|
const allBindings: Record<string, any> = { ...setupBindings }
|
|
|
|
for (const key in userImports) {
|
2020-11-20 01:17:39 +08:00
|
|
|
if (!userImports[key].isType) {
|
|
|
|
allBindings[key] = true
|
|
|
|
}
|
2020-11-17 07:27:15 +08:00
|
|
|
}
|
2020-11-11 07:06:38 +08:00
|
|
|
returned = `{ ${Object.keys(allBindings).join(', ')} }`
|
2020-11-11 05:28:34 +08:00
|
|
|
}
|
2020-07-08 05:54:01 +08:00
|
|
|
s.appendRight(endOffset, `\nreturn ${returned}\n}\n\n`)
|
|
|
|
|
2020-11-14 06:38:28 +08:00
|
|
|
// 11. finalize default export
|
2020-11-15 01:55:21 +08:00
|
|
|
// expose: [] makes <script setup> components "closed" by default.
|
|
|
|
let runtimeOptions = `\n expose: [],`
|
2020-11-25 04:28:35 +08:00
|
|
|
if (hasInheritAttrsFlag) {
|
|
|
|
runtimeOptions += `\n inheritAttrs: false,`
|
|
|
|
}
|
2020-11-21 03:22:51 +08:00
|
|
|
if (hasInlinedSsrRenderFn) {
|
|
|
|
runtimeOptions += `\n __ssrInlineRender: true,`
|
|
|
|
}
|
2020-11-25 04:12:59 +08:00
|
|
|
if (propsRuntimeDecl) {
|
|
|
|
runtimeOptions += `\n props: ${scriptSetup.content
|
|
|
|
.slice(propsRuntimeDecl.start!, propsRuntimeDecl.end!)
|
|
|
|
.trim()},`
|
|
|
|
} else if (propsTypeDecl) {
|
|
|
|
runtimeOptions += genRuntimeProps(typeDeclaredProps)
|
|
|
|
}
|
|
|
|
if (emitRuntimeDecl) {
|
|
|
|
runtimeOptions += `\n emits: ${scriptSetup.content
|
|
|
|
.slice(emitRuntimeDecl.start!, emitRuntimeDecl.end!)
|
2020-11-13 03:10:39 +08:00
|
|
|
.trim()},`
|
2020-11-25 04:12:59 +08:00
|
|
|
} else if (emitTypeDecl) {
|
|
|
|
runtimeOptions += genRuntimeEmits(typeDeclaredEmits)
|
2020-11-13 03:10:39 +08:00
|
|
|
}
|
2020-07-08 08:23:53 +08:00
|
|
|
if (isTS) {
|
|
|
|
// for TS, make sure the exported type is still valid type with
|
|
|
|
// correct props information
|
|
|
|
// we have to use object spread for types to be merged properly
|
|
|
|
// user's TS setting should compile it down to proper targets
|
2020-07-10 11:06:11 +08:00
|
|
|
const def = defaultExport ? `\n ...${defaultTempVar},` : ``
|
2020-11-13 03:10:39 +08:00
|
|
|
// wrap setup code with function.
|
|
|
|
// export the content of <script setup> as a named export, `setup`.
|
|
|
|
// this allows `import { setup } from '*.vue'` for testing purposes.
|
|
|
|
s.prependLeft(
|
|
|
|
startOffset,
|
2020-11-17 00:35:30 +08:00
|
|
|
`\nexport default ${helper(
|
|
|
|
`defineComponent`
|
|
|
|
)}({${def}${runtimeOptions}\n ${
|
2020-11-13 03:10:39 +08:00
|
|
|
hasAwait ? `async ` : ``
|
|
|
|
}setup(${args}) {\n`
|
2020-07-08 08:23:53 +08:00
|
|
|
)
|
2020-11-20 05:17:54 +08:00
|
|
|
s.appendRight(endOffset, `})`)
|
2020-07-08 05:54:01 +08:00
|
|
|
} else {
|
2020-07-08 08:23:53 +08:00
|
|
|
if (defaultExport) {
|
2020-11-13 03:10:39 +08:00
|
|
|
// can't rely on spread operator in non ts mode
|
|
|
|
s.prependLeft(
|
|
|
|
startOffset,
|
|
|
|
`\n${hasAwait ? `async ` : ``}function setup(${args}) {\n`
|
|
|
|
)
|
2020-07-10 11:06:11 +08:00
|
|
|
s.append(
|
2020-11-20 05:58:07 +08:00
|
|
|
`\nexport default /*#__PURE__*/ Object.assign(${defaultTempVar}, {${runtimeOptions}\n setup\n})\n`
|
2020-07-10 11:06:11 +08:00
|
|
|
)
|
2020-07-08 08:23:53 +08:00
|
|
|
} else {
|
2020-11-13 03:10:39 +08:00
|
|
|
s.prependLeft(
|
|
|
|
startOffset,
|
|
|
|
`\nexport default {${runtimeOptions}\n ` +
|
|
|
|
`${hasAwait ? `async ` : ``}setup(${args}) {\n`
|
|
|
|
)
|
2020-11-20 05:17:54 +08:00
|
|
|
s.appendRight(endOffset, `}`)
|
2020-07-08 08:23:53 +08:00
|
|
|
}
|
2020-07-08 05:54:01 +08:00
|
|
|
}
|
2020-07-07 03:56:24 +08:00
|
|
|
|
2020-11-14 06:38:28 +08:00
|
|
|
// 12. finalize Vue helper imports
|
2020-11-17 00:35:30 +08:00
|
|
|
if (helperImports.size > 0) {
|
|
|
|
s.prepend(
|
|
|
|
`import { ${[...helperImports]
|
|
|
|
.map(h => `${h} as _${h}`)
|
|
|
|
.join(', ')} } from 'vue'\n`
|
|
|
|
)
|
2020-10-30 03:03:39 +08:00
|
|
|
}
|
|
|
|
|
2020-07-10 00:16:08 +08:00
|
|
|
s.trim()
|
2020-07-07 03:56:24 +08:00
|
|
|
return {
|
2020-07-10 06:18:46 +08:00
|
|
|
...scriptSetup,
|
2020-10-30 03:03:39 +08:00
|
|
|
bindings: bindingMetadata,
|
2020-07-10 06:18:46 +08:00
|
|
|
content: s.toString(),
|
|
|
|
map: (s.generateMap({
|
2020-07-07 03:56:24 +08:00
|
|
|
source: filename,
|
2020-07-08 05:54:01 +08:00
|
|
|
hires: true,
|
2020-07-07 03:56:24 +08:00
|
|
|
includeContent: true
|
2020-08-29 04:21:03 +08:00
|
|
|
}) as unknown) as RawSourceMap,
|
|
|
|
scriptAst,
|
|
|
|
scriptSetupAst
|
2020-07-07 03:56:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 05:11:14 +08:00
|
|
|
function walkDeclaration(
|
|
|
|
node: Declaration,
|
2020-11-19 08:38:18 +08:00
|
|
|
bindings: Record<string, BindingTypes>,
|
|
|
|
userImportAlias: Record<string, string>
|
2020-11-13 05:11:14 +08:00
|
|
|
) {
|
2020-07-08 05:54:01 +08:00
|
|
|
if (node.type === 'VariableDeclaration') {
|
2020-11-13 03:10:39 +08:00
|
|
|
const isConst = node.kind === 'const'
|
2020-07-08 05:54:01 +08:00
|
|
|
// export const foo = ...
|
2020-11-13 03:10:39 +08:00
|
|
|
for (const { id, init } of node.declarations) {
|
2020-11-25 04:12:59 +08:00
|
|
|
const isDefineCall = !!(
|
|
|
|
isConst &&
|
|
|
|
(isCallOf(init, DEFINE_PROPS) || isCallOf(init, DEFINE_EMIT))
|
|
|
|
)
|
2020-07-08 07:47:16 +08:00
|
|
|
if (id.type === 'Identifier') {
|
2020-11-19 08:38:18 +08:00
|
|
|
let bindingType
|
|
|
|
if (
|
2020-11-13 03:10:39 +08:00
|
|
|
// if a declaration is a const literal, we can mark it so that
|
|
|
|
// the generated render fn code doesn't need to unref() it
|
2020-11-25 04:12:59 +08:00
|
|
|
isDefineCall ||
|
2020-11-13 05:11:14 +08:00
|
|
|
(isConst &&
|
2020-11-19 08:38:18 +08:00
|
|
|
canNeverBeRef(init!, userImportAlias['reactive'] || 'reactive'))
|
|
|
|
) {
|
|
|
|
bindingType = BindingTypes.SETUP_CONST
|
|
|
|
} else if (isConst) {
|
|
|
|
if (isCallOf(init, userImportAlias['ref'] || 'ref')) {
|
|
|
|
bindingType = BindingTypes.SETUP_REF
|
|
|
|
} else {
|
|
|
|
bindingType = BindingTypes.SETUP_MAYBE_REF
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bindingType = BindingTypes.SETUP_LET
|
|
|
|
}
|
|
|
|
bindings[id.name] = bindingType
|
2020-07-08 05:54:01 +08:00
|
|
|
} else if (id.type === 'ObjectPattern') {
|
2020-11-25 04:12:59 +08:00
|
|
|
walkObjectPattern(id, bindings, isConst, isDefineCall)
|
2020-07-08 05:54:01 +08:00
|
|
|
} else if (id.type === 'ArrayPattern') {
|
2020-11-25 04:12:59 +08:00
|
|
|
walkArrayPattern(id, bindings, isConst, isDefineCall)
|
2020-07-08 05:54:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (
|
|
|
|
node.type === 'FunctionDeclaration' ||
|
|
|
|
node.type === 'ClassDeclaration'
|
|
|
|
) {
|
|
|
|
// export function foo() {} / export class Foo {}
|
|
|
|
// export declarations must be named.
|
2020-11-19 04:17:50 +08:00
|
|
|
bindings[node.id!.name] = BindingTypes.SETUP_CONST
|
2020-07-08 05:54:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function walkObjectPattern(
|
|
|
|
node: ObjectPattern,
|
2020-11-13 05:11:14 +08:00
|
|
|
bindings: Record<string, BindingTypes>,
|
|
|
|
isConst: boolean,
|
2020-11-25 04:12:59 +08:00
|
|
|
isDefineCall = false
|
2020-07-08 05:54:01 +08:00
|
|
|
) {
|
|
|
|
for (const p of node.properties) {
|
|
|
|
if (p.type === 'ObjectProperty') {
|
|
|
|
// key can only be Identifier in ObjectPattern
|
|
|
|
if (p.key.type === 'Identifier') {
|
|
|
|
if (p.key === p.value) {
|
|
|
|
// const { x } = ...
|
2020-11-25 04:12:59 +08:00
|
|
|
bindings[p.key.name] = isDefineCall
|
2020-11-19 04:17:50 +08:00
|
|
|
? BindingTypes.SETUP_CONST
|
|
|
|
: isConst
|
2020-11-19 08:38:18 +08:00
|
|
|
? BindingTypes.SETUP_MAYBE_REF
|
2020-11-19 04:17:50 +08:00
|
|
|
: BindingTypes.SETUP_LET
|
2020-07-08 05:54:01 +08:00
|
|
|
} else {
|
2020-11-25 04:12:59 +08:00
|
|
|
walkPattern(p.value, bindings, isConst, isDefineCall)
|
2020-07-08 05:54:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// ...rest
|
|
|
|
// argument can only be identifer when destructuring
|
2020-11-13 05:11:14 +08:00
|
|
|
bindings[(p.argument as Identifier).name] = isConst
|
2020-11-19 04:17:50 +08:00
|
|
|
? BindingTypes.SETUP_CONST
|
|
|
|
: BindingTypes.SETUP_LET
|
2020-07-08 05:54:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function walkArrayPattern(
|
|
|
|
node: ArrayPattern,
|
2020-11-13 05:11:14 +08:00
|
|
|
bindings: Record<string, BindingTypes>,
|
|
|
|
isConst: boolean,
|
2020-11-25 04:12:59 +08:00
|
|
|
isDefineCall = false
|
2020-07-08 05:54:01 +08:00
|
|
|
) {
|
|
|
|
for (const e of node.elements) {
|
2020-11-25 04:12:59 +08:00
|
|
|
e && walkPattern(e, bindings, isConst, isDefineCall)
|
2020-07-08 05:54:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 03:10:39 +08:00
|
|
|
function walkPattern(
|
|
|
|
node: Node,
|
2020-11-13 05:11:14 +08:00
|
|
|
bindings: Record<string, BindingTypes>,
|
|
|
|
isConst: boolean,
|
2020-11-25 04:12:59 +08:00
|
|
|
isDefineCall = false
|
2020-11-13 03:10:39 +08:00
|
|
|
) {
|
2020-07-08 05:54:01 +08:00
|
|
|
if (node.type === 'Identifier') {
|
2020-11-25 04:12:59 +08:00
|
|
|
bindings[node.name] = isDefineCall
|
2020-11-19 04:17:50 +08:00
|
|
|
? BindingTypes.SETUP_CONST
|
|
|
|
: isConst
|
2020-11-19 08:38:18 +08:00
|
|
|
? BindingTypes.SETUP_MAYBE_REF
|
2020-11-19 04:17:50 +08:00
|
|
|
: BindingTypes.SETUP_LET
|
2020-07-08 05:54:01 +08:00
|
|
|
} else if (node.type === 'RestElement') {
|
|
|
|
// argument can only be identifer when destructuring
|
2020-11-13 05:11:14 +08:00
|
|
|
bindings[(node.argument as Identifier).name] = isConst
|
2020-11-19 04:17:50 +08:00
|
|
|
? BindingTypes.SETUP_CONST
|
|
|
|
: BindingTypes.SETUP_LET
|
2020-07-08 05:54:01 +08:00
|
|
|
} else if (node.type === 'ObjectPattern') {
|
2020-11-13 03:10:39 +08:00
|
|
|
walkObjectPattern(node, bindings, isConst)
|
2020-07-08 05:54:01 +08:00
|
|
|
} else if (node.type === 'ArrayPattern') {
|
2020-11-13 03:10:39 +08:00
|
|
|
walkArrayPattern(node, bindings, isConst)
|
2020-07-08 05:54:01 +08:00
|
|
|
} else if (node.type === 'AssignmentPattern') {
|
|
|
|
if (node.left.type === 'Identifier') {
|
2020-11-25 04:12:59 +08:00
|
|
|
bindings[node.left.name] = isDefineCall
|
2020-11-19 04:17:50 +08:00
|
|
|
? BindingTypes.SETUP_CONST
|
|
|
|
: isConst
|
2020-11-19 08:38:18 +08:00
|
|
|
? BindingTypes.SETUP_MAYBE_REF
|
2020-11-19 04:17:50 +08:00
|
|
|
: BindingTypes.SETUP_LET
|
2020-07-08 05:54:01 +08:00
|
|
|
} else {
|
2020-11-13 03:10:39 +08:00
|
|
|
walkPattern(node.left, bindings, isConst)
|
2020-07-08 05:54:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 23:55:04 +08:00
|
|
|
interface PropTypeData {
|
|
|
|
key: string
|
|
|
|
type: string[]
|
|
|
|
required: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
function recordType(node: Node, declaredTypes: Record<string, string[]>) {
|
|
|
|
if (node.type === 'TSInterfaceDeclaration') {
|
|
|
|
declaredTypes[node.id.name] = [`Object`]
|
|
|
|
} else if (node.type === 'TSTypeAliasDeclaration') {
|
|
|
|
declaredTypes[node.id.name] = inferRuntimeType(
|
|
|
|
node.typeAnnotation,
|
|
|
|
declaredTypes
|
|
|
|
)
|
|
|
|
} else if (node.type === 'ExportNamedDeclaration' && node.declaration) {
|
|
|
|
recordType(node.declaration, declaredTypes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function extractRuntimeProps(
|
|
|
|
node: TSTypeLiteral,
|
|
|
|
props: Record<string, PropTypeData>,
|
|
|
|
declaredTypes: Record<string, string[]>
|
|
|
|
) {
|
2020-07-08 08:23:53 +08:00
|
|
|
for (const m of node.members) {
|
|
|
|
if (m.type === 'TSPropertySignature' && m.key.type === 'Identifier') {
|
2020-07-09 23:55:04 +08:00
|
|
|
props[m.key.name] = {
|
|
|
|
key: m.key.name,
|
|
|
|
required: !m.optional,
|
|
|
|
type:
|
|
|
|
__DEV__ && m.typeAnnotation
|
|
|
|
? inferRuntimeType(m.typeAnnotation.typeAnnotation, declaredTypes)
|
|
|
|
: [`null`]
|
|
|
|
}
|
2020-07-08 08:23:53 +08:00
|
|
|
}
|
|
|
|
}
|
2020-07-08 05:54:01 +08:00
|
|
|
}
|
|
|
|
|
2020-07-09 23:55:04 +08:00
|
|
|
function inferRuntimeType(
|
|
|
|
node: TSType,
|
|
|
|
declaredTypes: Record<string, string[]>
|
|
|
|
): string[] {
|
|
|
|
switch (node.type) {
|
|
|
|
case 'TSStringKeyword':
|
|
|
|
return ['String']
|
|
|
|
case 'TSNumberKeyword':
|
|
|
|
return ['Number']
|
|
|
|
case 'TSBooleanKeyword':
|
|
|
|
return ['Boolean']
|
|
|
|
case 'TSObjectKeyword':
|
|
|
|
return ['Object']
|
|
|
|
case 'TSTypeLiteral':
|
|
|
|
// TODO (nice to have) generate runtime property validation
|
|
|
|
return ['Object']
|
|
|
|
case 'TSFunctionType':
|
|
|
|
return ['Function']
|
|
|
|
case 'TSArrayType':
|
|
|
|
case 'TSTupleType':
|
2020-07-17 23:24:53 +08:00
|
|
|
// TODO (nice to have) generate runtime element type/length checks
|
2020-07-09 23:55:04 +08:00
|
|
|
return ['Array']
|
|
|
|
|
|
|
|
case 'TSLiteralType':
|
|
|
|
switch (node.literal.type) {
|
|
|
|
case 'StringLiteral':
|
|
|
|
return ['String']
|
|
|
|
case 'BooleanLiteral':
|
|
|
|
return ['Boolean']
|
|
|
|
case 'NumericLiteral':
|
|
|
|
case 'BigIntLiteral':
|
|
|
|
return ['Number']
|
|
|
|
default:
|
|
|
|
return [`null`]
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'TSTypeReference':
|
|
|
|
if (node.typeName.type === 'Identifier') {
|
|
|
|
if (declaredTypes[node.typeName.name]) {
|
|
|
|
return declaredTypes[node.typeName.name]
|
|
|
|
}
|
|
|
|
switch (node.typeName.name) {
|
|
|
|
case 'Array':
|
|
|
|
case 'Function':
|
|
|
|
case 'Object':
|
|
|
|
case 'Set':
|
|
|
|
case 'Map':
|
|
|
|
case 'WeakSet':
|
|
|
|
case 'WeakMap':
|
|
|
|
return [node.typeName.name]
|
|
|
|
case 'Record':
|
|
|
|
case 'Partial':
|
|
|
|
case 'Readonly':
|
|
|
|
case 'Pick':
|
|
|
|
case 'Omit':
|
|
|
|
case 'Exclude':
|
|
|
|
case 'Extract':
|
|
|
|
case 'Required':
|
|
|
|
case 'InstanceType':
|
|
|
|
return ['Object']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [`null`]
|
|
|
|
|
|
|
|
case 'TSUnionType':
|
|
|
|
return [
|
|
|
|
...new Set(
|
|
|
|
[].concat(node.types.map(t =>
|
|
|
|
inferRuntimeType(t, declaredTypes)
|
|
|
|
) as any)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
case 'TSIntersectionType':
|
|
|
|
return ['Object']
|
|
|
|
|
|
|
|
default:
|
|
|
|
return [`null`] // no runtime check
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function genRuntimeProps(props: Record<string, PropTypeData>) {
|
|
|
|
const keys = Object.keys(props)
|
|
|
|
if (!keys.length) {
|
|
|
|
return ``
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!__DEV__) {
|
|
|
|
// production: generate array version only
|
|
|
|
return `\n props: [\n ${keys
|
|
|
|
.map(k => JSON.stringify(k))
|
|
|
|
.join(',\n ')}\n ] as unknown as undefined,`
|
|
|
|
}
|
|
|
|
|
|
|
|
return `\n props: {\n ${keys
|
|
|
|
.map(key => {
|
|
|
|
const { type, required } = props[key]
|
|
|
|
return `${key}: { type: ${toRuntimeTypeString(
|
|
|
|
type
|
|
|
|
)}, required: ${required} }`
|
|
|
|
})
|
|
|
|
.join(',\n ')}\n } as unknown as undefined,`
|
|
|
|
}
|
|
|
|
|
|
|
|
function toRuntimeTypeString(types: string[]) {
|
|
|
|
return types.some(t => t === 'null')
|
|
|
|
? `null`
|
|
|
|
: types.length > 1
|
|
|
|
? `[${types.join(', ')}]`
|
|
|
|
: types[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
function extractRuntimeEmits(
|
2020-11-13 07:11:25 +08:00
|
|
|
node: TSFunctionType | TSUnionType,
|
2020-07-08 08:23:53 +08:00
|
|
|
emits: Set<string>
|
2020-07-08 07:47:16 +08:00
|
|
|
) {
|
2020-11-13 07:11:25 +08:00
|
|
|
if (node.type === 'TSUnionType') {
|
|
|
|
for (let t of node.types) {
|
|
|
|
if (t.type === 'TSParenthesizedType') t = t.typeAnnotation
|
|
|
|
if (t.type === 'TSFunctionType') {
|
|
|
|
extractRuntimeEmits(t, emits)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const eventName = node.parameters[0]
|
2020-07-08 08:23:53 +08:00
|
|
|
if (
|
|
|
|
eventName.type === 'Identifier' &&
|
|
|
|
eventName.typeAnnotation &&
|
|
|
|
eventName.typeAnnotation.type === 'TSTypeAnnotation'
|
|
|
|
) {
|
|
|
|
const typeNode = eventName.typeAnnotation.typeAnnotation
|
|
|
|
if (typeNode.type === 'TSLiteralType') {
|
|
|
|
emits.add(String(typeNode.literal.value))
|
|
|
|
} else if (typeNode.type === 'TSUnionType') {
|
|
|
|
for (const t of typeNode.types) {
|
|
|
|
if (t.type === 'TSLiteralType') {
|
|
|
|
emits.add(String(t.literal.value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-08 05:54:01 +08:00
|
|
|
}
|
|
|
|
|
2020-07-09 23:55:04 +08:00
|
|
|
function genRuntimeEmits(emits: Set<string>) {
|
|
|
|
return emits.size
|
|
|
|
? `\n emits: [${Array.from(emits)
|
|
|
|
.map(p => JSON.stringify(p))
|
|
|
|
.join(', ')}] as unknown as undefined,`
|
|
|
|
: ``
|
|
|
|
}
|
|
|
|
|
2020-11-19 11:39:08 +08:00
|
|
|
const parentStack: Node[] = []
|
|
|
|
|
2020-07-08 05:54:01 +08:00
|
|
|
/**
|
2020-10-30 03:03:39 +08:00
|
|
|
* Walk an AST and find identifiers that are variable references.
|
|
|
|
* This is largely the same logic with `transformExpressions` in compiler-core
|
|
|
|
* but with some subtle differences as this needs to handle a wider range of
|
|
|
|
* possible syntax.
|
2020-07-08 05:54:01 +08:00
|
|
|
*/
|
2020-10-30 03:03:39 +08:00
|
|
|
function walkIdentifiers(
|
2020-07-08 05:54:01 +08:00
|
|
|
root: Node,
|
2020-10-30 03:03:39 +08:00
|
|
|
onIdentifier: (node: Identifier, parent: Node) => void
|
2020-07-08 05:54:01 +08:00
|
|
|
) {
|
|
|
|
const knownIds: Record<string, number> = Object.create(null)
|
|
|
|
;(walk as any)(root, {
|
2020-11-19 11:39:08 +08:00
|
|
|
enter(node: Node & { scopeIds?: Set<string> }, parent: Node | undefined) {
|
|
|
|
parent && parentStack.push(parent)
|
2020-07-08 05:54:01 +08:00
|
|
|
if (node.type === 'Identifier') {
|
2020-11-19 11:39:08 +08:00
|
|
|
if (!knownIds[node.name] && isRefIdentifier(node, parent!)) {
|
|
|
|
onIdentifier(node, parent!)
|
2020-07-08 05:54:01 +08:00
|
|
|
}
|
2020-07-11 06:00:13 +08:00
|
|
|
} else if (isFunction(node)) {
|
2020-07-08 05:54:01 +08:00
|
|
|
// walk function expressions and add its arguments to known identifiers
|
|
|
|
// so that we don't prefix them
|
|
|
|
node.params.forEach(p =>
|
|
|
|
(walk as any)(p, {
|
|
|
|
enter(child: Node, parent: Node) {
|
|
|
|
if (
|
|
|
|
child.type === 'Identifier' &&
|
|
|
|
// do not record as scope variable if is a destructured key
|
|
|
|
!isStaticPropertyKey(child, parent) &&
|
|
|
|
// do not record if this is a default value
|
|
|
|
// assignment of a destructured variable
|
|
|
|
!(
|
|
|
|
parent &&
|
|
|
|
parent.type === 'AssignmentPattern' &&
|
|
|
|
parent.right === child
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
const { name } = child
|
|
|
|
if (node.scopeIds && node.scopeIds.has(name)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (name in knownIds) {
|
|
|
|
knownIds[name]++
|
|
|
|
} else {
|
|
|
|
knownIds[name] = 1
|
|
|
|
}
|
|
|
|
;(node.scopeIds || (node.scopeIds = new Set())).add(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
2020-10-30 03:03:39 +08:00
|
|
|
} else if (
|
|
|
|
node.type === 'ObjectProperty' &&
|
2020-11-19 11:39:08 +08:00
|
|
|
parent!.type === 'ObjectPattern'
|
2020-10-30 03:03:39 +08:00
|
|
|
) {
|
|
|
|
// mark property in destructure pattern
|
|
|
|
;(node as any).inPattern = true
|
2020-07-08 05:54:01 +08:00
|
|
|
}
|
|
|
|
},
|
2020-11-19 11:39:08 +08:00
|
|
|
leave(node: Node & { scopeIds?: Set<string> }, parent: Node | undefined) {
|
|
|
|
parent && parentStack.pop()
|
2020-07-08 05:54:01 +08:00
|
|
|
if (node.scopeIds) {
|
|
|
|
node.scopeIds.forEach((id: string) => {
|
|
|
|
knownIds[id]--
|
|
|
|
if (knownIds[id] === 0) {
|
|
|
|
delete knownIds[id]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-30 03:03:39 +08:00
|
|
|
function isRefIdentifier(id: Identifier, parent: Node) {
|
|
|
|
// declaration id
|
|
|
|
if (
|
|
|
|
(parent.type === 'VariableDeclarator' ||
|
|
|
|
parent.type === 'ClassDeclaration') &&
|
|
|
|
parent.id === id
|
|
|
|
) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isFunction(parent)) {
|
|
|
|
// function decalration/expression id
|
|
|
|
if ((parent as any).id === id) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// params list
|
|
|
|
if (parent.params.includes(id)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// property key
|
|
|
|
// this also covers object destructure pattern
|
|
|
|
if (isStaticPropertyKey(id, parent)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-11-19 11:39:08 +08:00
|
|
|
// non-assignment array destructure pattern
|
|
|
|
if (
|
|
|
|
parent.type === 'ArrayPattern' &&
|
|
|
|
!isInDestructureAssignment(parent, parentStack)
|
|
|
|
) {
|
2020-10-30 03:03:39 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// member expression property
|
|
|
|
if (
|
|
|
|
(parent.type === 'MemberExpression' ||
|
|
|
|
parent.type === 'OptionalMemberExpression') &&
|
|
|
|
parent.property === id &&
|
|
|
|
!parent.computed
|
|
|
|
) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// is a special keyword but parsed as identifier
|
|
|
|
if (id.name === 'arguments') {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2020-07-08 05:54:01 +08:00
|
|
|
}
|
|
|
|
|
2020-10-30 03:03:39 +08:00
|
|
|
const isStaticProperty = (node: Node): node is ObjectProperty =>
|
|
|
|
node &&
|
|
|
|
(node.type === 'ObjectProperty' || node.type === 'ObjectMethod') &&
|
|
|
|
!node.computed
|
|
|
|
|
|
|
|
const isStaticPropertyKey = (node: Node, parent: Node) =>
|
|
|
|
isStaticProperty(parent) && parent.key === node
|
|
|
|
|
2020-07-11 06:00:13 +08:00
|
|
|
function isFunction(node: Node): node is FunctionNode {
|
|
|
|
return /Function(?:Expression|Declaration)$|Method$/.test(node.type)
|
|
|
|
}
|
|
|
|
|
2020-11-19 08:38:18 +08:00
|
|
|
function isCallOf(node: Node | null, name: string): node is CallExpression {
|
|
|
|
return !!(
|
|
|
|
node &&
|
|
|
|
node.type === 'CallExpression' &&
|
|
|
|
node.callee.type === 'Identifier' &&
|
|
|
|
node.callee.name === name
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function canNeverBeRef(node: Node, userReactiveImport: string): boolean {
|
|
|
|
if (isCallOf(node, userReactiveImport)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
switch (node.type) {
|
|
|
|
case 'UnaryExpression':
|
|
|
|
case 'BinaryExpression':
|
|
|
|
case 'ArrayExpression':
|
|
|
|
case 'ObjectExpression':
|
|
|
|
case 'FunctionExpression':
|
|
|
|
case 'ArrowFunctionExpression':
|
|
|
|
case 'UpdateExpression':
|
|
|
|
case 'ClassExpression':
|
|
|
|
case 'TaggedTemplateExpression':
|
|
|
|
return true
|
|
|
|
case 'SequenceExpression':
|
|
|
|
return canNeverBeRef(
|
|
|
|
node.expressions[node.expressions.length - 1],
|
|
|
|
userReactiveImport
|
|
|
|
)
|
|
|
|
default:
|
|
|
|
if (node.type.endsWith('Literal')) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 11:39:08 +08:00
|
|
|
function isInDestructureAssignment(parent: Node, parentStack: Node[]): boolean {
|
|
|
|
if (
|
|
|
|
parent &&
|
|
|
|
(parent.type === 'ObjectProperty' || parent.type === 'ArrayPattern')
|
|
|
|
) {
|
|
|
|
let i = parentStack.length
|
|
|
|
while (i--) {
|
|
|
|
const p = parentStack[i]
|
|
|
|
if (p.type === 'AssignmentExpression') {
|
|
|
|
const root = parentStack[0]
|
|
|
|
// if this is a ref: destructure, it should be treated like a
|
|
|
|
// variable decalration!
|
|
|
|
return !(root.type === 'LabeledStatement' && root.label.name === 'ref')
|
|
|
|
} else if (p.type !== 'ObjectProperty' && !p.type.endsWith('Pattern')) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-07-07 03:56:24 +08:00
|
|
|
/**
|
|
|
|
* Analyze bindings in normal `<script>`
|
|
|
|
* Note that `compileScriptSetup` already analyzes bindings as part of its
|
|
|
|
* compilation process so this should only be used on single `<script>` SFCs.
|
|
|
|
*/
|
2020-08-29 04:21:03 +08:00
|
|
|
function analyzeScriptBindings(ast: Statement[]): BindingMetadata {
|
|
|
|
for (const node of ast) {
|
|
|
|
if (
|
|
|
|
node.type === 'ExportDefaultDeclaration' &&
|
|
|
|
node.declaration.type === 'ObjectExpression'
|
|
|
|
) {
|
2020-11-13 03:10:39 +08:00
|
|
|
return analyzeBindingsFromOptions(node.declaration)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {}
|
|
|
|
}
|
2020-08-29 04:21:03 +08:00
|
|
|
|
2020-11-13 03:10:39 +08:00
|
|
|
function analyzeBindingsFromOptions(node: ObjectExpression): BindingMetadata {
|
|
|
|
const bindings: BindingMetadata = {}
|
|
|
|
for (const property of node.properties) {
|
|
|
|
if (
|
|
|
|
property.type === 'ObjectProperty' &&
|
|
|
|
!property.computed &&
|
|
|
|
property.key.type === 'Identifier'
|
|
|
|
) {
|
|
|
|
// props
|
|
|
|
if (property.key.name === 'props') {
|
|
|
|
// props: ['foo']
|
|
|
|
// props: { foo: ... }
|
2020-11-25 04:12:59 +08:00
|
|
|
for (const key of getObjectOrArrayExpressionKeys(property.value)) {
|
2020-11-13 05:11:14 +08:00
|
|
|
bindings[key] = BindingTypes.PROPS
|
2020-11-13 03:10:39 +08:00
|
|
|
}
|
|
|
|
}
|
2020-08-29 04:21:03 +08:00
|
|
|
|
2020-11-13 03:10:39 +08:00
|
|
|
// inject
|
|
|
|
else if (property.key.name === 'inject') {
|
|
|
|
// inject: ['foo']
|
|
|
|
// inject: { foo: {} }
|
2020-11-25 04:12:59 +08:00
|
|
|
for (const key of getObjectOrArrayExpressionKeys(property.value)) {
|
2020-11-13 05:11:14 +08:00
|
|
|
bindings[key] = BindingTypes.OPTIONS
|
2020-11-13 03:10:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// computed & methods
|
|
|
|
else if (
|
|
|
|
property.value.type === 'ObjectExpression' &&
|
|
|
|
(property.key.name === 'computed' || property.key.name === 'methods')
|
|
|
|
) {
|
|
|
|
// methods: { foo() {} }
|
|
|
|
// computed: { foo() {} }
|
|
|
|
for (const key of getObjectExpressionKeys(property.value)) {
|
2020-11-13 05:11:14 +08:00
|
|
|
bindings[key] = BindingTypes.OPTIONS
|
2020-08-29 04:21:03 +08:00
|
|
|
}
|
2020-11-13 03:10:39 +08:00
|
|
|
}
|
|
|
|
}
|
2020-08-29 04:21:03 +08:00
|
|
|
|
2020-11-13 03:10:39 +08:00
|
|
|
// setup & data
|
|
|
|
else if (
|
|
|
|
property.type === 'ObjectMethod' &&
|
|
|
|
property.key.type === 'Identifier' &&
|
|
|
|
(property.key.name === 'setup' || property.key.name === 'data')
|
|
|
|
) {
|
|
|
|
for (const bodyItem of property.body.body) {
|
|
|
|
// setup() {
|
|
|
|
// return {
|
|
|
|
// foo: null
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
if (
|
|
|
|
bodyItem.type === 'ReturnStatement' &&
|
|
|
|
bodyItem.argument &&
|
|
|
|
bodyItem.argument.type === 'ObjectExpression'
|
2020-08-29 04:21:03 +08:00
|
|
|
) {
|
2020-11-13 03:10:39 +08:00
|
|
|
for (const key of getObjectExpressionKeys(bodyItem.argument)) {
|
2020-11-13 05:11:14 +08:00
|
|
|
bindings[key] =
|
|
|
|
property.key.name === 'setup'
|
2020-11-19 08:38:18 +08:00
|
|
|
? BindingTypes.SETUP_MAYBE_REF
|
2020-11-13 05:11:14 +08:00
|
|
|
: BindingTypes.DATA
|
2020-08-29 04:21:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-08 05:54:01 +08:00
|
|
|
}
|
2020-08-29 04:21:03 +08:00
|
|
|
|
|
|
|
return bindings
|
2020-07-07 03:56:24 +08:00
|
|
|
}
|
2020-11-25 04:12:59 +08:00
|
|
|
|
|
|
|
function getObjectExpressionKeys(node: ObjectExpression): string[] {
|
|
|
|
const keys = []
|
|
|
|
for (const prop of node.properties) {
|
|
|
|
if (
|
|
|
|
(prop.type === 'ObjectProperty' || prop.type === 'ObjectMethod') &&
|
|
|
|
!prop.computed
|
|
|
|
) {
|
|
|
|
if (prop.key.type === 'Identifier') {
|
|
|
|
keys.push(prop.key.name)
|
|
|
|
} else if (prop.key.type === 'StringLiteral') {
|
|
|
|
keys.push(prop.key.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return keys
|
|
|
|
}
|
|
|
|
|
|
|
|
function getArrayExpressionKeys(node: ArrayExpression): string[] {
|
|
|
|
const keys = []
|
|
|
|
for (const element of node.elements) {
|
|
|
|
if (element && element.type === 'StringLiteral') {
|
|
|
|
keys.push(element.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return keys
|
|
|
|
}
|
|
|
|
|
|
|
|
function getObjectOrArrayExpressionKeys(value: Node): string[] {
|
|
|
|
if (value.type === 'ArrayExpression') {
|
|
|
|
return getArrayExpressionKeys(value)
|
|
|
|
}
|
|
|
|
if (value.type === 'ObjectExpression') {
|
|
|
|
return getObjectExpressionKeys(value)
|
|
|
|
}
|
|
|
|
return []
|
|
|
|
}
|