Skip to content

Commit

Permalink
fix(pluginutils): optimize createFilter and normalizePath (#1750)
Browse files Browse the repository at this point in the history
* perf(pluginutils): optimize `createFilter` and `normalizePath`

* fix

* fix

* fix: use `win32.sep` and `posix.sep`

---------

Co-authored-by: shellscape <andrew@shellscape.org>
  • Loading branch information
KermanX and shellscape authored Sep 23, 2024
1 parent a8e326d commit 87891d5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 4 additions & 1 deletion packages/pluginutils/src/createFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 3 additions & 1 deletion packages/pluginutils/src/normalizePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

0 comments on commit 87891d5

Please sign in to comment.