Bläddra i källkod

fix(sfc-playground): Transform named default exports without altering scope (#4154)

Co-authored-by: webfansplz <>
webfansplz 4 år sedan
förälder
incheckning
acb2a4d285
1 ändrade filer med 11 tillägg och 1 borttagningar
  1. 11 1
      packages/sfc-playground/src/output/moduleCompiler.ts

+ 11 - 1
packages/sfc-playground/src/output/moduleCompiler.ts

@@ -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'