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

Commit f752133

Browse files
committed
Fix aliasing with path.resolve() issue on windows
passes test cases: - Platform path.resolve('file-without-extension') aliasing - Windows absolute path aliasing - Platform path.resolve('file-with.ext') aliasing
1 parent 638f400 commit f752133

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/index.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { posix as path } from 'path';
1+
import platformPath, { posix as path } from 'path';
22
import { platform } from 'os';
33
import fs from 'fs';
44

@@ -31,7 +31,7 @@ const exists = uri => {
3131
};
3232

3333
const normalizeId = id => {
34-
if (IS_WINDOWS && typeof id === 'string') {
34+
if ((IS_WINDOWS && typeof id === 'string') || VOLUME.test(id)) {
3535
return slash(id.replace(VOLUME, ''));
3636
}
3737

@@ -65,27 +65,32 @@ export default function alias(options = {}) {
6565

6666
const entry = options[toReplace];
6767

68-
const updatedId = importeeId.replace(toReplace, entry);
68+
let updatedId = normalizeId(importeeId.replace(toReplace, entry));
6969

7070
if (isFilePath(updatedId)) {
7171
const directory = path.dirname(importerId);
7272

7373
// Resolve file names
7474
const filePath = path.resolve(directory, updatedId);
75-
const match = resolve.map(ext => `${filePath}${ext}`)
75+
const match = resolve.map(ext => (endsWith(ext, filePath) ? filePath : `${filePath}${ext}`))
7676
.find(exists);
7777

7878
if (match) {
79-
return match;
80-
}
81-
79+
updatedId = match;
8280
// To keep the previous behaviour we simply return the file path
8381
// with extension
84-
if (endsWith('.js', filePath)) {
85-
return filePath;
82+
} else if (endsWith('.js', filePath)) {
83+
updatedId = filePath;
84+
} else {
85+
updatedId = filePath + '.js';
8686
}
87+
}
8788

88-
return filePath + '.js';
89+
// if alias is windows absoulate path return platform
90+
// resolved path or rollup on windows will throw:
91+
// [TypeError: Cannot read property 'specifier' of undefined]
92+
if (VOLUME.test(entry)) {
93+
return platformPath.resolve(updatedId);
8994
}
9095

9196
return updatedId;

0 commit comments

Comments
 (0)