66import MagicString from 'magic-string'
77import { createUnplugin , type UnpluginInstance } from 'unplugin'
88import ReplacePlugin from 'unplugin-replace'
9- import { createFilter } from 'unplugin-utils'
109import { scanEnums } from './core/enum'
1110import { resolveOptions , type Options } from './core/options'
1211
@@ -18,8 +17,6 @@ const plugin: UnpluginInstance<Options | undefined, true> = createUnplugin<
1817 true
1918> ( ( rawOptions = { } , meta ) => {
2019 const options = resolveOptions ( rawOptions )
21- const filter = createFilter ( options . include , options . exclude )
22-
2320 const { declarations, defines } = scanEnums ( options )
2421
2522 const replacePlugin = ReplacePlugin . raw (
@@ -38,52 +35,56 @@ const plugin: UnpluginInstance<Options | undefined, true> = createUnplugin<
3835 name,
3936 enforce : options . enforce ,
4037
41- transformInclude ( id ) {
42- return filter ( id )
43- } ,
44-
45- transform ( code , id ) {
46- let s : MagicString | undefined
38+ transform : {
39+ filter : {
40+ id : {
41+ include : options . include ,
42+ exclude : options . exclude ,
43+ } ,
44+ } ,
45+ handler ( code , id ) {
46+ let s : MagicString | undefined
4747
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 ) } `
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 ) } `
6363
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- )
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+ }
7879 }
79- }
8080
81- if ( s ) {
82- return {
83- code : s . toString ( ) ,
84- map : s . generateMap ( ) ,
81+ if ( s ) {
82+ return {
83+ code : s . toString ( ) ,
84+ map : s . generateMap ( ) ,
85+ }
8586 }
86- }
87+ } ,
8788 } ,
8889 } ,
8990 ]
0 commit comments