1
1
import prettier from 'prettier'
2
2
const { resolveConfig } = prettier
3
3
4
- import type { Plugin as VitePlugin } from 'vite'
4
+ import { Plugin as VitePlugin , createFilter } from 'vite'
5
5
import { main } from './main'
6
6
import type { FinalConfig , PluginOptions } from './type'
7
7
import { isCSSModuleRequest } from './util'
8
8
9
9
export default function Plugin ( option : PluginOptions = { } ) : VitePlugin {
10
10
let cacheConfig : FinalConfig
11
+ let filter : ReturnType < typeof createFilter >
11
12
const enabledMode = option . enabledMode || [ 'development' ]
12
13
return {
13
14
name : 'vite-plugin-sass-dts' ,
14
15
async configResolved ( config ) {
16
+ filter = createFilter ( undefined , option . excludePath )
15
17
const prettierOptions =
16
18
( await resolveConfig ( option . prettierFilePath || config . root ) ) || { }
17
19
cacheConfig = {
@@ -20,15 +22,16 @@ export default function Plugin(option: PluginOptions = {}): VitePlugin {
20
22
}
21
23
} ,
22
24
handleHotUpdate ( context ) {
23
- if ( ! isCSSModuleRequest ( context . file ) ) return
25
+ if ( ! isCSSModuleRequest ( context . file ) || ! filter ( context . file ) ) return
24
26
main ( context . file , cacheConfig , option )
25
27
return
26
28
} ,
27
29
transform ( code , id ) {
28
30
const fileName = id . replace ( / (?: \? | & ) ( u s e d | d i r e c t | i n l i n e | v u e ) .* / , '' )
29
31
if (
30
32
! enabledMode . includes ( cacheConfig . env . MODE ) ||
31
- ! isCSSModuleRequest ( fileName )
33
+ ! isCSSModuleRequest ( fileName ) ||
34
+ ! filter ( id )
32
35
) {
33
36
// returning undefined will signal vite that the file has not been transformed
34
37
// avoiding warnings about source maps not being generated
@@ -40,7 +43,7 @@ export default function Plugin(option: PluginOptions = {}): VitePlugin {
40
43
)
41
44
} ,
42
45
watchChange ( id ) {
43
- if ( isCSSModuleRequest ( id ) ) {
46
+ if ( isCSSModuleRequest ( id ) && filter ( id ) ) {
44
47
this . addWatchFile ( id )
45
48
}
46
49
} ,
0 commit comments