11import { babelParse , getLang } from 'ast-kit'
22import { generateTransform , MagicString } from 'magic-string-ast'
33import { createUnplugin , type UnpluginInstance } from 'unplugin'
4- import { createFilter } from 'unplugin-utils'
54import { resolveOption , type Options } from './core/options'
65import { resolveName } from './core/utils'
76import type * as t from '@babel/types'
@@ -16,47 +15,53 @@ function getNodeStart(node: t.Node) {
1615export const VueNamedExport : UnpluginInstance < Options | undefined , false > =
1716 createUnplugin ( ( rawOptions = { } ) => {
1817 const options = resolveOption ( rawOptions )
19- const filter = createFilter ( options . include , options . exclude )
2018
2119 const name = 'unplugin-vue-named-export'
2220 return {
2321 name,
2422 enforce : 'post' ,
2523
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 )
3933
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
4240
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 )
4843
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 } = ` ,
5648 )
57- }
5849
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+ } ,
6065 } ,
6166
6267 vite : {
0 commit comments