Skip to content

Commit 77d3cd1

Browse files
authored
perf: precompile picomatch matchers once per context (#622)
1 parent 93dc8e6 commit 77d3cd1

2 files changed

Lines changed: 5 additions & 5 deletions

File tree

src/core/ctx.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import process from 'node:process'
66
import { isString, slash, throttle, toArray } from '@antfu/utils'
77
import { isPackageExists } from 'local-pkg'
88
import MagicString from 'magic-string'
9+
import pm from 'picomatch'
910
import { createUnimport, normalizeScanDirs, resolvePreset } from 'unimport'
1011
import { createFilter } from 'unplugin-utils'
1112
import { presets } from '../presets'
@@ -297,6 +298,8 @@ ${dts}`.trim()}\n`
297298
}))
298299
: []
299300

301+
const normalizedDirMatchers = normalizedDirPaths.map(dir => pm(dir.glob))
302+
300303
return {
301304
root,
302305
dirs,
@@ -309,7 +312,7 @@ ${dts}`.trim()}\n`
309312
generateESLint,
310313
unimport,
311314
configFilePaths,
312-
normalizedDirPaths,
315+
normalizedDirMatchers,
313316
}
314317
}
315318

src/core/unplugin.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { FilterPattern } from 'unplugin'
22
import type { Options } from '../types'
33
import { slash } from '@antfu/utils'
44
import { isPackageExists } from 'local-pkg'
5-
import pm from 'picomatch'
65
import { createUnplugin } from 'unplugin'
76
import { createContext, EXCLUDE_RE_LIST, INCLUDE_RE_LIST } from './ctx'
87

@@ -58,9 +57,7 @@ export default createUnplugin<Options>((options) => {
5857

5958
const normalizedFilePath = slash(file)
6059

61-
const shouldRescan = ctx.normalizedDirPaths.some(dirPath =>
62-
pm.isMatch(normalizedFilePath, dirPath.glob),
63-
)
60+
const shouldRescan = ctx.normalizedDirMatchers.some(match => match(normalizedFilePath))
6461

6562
if (shouldRescan)
6663
await ctx.scanDirs()

0 commit comments

Comments
 (0)