From 6c5dc1aee7e2f379b16268a8b5ebef10f9654e37 Mon Sep 17 00:00:00 2001 From: _Kerman Date: Wed, 24 Jul 2024 17:56:36 +0800 Subject: [PATCH 1/4] perf(pluginutils): optimize `createFilter` and `normalizePath` --- packages/pluginutils/src/createFilter.ts | 8 ++++++-- packages/pluginutils/src/normalizePath.ts | 4 +--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/pluginutils/src/createFilter.ts b/packages/pluginutils/src/createFilter.ts index e8d8899fe..ff842eebc 100755 --- a/packages/pluginutils/src/createFilter.ts +++ b/packages/pluginutils/src/createFilter.ts @@ -23,6 +23,8 @@ function getMatcherString(id: string, resolutionBase: string | false | null | un return posix.join(basePath, normalizePath(id)); } +const ALWAYS_MATCH = () => true; + const createFilter: CreateFilter = function createFilter(include?, exclude?, options?) { const resolutionBase = options && options.resolve; @@ -43,9 +45,11 @@ const createFilter: CreateFilter = function createFilter(include?, exclude?, opt const includeMatchers = ensureArray(include).map(getMatcher); const excludeMatchers = ensureArray(exclude).map(getMatcher); + if (!includeMatchers.length) return ALWAYS_MATCH; + 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); @@ -59,7 +63,7 @@ const createFilter: CreateFilter = function createFilter(include?, exclude?, opt if (matcher.test(pathId)) return true; } - return !includeMatchers.length; + return false; }; }; diff --git a/packages/pluginutils/src/normalizePath.ts b/packages/pluginutils/src/normalizePath.ts index fa3516f86..8b7eb2fe9 100644 --- a/packages/pluginutils/src/normalizePath.ts +++ b/packages/pluginutils/src/normalizePath.ts @@ -1,9 +1,7 @@ -import { win32, posix } from 'path'; - import type { NormalizePath } from '../types'; const normalizePath: NormalizePath = function normalizePath(filename: string) { - return filename.split(win32.sep).join(posix.sep); + return filename.replace(/\\/g, '/'); }; export { normalizePath as default }; From 792f73c4ab24828dec936da3b882078430f34e1d Mon Sep 17 00:00:00 2001 From: _Kerman Date: Wed, 24 Jul 2024 18:28:45 +0800 Subject: [PATCH 2/4] fix --- packages/pluginutils/src/createFilter.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/pluginutils/src/createFilter.ts b/packages/pluginutils/src/createFilter.ts index ff842eebc..3b0673ab5 100755 --- a/packages/pluginutils/src/createFilter.ts +++ b/packages/pluginutils/src/createFilter.ts @@ -23,8 +23,6 @@ function getMatcherString(id: string, resolutionBase: string | false | null | un return posix.join(basePath, normalizePath(id)); } -const ALWAYS_MATCH = () => true; - const createFilter: CreateFilter = function createFilter(include?, exclude?, options?) { const resolutionBase = options && options.resolve; @@ -45,7 +43,7 @@ const createFilter: CreateFilter = function createFilter(include?, exclude?, opt const includeMatchers = ensureArray(include).map(getMatcher); const excludeMatchers = ensureArray(exclude).map(getMatcher); - if (!includeMatchers.length) return ALWAYS_MATCH; + if (!includeMatchers.length) return (id) => typeof id === 'string' && !id.includes('\0'); return function result(id: string | unknown): boolean { if (typeof id !== 'string') return false; From 0cb9b2cb65deeb8369a09e8ba375b1591008eaa8 Mon Sep 17 00:00:00 2001 From: _Kerman Date: Wed, 24 Jul 2024 18:29:39 +0800 Subject: [PATCH 3/4] fix --- packages/pluginutils/src/createFilter.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/pluginutils/src/createFilter.ts b/packages/pluginutils/src/createFilter.ts index 3b0673ab5..9f2f093f6 100755 --- a/packages/pluginutils/src/createFilter.ts +++ b/packages/pluginutils/src/createFilter.ts @@ -43,7 +43,8 @@ const createFilter: CreateFilter = function createFilter(include?, exclude?, opt const includeMatchers = ensureArray(include).map(getMatcher); const excludeMatchers = ensureArray(exclude).map(getMatcher); - if (!includeMatchers.length) return (id) => typeof id === 'string' && !id.includes('\0'); + 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; @@ -61,7 +62,7 @@ const createFilter: CreateFilter = function createFilter(include?, exclude?, opt if (matcher.test(pathId)) return true; } - return false; + return !includeMatchers.length; }; }; From 85384e8d00384252365c9e959b639eb8060bef98 Mon Sep 17 00:00:00 2001 From: _Kerman Date: Mon, 23 Sep 2024 09:47:53 +0800 Subject: [PATCH 4/4] fix: use `win32.sep` and `posix.sep` --- packages/pluginutils/src/normalizePath.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/pluginutils/src/normalizePath.ts b/packages/pluginutils/src/normalizePath.ts index 8b7eb2fe9..cc2ab2e99 100644 --- a/packages/pluginutils/src/normalizePath.ts +++ b/packages/pluginutils/src/normalizePath.ts @@ -1,7 +1,11 @@ +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.replace(/\\/g, '/'); + return filename.replace(normalizePathRegExp, posix.sep); }; export { normalizePath as default };