Skip to content

Commit

Permalink
perf: cache Minimatch instance
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Apr 11, 2024
1 parent c99f85e commit 3bab7e8
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion shared/configs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { minimatch } from 'minimatch'
import { Minimatch } from 'minimatch'
import type { FlatConfigItem, MatchedFile } from './types'

const minimatchOpts = { dot: true, matchBase: true }
const _matchInstances = new Map<string, Minimatch>()

function minimatch(file: string, pattern: string) {
let m = _matchInstances.get(pattern)
if (!m) {
m = new Minimatch(pattern, minimatchOpts)
_matchInstances.set(pattern, m)
}
return m.match(file)
}

export function getMatchedGlobs(file: string, glob: (string | string[])[]) {
const globs = (Array.isArray(glob) ? glob : [glob]).flat()
return globs.filter(glob => minimatch(file, glob)).flat()
Expand Down

0 comments on commit 3bab7e8

Please sign in to comment.