From fd6de45d4d421a23e6633dc29b361686beb7e478 Mon Sep 17 00:00:00 2001 From: eight04 Date: Thu, 20 Aug 2020 21:40:52 +0800 Subject: [PATCH 1/4] fix(pluginutils): normalize pattern sep --- packages/pluginutils/test/createFilter.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/pluginutils/test/createFilter.ts b/packages/pluginutils/test/createFilter.ts index 7a68ca999..a8b964393 100755 --- a/packages/pluginutils/test/createFilter.ts +++ b/packages/pluginutils/test/createFilter.ts @@ -1,9 +1,14 @@ -import { resolve } from 'path'; +import { resolve as rawResolve, sep } from 'path'; import test from 'ava'; import { createFilter } from '../'; +const resolve = (...parts: string[]) => + rawResolve(...parts) + .split(sep) + .join('/'); + test.beforeEach(() => process.chdir(__dirname)); test('includes by default', (t) => { From 5a27dfcd87f5bba036e54725d8ab57a9b9d6b507 Mon Sep 17 00:00:00 2001 From: eight04 Date: Thu, 10 Sep 2020 17:30:48 +0800 Subject: [PATCH 2/4] fix: missing quotes --- packages/pluginutils/README.md | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/pluginutils/README.md b/packages/pluginutils/README.md index 7475be7af..a47b93736 100755 --- a/packages/pluginutils/README.md +++ b/packages/pluginutils/README.md @@ -50,9 +50,9 @@ export default function myPlugin(options = {}) { return { resolveId(code, id) { // only adds an extension if there isn't one already - id = addExtension(id); // `foo` -> `foo.js`, `foo.js -> foo.js` - id = addExtension(id, '.myext'); // `foo` -> `foo.myext`, `foo.js -> `foo.js` - } + id = addExtension(id); // `foo` -> `foo.js`, `foo.js` -> `foo.js` + id = addExtension(id, '.myext'); // `foo` -> `foo.myext`, `foo.js` -> `foo.js` + }, }; } ``` @@ -88,9 +88,9 @@ export default function myPlugin(options = {}) { }, leave(node) { if (node.scope) scope = scope.parent; - } + }, }); - } + }, }; } ``` @@ -126,7 +126,7 @@ import { createFilter } from '@rollup/pluginutils'; export default function myPlugin(options = {}) { // assume that the myPlugin accepts options of `options.include` and `options.exclude` var filter = createFilter(options.include, options.exclude, { - resolve: '/my/base/dir' + resolve: '/my/base/dir', }); return { @@ -134,7 +134,7 @@ export default function myPlugin(options = {}) { if (!filter(id)) return; // proceed with the transformation... - } + }, }; } ``` @@ -160,14 +160,14 @@ import { dataToEsm } from '@rollup/pluginutils'; const esModuleSource = dataToEsm( { custom: 'data', - to: ['treeshake'] + to: ['treeshake'], }, { compact: false, indent: '\t', preferConst: false, objectShorthand: false, - namedExports: true + namedExports: true, } ); /* @@ -207,11 +207,11 @@ export default function myPlugin(options = {}) { if (node.type === 'VariableDeclarator') { const declaredNames = extractAssignedNames(node.id); // do something with the declared names - // e.g. for `const {x, y: z} = ... => declaredNames = ['x', 'z'] + // e.g. for `const {x, y: z} = ...` => declaredNames = ['x', 'z'] } - } + }, }); - } + }, }; } ``` From 792bd35d8fa55c8cd2432391bf8f7aede4d12eb7 Mon Sep 17 00:00:00 2001 From: eight04 Date: Thu, 10 Sep 2020 17:37:51 +0800 Subject: [PATCH 3/4] doc: normalizePath --- packages/pluginutils/README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/packages/pluginutils/README.md b/packages/pluginutils/README.md index a47b93736..f02dda33a 100755 --- a/packages/pluginutils/README.md +++ b/packages/pluginutils/README.md @@ -232,6 +232,22 @@ makeLegalIdentifier('foo-bar'); // 'foo_bar' makeLegalIdentifier('typeof'); // '_typeof' ``` +### normalizePath + +Converts path separators to forward slash. + +Parameters: `(filename: String)`
+Returns: `String` + +#### Usage + +```js +import { normalizePath } from '@rollup/pluginutils'; + +normalizePath('foo\\bar'); // 'foo/bar' +normalizePath('foo/bar'); // 'foo/bar' +``` + ## Meta [CONTRIBUTING](/.github/CONTRIBUTING.md) From 090e98caca7976dbdd2f793b47b5f4bf2f98703a Mon Sep 17 00:00:00 2001 From: eight04 Date: Thu, 10 Sep 2020 18:04:20 +0800 Subject: [PATCH 4/4] feat: normalizePath --- packages/pluginutils/src/createFilter.ts | 9 ++++----- packages/pluginutils/src/index.ts | 7 +++++-- packages/pluginutils/src/normalizePath.ts | 9 +++++++++ packages/pluginutils/test/createFilter.ts | 9 +++------ packages/pluginutils/test/normalizePath.ts | 17 +++++++++++++++++ packages/pluginutils/types/index.d.ts | 7 +++++++ 6 files changed, 45 insertions(+), 13 deletions(-) create mode 100644 packages/pluginutils/src/normalizePath.ts create mode 100644 packages/pluginutils/test/normalizePath.ts diff --git a/packages/pluginutils/src/createFilter.ts b/packages/pluginutils/src/createFilter.ts index 09ca831d2..67d65fce6 100755 --- a/packages/pluginutils/src/createFilter.ts +++ b/packages/pluginutils/src/createFilter.ts @@ -1,10 +1,11 @@ -import { resolve, sep, posix, isAbsolute } from 'path'; +import { resolve, posix, isAbsolute } from 'path'; import pm from 'picomatch'; import { CreateFilter } from '../types'; import ensureArray from './utils/ensureArray'; +import normalizePath from './normalizePath'; function getMatcherString(id: string, resolutionBase: string | false | null | undefined) { if (resolutionBase === false || isAbsolute(id) || id.startsWith('*')) { @@ -12,9 +13,7 @@ function getMatcherString(id: string, resolutionBase: string | false | null | un } // resolve('') is valid and will default to process.cwd() - const basePath = resolve(resolutionBase || '') - .split(sep) - .join('/') + const basePath = normalizePath(resolve(resolutionBase || '')) // escape all possible (posix + win) path characters that might interfere with regex .replace(/[-^$*+?.()|[\]{}]/g, '\\$&'); // Note that we use posix.join because: @@ -48,7 +47,7 @@ const createFilter: CreateFilter = function createFilter(include?, exclude?, opt if (typeof id !== 'string') return false; if (/\0/.test(id)) return false; - const pathId = id.split(sep).join('/'); + const pathId = normalizePath(id); for (let i = 0; i < excludeMatchers.length; ++i) { const matcher = excludeMatchers[i]; diff --git a/packages/pluginutils/src/index.ts b/packages/pluginutils/src/index.ts index ac39803fe..61df2bcf9 100644 --- a/packages/pluginutils/src/index.ts +++ b/packages/pluginutils/src/index.ts @@ -4,6 +4,7 @@ import createFilter from './createFilter'; import dataToEsm from './dataToEsm'; import extractAssignedNames from './extractAssignedNames'; import makeLegalIdentifier from './makeLegalIdentifier'; +import normalizePath from './normalizePath'; export { addExtension, @@ -11,7 +12,8 @@ export { createFilter, dataToEsm, extractAssignedNames, - makeLegalIdentifier + makeLegalIdentifier, + normalizePath }; // TODO: remove this in next major @@ -21,5 +23,6 @@ export default { createFilter, dataToEsm, extractAssignedNames, - makeLegalIdentifier + makeLegalIdentifier, + normalizePath }; diff --git a/packages/pluginutils/src/normalizePath.ts b/packages/pluginutils/src/normalizePath.ts new file mode 100644 index 000000000..0b0b7f6a7 --- /dev/null +++ b/packages/pluginutils/src/normalizePath.ts @@ -0,0 +1,9 @@ +import { win32, posix } from 'path'; + +import { NormalizePath } from '../types'; + +const normalizePath: NormalizePath = function(filename: string) { + return filename.split(win32.sep).join(posix.sep); +}; + +export { normalizePath as default }; diff --git a/packages/pluginutils/test/createFilter.ts b/packages/pluginutils/test/createFilter.ts index a8b964393..ac878e9b3 100755 --- a/packages/pluginutils/test/createFilter.ts +++ b/packages/pluginutils/test/createFilter.ts @@ -1,13 +1,10 @@ -import { resolve as rawResolve, sep } from 'path'; +import { resolve as rawResolve } from 'path'; import test from 'ava'; -import { createFilter } from '../'; +import { createFilter, normalizePath } from '../'; -const resolve = (...parts: string[]) => - rawResolve(...parts) - .split(sep) - .join('/'); +const resolve = (...parts: string[]) => normalizePath(rawResolve(...parts)); test.beforeEach(() => process.chdir(__dirname)); diff --git a/packages/pluginutils/test/normalizePath.ts b/packages/pluginutils/test/normalizePath.ts new file mode 100644 index 000000000..06c9914c5 --- /dev/null +++ b/packages/pluginutils/test/normalizePath.ts @@ -0,0 +1,17 @@ +import test from 'ava'; + +import { normalizePath } from '../'; + +test('replaces \\ with /', (t) => { + t.is(normalizePath('foo\\bar'), 'foo/bar'); + t.is(normalizePath('foo\\bar\\baz'), 'foo/bar/baz'); +}); + +test('ignores forward slash', (t) => { + t.is(normalizePath('foo/bar'), 'foo/bar'); + t.is(normalizePath('foo/bar\\baz'), 'foo/bar/baz'); +}); + +test('handles empty string', (t) => { + t.is(normalizePath(''), ''); +}); diff --git a/packages/pluginutils/types/index.d.ts b/packages/pluginutils/types/index.d.ts index 33d40e509..39f7ab87a 100755 --- a/packages/pluginutils/types/index.d.ts +++ b/packages/pluginutils/types/index.d.ts @@ -68,11 +68,17 @@ export function extractAssignedNames(param: BaseNode): string[]; */ export function makeLegalIdentifier(str: string): string; +/** + * Converts path separators to forward slash. + */ +export function normalizePath(filename: string): string; + export type AddExtension = typeof addExtension; export type AttachScopes = typeof attachScopes; export type CreateFilter = typeof createFilter; export type ExtractAssignedNames = typeof extractAssignedNames; export type MakeLegalIdentifier = typeof makeLegalIdentifier; +export type NormalizePath = typeof normalizePath; export type DataToEsm = typeof dataToEsm; declare const defaultExport: { @@ -82,5 +88,6 @@ declare const defaultExport: { dataToEsm: DataToEsm; extractAssignedNames: ExtractAssignedNames; makeLegalIdentifier: MakeLegalIdentifier; + normalizePath: NormalizePath; }; export default defaultExport;