Skip to content

Commit 87891d5

Browse files
kermanxshellscape
andauthored
fix(pluginutils): optimize createFilter and normalizePath (#1750)
* perf(pluginutils): optimize `createFilter` and `normalizePath` * fix * fix * fix: use `win32.sep` and `posix.sep` --------- Co-authored-by: shellscape <[email protected]>
1 parent a8e326d commit 87891d5

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

packages/pluginutils/src/createFilter.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,12 @@ const createFilter: CreateFilter = function createFilter(include?, exclude?, opt
4343
const includeMatchers = ensureArray(include).map(getMatcher);
4444
const excludeMatchers = ensureArray(exclude).map(getMatcher);
4545

46+
if (!includeMatchers.length && !excludeMatchers.length)
47+
return (id) => typeof id === 'string' && !id.includes('\0');
48+
4649
return function result(id: string | unknown): boolean {
4750
if (typeof id !== 'string') return false;
48-
if (/\0/.test(id)) return false;
51+
if (id.includes('\0')) return false;
4952

5053
const pathId = normalizePath(id);
5154

packages/pluginutils/src/normalizePath.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import { win32, posix } from 'path';
22

33
import type { NormalizePath } from '../types';
44

5+
const normalizePathRegExp = new RegExp(`\\${win32.sep}`, 'g');
6+
57
const normalizePath: NormalizePath = function normalizePath(filename: string) {
6-
return filename.split(win32.sep).join(posix.sep);
8+
return filename.replace(normalizePathRegExp, posix.sep);
79
};
810

911
export { normalizePath as default };

0 commit comments

Comments
 (0)