Skip to content

Commit

Permalink
chore: update warning and error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Nov 9, 2020
1 parent 3cca6bc commit 8cf0a40
Showing 1 changed file with 42 additions and 21 deletions.
63 changes: 42 additions & 21 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ export interface SFCScriptCompileOptions {
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>`
Expand All @@ -49,12 +56,10 @@ export function compileScript(
): SFCScriptBlock {
const { script, scriptSetup, styles, source, filename } = sfc

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

Expand Down Expand Up @@ -450,17 +455,30 @@ export function compileScript(

// process `ref: x` bindings (convert to refs)
if (
enableRefSugar &&
node.type === 'LabeledStatement' &&
node.label.name === 'ref' &&
node.body.type === 'ExpressionStatement'
) {
s.overwrite(
node.label.start! + startOffset,
node.body.start! + startOffset,
'const '
)
processRefExpression(node.body.expression, node)
if (enableRefSugar) {
warnOnce(
`ref: sugar is still an experimental proposal and is not\n` +
`guaranteed to be a part of <script setup>.\n` +
`Follow its status at https://github.com/vuejs/rfcs/pull/228`
)
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') {
Expand All @@ -487,13 +505,16 @@ export function compileScript(
}
}

if (node.type === 'ExportNamedDeclaration' && node.exportKind !== 'type') {
// TODO warn
error(`<script setup> cannot contain non-type named exports.`, node)
}

if (node.type === 'ExportAllDeclaration') {
// TODO warn
if (
(node.type === 'ExportNamedDeclaration' && node.exportKind !== 'type') ||
node.type === 'ExportAllDeclaration'
) {
error(
`<script setup> cannot contain non-type named or * 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 (node.type === 'ExportDefaultDeclaration') {
Expand Down

0 comments on commit 8cf0a40

Please sign in to comment.