11import { createFilter } from '@rollup/pluginutils'
22import { babelParse , getLang } from 'ast-kit'
33import { generateTransform , MagicString } from 'magic-string-ast'
4- import { createUnplugin } from 'unplugin'
4+ import { createUnplugin , type UnpluginInstance } from 'unplugin'
55import { resolveOption , type Options } from './core/options'
66import { resolveName } from './core/utils'
77import type * as t from '@babel/types'
@@ -13,78 +13,80 @@ function getNodeStart(node: t.Node) {
1313 return node . start !
1414}
1515
16- export default createUnplugin < Options | undefined , false > ( ( rawOptions = { } ) => {
17- const options = resolveOption ( rawOptions )
18- const filter = createFilter ( options . include , options . exclude )
16+ const VueNamedExport : UnpluginInstance < Options | undefined , false > =
17+ createUnplugin ( ( rawOptions = { } ) => {
18+ const options = resolveOption ( rawOptions )
19+ const filter = createFilter ( options . include , options . exclude )
1920
20- const name = 'unplugin-vue-named-export'
21- return {
22- name,
23- enforce : 'post' ,
21+ const name = 'unplugin-vue-named-export'
22+ return {
23+ name,
24+ enforce : 'post' ,
2425
25- transformInclude ( id ) {
26- return filter ( id )
27- } ,
28-
29- async transform ( code , id ) {
30- const lang = getLang ( id )
26+ transformInclude ( id ) {
27+ return filter ( id )
28+ } ,
3129
32- const program = babelParse ( code , lang )
33- const defaultExport = program . body . find (
34- ( node ) : node is t . ExportDefaultDeclaration =>
35- node . type === 'ExportDefaultDeclaration' ,
36- )
37- if ( ! defaultExport ) return
30+ async transform ( code , id ) {
31+ const lang = getLang ( id )
3832
39- const s = new MagicString ( code )
40- const resolvedName = await ( options . resolveName || resolveName ) ( id )
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
4139
42- s . overwrite (
43- defaultExport . start ! ,
44- getNodeStart ( defaultExport . declaration ) ,
45- `export const ${ resolvedName } = ` ,
46- )
40+ const s = new MagicString ( code )
41+ const resolvedName = await ( options . resolveName || resolveName ) ( id )
4742
48- if ( ! options . removeDefault ) {
49- s . appendLeft ( defaultExport . end ! , `\nexport default ${ resolvedName } ;` )
50- } else {
51- // hack Vite HMR
52- s . replace (
53- / c o n s t \{ d e f a u l t : u p d a t e d , ( .* ) \} = m o d / ,
54- ( _ , $1 ) => `const { "${ resolvedName } ": updated, ${ $1 } } = mod` ,
43+ s . overwrite (
44+ defaultExport . start ! ,
45+ getNodeStart ( defaultExport . declaration ) ,
46+ `export const ${ resolvedName } = ` ,
5547 )
56- }
5748
58- return generateTransform ( s , id )
59- } ,
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` ,
56+ )
57+ }
58+
59+ return generateTransform ( s , id )
60+ } ,
6061
61- vite : {
62- config ( config , { command } ) {
63- if ( command !== 'serve' ) return
64- return {
65- optimizeDeps : {
66- esbuildOptions : {
67- plugins : [
68- {
69- name : `${ name } -optimize-deps` ,
70- setup ( build ) {
71- build . onLoad ( { filter : / \. v u e ( $ | \? ) / } , async ( args ) => {
72- const resolvedName = await (
73- options . resolveName || resolveName
74- ) ( args . path )
75- let js = `export const ${ resolvedName } = {}`
76- if ( ! options . removeDefault ) {
77- js += `\nexport default ${ resolvedName } `
78- }
79- return { contents : js }
80- } )
62+ vite : {
63+ config ( config , { command } ) {
64+ if ( command !== 'serve' ) return
65+ return {
66+ optimizeDeps : {
67+ esbuildOptions : {
68+ plugins : [
69+ {
70+ name : `${ name } -optimize-deps` ,
71+ setup ( build ) {
72+ build . onLoad ( { filter : / \. v u e ( $ | \? ) / } , async ( args ) => {
73+ const resolvedName = await (
74+ options . resolveName || resolveName
75+ ) ( args . path )
76+ let js = `export const ${ resolvedName } = {}`
77+ if ( ! options . removeDefault ) {
78+ js += `\nexport default ${ resolvedName } `
79+ }
80+ return { contents : js }
81+ } )
82+ } ,
8183 } ,
82- } ,
83- ] ,
84+ ] ,
85+ } ,
8486 } ,
85- } ,
86- }
87+ }
88+ } ,
8789 } ,
88- } ,
89- }
90- } )
90+ }
91+ } )
92+ export default VueNamedExport
0 commit comments