6
6
import MagicString from 'magic-string'
7
7
import { createUnplugin , type UnpluginInstance } from 'unplugin'
8
8
import ReplacePlugin from 'unplugin-replace'
9
- import { createFilter } from 'unplugin-utils'
10
9
import { scanEnums } from './core/enum'
11
10
import { resolveOptions , type Options } from './core/options'
12
11
@@ -18,8 +17,6 @@ const plugin: UnpluginInstance<Options | undefined, true> = createUnplugin<
18
17
true
19
18
> ( ( rawOptions = { } , meta ) => {
20
19
const options = resolveOptions ( rawOptions )
21
- const filter = createFilter ( options . include , options . exclude )
22
-
23
20
const { declarations, defines } = scanEnums ( options )
24
21
25
22
const replacePlugin = ReplacePlugin . raw (
@@ -38,52 +35,56 @@ const plugin: UnpluginInstance<Options | undefined, true> = createUnplugin<
38
35
name,
39
36
enforce : options . enforce ,
40
37
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
47
47
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 ) } `
63
63
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
+ }
78
79
}
79
- }
80
80
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
+ }
85
86
}
86
- }
87
+ } ,
87
88
} ,
88
89
} ,
89
90
]
0 commit comments