2020-07-07 03:56:24 +08:00
|
|
|
import MagicString, { SourceMap } from 'magic-string'
|
|
|
|
import { SFCDescriptor, SFCScriptBlock } from './parse'
|
|
|
|
import { parse, ParserPlugin } from '@babel/parser'
|
2020-07-08 05:54:01 +08:00
|
|
|
import { babelParserDefautPlugins, generateCodeFrame } from '@vue/shared'
|
|
|
|
import {
|
|
|
|
Node,
|
|
|
|
Declaration,
|
|
|
|
ObjectPattern,
|
|
|
|
ArrayPattern,
|
|
|
|
Identifier,
|
|
|
|
ExpressionStatement,
|
|
|
|
ArrowFunctionExpression,
|
|
|
|
TSTypeLiteral,
|
|
|
|
TSFunctionType,
|
|
|
|
TSDeclareFunction
|
|
|
|
} from '@babel/types'
|
|
|
|
import { walk } from 'estree-walker'
|
2020-07-07 03:56:24 +08:00
|
|
|
|
|
|
|
export interface BindingMetadata {
|
|
|
|
[key: string]: 'data' | 'props' | 'setup' | 'ctx'
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface SFCScriptCompileOptions {
|
|
|
|
parserPlugins?: ParserPlugin[]
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compile `<script setup>`
|
|
|
|
* It requires the whole SFC descriptor because we need to handle and merge
|
|
|
|
* normal `<script>` + `<script setup>` if both are present.
|
|
|
|
*/
|
|
|
|
export function compileScriptSetup(
|
|
|
|
sfc: SFCDescriptor,
|
|
|
|
options: SFCScriptCompileOptions = {}
|
|
|
|
) {
|
|
|
|
const { script, scriptSetup, source, filename } = sfc
|
|
|
|
if (!scriptSetup) {
|
|
|
|
throw new Error('SFC has no <script setup>.')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (script && script.lang !== scriptSetup.lang) {
|
|
|
|
throw new Error(
|
|
|
|
`<script> and <script setup> must have the same language type.`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const bindings: BindingMetadata = {}
|
2020-07-08 07:47:16 +08:00
|
|
|
const imports: Record<string, string> = {}
|
2020-07-08 05:54:01 +08:00
|
|
|
const setupScopeVars: Record<string, boolean> = {}
|
|
|
|
const setupExports: Record<string, boolean> = {}
|
2020-07-07 03:56:24 +08:00
|
|
|
let exportAllIndex = 0
|
2020-07-08 05:54:01 +08:00
|
|
|
let defaultExport: Node | undefined
|
|
|
|
let needDefaultExportCheck: boolean = false
|
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-07-08 07:47:16 +08:00
|
|
|
const isTS = scriptSetup.lang === 'ts'
|
2020-07-07 03:56:24 +08:00
|
|
|
const plugins: ParserPlugin[] = [
|
|
|
|
...(options.parserPlugins || []),
|
2020-07-08 07:47:16 +08:00
|
|
|
...(babelParserDefautPlugins as ParserPlugin[]),
|
|
|
|
...(isTS ? (['typescript'] as const) : [])
|
2020-07-07 03:56:24 +08:00
|
|
|
]
|
|
|
|
|
2020-07-08 08:23:53 +08:00
|
|
|
// 1. process normal <script> first if it exists
|
2020-07-08 05:54:01 +08:00
|
|
|
if (script) {
|
|
|
|
// import dedupe between <script> and <script setup>
|
|
|
|
const scriptAST = parse(script.content, {
|
|
|
|
plugins,
|
|
|
|
sourceType: 'module'
|
|
|
|
}).program.body
|
|
|
|
|
|
|
|
for (const node of scriptAST) {
|
|
|
|
if (node.type === 'ImportDeclaration') {
|
|
|
|
// record imports for dedupe
|
|
|
|
for (const {
|
|
|
|
local: { name }
|
|
|
|
} of node.specifiers) {
|
2020-07-08 07:47:16 +08:00
|
|
|
imports[name] = node.source.value
|
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,
|
|
|
|
`const __default__ =`
|
|
|
|
)
|
|
|
|
} else if (
|
|
|
|
node.type === 'ExportNamedDeclaration' &&
|
|
|
|
node.specifiers &&
|
|
|
|
node.specifiers.some(s => s.exported.name === 'default')
|
|
|
|
) {
|
|
|
|
defaultExport = node
|
|
|
|
if (node.source) {
|
|
|
|
// export { x as default } from './x'
|
2020-07-08 07:47:16 +08:00
|
|
|
// TODO
|
2020-07-08 05:54:01 +08:00
|
|
|
} else {
|
|
|
|
// export { x as default }
|
2020-07-08 07:47:16 +08:00
|
|
|
// TODO
|
2020-07-08 05:54:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 08:23:53 +08:00
|
|
|
// 2. check <script setup="xxx"> function signature
|
2020-07-08 07:47:16 +08:00
|
|
|
const hasExplicitSignature = typeof scriptSetup.setup === 'string'
|
2020-07-08 05:54:01 +08:00
|
|
|
let propsVar = `$props`
|
|
|
|
let emitVar = `$emit`
|
2020-07-08 07:47:16 +08:00
|
|
|
let slotsVar = `$slots`
|
|
|
|
let attrsVar = `$attrs`
|
|
|
|
|
|
|
|
let propsType = `{}`
|
|
|
|
let emitType = `(e: string, ...args: any[]) => void`
|
|
|
|
let slotsType = `__Slots__`
|
|
|
|
let attrsType = `Record<string, any>`
|
|
|
|
|
|
|
|
let propsASTNode
|
|
|
|
let setupCtxASTNode
|
|
|
|
|
|
|
|
// props/emits declared via types
|
2020-07-08 08:23:53 +08:00
|
|
|
const typeDeclaredProps: Set<string> = new Set()
|
|
|
|
const typeDeclaredEmits: Set<string> = new Set()
|
2020-07-08 07:47:16 +08:00
|
|
|
|
|
|
|
if (isTS && hasExplicitSignature) {
|
2020-07-08 05:54:01 +08:00
|
|
|
// <script setup="xxx" lang="ts">
|
|
|
|
// parse the signature to extract the props/emit variables the user wants
|
|
|
|
// we need them to find corresponding type declarations.
|
2020-07-08 07:47:16 +08:00
|
|
|
const signatureAST = parse(`(${scriptSetup.setup})=>{}`, { plugins })
|
|
|
|
.program.body[0]
|
|
|
|
const params = ((signatureAST as ExpressionStatement)
|
|
|
|
.expression as ArrowFunctionExpression).params
|
|
|
|
if (params[0] && params[0].type === 'Identifier') {
|
|
|
|
propsASTNode = params[0]
|
|
|
|
propsVar = propsASTNode.name
|
|
|
|
}
|
|
|
|
if (params[1] && params[1].type === 'ObjectPattern') {
|
|
|
|
setupCtxASTNode = params[1]
|
|
|
|
for (const p of params[1].properties) {
|
|
|
|
if (
|
|
|
|
p.type === 'ObjectProperty' &&
|
|
|
|
p.key.type === 'Identifier' &&
|
|
|
|
p.value.type === 'Identifier'
|
|
|
|
) {
|
|
|
|
if (p.key.name === 'emit') {
|
2020-07-08 05:54:01 +08:00
|
|
|
emitVar = p.value.name
|
2020-07-08 07:47:16 +08:00
|
|
|
} else if (p.key.name === 'slots') {
|
|
|
|
slotsVar = p.value.name
|
|
|
|
} else if (p.key.name === 'attrs') {
|
|
|
|
attrsVar = p.value.name
|
2020-07-08 05:54:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 08:23:53 +08:00
|
|
|
// 3. parse <script setup> and walk over top level statements
|
|
|
|
for (const node of parse(scriptSetup.content, {
|
2020-07-07 03:56:24 +08:00
|
|
|
plugins,
|
|
|
|
sourceType: 'module'
|
2020-07-08 08:23:53 +08:00
|
|
|
}).program.body) {
|
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
|
|
|
|
// 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-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-07-08 05:54:01 +08:00
|
|
|
// dedupe imports
|
|
|
|
let prev
|
|
|
|
let removed = 0
|
|
|
|
for (const specifier of node.specifiers) {
|
|
|
|
if (imports[specifier.local.name]) {
|
|
|
|
// already imported in <script setup>, dedupe
|
|
|
|
removed++
|
|
|
|
s.remove(
|
|
|
|
prev ? prev.end! + startOffset : specifier.start! + startOffset,
|
|
|
|
specifier.end! + startOffset
|
|
|
|
)
|
|
|
|
} else {
|
2020-07-08 07:47:16 +08:00
|
|
|
imports[specifier.local.name] = node.source.value
|
2020-07-08 05:54:01 +08:00
|
|
|
}
|
|
|
|
prev = specifier
|
|
|
|
}
|
|
|
|
if (removed === node.specifiers.length) {
|
|
|
|
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-07-08 07:47:16 +08:00
|
|
|
if (node.type === 'ExportNamedDeclaration' && node.exportKind !== 'type') {
|
2020-07-07 03:56:24 +08:00
|
|
|
// named exports
|
|
|
|
if (node.declaration) {
|
|
|
|
// variable/function/class declarations.
|
|
|
|
// remove leading `export ` keyword
|
|
|
|
s.remove(start, start + 7)
|
2020-07-08 07:47:16 +08:00
|
|
|
walkDeclaration(node.declaration, setupExports)
|
2020-07-07 03:56:24 +08:00
|
|
|
}
|
|
|
|
if (node.specifiers.length) {
|
2020-07-08 05:54:01 +08:00
|
|
|
// named export with specifiers
|
2020-07-07 03:56:24 +08:00
|
|
|
if (node.source) {
|
|
|
|
// export { x } from './x'
|
|
|
|
// change it to import and move to top
|
|
|
|
s.overwrite(start, start + 6, 'import')
|
|
|
|
s.move(start, end, 0)
|
|
|
|
} else {
|
|
|
|
// export { x }
|
|
|
|
s.remove(start, end)
|
|
|
|
}
|
2020-07-08 05:54:01 +08:00
|
|
|
for (const specifier of node.specifiers) {
|
|
|
|
if (specifier.type == 'ExportDefaultSpecifier') {
|
|
|
|
// export default from './x'
|
|
|
|
// rewrite to `import __default__ from './x'`
|
|
|
|
defaultExport = node
|
|
|
|
s.overwrite(
|
|
|
|
specifier.exported.start! + startOffset,
|
|
|
|
specifier.exported.start! + startOffset + 7,
|
|
|
|
'__default__'
|
|
|
|
)
|
|
|
|
} else if (specifier.type == 'ExportSpecifier') {
|
|
|
|
if (specifier.exported.name === 'default') {
|
|
|
|
defaultExport = node
|
|
|
|
if (!node.source) {
|
|
|
|
// export { x as default }
|
|
|
|
// rewrite to `const __default__ = x`
|
|
|
|
s.overwrite(
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
`const __default__ = ${specifier.local.name}\n`
|
|
|
|
)
|
|
|
|
s.move(start, end, source.length)
|
|
|
|
} else {
|
|
|
|
// export { x as default } from './x'
|
|
|
|
// rewrite to `import { x as __default__ } from './x'`
|
|
|
|
s.overwrite(
|
|
|
|
specifier.exported.start! + startOffset,
|
|
|
|
specifier.exported.start! + startOffset + 7,
|
|
|
|
'__default__'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
setupExports[specifier.exported.name] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-07 03:56:24 +08:00
|
|
|
}
|
2020-07-08 05:54:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (node.type === 'ExportAllDeclaration') {
|
2020-07-07 03:56:24 +08:00
|
|
|
// export * from './x'
|
|
|
|
s.overwrite(
|
|
|
|
start,
|
|
|
|
node.source.start! + startOffset,
|
|
|
|
`import * as __import_all_${exportAllIndex++}__ from `
|
|
|
|
)
|
|
|
|
s.move(start, end, 0)
|
|
|
|
}
|
2020-07-08 05:54:01 +08:00
|
|
|
|
|
|
|
if (node.type === 'ExportDefaultDeclaration') {
|
|
|
|
if (defaultExport) {
|
|
|
|
// <script> already has export default
|
|
|
|
throw new Error(
|
|
|
|
`Default export is already declared in normal <script>.\n\n` +
|
|
|
|
generateCodeFrame(
|
|
|
|
source,
|
|
|
|
node.start! + startOffset,
|
|
|
|
node.start! + startOffset + `export default`.length
|
|
|
|
)
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// export default {} inside <script setup>
|
|
|
|
// this should be kept in module scope - move it to the end
|
|
|
|
s.move(start, end, source.length)
|
|
|
|
s.overwrite(
|
|
|
|
start,
|
|
|
|
start + `export default`.length,
|
|
|
|
`const __default__ =`
|
|
|
|
)
|
|
|
|
// save it for analysis when all imports and variable declarations have
|
|
|
|
// been recorded
|
|
|
|
defaultExport = node
|
|
|
|
needDefaultExportCheck = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-08 07:47:16 +08:00
|
|
|
walkDeclaration(node, setupScopeVars)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type declarations
|
|
|
|
if (node.type === 'VariableDeclaration' && node.declare) {
|
|
|
|
s.remove(start, end)
|
|
|
|
for (const { id } of node.declarations) {
|
|
|
|
if (id.type === 'Identifier') {
|
|
|
|
if (
|
|
|
|
id.typeAnnotation &&
|
|
|
|
id.typeAnnotation.type === 'TSTypeAnnotation'
|
|
|
|
) {
|
|
|
|
const typeNode = id.typeAnnotation.typeAnnotation
|
|
|
|
const typeString = source.slice(
|
|
|
|
typeNode.start! + startOffset,
|
|
|
|
typeNode.end! + startOffset
|
|
|
|
)
|
|
|
|
if (typeNode.type === 'TSTypeLiteral') {
|
|
|
|
if (id.name === propsVar) {
|
|
|
|
propsType = typeString
|
|
|
|
extractProps(typeNode, typeDeclaredProps)
|
|
|
|
} else if (id.name === slotsVar) {
|
|
|
|
slotsType = typeString
|
|
|
|
} else if (id.name === attrsVar) {
|
|
|
|
attrsType = typeString
|
|
|
|
}
|
|
|
|
} else if (
|
|
|
|
id.name === emitVar &&
|
|
|
|
typeNode.type === 'TSFunctionType'
|
|
|
|
) {
|
|
|
|
emitType = typeString
|
|
|
|
extractEmits(typeNode, typeDeclaredEmits)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-08 05:54:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
node.type === 'TSDeclareFunction' &&
|
|
|
|
node.id &&
|
|
|
|
node.id.name === emitVar
|
|
|
|
) {
|
2020-07-08 07:47:16 +08:00
|
|
|
const index = node.id.start! + startOffset
|
|
|
|
s.overwrite(index, index + emitVar.length, '__emit__')
|
|
|
|
emitType = `typeof __emit__`
|
|
|
|
extractEmits(node, typeDeclaredEmits)
|
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')
|
|
|
|
) {
|
|
|
|
s.move(start, end, 0)
|
|
|
|
}
|
2020-07-08 05:54:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// check default export to make sure it doesn't reference setup scope
|
|
|
|
// variables
|
|
|
|
if (needDefaultExportCheck) {
|
|
|
|
checkDefaultExport(
|
|
|
|
defaultExport!,
|
|
|
|
setupScopeVars,
|
|
|
|
imports,
|
|
|
|
setupExports,
|
|
|
|
source,
|
|
|
|
startOffset
|
|
|
|
)
|
2020-07-07 03:56:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove non-script content
|
|
|
|
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-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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// wrap setup code with function
|
2020-07-08 07:47:16 +08:00
|
|
|
// finalize the argument signature.
|
|
|
|
let args
|
|
|
|
if (isTS) {
|
|
|
|
if (slotsType === '__Slots__') {
|
|
|
|
s.prepend(`import { Slots as __Slots__ } from 'vue'\n`)
|
|
|
|
}
|
|
|
|
const ctxType = `{
|
|
|
|
emit: ${emitType},
|
|
|
|
slots: ${slotsType},
|
|
|
|
attrs: ${attrsType}
|
|
|
|
}`
|
|
|
|
if (hasExplicitSignature) {
|
|
|
|
// inject types to user signature
|
|
|
|
args = scriptSetup.setup as string
|
|
|
|
const ss = new MagicString(args)
|
|
|
|
if (propsASTNode) {
|
|
|
|
// compensate for () wraper offset
|
|
|
|
ss.appendRight(propsASTNode.end! - 1, `: ${propsType}`)
|
|
|
|
}
|
|
|
|
if (setupCtxASTNode) {
|
|
|
|
ss.appendRight(setupCtxASTNode.end! - 1!, `: ${ctxType}`)
|
|
|
|
}
|
|
|
|
args = ss.toString()
|
|
|
|
} else {
|
|
|
|
args = `$props: ${propsType}, { emit: $emit, slots: $slots, attrs: $attrs }: ${ctxType}`
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
args = hasExplicitSignature
|
|
|
|
? scriptSetup.setup
|
|
|
|
: `$props, { emit: $emit, slots: $slots, attrs: $attrs }`
|
|
|
|
}
|
|
|
|
|
2020-07-07 03:56:24 +08:00
|
|
|
// export the content of <script setup> as a named export, `setup`.
|
|
|
|
// this allows `import { setup } from '*.vue'` for testing purposes.
|
|
|
|
s.appendLeft(startOffset, `\nexport function setup(${args}) {\n`)
|
|
|
|
|
|
|
|
// generate return statement
|
2020-07-08 05:54:01 +08:00
|
|
|
let returned = `{ ${Object.keys(setupExports).join(', ')} }`
|
2020-07-07 03:56:24 +08:00
|
|
|
|
|
|
|
// handle `export * from`. We need to call `toRefs` on the imported module
|
|
|
|
// object before merging.
|
|
|
|
if (exportAllIndex > 0) {
|
|
|
|
s.prepend(`import { toRefs as __toRefs__ } from 'vue'\n`)
|
|
|
|
for (let i = 0; i < exportAllIndex; i++) {
|
|
|
|
returned += `,\n __toRefs__(__export_all_${i}__)`
|
|
|
|
}
|
|
|
|
returned = `Object.assign(\n ${returned}\n)`
|
|
|
|
}
|
|
|
|
|
2020-07-08 05:54:01 +08:00
|
|
|
s.appendRight(endOffset, `\nreturn ${returned}\n}\n\n`)
|
|
|
|
|
2020-07-08 08:23:53 +08:00
|
|
|
// finalize default export
|
|
|
|
if (isTS) {
|
|
|
|
// for TS, make sure the exported type is still valid type with
|
|
|
|
// correct props information
|
|
|
|
s.prepend(`import { defineComponent as __define__ } from 'vue'\n`)
|
|
|
|
// we have to use object spread for types to be merged properly
|
|
|
|
// user's TS setting should compile it down to proper targets
|
|
|
|
const def = defaultExport ? `\n ...__default__,` : ``
|
|
|
|
const runtimeProps = typeDeclaredProps.size
|
|
|
|
? `\n props: [${Array.from(typeDeclaredProps)
|
|
|
|
.map(p => JSON.stringify(p))
|
|
|
|
.join(', ')}] as unknown as undefined,`
|
|
|
|
: ``
|
|
|
|
const runtimeEmits = typeDeclaredEmits.size
|
|
|
|
? `\n emits: [${Array.from(typeDeclaredEmits)
|
|
|
|
.map(p => JSON.stringify(p))
|
|
|
|
.join(', ')}] as unknown as undefined,`
|
|
|
|
: ``
|
|
|
|
s.append(
|
|
|
|
`export default __define__({${def}${runtimeProps}${runtimeEmits}\n setup\n})`
|
|
|
|
)
|
2020-07-08 05:54:01 +08:00
|
|
|
} else {
|
2020-07-08 08:23:53 +08:00
|
|
|
if (defaultExport) {
|
|
|
|
s.append(`__default__.setup = setup\nexport default __default__`)
|
|
|
|
} else {
|
|
|
|
s.append(`export default { setup }`)
|
|
|
|
}
|
2020-07-08 05:54:01 +08:00
|
|
|
}
|
2020-07-07 03:56:24 +08:00
|
|
|
|
|
|
|
s.trim()
|
|
|
|
|
2020-07-08 05:54:01 +08:00
|
|
|
// analyze bindings for template compiler optimization
|
|
|
|
if (script) {
|
|
|
|
Object.assign(bindings, analyzeScriptBindings(script))
|
|
|
|
}
|
|
|
|
Object.keys(setupExports).forEach(key => {
|
2020-07-07 03:56:24 +08:00
|
|
|
bindings[key] = 'setup'
|
|
|
|
})
|
|
|
|
|
|
|
|
return {
|
|
|
|
bindings,
|
|
|
|
code: s.toString(),
|
|
|
|
map: s.generateMap({
|
|
|
|
source: filename,
|
2020-07-08 05:54:01 +08:00
|
|
|
hires: true,
|
2020-07-07 03:56:24 +08:00
|
|
|
includeContent: true
|
|
|
|
}) as SourceMap
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 07:47:16 +08:00
|
|
|
function walkDeclaration(node: Declaration, bindings: Record<string, boolean>) {
|
2020-07-08 05:54:01 +08:00
|
|
|
if (node.type === 'VariableDeclaration') {
|
|
|
|
// export const foo = ...
|
|
|
|
for (const { id } of node.declarations) {
|
2020-07-08 07:47:16 +08:00
|
|
|
if (id.type === 'Identifier') {
|
2020-07-08 05:54:01 +08:00
|
|
|
bindings[id.name] = true
|
|
|
|
} else if (id.type === 'ObjectPattern') {
|
|
|
|
walkObjectPattern(id, bindings)
|
|
|
|
} else if (id.type === 'ArrayPattern') {
|
|
|
|
walkArrayPattern(id, bindings)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (
|
|
|
|
node.type === 'FunctionDeclaration' ||
|
|
|
|
node.type === 'ClassDeclaration'
|
|
|
|
) {
|
|
|
|
// export function foo() {} / export class Foo {}
|
|
|
|
// export declarations must be named.
|
|
|
|
bindings[node.id!.name] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function walkObjectPattern(
|
|
|
|
node: ObjectPattern,
|
|
|
|
bindings: Record<string, boolean>
|
|
|
|
) {
|
|
|
|
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 } = ...
|
|
|
|
bindings[p.key.name] = true
|
|
|
|
} else {
|
|
|
|
walkPattern(p.value, bindings)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// ...rest
|
|
|
|
// argument can only be identifer when destructuring
|
|
|
|
bindings[(p.argument as Identifier).name] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function walkArrayPattern(
|
|
|
|
node: ArrayPattern,
|
|
|
|
bindings: Record<string, boolean>
|
|
|
|
) {
|
|
|
|
for (const e of node.elements) {
|
|
|
|
e && walkPattern(e, bindings)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function walkPattern(node: Node, bindings: Record<string, boolean>) {
|
|
|
|
if (node.type === 'Identifier') {
|
|
|
|
bindings[node.name] = true
|
|
|
|
} else if (node.type === 'RestElement') {
|
|
|
|
// argument can only be identifer when destructuring
|
|
|
|
bindings[(node.argument as Identifier).name] = true
|
|
|
|
} else if (node.type === 'ObjectPattern') {
|
|
|
|
walkObjectPattern(node, bindings)
|
|
|
|
} else if (node.type === 'ArrayPattern') {
|
|
|
|
walkArrayPattern(node, bindings)
|
|
|
|
} else if (node.type === 'AssignmentPattern') {
|
|
|
|
if (node.left.type === 'Identifier') {
|
|
|
|
bindings[node.left.name] = true
|
|
|
|
} else {
|
|
|
|
walkPattern(node.left, bindings)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 08:23:53 +08:00
|
|
|
function extractProps(node: TSTypeLiteral, props: Set<string>) {
|
|
|
|
for (const m of node.members) {
|
|
|
|
if (m.type === 'TSPropertySignature' && m.key.type === 'Identifier') {
|
|
|
|
props.add(m.key.name)
|
|
|
|
}
|
|
|
|
}
|
2020-07-08 05:54:01 +08:00
|
|
|
}
|
|
|
|
|
2020-07-08 07:47:16 +08:00
|
|
|
function extractEmits(
|
|
|
|
node: TSFunctionType | TSDeclareFunction,
|
2020-07-08 08:23:53 +08:00
|
|
|
emits: Set<string>
|
2020-07-08 07:47:16 +08:00
|
|
|
) {
|
2020-07-08 08:23:53 +08:00
|
|
|
const eventName =
|
|
|
|
node.type === 'TSDeclareFunction' ? node.params[0] : node.parameters[0]
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* export default {} inside <script setup> cannot access variables declared
|
|
|
|
* inside since it's hoisted. Walk and check to make sure.
|
|
|
|
*/
|
|
|
|
function checkDefaultExport(
|
|
|
|
root: Node,
|
|
|
|
scopeVars: Record<string, boolean>,
|
2020-07-08 07:47:16 +08:00
|
|
|
imports: Record<string, string>,
|
2020-07-08 05:54:01 +08:00
|
|
|
exports: Record<string, boolean>,
|
|
|
|
source: string,
|
|
|
|
offset: number
|
|
|
|
) {
|
|
|
|
const knownIds: Record<string, number> = Object.create(null)
|
|
|
|
;(walk as any)(root, {
|
|
|
|
enter(node: Node & { scopeIds?: Set<string> }, parent: Node) {
|
|
|
|
if (node.type === 'Identifier') {
|
|
|
|
if (
|
|
|
|
!knownIds[node.name] &&
|
|
|
|
!isStaticPropertyKey(node, parent) &&
|
|
|
|
(scopeVars[node.name] || (!imports[node.name] && exports[node.name]))
|
|
|
|
) {
|
|
|
|
throw new Error(
|
|
|
|
`\`export default\` 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.\n\n` +
|
|
|
|
generateCodeFrame(
|
|
|
|
source,
|
|
|
|
node.start! + offset,
|
|
|
|
node.end! + offset
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else if (
|
|
|
|
node.type === 'FunctionDeclaration' ||
|
|
|
|
node.type === 'FunctionExpression' ||
|
|
|
|
node.type === 'ArrowFunctionExpression'
|
|
|
|
) {
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
leave(node: Node & { scopeIds?: Set<string> }) {
|
|
|
|
if (node.scopeIds) {
|
|
|
|
node.scopeIds.forEach((id: string) => {
|
|
|
|
knownIds[id]--
|
|
|
|
if (knownIds[id] === 0) {
|
|
|
|
delete knownIds[id]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function isStaticPropertyKey(node: Node, parent: Node): boolean {
|
|
|
|
return (
|
|
|
|
parent &&
|
|
|
|
(parent.type === 'ObjectProperty' || parent.type === 'ObjectMethod') &&
|
|
|
|
!parent.computed &&
|
|
|
|
parent.key === node
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
export function analyzeScriptBindings(
|
|
|
|
_script: SFCScriptBlock
|
|
|
|
): BindingMetadata {
|
2020-07-08 05:54:01 +08:00
|
|
|
return {
|
|
|
|
// TODO
|
|
|
|
}
|
2020-07-07 03:56:24 +08:00
|
|
|
}
|