Skip to content
This repository was archived by the owner on Aug 4, 2021. It is now read-only.

Commit 5c6144b

Browse files
authored
Merge pull request #6 from zacharygolba/fix-windows-file-paths
fix: isFilePath not working on windows
2 parents 24a377d + 91e2581 commit 5c6144b

File tree

4 files changed

+34
-23
lines changed

4 files changed

+34
-23
lines changed

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
"nyc": "^5.6.0",
4444
"rimraf": "^2.5.2",
4545
"rollup": "^0.25.4",
46-
"rollup-babel-lib-bundler": "^2.2.4"
47-
}
46+
"rollup-babel-lib-bundler": "^2.2.4",
47+
"slash": "^1.0.0"
48+
},
49+
"dependencies": {}
4850
}

src/index.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
import path from 'path';
1+
import { posix as path } from 'path';
2+
import { platform } from 'os';
23
import fs from 'fs';
34

5+
import slash from 'slash';
6+
7+
const VOLUME = /^([A-Z]:)/;
8+
const IS_WINDOWS = platform() === 'win32';
9+
410
// Helper functions
511
const noop = () => null;
612
const matches = (key, importee) => {
@@ -24,6 +30,14 @@ const exists = uri => {
2430
}
2531
};
2632

33+
const normalizeId = id => {
34+
if (IS_WINDOWS && typeof id === 'string') {
35+
return slash(id.replace(VOLUME, ''));
36+
}
37+
38+
return id;
39+
};
40+
2741
export default function alias(options = {}) {
2842
const hasResolve = Array.isArray(options.resolve);
2943
const resolve = hasResolve ? options.resolve : ['.js'];
@@ -39,19 +53,22 @@ export default function alias(options = {}) {
3953

4054
return {
4155
resolveId(importee, importer) {
56+
const importeeId = normalizeId(importee);
57+
const importerId = normalizeId(importer);
58+
4259
// First match is supposed to be the correct one
43-
const toReplace = aliasKeys.find(key => matches(key, importee));
60+
const toReplace = aliasKeys.find(key => matches(key, importeeId));
4461

4562
if (!toReplace) {
4663
return null;
4764
}
4865

4966
const entry = options[toReplace];
5067

51-
const updatedId = importee.replace(toReplace, entry);
68+
const updatedId = importeeId.replace(toReplace, entry);
5269

5370
if (isFilePath(updatedId)) {
54-
const directory = path.dirname(importer);
71+
const directory = path.dirname(importerId);
5572

5673
// Resolve file names
5774
const filePath = path.resolve(directory, updatedId);

test/index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import test from 'ava';
2-
import path from 'path';
2+
import { posix as path } from 'path';
33
import { rollup } from 'rollup';
44
import alias from '../dist/rollup-plugin-alias';
5+
import slash from 'slash';
6+
7+
const DIRNAME = slash(__dirname.replace(/^([A-Z]:)/, ''));
58

69
test(t => {
710
t.is(typeof alias, 'function');
@@ -90,9 +93,9 @@ test('Test for the resolve property', t => {
9093
resolve: ['.js', '.jsx'],
9194
});
9295

93-
const resolved = result.resolveId('ember', path.resolve(__dirname, './files/index.js'));
96+
const resolved = result.resolveId('ember', path.resolve(DIRNAME, './files/index.js'));
9497

95-
t.is(resolved, path.resolve(__dirname, './files/folder/hipster.jsx'));
98+
t.is(resolved, path.resolve(DIRNAME, './files/folder/hipster.jsx'));
9699
});
97100

98101
test(t => {
@@ -110,9 +113,9 @@ test(t => {
110113
resolve: './i/am/a/local/file',
111114
});
112115

113-
const resolved = result.resolveId('resolve', path.resolve(__dirname, './files/index.js'));
116+
const resolved = result.resolveId('resolve', path.resolve(DIRNAME, './files/index.js'));
114117

115-
t.is(resolved, path.resolve(__dirname, './files/i/am/a/local/file.js'));
118+
t.is(resolved, path.resolve(DIRNAME, './files/i/am/a/local/file.js'));
116119
});
117120

118121
// Tests in Rollup

yarn.lock

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,10 +1466,6 @@ esprima@2.7.x, esprima@^2.6.0, esprima@^2.7.1:
14661466
version "2.7.3"
14671467
resolved "https://registry.yarnpkg.com/esprima/-/esprima-2.7.3.tgz#96e3b70d5779f6ad49cd032673d1c312767ba581"
14681468

1469-
esprima@^3.1.1:
1470-
version "3.1.3"
1471-
resolved "https://registry.yarnpkg.com/esprima/-/esprima-3.1.3.tgz#fdca51cee6133895e3c88d535ce49dbff62a4633"
1472-
14731469
espurify@^1.6.0:
14741470
version "1.7.0"
14751471
resolved "https://registry.yarnpkg.com/espurify/-/espurify-1.7.0.tgz#1c5cf6cbccc32e6f639380bd4f991fab9ba9d226"
@@ -2184,20 +2180,13 @@ js-tokens@^3.0.0:
21842180
version "3.0.1"
21852181
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.1.tgz#08e9f132484a2c45a30907e9dc4d5567b7f114d7"
21862182

2187-
js-yaml@3.6.1:
2183+
js-yaml@3.6.1, js-yaml@3.x, js-yaml@^3.5.1:
21882184
version "3.6.1"
21892185
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.6.1.tgz#6e5fe67d8b205ce4d22fad05b7781e8dadcc4b30"
21902186
dependencies:
21912187
argparse "^1.0.7"
21922188
esprima "^2.6.0"
21932189

2194-
js-yaml@3.x, js-yaml@^3.5.1:
2195-
version "3.8.2"
2196-
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.8.2.tgz#02d3e2c0f6beab20248d412c352203827d786721"
2197-
dependencies:
2198-
argparse "^1.0.7"
2199-
esprima "^3.1.1"
2200-
22012190
jsbn@~0.1.0:
22022191
version "0.1.1"
22032192
resolved "https://registry.yarnpkg.com/jsbn/-/jsbn-0.1.1.tgz#a5e654c2e5a2deb5f201d96cefbca80c0ef2f513"

0 commit comments

Comments
 (0)