]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(sfc-playground): Transform named default exports without altering scope (#4154)
authorwebfansplz <308241863@qq.com>
Tue, 20 Jul 2021 14:28:02 +0000 (22:28 +0800)
committerGitHub <noreply@github.com>
Tue, 20 Jul 2021 14:28:02 +0000 (10:28 -0400)
Co-authored-by: webfansplz <>
packages/sfc-playground/src/output/moduleCompiler.ts

index dbb82a52f435c6c16146a32893b656fadc00b4a3..89062897375e650305d2dc7a355a5371713dfd0f 100644 (file)
@@ -140,7 +140,17 @@ function processFile(file: File, seen = new Set<File>()) {
 
     // 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'