@@ -13,21 +13,32 @@ export type PluginFilterWithFallback = (
13
13
) => boolean | FallbackValues
14
14
export type TransformHookFilter = ( id : string , code : string ) => boolean
15
15
16
- export type StringFilter =
17
- | string
18
- | RegExp
19
- | Array < string | RegExp >
16
+ export type StringFilter < Value = string | RegExp > =
17
+ | Value
18
+ | Array < Value >
20
19
| {
21
- include ?: string | RegExp | Array < string | RegExp >
22
- exclude ?: string | RegExp | Array < string | RegExp >
20
+ include ?: Value | Array < Value >
21
+ exclude ?: Value | Array < Value >
23
22
}
24
23
25
24
type NormalizedStringFilter = {
26
25
include ?: Array < string | RegExp >
27
26
exclude ?: Array < string | RegExp >
28
27
}
29
28
30
- function patternToIdFilter ( pattern : string | RegExp ) : PluginFilter {
29
+ function getMatcherString ( glob : string , cwd : string ) {
30
+ if ( glob . startsWith ( '**' ) || path . isAbsolute ( glob ) ) {
31
+ return slash ( glob )
32
+ }
33
+
34
+ const resolved = path . join ( cwd , glob )
35
+ return slash ( resolved )
36
+ }
37
+
38
+ function patternToIdFilter (
39
+ pattern : string | RegExp ,
40
+ cwd : string ,
41
+ ) : PluginFilter {
31
42
if ( pattern instanceof RegExp ) {
32
43
return ( id : string ) => {
33
44
const normalizedId = slash ( id )
@@ -37,10 +48,10 @@ function patternToIdFilter(pattern: string | RegExp): PluginFilter {
37
48
}
38
49
}
39
50
40
- const cwd = process . cwd ( )
41
- const matcher = picomatch ( pattern , { dot : true } )
51
+ const glob = getMatcherString ( pattern , cwd )
52
+ const matcher = picomatch ( glob , { dot : true } )
42
53
return ( id : string ) => {
43
- const normalizedId = slash ( path . relative ( cwd , id ) )
54
+ const normalizedId = slash ( id )
44
55
return matcher ( normalizedId )
45
56
}
46
57
}
@@ -94,11 +105,12 @@ function normalizeFilter(filter: StringFilter): NormalizedStringFilter {
94
105
95
106
export function createIdFilter (
96
107
filter : StringFilter | undefined ,
108
+ cwd = process . cwd ( ) ,
97
109
) : PluginFilterWithFallback | undefined {
98
110
if ( ! filter ) return
99
111
const { exclude, include } = normalizeFilter ( filter )
100
- const excludeFilter = exclude ?. map ( patternToIdFilter )
101
- const includeFilter = include ?. map ( patternToIdFilter )
112
+ const excludeFilter = exclude ?. map ( ( p ) => patternToIdFilter ( p , cwd ) )
113
+ const includeFilter = include ?. map ( ( p ) => patternToIdFilter ( p , cwd ) )
102
114
return createFilter ( excludeFilter , includeFilter )
103
115
}
104
116
@@ -115,9 +127,10 @@ export function createCodeFilter(
115
127
export function createFilterForTransform (
116
128
idFilter : StringFilter | undefined ,
117
129
codeFilter : StringFilter | undefined ,
130
+ cwd ?: string ,
118
131
) : TransformHookFilter | undefined {
119
132
if ( ! idFilter && ! codeFilter ) return
120
- const idFilterFn = createIdFilter ( idFilter )
133
+ const idFilterFn = createIdFilter ( idFilter , cwd )
121
134
const codeFilterFn = createCodeFilter ( codeFilter )
122
135
return ( id , code ) => {
123
136
let fallback = true
0 commit comments