From acb2a4d285bfdee6437970b3dc9435abfe1c4ddf Mon Sep 17 00:00:00 2001 From: webfansplz <308241863@qq.com> Date: Tue, 20 Jul 2021 22:28:02 +0800 Subject: [PATCH] fix(sfc-playground): Transform named default exports without altering scope (#4154) Co-authored-by: webfansplz <> --- packages/sfc-playground/src/output/moduleCompiler.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/sfc-playground/src/output/moduleCompiler.ts b/packages/sfc-playground/src/output/moduleCompiler.ts index dbb82a52f43..89062897375 100644 --- a/packages/sfc-playground/src/output/moduleCompiler.ts +++ b/packages/sfc-playground/src/output/moduleCompiler.ts @@ -140,7 +140,17 @@ function processFile(file: File, seen = new Set()) { // default export if (node.type === 'ExportDefaultDeclaration') { - s.overwrite(node.start!, node.start! + 14, `${moduleKey}.default =`) + if ('id' in node.declaration && node.declaration.id) { + // named hoistable/class exports + // export default function foo() {} + // export default class A {} + const { name } = node.declaration.id + s.remove(node.start!, node.start! + 15) + s.append(`\n${exportKey}(${moduleKey}, "default", () => ${name})`) + } else { + // anonymous default exports + s.overwrite(node.start!, node.start! + 14, `${moduleKey}.default =`) + } } // export * from './foo'