1
1
import { babelParse , getLang } from 'ast-kit'
2
2
import { generateTransform , MagicString } from 'magic-string-ast'
3
3
import { createUnplugin , type UnpluginInstance } from 'unplugin'
4
- import { createFilter } from 'unplugin-utils'
5
4
import { resolveOption , type Options } from './core/options'
6
5
import { resolveName } from './core/utils'
7
6
import type * as t from '@babel/types'
@@ -16,47 +15,53 @@ function getNodeStart(node: t.Node) {
16
15
export const VueNamedExport : UnpluginInstance < Options | undefined , false > =
17
16
createUnplugin ( ( rawOptions = { } ) => {
18
17
const options = resolveOption ( rawOptions )
19
- const filter = createFilter ( options . include , options . exclude )
20
18
21
19
const name = 'unplugin-vue-named-export'
22
20
return {
23
21
name,
24
22
enforce : 'post' ,
25
23
26
- transformInclude ( id ) {
27
- return filter ( id )
28
- } ,
29
-
30
- async transform ( code , id ) {
31
- const lang = getLang ( id )
32
-
33
- const program = babelParse ( code , lang )
34
- const defaultExport = program . body . find (
35
- ( node ) : node is t . ExportDefaultDeclaration =>
36
- node . type === 'ExportDefaultDeclaration' ,
37
- )
38
- if ( ! defaultExport ) return
24
+ transform : {
25
+ filter : {
26
+ id : {
27
+ include : options . include ,
28
+ exclude : options . exclude ,
29
+ } ,
30
+ } ,
31
+ async handler ( code , id ) {
32
+ const lang = getLang ( id )
39
33
40
- const s = new MagicString ( code )
41
- const resolvedName = await ( options . resolveName || resolveName ) ( id )
34
+ const program = babelParse ( code , lang )
35
+ const defaultExport = program . body . find (
36
+ ( node ) : node is t . ExportDefaultDeclaration =>
37
+ node . type === 'ExportDefaultDeclaration' ,
38
+ )
39
+ if ( ! defaultExport ) return
42
40
43
- s . overwrite (
44
- defaultExport . start ! ,
45
- getNodeStart ( defaultExport . declaration ) ,
46
- `export const ${ resolvedName } = ` ,
47
- )
41
+ const s = new MagicString ( code )
42
+ const resolvedName = await ( options . resolveName || resolveName ) ( id )
48
43
49
- if ( ! options . removeDefault ) {
50
- s . appendLeft ( defaultExport . end ! , `\nexport default ${ resolvedName } ;` )
51
- } else {
52
- // hack Vite HMR
53
- s . replace (
54
- / c o n s t \{ d e f a u l t : u p d a t e d , ( .* ) \} = m o d / ,
55
- ( _ , $1 ) => `const { "${ resolvedName } ": updated, ${ $1 } } = mod` ,
44
+ s . overwrite (
45
+ defaultExport . start ! ,
46
+ getNodeStart ( defaultExport . declaration ) ,
47
+ `export const ${ resolvedName } = ` ,
56
48
)
57
- }
58
49
59
- return generateTransform ( s , id )
50
+ if ( ! options . removeDefault ) {
51
+ s . appendLeft (
52
+ defaultExport . end ! ,
53
+ `\nexport default ${ resolvedName } ;` ,
54
+ )
55
+ } else {
56
+ // hack Vite HMR
57
+ s . replace (
58
+ / c o n s t \{ d e f a u l t : u p d a t e d , ( .* ) \} = m o d / ,
59
+ ( _ , $1 ) => `const { "${ resolvedName } ": updated, ${ $1 } } = mod` ,
60
+ )
61
+ }
62
+
63
+ return generateTransform ( s , id )
64
+ } ,
60
65
} ,
61
66
62
67
vite : {
0 commit comments