feat(compiler-sfc): support kebab-case components in <script setup> sfc template

This commit is contained in:
Evan You 2020-11-09 17:22:58 -05:00
parent 8cf0a40d5b
commit 3f99e239e0
3 changed files with 20 additions and 9 deletions

View File

@ -22,7 +22,8 @@ import {
isArray, isArray,
NOOP, NOOP,
PatchFlags, PatchFlags,
PatchFlagNames PatchFlagNames,
EMPTY_OBJ
} from '@vue/shared' } from '@vue/shared'
import { defaultOnError } from './errors' import { defaultOnError } from './errors'
import { import {
@ -122,7 +123,7 @@ export function createTransformContext(
scopeId = null, scopeId = null,
ssr = false, ssr = false,
ssrCssVars = ``, ssrCssVars = ``,
bindingMetadata = {}, bindingMetadata = EMPTY_OBJ,
onError = defaultOnError onError = defaultOnError
}: TransformOptions }: TransformOptions
): TransformContext { ): TransformContext {

View File

@ -26,7 +26,10 @@ import {
isSymbol, isSymbol,
isOn, isOn,
isObject, isObject,
isReservedProp isReservedProp,
capitalize,
camelize,
EMPTY_OBJ
} from '@vue/shared' } from '@vue/shared'
import { createCompilerError, ErrorCodes } from '../errors' import { createCompilerError, ErrorCodes } from '../errors'
import { import {
@ -246,8 +249,15 @@ export function resolveComponentType(
} }
// 3. user component (from setup bindings) // 3. user component (from setup bindings)
if (context.bindingMetadata[tag] === 'setup') { let tagFromSetup = tag
return `$setup[${JSON.stringify(tag)}]` const bindings = context.bindingMetadata
if (
bindings !== EMPTY_OBJ &&
(bindings[tagFromSetup] === 'setup' ||
bindings[(tagFromSetup = camelize(tag))] === 'setup' ||
bindings[(tagFromSetup = capitalize(camelize(tag)))] === 'setup')
) {
return `$setup[${JSON.stringify(tagFromSetup)}]`
} }
// 4. user component (resolve) // 4. user component (resolve)

View File

@ -41,7 +41,7 @@ const hasWarned: Record<string, boolean> = {}
function warnOnce(msg: string) { function warnOnce(msg: string) {
if (!hasWarned[msg]) { if (!hasWarned[msg]) {
hasWarned[msg] = true hasWarned[msg] = true
console.log(`\n\x1b[33m[@vue/compiler-sfc] %s\x1b[0m\n`, msg) console.log(`\x1b[33m[@vue/compiler-sfc] %s\x1b[0m\n`, msg)
} }
} }
@ -59,7 +59,7 @@ export function compileScript(
if (__DEV__ && !__TEST__ && scriptSetup) { if (__DEV__ && !__TEST__ && scriptSetup) {
warnOnce( warnOnce(
`<script setup> is still an experimental proposal.\n` + `<script setup> is still an experimental proposal.\n` +
`Follow https://github.com/vuejs/rfcs/pull/227 for its status.` `Follow its status at https://github.com/vuejs/rfcs/pull/227.`
) )
} }
@ -461,9 +461,9 @@ export function compileScript(
) { ) {
if (enableRefSugar) { if (enableRefSugar) {
warnOnce( warnOnce(
`ref: sugar is still an experimental proposal and is not\n` + `ref: sugar is still an experimental proposal and is not ` +
`guaranteed to be a part of <script setup>.\n` + `guaranteed to be a part of <script setup>.\n` +
`Follow its status at https://github.com/vuejs/rfcs/pull/228` `Follow its status at https://github.com/vuejs/rfcs/pull/228.`
) )
s.overwrite( s.overwrite(
node.label.start! + startOffset, node.label.start! + startOffset,