@@ -2,7 +2,7 @@ import { parse, ParserPlugin } from '@babel/parser'
2
2
import MagicString from 'magic-string'
3
3
4
4
const defaultExportRE = / ( (?: ^ | \n | ; ) \s * ) e x p o r t ( \s * ) d e f a u l t /
5
- const namedDefaultExportRE = / ( (?: ^ | \n | ; ) \s * ) e x p o r t ( .+ ) a s ( \s * ) d e f a u l t / s
5
+ const namedDefaultExportRE = / ( (?: ^ | \n | ; ) \s * ) e x p o r t ( .+ ) (?: a s ) ? ( \s * ) d e f a u l t / s
6
6
const exportDefaultClassRE =
7
7
/ ( (?: ^ | \n | ; ) \s * ) e x p o r t \s + d e f a u l t \s + c l a s s \s + ( [ \w $ ] + ) /
8
8
@@ -45,21 +45,39 @@ export function rewriteDefault(
45
45
s . overwrite ( node . start ! , node . declaration . start ! , `const ${ as } = ` )
46
46
}
47
47
if ( node . type === 'ExportNamedDeclaration' ) {
48
- node . specifiers . forEach ( specifier => {
48
+ for ( const specifier of node . specifiers ) {
49
49
if (
50
50
specifier . type === 'ExportSpecifier' &&
51
51
specifier . exported . type === 'Identifier' &&
52
52
specifier . exported . name === 'default'
53
53
) {
54
- const end = specifier . end !
55
- s . overwrite (
56
- specifier . start ! ,
57
- input . charAt ( end ) === ',' ? end + 1 : end ,
58
- ``
59
- )
54
+ if ( node . source ) {
55
+ if ( specifier . local . name === 'default' ) {
56
+ const end = specifierEnd ( input , specifier . local . end ! , node . end )
57
+ s . prepend (
58
+ `import { default as __VUE_DEFAULT__ } from '${ node . source . value } '\n`
59
+ )
60
+ s . overwrite ( specifier . start ! , end , `` )
61
+ s . append ( `\nconst ${ as } = __VUE_DEFAULT__` )
62
+ continue
63
+ } else {
64
+ const end = specifierEnd ( input , specifier . exported . end ! , node . end )
65
+ s . prepend (
66
+ `import { ${ input . slice (
67
+ specifier . local . start ! ,
68
+ specifier . local . end !
69
+ ) } } from '${ node . source ?. value } '\n`
70
+ )
71
+ s . overwrite ( specifier . start ! , end , `` )
72
+ s . append ( `\nconst ${ as } = ${ specifier . local . name } ` )
73
+ continue
74
+ }
75
+ }
76
+ const end = specifierEnd ( input , specifier . end ! , node . end )
77
+ s . overwrite ( specifier . start ! , end , `` )
60
78
s . append ( `\nconst ${ as } = ${ specifier . local . name } ` )
61
79
}
62
- } )
80
+ }
63
81
}
64
82
} )
65
83
return s . toString ( )
@@ -68,3 +86,21 @@ export function rewriteDefault(
68
86
export function hasDefaultExport ( input : string ) : boolean {
69
87
return defaultExportRE . test ( input ) || namedDefaultExportRE . test ( input )
70
88
}
89
+
90
+ function specifierEnd ( input : string , end : number , nodeEnd : number | null ) {
91
+ // export { default , foo } ...
92
+ let hasCommas = false
93
+ let oldEnd = end
94
+ while ( end < nodeEnd ! ) {
95
+ if ( / \s / . test ( input . charAt ( end ) ) ) {
96
+ end ++
97
+ } else if ( input . charAt ( end ) === ',' ) {
98
+ end ++
99
+ hasCommas = true
100
+ break
101
+ } else if ( input . charAt ( end ) === '}' ) {
102
+ break
103
+ }
104
+ }
105
+ return hasCommas ? end : oldEnd
106
+ }
0 commit comments