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

Commit ad7220b

Browse files
committed
fix: use path.posix instead of path module
1 parent c2559ab commit ad7220b

File tree

4 files changed

+56
-27
lines changed

4 files changed

+56
-27
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: 37 additions & 9 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) => {
@@ -15,7 +21,7 @@ const matches = (key, importee) => {
1521
return importeeStartsWithKey && importeeHasSlashAfterKey;
1622
};
1723
const endsWith = (needle, haystack) => haystack.slice(-needle.length) === needle;
18-
const isFilePath = id => new RegExp(`^\\.?\\${path.sep}`).test(id);
24+
const isFilePath = id => /^\.?\//.test(id);
1925
const exists = uri => {
2026
try {
2127
return fs.statSync(uri).isFile();
@@ -24,6 +30,24 @@ const exists = uri => {
2430
}
2531
};
2632

33+
const getVolume = id => {
34+
let volume = '';
35+
36+
if (IS_WINDOWS && typeof id === 'string') {
37+
[volume] = id.match(VOLUME) || [''];
38+
}
39+
40+
return volume;
41+
};
42+
43+
const normalizeId = id => {
44+
if (IS_WINDOWS && typeof id === 'string') {
45+
return slash(id.replace(VOLUME, ''));
46+
}
47+
48+
return id;
49+
};
50+
2751
export default function alias(options = {}) {
2852
const hasResolve = Array.isArray(options.resolve);
2953
const resolve = hasResolve ? options.resolve : ['.js'];
@@ -39,39 +63,43 @@ export default function alias(options = {}) {
3963

4064
return {
4165
resolveId(importee, importer) {
66+
const volume = getVolume(importee) || getVolume(importer);
67+
const importeeId = normalizeId(importee);
68+
const importerId = normalizeId(importer);
69+
4270
// First match is supposed to be the correct one
43-
const toReplace = aliasKeys.find(key => matches(key, importee));
71+
const toReplace = aliasKeys.find(key => matches(key, importeeId));
4472

4573
if (!toReplace) {
4674
return null;
4775
}
4876

4977
const entry = options[toReplace];
5078

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

5381
if (isFilePath(updatedId)) {
54-
const directory = path.dirname(importer);
82+
const directory = path.dirname(importerId);
5583

5684
// Resolve file names
5785
const filePath = path.resolve(directory, updatedId);
5886
const match = resolve.map(ext => `${filePath}${ext}`)
5987
.find(exists);
6088

6189
if (match) {
62-
return match;
90+
return path.join(volume, match);
6391
}
6492

6593
// To keep the previous behaviour we simply return the file path
6694
// with extension
6795
if (endsWith('.js', filePath)) {
68-
return filePath;
96+
return path.join(volume, filePath);
6997
}
7098

71-
return filePath + '.js';
99+
return path.join(volume, filePath + '.js');
72100
}
73101

74-
return updatedId;
102+
return path.join(volume, updatedId);
75103
},
76104
};
77105
}

test/index.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import test from 'ava';
22
import path from 'path';
33
import { rollup } from 'rollup';
44
import alias from '../dist/rollup-plugin-alias';
5+
import slash from 'slash';
56

67
test(t => {
78
t.is(typeof alias, 'function');
@@ -90,9 +91,12 @@ test('Test for the resolve property', t => {
9091
resolve: ['.js', '.jsx'],
9192
});
9293

93-
const resolved = result.resolveId('ember', path.resolve(__dirname, './files/index.js'));
94+
const resolved = result.resolveId(
95+
'ember',
96+
slash(path.resolve(__dirname, './files/index.js'))
97+
);
9498

95-
t.is(resolved, path.resolve(__dirname, './files/folder/hipster.jsx'));
99+
t.is(resolved, slash(path.resolve(__dirname, './files/folder/hipster.jsx')));
96100
});
97101

98102
test(t => {
@@ -110,9 +114,15 @@ test(t => {
110114
resolve: './i/am/a/local/file',
111115
});
112116

113-
const resolved = result.resolveId('resolve', path.resolve(__dirname, './files/index.js'));
117+
const resolved = result.resolveId(
118+
'resolve',
119+
slash(path.resolve(__dirname, './files/index.js'))
120+
);
114121

115-
t.is(resolved, path.resolve(__dirname, './files/i/am/a/local/file.js'));
122+
t.is(
123+
resolved,
124+
slash(path.resolve(__dirname, './files/i/am/a/local/file.js'))
125+
);
116126
});
117127

118128
// 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)