@@ -19,13 +19,16 @@ const InlineEnum: UnpluginInstance<Options | undefined, true> = createUnplugin<
19
19
const options = resolveOptions ( rawOptions )
20
20
const { declarations, defines } = scanEnums ( options )
21
21
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' } ,
29
32
)
30
33
31
34
const name = 'unplugin-inline-enum'
@@ -43,45 +46,49 @@ const InlineEnum: UnpluginInstance<Options | undefined, true> = createUnplugin<
43
46
} ,
44
47
} ,
45
48
handler ( code , id ) {
46
- let s : MagicString | undefined
49
+ if ( ! ( id in declarations ) ) return
47
50
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 ) } `
63
65
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
+ )
79
80
}
80
81
81
- if ( s ) {
82
+ if ( s . hasChanged ( ) ) {
82
83
return {
83
84
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
+ } ,
85
92
}
86
93
}
87
94
} ,
0 commit comments