File tree 2 files changed +62
-1
lines changed
2 files changed +62
-1
lines changed Original file line number Diff line number Diff line change @@ -26,4 +26,55 @@ describe('compiler sfc: rewriteDefault', () => {
26
26
const script = a"
27
27
` )
28
28
} )
29
+
30
+ test ( 'w/ comments' , async ( ) => {
31
+ expect ( rewriteDefault ( `// export default\nexport default {}` , 'script' ) )
32
+ . toMatchInlineSnapshot ( `
33
+ "// export default
34
+ const script = {}"
35
+ ` )
36
+ } )
37
+
38
+ test ( 'export default class' , async ( ) => {
39
+ expect ( rewriteDefault ( `export default class Foo {}` , 'script' ) )
40
+ . toMatchInlineSnapshot ( `
41
+ "class Foo {}
42
+ const script = Foo"
43
+ ` )
44
+ } )
45
+
46
+ test ( 'export default class w/ comments' , async ( ) => {
47
+ expect (
48
+ rewriteDefault ( `// export default\nexport default class Foo {}` , 'script' )
49
+ ) . toMatchInlineSnapshot ( `
50
+ "// export default
51
+ class Foo {}
52
+ const script = Foo"
53
+ ` )
54
+ } )
55
+
56
+ test ( 'export default class w/ comments 2' , async ( ) => {
57
+ expect (
58
+ rewriteDefault (
59
+ `export default {}\n` + `// export default class Foo {}` ,
60
+ 'script'
61
+ )
62
+ ) . toMatchInlineSnapshot ( `
63
+ "const script = {}
64
+ // export default class Foo {}"
65
+ ` )
66
+ } )
67
+
68
+ test ( 'export default class w/ comments 3' , async ( ) => {
69
+ expect (
70
+ rewriteDefault (
71
+ `/*\nexport default class Foo {}*/\n` + `export default class Bar {}` ,
72
+ 'script'
73
+ )
74
+ ) . toMatchInlineSnapshot ( `
75
+ "/*
76
+ export default class Foo {}*/
77
+ const script = class Bar {}"
78
+ ` )
79
+ } )
29
80
} )
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ 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
5
const namedDefaultExportRE = / ( (?: ^ | \n | ; ) \s * ) e x p o r t ( .+ ) a s ( \s * ) d e f a u l t /
6
+ const exportDefaultClassRE = / ( (?: ^ | \n | ; ) \s * ) e x p o r t \s + d e f a u l t \s + c l a s s \s + ( [ \w $ ] + ) /
6
7
7
8
/**
8
9
* Utility for rewriting `export default` in a script block into a variable
@@ -17,7 +18,16 @@ export function rewriteDefault(
17
18
return input + `\nconst ${ as } = {}`
18
19
}
19
20
20
- const replaced = input . replace ( defaultExportRE , `$1const ${ as } =` )
21
+ let replaced : string | undefined
22
+
23
+ const classMatch = input . match ( exportDefaultClassRE )
24
+ if ( classMatch ) {
25
+ replaced =
26
+ input . replace ( exportDefaultClassRE , '$1class $2' ) +
27
+ `\nconst ${ as } = ${ classMatch [ 2 ] } `
28
+ } else {
29
+ replaced = input . replace ( defaultExportRE , `$1const ${ as } =` )
30
+ }
21
31
if ( ! hasDefaultExport ( replaced ) ) {
22
32
return replaced
23
33
}
You can’t perform that action at this time.
0 commit comments