From 17dbb7e16821979342a358c9aa237e0b443b452e Mon Sep 17 00:00:00 2001 From: Roman Vyakhirev Date: Tue, 12 Apr 2022 14:19:00 +0200 Subject: [PATCH] fix(pluginutils): fix path normalisation for createFilter on absolute and * paths --- packages/pluginutils/package.json | 2 +- packages/pluginutils/src/createFilter.ts | 4 ++-- packages/pluginutils/test/createFilter.ts | 28 +++++++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/packages/pluginutils/package.json b/packages/pluginutils/package.json index 907eda03b..6fc5b0d31 100644 --- a/packages/pluginutils/package.json +++ b/packages/pluginutils/package.json @@ -1,6 +1,6 @@ { "name": "@rollup/pluginutils", - "version": "4.2.0", + "version": "4.3.0", "publishConfig": { "access": "public" }, diff --git a/packages/pluginutils/src/createFilter.ts b/packages/pluginutils/src/createFilter.ts index 67d65fce6..f8e7a5657 100755 --- a/packages/pluginutils/src/createFilter.ts +++ b/packages/pluginutils/src/createFilter.ts @@ -9,7 +9,7 @@ import normalizePath from './normalizePath'; function getMatcherString(id: string, resolutionBase: string | false | null | undefined) { if (resolutionBase === false || isAbsolute(id) || id.startsWith('*')) { - return id; + return normalizePath(id); } // resolve('') is valid and will default to process.cwd() @@ -20,7 +20,7 @@ function getMatcherString(id: string, resolutionBase: string | false | null | un // 1. the basePath has been normalized to use / // 2. the incoming glob (id) matcher, also uses / // otherwise Node will force backslash (\) on windows - return posix.join(basePath, id); + return posix.join(basePath, normalizePath(id)); } const createFilter: CreateFilter = function createFilter(include?, exclude?, options?) { diff --git a/packages/pluginutils/test/createFilter.ts b/packages/pluginutils/test/createFilter.ts index ac878e9b3..49062a5ce 100755 --- a/packages/pluginutils/test/createFilter.ts +++ b/packages/pluginutils/test/createFilter.ts @@ -134,6 +134,34 @@ test('does not add current working directory when pattern is an absolute path', t.falsy(filter(resolve('..', 'c'))); }); +test('normalizes path when pattern is an absolute path', (t) => { + const filterPosix = createFilter([`${resolve('.')}/*`]); + const filterWin = createFilter([`${resolve('.')}\\*`]); + + t.truthy(filterPosix(resolve('a'))); + t.truthy(filterWin(resolve('a'))); +}); + +test('normalizes path when pattern starts with *', (t) => { + const filterPosix = createFilter([`**/a`]); + const filterWin = createFilter([`**\\a`]); + + t.truthy(filterPosix(resolve('a'))); + t.truthy(filterWin(resolve('a'))); +}); + +test('normalizes path when pattern has resolution base', (t) => { + const filterPosix = createFilter([`test/*`], [], { + resolve: __dirname + }); + const filterWin = createFilter([`test\\*`], [], { + resolve: __dirname + }); + + t.truthy(filterPosix(resolve('test/a'))); + t.truthy(filterWin(resolve('test/a'))); +}); + test('does not add current working directory when pattern starts with a glob', (t) => { const filter = createFilter(['**/*']); t.truthy(filter(resolve('a')));