From 87891d5795bcf8b8a1631d7c4bed4e713f9575f8 Mon Sep 17 00:00:00 2001 From: _Kerman Date: Tue, 24 Sep 2024 03:15:57 +0800 Subject: [PATCH] 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 --- packages/pluginutils/src/createFilter.ts | 5 ++++- packages/pluginutils/src/normalizePath.ts | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/pluginutils/src/createFilter.ts b/packages/pluginutils/src/createFilter.ts index e8d8899fe..9f2f093f6 100755 --- a/packages/pluginutils/src/createFilter.ts +++ b/packages/pluginutils/src/createFilter.ts @@ -43,9 +43,12 @@ const createFilter: CreateFilter = function createFilter(include?, exclude?, opt const includeMatchers = ensureArray(include).map(getMatcher); const excludeMatchers = ensureArray(exclude).map(getMatcher); + if (!includeMatchers.length && !excludeMatchers.length) + return (id) => typeof id === 'string' && !id.includes('\0'); + return function result(id: string | unknown): boolean { if (typeof id !== 'string') return false; - if (/\0/.test(id)) return false; + if (id.includes('\0')) return false; const pathId = normalizePath(id); diff --git a/packages/pluginutils/src/normalizePath.ts b/packages/pluginutils/src/normalizePath.ts index fa3516f86..cc2ab2e99 100644 --- a/packages/pluginutils/src/normalizePath.ts +++ b/packages/pluginutils/src/normalizePath.ts @@ -2,8 +2,10 @@ import { win32, posix } from 'path'; import type { NormalizePath } from '../types'; +const normalizePathRegExp = new RegExp(`\\${win32.sep}`, 'g'); + const normalizePath: NormalizePath = function normalizePath(filename: string) { - return filename.split(win32.sep).join(posix.sep); + return filename.replace(normalizePathRegExp, posix.sep); }; export { normalizePath as default };