From 4b8267dc9570d6080e08ed8e4dc27ba51d523de1 Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 29 Jan 2021 15:39:49 -0500 Subject: [PATCH] fix(plugin-vue): special handling for class default export in sfc close #1476 --- packages/plugin-vue/src/main.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/plugin-vue/src/main.ts b/packages/plugin-vue/src/main.ts index c5c40279..31be19ee 100644 --- a/packages/plugin-vue/src/main.ts +++ b/packages/plugin-vue/src/main.ts @@ -207,6 +207,8 @@ async function genTemplateCode( } } +const exportDefaultClassRE = /export\s+default\s+class\s+([\w$]+)/ + async function genScriptCode( descriptor: SFCDescriptor, options: ResolvedOptions, @@ -226,7 +228,14 @@ async function genScriptCode( (!script.lang || (script.lang === 'ts' && options.devServer)) && !script.src ) { - scriptCode = rewriteDefault(script.content, `_sfc_main`) + const classMatch = script.content.match(exportDefaultClassRE) + if (classMatch) { + scriptCode = + script.content.replace(exportDefaultClassRE, `class $1`) + + `\nconst _sfc_main = ${classMatch[1]}` + } else { + scriptCode = rewriteDefault(script.content, `_sfc_main`) + } map = script.map if (script.lang === 'ts') { const result = await options.devServer!.transformWithEsbuild(