chore: update warning and error messages

This commit is contained in:
Evan You 2020-11-09 17:00:50 -05:00
parent 3cca6bc3a8
commit 8cf0a40d5b

View File

@ -36,7 +36,14 @@ export interface SFCScriptCompileOptions {
refSugar?: boolean refSugar?: boolean
} }
let hasWarned = false const hasWarned: Record<string, boolean> = {}
function warnOnce(msg: string) {
if (!hasWarned[msg]) {
hasWarned[msg] = true
console.log(`\n\x1b[33m[@vue/compiler-sfc] %s\x1b[0m\n`, msg)
}
}
/** /**
* Compile `<script setup>` * Compile `<script setup>`
@ -49,12 +56,10 @@ export function compileScript(
): SFCScriptBlock { ): SFCScriptBlock {
const { script, scriptSetup, styles, source, filename } = sfc const { script, scriptSetup, styles, source, filename } = sfc
if (__DEV__ && !__TEST__ && !hasWarned && scriptSetup) { if (__DEV__ && !__TEST__ && scriptSetup) {
hasWarned = true warnOnce(
// @ts-ignore `console.info` cannot be null error `<script setup> is still an experimental proposal.\n` +
console[console.info ? 'info' : 'log']( `Follow https://github.com/vuejs/rfcs/pull/227 for its status.`
`\n[@vue/compiler-sfc] <script setup> is still an experimental proposal.\n` +
`Follow https://github.com/vuejs/rfcs/pull/182 for its status.\n`
) )
} }
@ -450,17 +455,30 @@ export function compileScript(
// process `ref: x` bindings (convert to refs) // process `ref: x` bindings (convert to refs)
if ( if (
enableRefSugar &&
node.type === 'LabeledStatement' && node.type === 'LabeledStatement' &&
node.label.name === 'ref' && node.label.name === 'ref' &&
node.body.type === 'ExpressionStatement' node.body.type === 'ExpressionStatement'
) { ) {
s.overwrite( if (enableRefSugar) {
node.label.start! + startOffset, warnOnce(
node.body.start! + startOffset, `ref: sugar is still an experimental proposal and is not\n` +
'const ' `guaranteed to be a part of <script setup>.\n` +
) `Follow its status at https://github.com/vuejs/rfcs/pull/228`
processRefExpression(node.body.expression, node) )
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
)
}
} }
if (node.type === 'ImportDeclaration') { if (node.type === 'ImportDeclaration') {
@ -487,13 +505,16 @@ export function compileScript(
} }
} }
if (node.type === 'ExportNamedDeclaration' && node.exportKind !== 'type') { if (
// TODO warn (node.type === 'ExportNamedDeclaration' && node.exportKind !== 'type') ||
error(`<script setup> cannot contain non-type named exports.`, node) node.type === 'ExportAllDeclaration'
} ) {
error(
if (node.type === 'ExportAllDeclaration') { `<script setup> cannot contain non-type named or * exports. ` +
// TODO warn `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 (node.type === 'ExportDefaultDeclaration') { if (node.type === 'ExportDefaultDeclaration') {