| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836 |
- import MagicString from 'magic-string'
- import {
- BindingMetadata,
- BindingTypes,
- createRoot,
- NodeTypes,
- transform,
- parserOptions,
- UNREF,
- SimpleExpressionNode,
- isFunctionType,
- walkIdentifiers
- } from '@vue/compiler-dom'
- import { SFCDescriptor, SFCScriptBlock } from './parse'
- import { parse as _parse, ParserOptions, ParserPlugin } from '@babel/parser'
- import {
- babelParserDefaultPlugins,
- camelize,
- capitalize,
- generateCodeFrame,
- makeMap
- } from '@vue/shared'
- import {
- Node,
- Declaration,
- ObjectPattern,
- ObjectExpression,
- ArrayPattern,
- Identifier,
- ExportSpecifier,
- TSType,
- TSTypeLiteral,
- TSFunctionType,
- ObjectProperty,
- ArrayExpression,
- Statement,
- CallExpression,
- RestElement,
- TSInterfaceBody,
- AwaitExpression,
- Program,
- ObjectMethod
- } from '@babel/types'
- import { walk } from 'estree-walker'
- import { RawSourceMap } from 'source-map'
- import {
- CSS_VARS_HELPER,
- genCssVarsCode,
- genNormalScriptCssVarsCode
- } from './cssVars'
- import { compileTemplate, SFCTemplateCompileOptions } from './compileTemplate'
- import { warnOnce } from './warn'
- import { rewriteDefault } from './rewriteDefault'
- import { createCache } from './cache'
- import {
- shouldTransform as shouldTransformRef,
- transformAST as transformRefAST
- } from '@vue/ref-transform'
- // Special compiler macros
- const DEFINE_PROPS = 'defineProps'
- const DEFINE_EMITS = 'defineEmits'
- const DEFINE_EXPOSE = 'defineExpose'
- const WITH_DEFAULTS = 'withDefaults'
- const isBuiltInDir = makeMap(
- `once,memo,if,else,else-if,slot,text,html,on,bind,model,show,cloak,is`
- )
- export interface SFCScriptCompileOptions {
- /**
- * Scope ID for prefixing injected CSS varialbes.
- * This must be consistent with the `id` passed to `compileStyle`.
- */
- id: string
- /**
- * Production mode. Used to determine whether to generate hashed CSS variables
- */
- isProd?: boolean
- /**
- * https://babeljs.io/docs/en/babel-parser#plugins
- */
- babelParserPlugins?: ParserPlugin[]
- /**
- * (Experimental) Enable syntax transform for using refs without `.value`
- * https://github.com/vuejs/rfcs/discussions/369
- * @default true
- */
- refTransform?: boolean
- /**
- * @deprecated use `refTransform` instead.
- */
- refSugar?: boolean
- /**
- * 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
- /**
- * Options for template compilation when inlining. Note these are options that
- * would normally be pased to `compiler-sfc`'s own `compileTemplate()`, not
- * options passed to `compiler-dom`.
- */
- templateOptions?: Partial<SFCTemplateCompileOptions>
- }
- interface ImportBinding {
- isType: boolean
- imported: string
- source: string
- isFromSetup: boolean
- isUsedInTemplate: boolean
- }
- /**
- * 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 compileScript(
- sfc: SFCDescriptor,
- options: SFCScriptCompileOptions
- ): SFCScriptBlock {
- let { script, scriptSetup, source, filename } = sfc
- // feature flags
- const enableRefTransform = !!options.refSugar || !!options.refTransform
- let refBindings: string[] | undefined
- // 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.`
- )
- }
- const scopeId = options.id ? options.id.replace(/^data-v-/, '') : ''
- const cssVars = sfc.cssVars
- const scriptLang = script && script.lang
- const scriptSetupLang = scriptSetup && scriptSetup.lang
- const isTS =
- scriptLang === 'ts' ||
- scriptLang === 'tsx' ||
- scriptSetupLang === 'ts' ||
- scriptSetupLang === 'tsx'
- const plugins: ParserPlugin[] = [...babelParserDefaultPlugins]
- if (!isTS || scriptLang === 'tsx' || scriptSetupLang === 'tsx') {
- plugins.push('jsx')
- }
- if (options.babelParserPlugins) plugins.push(...options.babelParserPlugins)
- if (isTS) plugins.push('typescript', 'decorators-legacy')
- if (!scriptSetup) {
- if (!script) {
- throw new Error(`[@vue/compiler-sfc] SFC contains no <script> tags.`)
- }
- if (scriptLang && !isTS && scriptLang !== 'jsx') {
- // do not process non js/ts script blocks
- return script
- }
- try {
- let content = script.content
- let map = script.map
- const scriptAst = _parse(content, {
- plugins,
- sourceType: 'module'
- }).program
- const bindings = analyzeScriptBindings(scriptAst.body)
- if (enableRefTransform && shouldTransformRef(content)) {
- const s = new MagicString(source)
- const startOffset = script.loc.start.offset
- const endOffset = script.loc.end.offset
- const { importedHelpers } = transformRefAST(scriptAst, s, startOffset)
- if (importedHelpers.length) {
- s.prepend(
- `import { ${importedHelpers
- .map(h => `${h} as _${h}`)
- .join(', ')} } from 'vue'\n`
- )
- }
- s.remove(0, startOffset)
- s.remove(endOffset, source.length)
- content = s.toString()
- map = s.generateMap({
- source: filename,
- hires: true,
- includeContent: true
- }) as unknown as RawSourceMap
- }
- if (cssVars.length) {
- content = rewriteDefault(content, `__default__`, plugins)
- content += genNormalScriptCssVarsCode(
- cssVars,
- bindings,
- scopeId,
- !!options.isProd
- )
- content += `\nexport default __default__`
- }
- return {
- ...script,
- content,
- map,
- bindings,
- scriptAst: scriptAst.body
- }
- } catch (e: any) {
- // silently fallback if parse fails since user may be using custom
- // babel syntax
- return script
- }
- }
- if (script && scriptLang !== scriptSetupLang) {
- throw new Error(
- `[@vue/compiler-sfc] <script> and <script setup> must have the same ` +
- `language type.`
- )
- }
- if (scriptSetupLang && !isTS && scriptSetupLang !== 'jsx') {
- // do not process non js/ts script blocks
- return scriptSetup
- }
- // metadata that needs to be returned
- const bindingMetadata: BindingMetadata = {}
- const defaultTempVar = `__default__`
- const helperImports: Set<string> = new Set()
- const userImports: Record<string, ImportBinding> = Object.create(null)
- const userImportAlias: Record<string, string> = Object.create(null)
- const setupBindings: Record<string, BindingTypes> = Object.create(null)
- let defaultExport: Node | undefined
- let hasDefinePropsCall = false
- let hasDefineEmitCall = false
- let hasDefineExposeCall = false
- let propsRuntimeDecl: Node | undefined
- let propsRuntimeDefaults: ObjectExpression | undefined
- let propsTypeDecl: TSTypeLiteral | TSInterfaceBody | undefined
- let propsTypeDeclRaw: Node | undefined
- let propsIdentifier: string | undefined
- let emitsRuntimeDecl: Node | undefined
- let emitsTypeDecl:
- | TSFunctionType
- | TSTypeLiteral
- | TSInterfaceBody
- | undefined
- let emitsTypeDeclRaw: Node | undefined
- let emitIdentifier: string | undefined
- let hasAwait = false
- let hasInlinedSsrRenderFn = false
- // 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[]> = {}
- // magic-string state
- const s = new MagicString(source)
- const startOffset = scriptSetup.loc.start.offset
- const endOffset = scriptSetup.loc.end.offset
- const scriptStartOffset = script && script.loc.start.offset
- const scriptEndOffset = script && script.loc.end.offset
- function helper(key: string): string {
- helperImports.add(key)
- return `_${key}`
- }
- function parse(
- input: string,
- options: ParserOptions,
- offset: number
- ): Program {
- try {
- return _parse(input, options).program
- } catch (e: any) {
- e.message = `[@vue/compiler-sfc] ${e.message}\n\n${
- sfc.filename
- }\n${generateCodeFrame(source, e.pos + offset, e.pos + offset + 1)}`
- throw e
- }
- }
- function error(
- msg: string,
- node: Node,
- end: number = node.end! + startOffset
- ): never {
- throw new Error(
- `[@vue/compiler-sfc] ${msg}\n\n${sfc.filename}\n${generateCodeFrame(
- source,
- node.start! + startOffset,
- end
- )}`
- )
- }
- function registerUserImport(
- source: string,
- local: string,
- imported: string | false,
- isType: boolean,
- isFromSetup: boolean
- ) {
- if (source === 'vue' && imported) {
- userImportAlias[imported] = local
- }
- let isUsedInTemplate = true
- if (isTS && sfc.template && !sfc.template.src && !sfc.template.lang) {
- isUsedInTemplate = new RegExp(
- // #4274 escape $ since it's a special char in regex
- // (and is the only regex special char that is valid in identifiers)
- `[^\\w$_]${local.replace(/\$/g, '\\$')}[^\\w$_]`
- ).test(resolveTemplateUsageCheckString(sfc))
- }
- userImports[local] = {
- isType,
- imported: imported || 'default',
- source,
- isFromSetup,
- isUsedInTemplate
- }
- }
- function processDefineProps(node: Node): boolean {
- if (!isCallOf(node, DEFINE_PROPS)) {
- return false
- }
- if (hasDefinePropsCall) {
- error(`duplicate ${DEFINE_PROPS}() call`, node)
- }
- hasDefinePropsCall = true
- propsRuntimeDecl = node.arguments[0]
- // 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
- )
- }
- propsTypeDeclRaw = node.typeParameters.params[0]
- propsTypeDecl = resolveQualifiedType(
- propsTypeDeclRaw,
- node => node.type === 'TSTypeLiteral'
- ) as TSTypeLiteral | TSInterfaceBody | undefined
- if (!propsTypeDecl) {
- error(
- `type argument passed to ${DEFINE_PROPS}() must be a literal type, ` +
- `or a reference to an interface or literal type.`,
- propsTypeDeclRaw
- )
- }
- }
- return true
- }
- function processWithDefaults(node: Node): boolean {
- if (!isCallOf(node, WITH_DEFAULTS)) {
- return false
- }
- if (processDefineProps(node.arguments[0])) {
- if (propsRuntimeDecl) {
- error(
- `${WITH_DEFAULTS} can only be used with type-based ` +
- `${DEFINE_PROPS} declaration.`,
- node
- )
- }
- propsRuntimeDefaults = node.arguments[1] as ObjectExpression
- if (
- !propsRuntimeDefaults ||
- propsRuntimeDefaults.type !== 'ObjectExpression'
- ) {
- error(
- `The 2nd argument of ${WITH_DEFAULTS} must be an object literal.`,
- propsRuntimeDefaults || node
- )
- }
- } else {
- error(
- `${WITH_DEFAULTS}' first argument must be a ${DEFINE_PROPS} call.`,
- node.arguments[0] || node
- )
- }
- return true
- }
- function processDefineEmits(node: Node): boolean {
- if (!isCallOf(node, DEFINE_EMITS)) {
- return false
- }
- if (hasDefineEmitCall) {
- error(`duplicate ${DEFINE_EMITS}() call`, node)
- }
- hasDefineEmitCall = true
- emitsRuntimeDecl = node.arguments[0]
- if (node.typeParameters) {
- if (emitsRuntimeDecl) {
- error(
- `${DEFINE_EMITS}() cannot accept both type and non-type arguments ` +
- `at the same time. Use one or the other.`,
- node
- )
- }
- emitsTypeDeclRaw = node.typeParameters.params[0]
- emitsTypeDecl = resolveQualifiedType(
- emitsTypeDeclRaw,
- node => node.type === 'TSFunctionType' || node.type === 'TSTypeLiteral'
- ) as TSFunctionType | TSTypeLiteral | TSInterfaceBody | undefined
- if (!emitsTypeDecl) {
- error(
- `type argument passed to ${DEFINE_EMITS}() must be a function type, ` +
- `a literal type with call signatures, or a reference to the above types.`,
- emitsTypeDeclRaw
- )
- }
- }
- return true
- }
- function resolveQualifiedType(
- node: Node,
- qualifier: (node: Node) => boolean
- ) {
- if (qualifier(node)) {
- return node
- }
- if (
- node.type === 'TSTypeReference' &&
- node.typeName.type === 'Identifier'
- ) {
- const refName = node.typeName.name
- const isQualifiedType = (node: Node): Node | undefined => {
- if (
- node.type === 'TSInterfaceDeclaration' &&
- node.id.name === refName
- ) {
- return node.body
- } else if (
- node.type === 'TSTypeAliasDeclaration' &&
- node.id.name === refName &&
- qualifier(node.typeAnnotation)
- ) {
- return node.typeAnnotation
- } else if (node.type === 'ExportNamedDeclaration' && node.declaration) {
- return isQualifiedType(node.declaration)
- }
- }
- const body = scriptAst
- ? [...scriptSetupAst.body, ...scriptAst.body]
- : scriptSetupAst.body
- for (const node of body) {
- const qualified = isQualifiedType(node)
- if (qualified) {
- return qualified
- }
- }
- }
- }
- function processDefineExpose(node: Node): boolean {
- if (isCallOf(node, DEFINE_EXPOSE)) {
- if (hasDefineExposeCall) {
- error(`duplicate ${DEFINE_EXPOSE}() call`, node)
- }
- hasDefineExposeCall = true
- return true
- }
- return false
- }
- 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
- )
- }
- })
- }
- /**
- * await foo()
- * -->
- * (([__temp, __restore] = withAsyncContext(() => foo())),__temp=await __temp,__restore(),__temp)
- */
- function processAwait(node: AwaitExpression, isStatement: boolean) {
- s.overwrite(
- node.start! + startOffset,
- node.argument.start! + startOffset,
- `${isStatement ? `;` : ``}(([__temp,__restore]=${helper(
- `withAsyncContext`
- )}(()=>(`
- )
- s.appendLeft(
- node.end! + startOffset,
- `))),__temp=await __temp,__restore()${isStatement ? `` : `,__temp`})`
- )
- }
- /**
- * check defaults. If the default object is an object literal with only
- * static properties, we can directly generate more optimzied default
- * declarations. Otherwise we will have to fallback to runtime merging.
- */
- function checkStaticDefaults() {
- return (
- propsRuntimeDefaults &&
- propsRuntimeDefaults.type === 'ObjectExpression' &&
- propsRuntimeDefaults.properties.every(
- node =>
- (node.type === 'ObjectProperty' && !node.computed) ||
- node.type === 'ObjectMethod'
- )
- )
- }
- function genRuntimeProps(props: Record<string, PropTypeData>) {
- const keys = Object.keys(props)
- if (!keys.length) {
- return ``
- }
- const hasStaticDefaults = checkStaticDefaults()
- const scriptSetupSource = scriptSetup!.content
- let propsDecls = `{
- ${keys
- .map(key => {
- let defaultString: string | undefined
- if (hasStaticDefaults) {
- const prop = propsRuntimeDefaults!.properties.find(
- (node: any) => node.key.name === key
- ) as ObjectProperty | ObjectMethod
- if (prop) {
- if (prop.type === 'ObjectProperty') {
- // prop has corresponding static default value
- defaultString = `default: ${scriptSetupSource.slice(
- prop.value.start!,
- prop.value.end!
- )}`
- } else {
- defaultString = `default() ${scriptSetupSource.slice(
- prop.body.start!,
- prop.body.end!
- )}`
- }
- }
- }
- if (__DEV__) {
- const { type, required } = props[key]
- return `${key}: { type: ${toRuntimeTypeString(
- type
- )}, required: ${required}${
- defaultString ? `, ${defaultString}` : ``
- } }`
- } else {
- // production: checks are useless
- return `${key}: ${defaultString ? `{ ${defaultString} }` : 'null'}`
- }
- })
- .join(',\n ')}\n }`
- if (propsRuntimeDefaults && !hasStaticDefaults) {
- propsDecls = `${helper('mergeDefaults')}(${propsDecls}, ${source.slice(
- propsRuntimeDefaults.start! + startOffset,
- propsRuntimeDefaults.end! + startOffset
- )})`
- }
- return `\n props: ${propsDecls},`
- }
- function genSetupPropsType(node: TSTypeLiteral | TSInterfaceBody) {
- const scriptSetupSource = scriptSetup!.content
- if (checkStaticDefaults()) {
- // if withDefaults() is used, we need to remove the optional flags
- // on props that have default values
- let res = `{ `
- const members = node.type === 'TSTypeLiteral' ? node.members : node.body
- for (const m of members) {
- if (
- (m.type === 'TSPropertySignature' ||
- m.type === 'TSMethodSignature') &&
- m.typeAnnotation &&
- m.key.type === 'Identifier'
- ) {
- if (
- propsRuntimeDefaults!.properties.some(
- (p: any) => p.key.name === (m.key as Identifier).name
- )
- ) {
- res +=
- m.key.name +
- (m.type === 'TSMethodSignature' ? '()' : '') +
- scriptSetupSource.slice(
- m.typeAnnotation.start!,
- m.typeAnnotation.end!
- ) +
- ', '
- } else {
- res +=
- scriptSetupSource.slice(m.start!, m.typeAnnotation.end!) + `, `
- }
- }
- }
- return (res.length ? res.slice(0, -2) : res) + ` }`
- } else {
- return scriptSetupSource.slice(node.start!, node.end!)
- }
- }
- // 1. process normal <script> first if it exists
- let scriptAst: Program | undefined
- if (script) {
- // import dedupe between <script> and <script setup>
- scriptAst = parse(
- script.content,
- {
- plugins,
- sourceType: 'module'
- },
- scriptStartOffset!
- )
- for (const node of scriptAst.body) {
- if (node.type === 'ImportDeclaration') {
- // record imports for dedupe
- for (const specifier of node.specifiers) {
- const imported =
- specifier.type === 'ImportSpecifier' &&
- specifier.imported.type === 'Identifier' &&
- specifier.imported.name
- registerUserImport(
- node.source.value,
- specifier.local.name,
- imported,
- node.importKind === 'type',
- false
- )
- }
- } else if (node.type === 'ExportDefaultDeclaration') {
- // export default
- defaultExport = node
- const start = node.start! + scriptStartOffset!
- const end = node.declaration.start! + scriptStartOffset!
- s.overwrite(start, end, `const ${defaultTempVar} = `)
- } else if (node.type === 'ExportNamedDeclaration' && node.specifiers) {
- const defaultSpecifier = node.specifiers.find(
- s => s.exported.type === 'Identifier' && s.exported.name === 'default'
- ) 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(
- `import { ${defaultSpecifier.local.name} as ${defaultTempVar} } from '${node.source.value}'\n`
- )
- } else {
- // export { x as default }
- // rewrite to `const __default__ = x` and move to end
- s.append(
- `\nconst ${defaultTempVar} = ${defaultSpecifier.local.name}\n`
- )
- }
- }
- } else if (
- (node.type === 'VariableDeclaration' ||
- node.type === 'FunctionDeclaration' ||
- node.type === 'ClassDeclaration') &&
- !node.declare
- ) {
- walkDeclaration(node, setupBindings, userImportAlias)
- }
- }
- // apply ref transform
- if (enableRefTransform && shouldTransformRef(script.content)) {
- const { rootVars, importedHelpers } = transformRefAST(
- scriptAst,
- s,
- scriptStartOffset!
- )
- refBindings = rootVars
- for (const h of importedHelpers) {
- helperImports.add(h)
- }
- }
- }
- // 2. parse <script setup> and walk over top level statements
- const scriptSetupAst = parse(
- scriptSetup.content,
- {
- plugins: [
- ...plugins,
- // allow top level await but only inside <script setup>
- 'topLevelAwait'
- ],
- sourceType: 'module'
- },
- startOffset
- )
- for (const node of scriptSetupAst.body) {
- const start = node.start! + startOffset
- let end = node.end! + startOffset
- // locate comment
- if (node.trailingComments && node.trailingComments.length > 0) {
- const lastCommentNode =
- node.trailingComments[node.trailingComments.length - 1]
- end = lastCommentNode.end + startOffset
- }
- // locate the end of whitespace between this statement and the next
- while (end <= source.length) {
- if (!/\s/.test(source.charAt(end))) {
- break
- }
- end++
- }
- // (Dropped) `ref: x` bindings
- if (
- node.type === 'LabeledStatement' &&
- node.label.name === 'ref' &&
- node.body.type === 'ExpressionStatement'
- ) {
- error(
- `ref sugar using the label syntax was an experimental proposal and ` +
- `has been dropped based on community feedback. Please check out ` +
- `the new proposal at https://github.com/vuejs/rfcs/discussions/369`,
- node
- )
- }
- if (node.type === 'ImportDeclaration') {
- // import declarations are moved to top
- s.move(start, end, 0)
- // dedupe imports
- let removed = 0
- const removeSpecifier = (i: number) => {
- const removeLeft = i > removed
- removed++
- const current = node.specifiers[i]
- const next = node.specifiers[i + 1]
- s.remove(
- removeLeft
- ? node.specifiers[i - 1].end! + startOffset
- : current.start! + startOffset,
- next && !removeLeft
- ? next.start! + startOffset
- : current.end! + startOffset
- )
- }
- for (let i = 0; i < node.specifiers.length; i++) {
- const specifier = node.specifiers[i]
- 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]
- if (
- source === 'vue' &&
- (imported === DEFINE_PROPS ||
- imported === DEFINE_EMITS ||
- imported === DEFINE_EXPOSE)
- ) {
- warnOnce(
- `\`${imported}\` is a compiler macro and no longer needs to be imported.`
- )
- removeSpecifier(i)
- } else if (existing) {
- if (existing.source === source && existing.imported === imported) {
- // already imported in <script setup>, dedupe
- removeSpecifier(i)
- } else {
- error(`different imports aliased to same local name.`, specifier)
- }
- } else {
- registerUserImport(
- source,
- local,
- imported,
- node.importKind === 'type',
- true
- )
- }
- }
- if (node.specifiers.length && removed === node.specifiers.length) {
- s.remove(node.start! + startOffset, node.end! + startOffset)
- }
- }
- if (node.type === 'ExpressionStatement') {
- // process `defineProps` and `defineEmit(s)` calls
- if (
- processDefineProps(node.expression) ||
- processDefineEmits(node.expression) ||
- processWithDefaults(node.expression)
- ) {
- s.remove(node.start! + startOffset, node.end! + startOffset)
- } else if (processDefineExpose(node.expression)) {
- // defineExpose({}) -> expose({})
- const callee = (node.expression as CallExpression).callee
- s.overwrite(
- callee.start! + startOffset,
- callee.end! + startOffset,
- 'expose'
- )
- }
- }
- if (node.type === 'VariableDeclaration' && !node.declare) {
- const total = node.declarations.length
- let left = total
- for (let i = 0; i < total; i++) {
- const decl = node.declarations[i]
- if (decl.init) {
- // defineProps / defineEmits
- const isDefineProps =
- processDefineProps(decl.init) || processWithDefaults(decl.init)
- if (isDefineProps) {
- propsIdentifier = scriptSetup.content.slice(
- decl.id.start!,
- decl.id.end!
- )
- }
- const isDefineEmits = processDefineEmits(decl.init)
- if (isDefineEmits) {
- emitIdentifier = scriptSetup.content.slice(
- decl.id.start!,
- decl.id.end!
- )
- }
- if (isDefineProps || isDefineEmits) {
- if (left === 1) {
- s.remove(node.start! + startOffset, node.end! + startOffset)
- } else {
- let start = decl.start! + startOffset
- let end = decl.end! + startOffset
- if (i < total - 1) {
- // not the last one, locate the start of the next
- end = node.declarations[i + 1].start! + startOffset
- } else {
- // last one, locate the end of the prev
- start = node.declarations[i - 1].end! + startOffset
- }
- s.remove(start, end)
- left--
- }
- }
- }
- }
- }
- // walk declarations to record declared bindings
- if (
- (node.type === 'VariableDeclaration' ||
- node.type === 'FunctionDeclaration' ||
- node.type === 'ClassDeclaration') &&
- !node.declare
- ) {
- walkDeclaration(node, setupBindings, userImportAlias)
- }
- // walk statements & named exports / variable declarations for top level
- // await
- if (
- (node.type === 'VariableDeclaration' && !node.declare) ||
- node.type.endsWith('Statement')
- ) {
- ;(walk as any)(node, {
- enter(child: Node, parent: Node) {
- if (isFunctionType(child)) {
- this.skip()
- }
- if (child.type === 'AwaitExpression') {
- hasAwait = true
- processAwait(child, parent.type === 'ExpressionStatement')
- }
- }
- })
- }
- 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
- )
- }
- if (isTS) {
- // runtime enum
- if (node.type === 'TSEnumDeclaration') {
- registerBinding(setupBindings, node.id, BindingTypes.SETUP_CONST)
- }
- // move all Type declarations to outer scope
- if (
- node.type.startsWith('TS') ||
- (node.type === 'ExportNamedDeclaration' &&
- node.exportKind === 'type') ||
- (node.type === 'VariableDeclaration' && node.declare)
- ) {
- recordType(node, declaredTypes)
- s.move(start, end, 0)
- }
- }
- }
- // 3. Apply ref sugar transform
- if (enableRefTransform && shouldTransformRef(scriptSetup.content)) {
- const { rootVars, importedHelpers } = transformRefAST(
- scriptSetupAst,
- s,
- startOffset,
- refBindings
- )
- refBindings = refBindings ? [...refBindings, ...rootVars] : rootVars
- for (const h of importedHelpers) {
- helperImports.add(h)
- }
- }
- // 4. extract runtime props/emits code from setup context type
- if (propsTypeDecl) {
- extractRuntimeProps(propsTypeDecl, typeDeclaredProps, declaredTypes)
- }
- if (emitsTypeDecl) {
- extractRuntimeEmits(emitsTypeDecl, typeDeclaredEmits)
- }
- // 5. check useOptions args to make sure it doesn't reference setup scope
- // variables
- checkInvalidScopeReference(propsRuntimeDecl, DEFINE_PROPS)
- checkInvalidScopeReference(propsRuntimeDefaults, DEFINE_PROPS)
- checkInvalidScopeReference(emitsRuntimeDecl, DEFINE_PROPS)
- // 6. remove non-script content
- if (script) {
- if (startOffset < scriptStartOffset!) {
- // <script setup> before <script>
- s.remove(0, startOffset)
- s.remove(endOffset, scriptStartOffset!)
- s.remove(scriptEndOffset!, source.length)
- } else {
- // <script> before <script setup>
- s.remove(0, scriptStartOffset!)
- s.remove(scriptEndOffset!, startOffset)
- s.remove(endOffset, source.length)
- }
- } else {
- // only <script setup>
- s.remove(0, startOffset)
- s.remove(endOffset, source.length)
- }
- // 7. analyze binding metadata
- if (scriptAst) {
- Object.assign(bindingMetadata, analyzeScriptBindings(scriptAst.body))
- }
- if (propsRuntimeDecl) {
- for (const key of getObjectOrArrayExpressionKeys(propsRuntimeDecl)) {
- bindingMetadata[key] = BindingTypes.PROPS
- }
- }
- for (const key in typeDeclaredProps) {
- bindingMetadata[key] = BindingTypes.PROPS
- }
- for (const [key, { isType, imported, source }] of Object.entries(
- userImports
- )) {
- if (isType) continue
- bindingMetadata[key] =
- (imported === 'default' && source.endsWith('.vue')) || source === 'vue'
- ? BindingTypes.SETUP_CONST
- : BindingTypes.SETUP_MAYBE_REF
- }
- for (const key in setupBindings) {
- bindingMetadata[key] = setupBindings[key]
- }
- // known ref bindings
- if (refBindings) {
- for (const key of refBindings) {
- bindingMetadata[key] = BindingTypes.SETUP_REF
- }
- }
- // 8. inject `useCssVars` calls
- if (cssVars.length) {
- helperImports.add(CSS_VARS_HELPER)
- helperImports.add('unref')
- s.prependRight(
- startOffset,
- `\n${genCssVarsCode(
- cssVars,
- bindingMetadata,
- scopeId,
- !!options.isProd
- )}\n`
- )
- }
- // 9. finalize setup() argument signature
- let args = `__props`
- if (propsTypeDecl) {
- // mark as any and only cast on assignment
- // since the user defined complex types may be incompatible with the
- // inferred type from generated runtime declarations
- args += `: any`
- }
- // 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${
- propsTypeDecl ? ` as ${genSetupPropsType(propsTypeDecl)}` : ``
- }`
- )
- }
- // inject temp variables for async context preservation
- if (hasAwait) {
- const any = isTS ? `: any` : ``
- s.prependRight(startOffset, `\nlet __temp${any}, __restore${any}\n`)
- }
- const destructureElements =
- hasDefineExposeCall || !options.inlineTemplate ? [`expose`] : []
- if (emitIdentifier) {
- destructureElements.push(
- emitIdentifier === `emit` ? `emit` : `emit: ${emitIdentifier}`
- )
- }
- if (destructureElements.length) {
- args += `, { ${destructureElements.join(', ')} }`
- if (emitsTypeDecl) {
- args += `: { emit: (${scriptSetup.content.slice(
- emitsTypeDecl.start!,
- emitsTypeDecl.end!
- )}), expose: any, slots: any, attrs: any }`
- }
- }
- // 10. generate return statement
- let returned
- if (options.inlineTemplate) {
- if (sfc.template && !sfc.template.src) {
- if (options.templateOptions && options.templateOptions.ssr) {
- hasInlinedSsrRenderFn = true
- }
- // inline render function mode - we are going to compile the template and
- // inline it right here
- const { code, ast, preamble, tips, errors } = compileTemplate({
- filename,
- source: sfc.template.content,
- inMap: sfc.template.map,
- ...options.templateOptions,
- id: scopeId,
- scoped: sfc.styles.some(s => s.scoped),
- isProd: options.isProd,
- ssrCssVars: sfc.cssVars,
- compilerOptions: {
- ...(options.templateOptions &&
- options.templateOptions.compilerOptions),
- inline: true,
- isTS,
- bindingMetadata
- }
- })
- if (tips.length) {
- tips.forEach(warnOnce)
- }
- const err = errors[0]
- if (typeof err === 'string') {
- throw new Error(err)
- } else if (err) {
- if (err.loc) {
- err.message +=
- `\n\n` +
- sfc.filename +
- '\n' +
- generateCodeFrame(
- source,
- err.loc.start.offset,
- err.loc.end.offset
- ) +
- `\n`
- }
- throw err
- }
- if (preamble) {
- s.prepend(preamble)
- }
- // 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')
- }
- returned = code
- } else {
- returned = `() => {}`
- }
- } else {
- // return bindings from setup
- const allBindings: Record<string, any> = { ...setupBindings }
- for (const key in userImports) {
- if (!userImports[key].isType && userImports[key].isUsedInTemplate) {
- allBindings[key] = true
- }
- }
- returned = `{ ${Object.keys(allBindings).join(', ')} }`
- }
- if (!options.inlineTemplate && !__TEST__) {
- // in non-inline mode, the `__isScriptSetup: true` flag is used by
- // componentPublicInstance proxy to allow properties that start with $ or _
- s.appendRight(
- endOffset,
- `\nconst __returned__ = ${returned}\n` +
- `Object.defineProperty(__returned__, '__isScriptSetup', { enumerable: false, value: true })\n` +
- `return __returned__` +
- `\n}\n\n`
- )
- } else {
- s.appendRight(endOffset, `\nreturn ${returned}\n}\n\n`)
- }
- // 11. finalize default export
- let runtimeOptions = ``
- if (hasInlinedSsrRenderFn) {
- runtimeOptions += `\n __ssrInlineRender: true,`
- }
- if (propsRuntimeDecl) {
- runtimeOptions += `\n props: ${scriptSetup.content
- .slice(propsRuntimeDecl.start!, propsRuntimeDecl.end!)
- .trim()},`
- } else if (propsTypeDecl) {
- runtimeOptions += genRuntimeProps(typeDeclaredProps)
- }
- if (emitsRuntimeDecl) {
- runtimeOptions += `\n emits: ${scriptSetup.content
- .slice(emitsRuntimeDecl.start!, emitsRuntimeDecl.end!)
- .trim()},`
- } else if (emitsTypeDecl) {
- runtimeOptions += genRuntimeEmits(typeDeclaredEmits)
- }
- // <script setup> components are closed by default. If the user did not
- // explicitly call `defineExpose`, call expose() with no args.
- const exposeCall =
- hasDefineExposeCall || options.inlineTemplate ? `` : ` expose()\n`
- 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
- const def = defaultExport ? `\n ...${defaultTempVar},` : ``
- // 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.
- if (defaultExport) {
- s.prependLeft(
- startOffset,
- `\n${hasAwait ? `async ` : ``}function setup(${args}) {\n`
- )
- s.append(
- `\nexport default /*#__PURE__*/${helper(
- `defineComponent`
- )}({${def}${runtimeOptions}\n setup})`
- )
- } else {
- s.prependLeft(
- startOffset,
- `\nexport default /*#__PURE__*/${helper(
- `defineComponent`
- )}({${def}${runtimeOptions}\n ${
- hasAwait ? `async ` : ``
- }setup(${args}) {\n${exposeCall}`
- )
- s.appendRight(endOffset, `})`)
- }
- } else {
- if (defaultExport) {
- // can't rely on spread operator in non ts mode
- s.prependLeft(
- startOffset,
- `\n${hasAwait ? `async ` : ``}function setup(${args}) {\n`
- )
- s.append(
- `\nexport default /*#__PURE__*/ Object.assign(${defaultTempVar}, {${runtimeOptions}\n setup\n})\n`
- )
- } else {
- s.prependLeft(
- startOffset,
- `\nexport default {${runtimeOptions}\n ` +
- `${hasAwait ? `async ` : ``}setup(${args}) {\n${exposeCall}`
- )
- s.appendRight(endOffset, `}`)
- }
- }
- // 12. finalize Vue helper imports
- if (helperImports.size > 0) {
- s.prepend(
- `import { ${[...helperImports]
- .map(h => `${h} as _${h}`)
- .join(', ')} } from 'vue'\n`
- )
- }
- s.trim()
- return {
- ...scriptSetup,
- bindings: bindingMetadata,
- content: s.toString(),
- map: s.generateMap({
- source: filename,
- hires: true,
- includeContent: true
- }) as unknown as RawSourceMap,
- scriptAst: scriptAst?.body,
- scriptSetupAst: scriptSetupAst?.body
- }
- }
- function registerBinding(
- bindings: Record<string, BindingTypes>,
- node: Identifier,
- type: BindingTypes
- ) {
- bindings[node.name] = type
- }
- function walkDeclaration(
- node: Declaration,
- bindings: Record<string, BindingTypes>,
- userImportAlias: Record<string, string>
- ) {
- if (node.type === 'VariableDeclaration') {
- const isConst = node.kind === 'const'
- // export const foo = ...
- for (const { id, init } of node.declarations) {
- const isDefineCall = !!(
- isConst &&
- isCallOf(
- init,
- c => c === DEFINE_PROPS || c === DEFINE_EMITS || c === WITH_DEFAULTS
- )
- )
- if (id.type === 'Identifier') {
- let bindingType
- const userReactiveBinding = userImportAlias['reactive'] || 'reactive'
- if (isCallOf(init, userReactiveBinding)) {
- // treat reactive() calls as let since it's meant to be mutable
- bindingType = BindingTypes.SETUP_LET
- } else if (
- // if a declaration is a const literal, we can mark it so that
- // the generated render fn code doesn't need to unref() it
- isDefineCall ||
- (isConst && canNeverBeRef(init!, userReactiveBinding))
- ) {
- 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
- }
- registerBinding(bindings, id, bindingType)
- } else if (id.type === 'ObjectPattern') {
- walkObjectPattern(id, bindings, isConst, isDefineCall)
- } else if (id.type === 'ArrayPattern') {
- walkArrayPattern(id, bindings, isConst, isDefineCall)
- }
- }
- } else if (
- node.type === 'FunctionDeclaration' ||
- node.type === 'ClassDeclaration'
- ) {
- // export function foo() {} / export class Foo {}
- // export declarations must be named.
- bindings[node.id!.name] = BindingTypes.SETUP_CONST
- }
- }
- function walkObjectPattern(
- node: ObjectPattern,
- bindings: Record<string, BindingTypes>,
- isConst: boolean,
- isDefineCall = false
- ) {
- for (const p of node.properties) {
- if (p.type === 'ObjectProperty') {
- if (p.key.type === 'Identifier' && p.key === p.value) {
- // shorthand: const { x } = ...
- const type = isDefineCall
- ? BindingTypes.SETUP_CONST
- : isConst
- ? BindingTypes.SETUP_MAYBE_REF
- : BindingTypes.SETUP_LET
- registerBinding(bindings, p.key, type)
- } else {
- walkPattern(p.value, bindings, isConst, isDefineCall)
- }
- } else {
- // ...rest
- // argument can only be identifer when destructuring
- const type = isConst ? BindingTypes.SETUP_CONST : BindingTypes.SETUP_LET
- registerBinding(bindings, p.argument as Identifier, type)
- }
- }
- }
- function walkArrayPattern(
- node: ArrayPattern,
- bindings: Record<string, BindingTypes>,
- isConst: boolean,
- isDefineCall = false
- ) {
- for (const e of node.elements) {
- e && walkPattern(e, bindings, isConst, isDefineCall)
- }
- }
- function walkPattern(
- node: Node,
- bindings: Record<string, BindingTypes>,
- isConst: boolean,
- isDefineCall = false
- ) {
- if (node.type === 'Identifier') {
- const type = isDefineCall
- ? BindingTypes.SETUP_CONST
- : isConst
- ? BindingTypes.SETUP_MAYBE_REF
- : BindingTypes.SETUP_LET
- registerBinding(bindings, node, type)
- } else if (node.type === 'RestElement') {
- // argument can only be identifer when destructuring
- const type = isConst ? BindingTypes.SETUP_CONST : BindingTypes.SETUP_LET
- registerBinding(bindings, node.argument as Identifier, type)
- } else if (node.type === 'ObjectPattern') {
- walkObjectPattern(node, bindings, isConst)
- } else if (node.type === 'ArrayPattern') {
- walkArrayPattern(node, bindings, isConst)
- } else if (node.type === 'AssignmentPattern') {
- if (node.left.type === 'Identifier') {
- const type = isDefineCall
- ? BindingTypes.SETUP_CONST
- : isConst
- ? BindingTypes.SETUP_MAYBE_REF
- : BindingTypes.SETUP_LET
- registerBinding(bindings, node.left, type)
- } else {
- walkPattern(node.left, bindings, isConst)
- }
- }
- }
- 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 | TSInterfaceBody,
- props: Record<string, PropTypeData>,
- declaredTypes: Record<string, string[]>
- ) {
- const members = node.type === 'TSTypeLiteral' ? node.members : node.body
- for (const m of members) {
- if (
- (m.type === 'TSPropertySignature' || m.type === 'TSMethodSignature') &&
- m.key.type === 'Identifier'
- ) {
- let type
- if (__DEV__) {
- if (m.type === 'TSMethodSignature') {
- type = ['Function']
- } else if (m.typeAnnotation) {
- type = inferRuntimeType(
- m.typeAnnotation.typeAnnotation,
- declaredTypes
- )
- }
- }
- props[m.key.name] = {
- key: m.key.name,
- required: !m.optional,
- type: type || [`null`]
- }
- }
- }
- }
- 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':
- // TODO (nice to have) generate runtime element type/length checks
- 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':
- case 'Date':
- 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 'TSParenthesizedType':
- return inferRuntimeType(node.typeAnnotation, declaredTypes)
- 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 toRuntimeTypeString(types: string[]) {
- return types.length > 1 ? `[${types.join(', ')}]` : types[0]
- }
- function extractRuntimeEmits(
- node: TSFunctionType | TSTypeLiteral | TSInterfaceBody,
- emits: Set<string>
- ) {
- if (node.type === 'TSTypeLiteral' || node.type === 'TSInterfaceBody') {
- const members = node.type === 'TSTypeLiteral' ? node.members : node.body
- for (let t of members) {
- if (t.type === 'TSCallSignatureDeclaration') {
- extractEventNames(t.parameters[0], emits)
- }
- }
- return
- } else {
- extractEventNames(node.parameters[0], emits)
- }
- }
- function extractEventNames(
- eventName: Identifier | RestElement,
- emits: Set<string>
- ) {
- if (
- eventName.type === 'Identifier' &&
- eventName.typeAnnotation &&
- eventName.typeAnnotation.type === 'TSTypeAnnotation'
- ) {
- const typeNode = eventName.typeAnnotation.typeAnnotation
- if (typeNode.type === 'TSLiteralType') {
- if (typeNode.literal.type !== 'UnaryExpression') {
- emits.add(String(typeNode.literal.value))
- }
- } else if (typeNode.type === 'TSUnionType') {
- for (const t of typeNode.types) {
- if (
- t.type === 'TSLiteralType' &&
- t.literal.type !== 'UnaryExpression'
- ) {
- emits.add(String(t.literal.value))
- }
- }
- }
- }
- }
- function genRuntimeEmits(emits: Set<string>) {
- return emits.size
- ? `\n emits: [${Array.from(emits)
- .map(p => JSON.stringify(p))
- .join(', ')}],`
- : ``
- }
- function isCallOf(
- node: Node | null | undefined,
- test: string | ((id: string) => boolean)
- ): node is CallExpression {
- return !!(
- node &&
- node.type === 'CallExpression' &&
- node.callee.type === 'Identifier' &&
- (typeof test === 'string'
- ? node.callee.name === test
- : test(node.callee.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
- }
- }
- /**
- * 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.
- */
- function analyzeScriptBindings(ast: Statement[]): BindingMetadata {
- for (const node of ast) {
- if (
- node.type === 'ExportDefaultDeclaration' &&
- node.declaration.type === 'ObjectExpression'
- ) {
- return analyzeBindingsFromOptions(node.declaration)
- }
- }
- return {}
- }
- function analyzeBindingsFromOptions(node: ObjectExpression): BindingMetadata {
- const bindings: BindingMetadata = {}
- // #3270, #3275
- // mark non-script-setup so we don't resolve components/directives from these
- Object.defineProperty(bindings, '__isScriptSetup', {
- enumerable: false,
- value: false
- })
- 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: ... }
- for (const key of getObjectOrArrayExpressionKeys(property.value)) {
- bindings[key] = BindingTypes.PROPS
- }
- }
- // inject
- else if (property.key.name === 'inject') {
- // inject: ['foo']
- // inject: { foo: {} }
- for (const key of getObjectOrArrayExpressionKeys(property.value)) {
- bindings[key] = BindingTypes.OPTIONS
- }
- }
- // 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)) {
- bindings[key] = BindingTypes.OPTIONS
- }
- }
- }
- // 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'
- ) {
- for (const key of getObjectExpressionKeys(bodyItem.argument)) {
- bindings[key] =
- property.key.name === 'setup'
- ? BindingTypes.SETUP_MAYBE_REF
- : BindingTypes.DATA
- }
- }
- }
- }
- }
- return bindings
- }
- 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 []
- }
- const templateUsageCheckCache = createCache<string>()
- function resolveTemplateUsageCheckString(sfc: SFCDescriptor) {
- const { content, ast } = sfc.template!
- const cached = templateUsageCheckCache.get(content)
- if (cached) {
- return cached
- }
- let code = ''
- transform(createRoot([ast]), {
- nodeTransforms: [
- node => {
- if (node.type === NodeTypes.ELEMENT) {
- if (
- !parserOptions.isNativeTag!(node.tag) &&
- !parserOptions.isBuiltInComponent!(node.tag)
- ) {
- code += `,${camelize(node.tag)},${capitalize(camelize(node.tag))}`
- }
- for (let i = 0; i < node.props.length; i++) {
- const prop = node.props[i]
- if (prop.type === NodeTypes.DIRECTIVE) {
- if (!isBuiltInDir(prop.name)) {
- code += `,v${capitalize(camelize(prop.name))}`
- }
- if (prop.exp) {
- code += `,${stripStrings(
- (prop.exp as SimpleExpressionNode).content
- )}`
- }
- }
- }
- } else if (node.type === NodeTypes.INTERPOLATION) {
- code += `,${stripStrings(
- (node.content as SimpleExpressionNode).content
- )}`
- }
- }
- ]
- })
- code += ';'
- templateUsageCheckCache.set(content, code)
- return code
- }
- function stripStrings(exp: string) {
- return exp
- .replace(/'[^']+'|"[^"]+"/g, '')
- .replace(/`[^`]+`/g, stripTemplateString)
- }
- function stripTemplateString(str: string): string {
- const interpMatch = str.match(/\${[^}]+}/g)
- if (interpMatch) {
- return interpMatch.map(m => m.slice(2, -1)).join(',')
- }
- return ''
- }
|