| 1 |
import mm from 'micromatch'; |
| 2 |
import { resolve, sep } from 'path'; |
| 3 |
import { CreateFilter } from './pluginutils'; |
| 4 |
import ensureArray from './utils/ensureArray'; |
| 5 |
|
| 6 |
function getMatcherString(id: string, resolutionBase: string | false | null | undefined) { |
| 7 |
if (resolutionBase === false) { |
| 8 |
return id; |
| 9 |
} |
| 10 |
return resolve(...(typeof resolutionBase === 'string' ? [resolutionBase, id] : [id])); |
| 11 |
} |
| 12 |
|
| 13 |
const createFilter: CreateFilter = function createFilter(include?, exclude?, options?) { |
| 14 |
const resolutionBase = options && options.resolve; |
| 15 |
|
| 16 |
const getMatcher = (id: string | RegExp) => { |
| 17 |
return id instanceof RegExp |
| 18 |
? id |
| 19 |
: { |
| 20 |
test: mm.matcher( |
| 21 |
getMatcherString(id, resolutionBase) |
| 22 |
.split(sep) |
| 23 |
.join('/'), |
| 24 |
{ dot: true } |
| 25 |
) |
| 26 |
}; |
| 27 |
}; |
| 28 |
|
| 29 |
const includeMatchers = ensureArray(include).map(getMatcher); |
| 30 |
const excludeMatchers = ensureArray(exclude).map(getMatcher); |
| 31 |
|
| 32 |
return function(id: string | any): boolean { |
| 33 |
if (typeof id !== 'string') return false; |
| 34 |
if (/\0/.test(id)) return false; |
| 35 |
|
| 36 |
id = id.split(sep).join('/'); |
| 37 |
|
| 38 |
for (let i = 0; i < excludeMatchers.length; ++i) { |
| 39 |
const matcher = excludeMatchers[i]; |
| 40 |
if (matcher.test(id)) return false; |
| 41 |
} |
| 42 |
|
| 43 |
for (let i = 0; i < includeMatchers.length; ++i) { |
| 44 |
const matcher = includeMatchers[i]; |
| 45 |
if (matcher.test(id)) return true; |
| 46 |
} |
| 47 |
|
| 48 |
return !includeMatchers.length; |
| 49 |
}; |
| 50 |
}; |
| 51 |
|
| 52 |
export { createFilter as default }; |