@@ -19,13 +19,16 @@ const InlineEnum: UnpluginInstance<Options | undefined, true> = createUnplugin<
1919 const options = resolveOptions ( rawOptions )
2020 const { declarations, defines } = scanEnums ( options )
2121
22- const replacePlugin = ReplacePlugin . raw (
23- {
24- include : options . include ,
25- exclude : options . exclude ,
26- values : defines ,
27- } ,
28- meta ,
22+ const replacePlugin = Object . assign (
23+ ReplacePlugin . raw (
24+ {
25+ include : options . include ,
26+ exclude : options . exclude ,
27+ values : defines ,
28+ } ,
29+ meta ,
30+ ) ,
31+ { name : 'unplugin-inline-enum:replace' } ,
2932 )
3033
3134 const name = 'unplugin-inline-enum'
@@ -43,45 +46,49 @@ const InlineEnum: UnpluginInstance<Options | undefined, true> = createUnplugin<
4346 } ,
4447 } ,
4548 handler ( code , id ) {
46- let s : MagicString | undefined
49+ if ( ! ( id in declarations ) ) return
4750
48- if ( id in declarations ) {
49- s ||= new MagicString ( code )
50- for ( const declaration of declarations [ id ] ) {
51- const {
52- range : [ start , end ] ,
53- id,
54- members,
55- } = declaration
56- s . update (
57- start ,
58- end ,
59- `export const ${ id } = {${ members
60- . flatMap ( ( { name, value } ) => {
61- const forwardMapping = `${ JSON . stringify ( name ) } : ${ JSON . stringify ( value ) } `
62- const reverseMapping = `${ JSON . stringify ( value . toString ( ) ) } : ${ JSON . stringify ( name ) } `
51+ const s : MagicString = new MagicString ( code )
52+ for ( const declaration of declarations [ id ] ) {
53+ const {
54+ range : [ start , end ] ,
55+ id,
56+ members,
57+ } = declaration
58+ s . update (
59+ start ,
60+ end ,
61+ `export const ${ id } = {${ members
62+ . flatMap ( ( { name, value } ) => {
63+ const forwardMapping = `${ JSON . stringify ( name ) } : ${ JSON . stringify ( value ) } `
64+ const reverseMapping = `${ JSON . stringify ( value . toString ( ) ) } : ${ JSON . stringify ( name ) } `
6365
64- // see https://www.typescriptlang.org/docs/handbook/enums.html#reverse-mappings
65- return typeof value === 'string'
66- ? [
67- forwardMapping ,
68- // string enum members do not get a reverse mapping generated at all
69- ]
70- : [
71- forwardMapping ,
72- // other enum members should support enum reverse mapping
73- reverseMapping ,
74- ]
75- } )
76- . join ( ',\n' ) } }`,
77- )
78- }
66+ // see https://www.typescriptlang.org/docs/handbook/enums.html#reverse-mappings
67+ return typeof value === 'string'
68+ ? [
69+ forwardMapping ,
70+ // string enum members do not get a reverse mapping generated at all
71+ ]
72+ : [
73+ forwardMapping ,
74+ // other enum members should support enum reverse mapping
75+ reverseMapping ,
76+ ]
77+ } )
78+ . join ( ',\n' ) } }`,
79+ )
7980 }
8081
81- if ( s ) {
82+ if ( s . hasChanged ( ) ) {
8283 return {
8384 code : s . toString ( ) ,
84- map : s . generateMap ( ) ,
85+ get map ( ) {
86+ return s . generateMap ( {
87+ hires : 'boundary' ,
88+ source : id ,
89+ includeContent : true ,
90+ } )
91+ } ,
8592 }
8693 }
8794 } ,
0 commit comments