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

Commit

Permalink
Merge pull request #22 from mhhegazy/fix-windows-resolved-alias
Browse files Browse the repository at this point in the history
Fix aliasing with path.resolve() issue on windows
  • Loading branch information
Rich-Harris authored Oct 19, 2017
2 parents 4b8a087 + 2bec324 commit c7770dd
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 18 deletions.
29 changes: 17 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { posix as path } from 'path';
import path, { posix } from 'path';
import { platform } from 'os';
import fs from 'fs';

Expand Down Expand Up @@ -31,7 +31,7 @@ const exists = uri => {
};

const normalizeId = id => {
if (IS_WINDOWS && typeof id === 'string') {
if ((IS_WINDOWS && typeof id === 'string') || VOLUME.test(id)) {
return slash(id.replace(VOLUME, ''));
}

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

const entry = options[toReplace];

const updatedId = importeeId.replace(toReplace, entry);
let updatedId = normalizeId(importeeId.replace(toReplace, entry));

if (isFilePath(updatedId)) {
const directory = path.dirname(importerId);
const directory = posix.dirname(importerId);

// Resolve file names
const filePath = path.resolve(directory, updatedId);
const match = resolve.map(ext => `${filePath}${ext}`)
const filePath = posix.resolve(directory, updatedId);
const match = resolve.map(ext => (endsWith(ext, filePath) ? filePath : `${filePath}${ext}`))
.find(exists);

if (match) {
return match;
}

updatedId = match;
// To keep the previous behaviour we simply return the file path
// with extension
if (endsWith('.js', filePath)) {
return filePath;
} else if (endsWith('.js', filePath)) {
updatedId = filePath;
} else {
updatedId = filePath + '.js';
}
}

return filePath + '.js';
// if alias is windows absoulate path return resolved path or
// rollup on windows will throw:
// [TypeError: Cannot read property 'specifier' of undefined]
if (VOLUME.test(entry)) {
return path.resolve(updatedId);
}

return updatedId;
Expand Down
50 changes: 44 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import test from 'ava';
import { posix as path } from 'path';
import path, { posix } from 'path';
import { rollup } from 'rollup';
import alias from '../dist/rollup-plugin-alias';
import slash from 'slash';

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

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

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

t.is(resolved, path.resolve(DIRNAME, './files/folder/hipster.jsx'));
t.is(resolved, posix.resolve(DIRNAME, './files/folder/hipster.jsx'));
});

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

const resolved = result.resolveId('resolve', path.resolve(DIRNAME, './files/index.js'));
const resolved = result.resolveId('resolve', posix.resolve(DIRNAME, './files/index.js'));

t.is(resolved, path.resolve(DIRNAME, './files/i/am/a/local/file.js'));
t.is(resolved, posix.resolve(DIRNAME, './files/i/am/a/local/file.js'));
});

// this test with old behaviour will fail on windows and pass on Uinx-Like platforms
test('Platform path.resolve(\'file-without-extension\') aliasing', t => {
// this what used in React and Vue
const result = alias({
test: path.resolve('./files/aliasMe'),
});

const resolved = result.resolveId('test', posix.resolve(DIRNAME, './files/index.js'));

t.is(resolved, path.resolve('./files/aliasMe.js'));
});

// this test with old behaviour will fail on windows and Uinx-Like platforms
test('Windows absolute path aliasing', t => {
const result = alias({
resolve: 'E:\\react\\node_modules\\fbjs\\lib\\warning',
});

const resolved = result.resolveId('resolve', posix.resolve(DIRNAME, './files/index.js'));

t.is(
normalizePath(resolved),
normalizePath('E:\\react\\node_modules\\fbjs\\lib\\warning.js')
);
});
// test alaising with resolved paths
test('Platform path.resolve(\'file-with.ext\') aliasing', t => {
const result = alias({
test: path.resolve('./files/folder/hipster.jsx'),
resolve: ['.js', '.jsx'],
});

const resolved = result.resolveId('test', posix.resolve(DIRNAME, './files/index.js'));

t.is(resolved, path.resolve('./files/folder/hipster.jsx'));
});

// Tests in Rollup
Expand Down

0 comments on commit c7770dd

Please sign in to comment.