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

Commit c7770dd

Browse files
authored
Merge pull request #22 from mhhegazy/fix-windows-resolved-alias
Fix aliasing with path.resolve() issue on windows
2 parents 4b8a087 + 2bec324 commit c7770dd

File tree

2 files changed

+61
-18
lines changed

2 files changed

+61
-18
lines changed

src/index.js

+17-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { posix as path } from 'path';
1+
import path, { posix } 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)) {
71-
const directory = path.dirname(importerId);
71+
const directory = posix.dirname(importerId);
7272

7373
// Resolve file names
74-
const filePath = path.resolve(directory, updatedId);
75-
const match = resolve.map(ext => `${filePath}${ext}`)
74+
const filePath = posix.resolve(directory, updatedId);
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 resolved path or
90+
// rollup on windows will throw:
91+
// [TypeError: Cannot read property 'specifier' of undefined]
92+
if (VOLUME.test(entry)) {
93+
return path.resolve(updatedId);
8994
}
9095

9196
return updatedId;

test/index.js

+44-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import test from 'ava';
2-
import { posix as path } from 'path';
2+
import path, { posix } from 'path';
33
import { rollup } from 'rollup';
44
import alias from '../dist/rollup-plugin-alias';
55
import slash from 'slash';
66

7-
const DIRNAME = slash(__dirname.replace(/^([A-Z]:)/, ''));
7+
const normalizePath = (pathToNormalize) => slash(pathToNormalize.replace(/^([A-Z]:)/, ''));
8+
const DIRNAME = normalizePath(__dirname);
89

910
test(t => {
1011
t.is(typeof alias, 'function');
@@ -103,9 +104,9 @@ test('Test for the resolve property', t => {
103104
resolve: ['.js', '.jsx'],
104105
});
105106

106-
const resolved = result.resolveId('ember', path.resolve(DIRNAME, './files/index.js'));
107+
const resolved = result.resolveId('ember', posix.resolve(DIRNAME, './files/index.js'));
107108

108-
t.is(resolved, path.resolve(DIRNAME, './files/folder/hipster.jsx'));
109+
t.is(resolved, posix.resolve(DIRNAME, './files/folder/hipster.jsx'));
109110
});
110111

111112
test(t => {
@@ -123,9 +124,46 @@ test(t => {
123124
resolve: './i/am/a/local/file',
124125
});
125126

126-
const resolved = result.resolveId('resolve', path.resolve(DIRNAME, './files/index.js'));
127+
const resolved = result.resolveId('resolve', posix.resolve(DIRNAME, './files/index.js'));
127128

128-
t.is(resolved, path.resolve(DIRNAME, './files/i/am/a/local/file.js'));
129+
t.is(resolved, posix.resolve(DIRNAME, './files/i/am/a/local/file.js'));
130+
});
131+
132+
// this test with old behaviour will fail on windows and pass on Uinx-Like platforms
133+
test('Platform path.resolve(\'file-without-extension\') aliasing', t => {
134+
// this what used in React and Vue
135+
const result = alias({
136+
test: path.resolve('./files/aliasMe'),
137+
});
138+
139+
const resolved = result.resolveId('test', posix.resolve(DIRNAME, './files/index.js'));
140+
141+
t.is(resolved, path.resolve('./files/aliasMe.js'));
142+
});
143+
144+
// this test with old behaviour will fail on windows and Uinx-Like platforms
145+
test('Windows absolute path aliasing', t => {
146+
const result = alias({
147+
resolve: 'E:\\react\\node_modules\\fbjs\\lib\\warning',
148+
});
149+
150+
const resolved = result.resolveId('resolve', posix.resolve(DIRNAME, './files/index.js'));
151+
152+
t.is(
153+
normalizePath(resolved),
154+
normalizePath('E:\\react\\node_modules\\fbjs\\lib\\warning.js')
155+
);
156+
});
157+
// test alaising with resolved paths
158+
test('Platform path.resolve(\'file-with.ext\') aliasing', t => {
159+
const result = alias({
160+
test: path.resolve('./files/folder/hipster.jsx'),
161+
resolve: ['.js', '.jsx'],
162+
});
163+
164+
const resolved = result.resolveId('test', posix.resolve(DIRNAME, './files/index.js'));
165+
166+
t.is(resolved, path.resolve('./files/folder/hipster.jsx'));
129167
});
130168

131169
// Tests in Rollup

0 commit comments

Comments
 (0)