11import path from 'node:path'
22import { createUnplugin , type UnpluginInstance } from 'unplugin'
3- import { createFilter } from 'unplugin-utils'
43import { resolveOption , type Options } from './core/options'
54import { transformCss , transformCssModule } from './core/transform'
65
76const plugin : UnpluginInstance < Options | undefined , false > = createUnplugin (
87 ( rawOptions = { } ) => {
98 const options = resolveOption ( rawOptions )
10- const filter = createFilter ( options . include , options . exclude )
119
1210 const transformedFiles = new Map < string , string > ( )
1311
@@ -16,27 +14,37 @@ const plugin: UnpluginInstance<Options | undefined, false> = createUnplugin(
1614 name,
1715 enforce : options . enforce ,
1816
19- transformInclude ( id ) {
20- return filter ( id )
21- } ,
22-
2317 resolveId ( id , importer ) {
2418 if ( id . endsWith ( '.module_built.css' ) ) return id
2519 if ( id . endsWith ( '.module.css' ) ) {
2620 return `${ path . resolve ( path . dirname ( importer || '' ) , id ) } ?css_module`
2721 }
2822 } ,
2923
30- transform ( code , id ) {
31- return transformCss ( id , code , options . options )
24+ transform : {
25+ filter : {
26+ id : {
27+ include : options . include ,
28+ exclude : options . exclude ,
29+ } ,
30+ } ,
31+ handler ( code , id ) {
32+ return transformCss ( id , code , options . options )
33+ } ,
3234 } ,
3335
34- async load ( id ) {
35- if ( id . endsWith ( '.module_built.css' ) ) {
36- const code = transformedFiles . get ( id ) !
37- return { id, code }
38- }
39- if ( id . endsWith ( '?css_module' ) ) {
36+ load : {
37+ filter : {
38+ id : {
39+ include : [ / \. m o d u l e _ b u i l t \. c s s $ / , / \? c s s _ m o d u l e $ / ] ,
40+ } ,
41+ } ,
42+ async handler ( id ) {
43+ if ( id . endsWith ( '.module_built.css' ) ) {
44+ const code = transformedFiles . get ( id ) !
45+ return { id, code }
46+ }
47+
4048 const {
4149 code,
4250 map,
@@ -48,7 +56,7 @@ const plugin: UnpluginInstance<Options | undefined, false> = createUnplugin(
4856 code : `import "${ compiledId } ";\n${ exports } ` ,
4957 map,
5058 }
51- }
59+ } ,
5260 } ,
5361 }
5462 } ,
0 commit comments