Skip to content

Commit

Permalink
feat(pluginutils): normalizePath (#550)
Browse files Browse the repository at this point in the history
* fix(pluginutils): normalize pattern sep

* fix: missing quotes

* doc: normalizePath

* feat: normalizePath
  • Loading branch information
eight04 committed Sep 10, 2020
1 parent 7941389 commit 6cd15b9
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 21 deletions.
40 changes: 28 additions & 12 deletions packages/pluginutils/README.md
Expand Up @@ -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`
},
};
}
```
Expand Down Expand Up @@ -88,9 +88,9 @@ export default function myPlugin(options = {}) {
},
leave(node) {
if (node.scope) scope = scope.parent;
}
},
});
}
},
};
}
```
Expand Down Expand Up @@ -126,15 +126,15 @@ 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 {
transform(code, id) {
if (!filter(id)) return;

// proceed with the transformation...
}
},
};
}
```
Expand All @@ -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,
}
);
/*
Expand Down Expand Up @@ -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']
}
}
},
});
}
},
};
}
```
Expand All @@ -232,6 +232,22 @@ makeLegalIdentifier('foo-bar'); // 'foo_bar'
makeLegalIdentifier('typeof'); // '_typeof'
```

### normalizePath

Converts path separators to forward slash.

Parameters: `(filename: String)`<br>
Returns: `String`

#### Usage

```js
import { normalizePath } from '@rollup/pluginutils';

normalizePath('foo\\bar'); // 'foo/bar'
normalizePath('foo/bar'); // 'foo/bar'
```

## Meta

[CONTRIBUTING](/.github/CONTRIBUTING.md)
Expand Down
9 changes: 4 additions & 5 deletions packages/pluginutils/src/createFilter.ts
@@ -1,20 +1,19 @@
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('*')) {
return id;
}

// 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:
Expand Down Expand Up @@ -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];
Expand Down
7 changes: 5 additions & 2 deletions packages/pluginutils/src/index.ts
Expand Up @@ -4,14 +4,16 @@ import createFilter from './createFilter';
import dataToEsm from './dataToEsm';
import extractAssignedNames from './extractAssignedNames';
import makeLegalIdentifier from './makeLegalIdentifier';
import normalizePath from './normalizePath';

export {
addExtension,
attachScopes,
createFilter,
dataToEsm,
extractAssignedNames,
makeLegalIdentifier
makeLegalIdentifier,
normalizePath
};

// TODO: remove this in next major
Expand All @@ -21,5 +23,6 @@ export default {
createFilter,
dataToEsm,
extractAssignedNames,
makeLegalIdentifier
makeLegalIdentifier,
normalizePath
};
9 changes: 9 additions & 0 deletions 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 };
6 changes: 4 additions & 2 deletions packages/pluginutils/test/createFilter.ts
@@ -1,8 +1,10 @@
import { resolve } from 'path';
import { resolve as rawResolve } from 'path';

import test from 'ava';

import { createFilter } from '../';
import { createFilter, normalizePath } from '../';

const resolve = (...parts: string[]) => normalizePath(rawResolve(...parts));

test.beforeEach(() => process.chdir(__dirname));

Expand Down
17 changes: 17 additions & 0 deletions 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(''), '');
});
7 changes: 7 additions & 0 deletions packages/pluginutils/types/index.d.ts
Expand Up @@ -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: {
Expand All @@ -82,5 +88,6 @@ declare const defaultExport: {
dataToEsm: DataToEsm;
extractAssignedNames: ExtractAssignedNames;
makeLegalIdentifier: MakeLegalIdentifier;
normalizePath: NormalizePath;
};
export default defaultExport;

0 comments on commit 6cd15b9

Please sign in to comment.