Skip to content

Commit 8cf0a40

Browse files
committed
chore: update warning and error messages
1 parent 3cca6bc commit 8cf0a40

File tree

1 file changed

+42
-21
lines changed

1 file changed

+42
-21
lines changed

packages/compiler-sfc/src/compileScript.ts

+42-21
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,14 @@ export interface SFCScriptCompileOptions {
3636
refSugar?: boolean
3737
}
3838

39-
let hasWarned = false
39+
const hasWarned: Record<string, boolean> = {}
40+
41+
function warnOnce(msg: string) {
42+
if (!hasWarned[msg]) {
43+
hasWarned[msg] = true
44+
console.log(`\n\x1b[33m[@vue/compiler-sfc] %s\x1b[0m\n`, msg)
45+
}
46+
}
4047

4148
/**
4249
* Compile `<script setup>`
@@ -49,12 +56,10 @@ export function compileScript(
4956
): SFCScriptBlock {
5057
const { script, scriptSetup, styles, source, filename } = sfc
5158

52-
if (__DEV__ && !__TEST__ && !hasWarned && scriptSetup) {
53-
hasWarned = true
54-
// @ts-ignore `console.info` cannot be null error
55-
console[console.info ? 'info' : 'log'](
56-
`\n[@vue/compiler-sfc] <script setup> is still an experimental proposal.\n` +
57-
`Follow https://github.com/vuejs/rfcs/pull/182 for its status.\n`
59+
if (__DEV__ && !__TEST__ && scriptSetup) {
60+
warnOnce(
61+
`<script setup> is still an experimental proposal.\n` +
62+
`Follow https://github.com/vuejs/rfcs/pull/227 for its status.`
5863
)
5964
}
6065

@@ -450,17 +455,30 @@ export function compileScript(
450455

451456
// process `ref: x` bindings (convert to refs)
452457
if (
453-
enableRefSugar &&
454458
node.type === 'LabeledStatement' &&
455459
node.label.name === 'ref' &&
456460
node.body.type === 'ExpressionStatement'
457461
) {
458-
s.overwrite(
459-
node.label.start! + startOffset,
460-
node.body.start! + startOffset,
461-
'const '
462-
)
463-
processRefExpression(node.body.expression, node)
462+
if (enableRefSugar) {
463+
warnOnce(
464+
`ref: sugar is still an experimental proposal and is not\n` +
465+
`guaranteed to be a part of <script setup>.\n` +
466+
`Follow its status at https://github.com/vuejs/rfcs/pull/228`
467+
)
468+
s.overwrite(
469+
node.label.start! + startOffset,
470+
node.body.start! + startOffset,
471+
'const '
472+
)
473+
processRefExpression(node.body.expression, node)
474+
} else {
475+
// TODO if we end up shipping ref: sugar as an opt-in feature,
476+
// need to proxy the option in vite, vue-loader and rollup-plugin-vue.
477+
error(
478+
`ref: sugar needs to be explicitly enabled via vite or vue-loader options.`,
479+
node
480+
)
481+
}
464482
}
465483

466484
if (node.type === 'ImportDeclaration') {
@@ -487,13 +505,16 @@ export function compileScript(
487505
}
488506
}
489507

490-
if (node.type === 'ExportNamedDeclaration' && node.exportKind !== 'type') {
491-
// TODO warn
492-
error(`<script setup> cannot contain non-type named exports.`, node)
493-
}
494-
495-
if (node.type === 'ExportAllDeclaration') {
496-
// TODO warn
508+
if (
509+
(node.type === 'ExportNamedDeclaration' && node.exportKind !== 'type') ||
510+
node.type === 'ExportAllDeclaration'
511+
) {
512+
error(
513+
`<script setup> cannot contain non-type named or * exports. ` +
514+
`If you are using a previous version of <script setup>, please ` +
515+
`consult the updated RFC at https://github.com/vuejs/rfcs/pull/227.`,
516+
node
517+
)
497518
}
498519

499520
if (node.type === 'ExportDefaultDeclaration') {

0 commit comments

Comments
 (0)