From 638f40057d7132cded2530dbca200b03cb7619d6 Mon Sep 17 00:00:00 2001 From: "Mohamed H. Hegazy" Date: Mon, 15 May 2017 08:00:51 +0200 Subject: [PATCH 1/4] Add test cases for aliasing with path.resolve() added 3 test cases: - alias with path.resolve() with file without extention - alias with windows absolute path - alias with path.resolve() with file with extention --- test/index.js | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/test/index.js b/test/index.js index 01cecb9..f7c5d94 100644 --- a/test/index.js +++ b/test/index.js @@ -1,10 +1,11 @@ import test from 'ava'; -import { posix as path } from 'path'; +import paltformPath, { posix as path } 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'); @@ -118,6 +119,43 @@ test(t => { t.is(resolved, path.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: paltformPath.resolve('./files/aliasMe'), + }); + + const resolved = result.resolveId('test', path.resolve(DIRNAME, './files/index.js')); + + t.is(resolved, paltformPath.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', path.resolve(DIRNAME, './files/index.js')); + + t.is( + normalizePath(resolved), + normalizePath('E:\\react\\node_modules\\fbjs\\lib\\warning.js') + ); +}); + +test('Platform path.resolve(\'file-with.ext\') aliasing', t => { + const result = alias({ + test: paltformPath.resolve('./files/folder/hipster.jsx'), + resolve: ['.js', '.jsx'], + }); + + const resolved = result.resolveId('test', path.resolve(DIRNAME, './files/index.js')); + + t.is(resolved, paltformPath.resolve('./files/folder/hipster.jsx')); +}); + // Tests in Rollup test(t => rollup({ From f752133bf07077a5b91d9715646bbcefb2dc2675 Mon Sep 17 00:00:00 2001 From: "Mohamed H. Hegazy" Date: Mon, 15 May 2017 08:07:29 +0200 Subject: [PATCH 2/4] 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 --- src/index.js | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/index.js b/src/index.js index d789813..25917d4 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ -import { posix as path } from 'path'; +import platformPath, { posix as path } from 'path'; import { platform } from 'os'; import fs from 'fs'; @@ -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, '')); } @@ -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); // Resolve file names const filePath = path.resolve(directory, updatedId); - const match = resolve.map(ext => `${filePath}${ext}`) + 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 platform + // resolved path or rollup on windows will throw: + // [TypeError: Cannot read property 'specifier' of undefined] + if (VOLUME.test(entry)) { + return platformPath.resolve(updatedId); } return updatedId; From e423fe64250d804f1acaa0d04de01cdd01a3af88 Mon Sep 17 00:00:00 2001 From: "Mohamed H. Hegazy" Date: Tue, 23 May 2017 04:05:22 +0200 Subject: [PATCH 3/4] update import names for path and poisx make it more clear --- src/index.js | 12 ++++++------ test/index.js | 24 ++++++++++++------------ 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/index.js b/src/index.js index 25917d4..6bdad9e 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ -import platformPath, { posix as path } from 'path'; +import path, { posix } from 'path'; import { platform } from 'os'; import fs from 'fs'; @@ -68,10 +68,10 @@ export default function alias(options = {}) { 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 filePath = posix.resolve(directory, updatedId); const match = resolve.map(ext => (endsWith(ext, filePath) ? filePath : `${filePath}${ext}`)) .find(exists); @@ -86,11 +86,11 @@ export default function alias(options = {}) { } } - // if alias is windows absoulate path return platform - // resolved path or rollup on windows will throw: + // 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 platformPath.resolve(updatedId); + return path.resolve(updatedId); } return updatedId; diff --git a/test/index.js b/test/index.js index f7c5d94..4524a0a 100644 --- a/test/index.js +++ b/test/index.js @@ -1,5 +1,5 @@ import test from 'ava'; -import paltformPath, { 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'; @@ -94,9 +94,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 => { @@ -114,21 +114,21 @@ 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: paltformPath.resolve('./files/aliasMe'), + test: path.resolve('./files/aliasMe'), }); - const resolved = result.resolveId('test', path.resolve(DIRNAME, './files/index.js')); + const resolved = result.resolveId('test', posix.resolve(DIRNAME, './files/index.js')); - t.is(resolved, paltformPath.resolve('./files/aliasMe.js')); + t.is(resolved, path.resolve('./files/aliasMe.js')); }); // this test with old behaviour will fail on windows and Uinx-Like platforms @@ -137,7 +137,7 @@ test('Windows absolute path aliasing', t => { resolve: 'E:\\react\\node_modules\\fbjs\\lib\\warning', }); - const resolved = result.resolveId('resolve', path.resolve(DIRNAME, './files/index.js')); + const resolved = result.resolveId('resolve', posix.resolve(DIRNAME, './files/index.js')); t.is( normalizePath(resolved), @@ -147,13 +147,13 @@ test('Windows absolute path aliasing', t => { test('Platform path.resolve(\'file-with.ext\') aliasing', t => { const result = alias({ - test: paltformPath.resolve('./files/folder/hipster.jsx'), + test: path.resolve('./files/folder/hipster.jsx'), resolve: ['.js', '.jsx'], }); - const resolved = result.resolveId('test', path.resolve(DIRNAME, './files/index.js')); + const resolved = result.resolveId('test', posix.resolve(DIRNAME, './files/index.js')); - t.is(resolved, paltformPath.resolve('./files/folder/hipster.jsx')); + t.is(resolved, path.resolve('./files/folder/hipster.jsx')); }); // Tests in Rollup From 8a194dcb90b6527c775af2571d7c15db4d03b1a7 Mon Sep 17 00:00:00 2001 From: "Mohamed H. Hegazy" Date: Sat, 30 Sep 2017 03:31:54 +0200 Subject: [PATCH 4/4] add comment to test --- test/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/index.js b/test/index.js index 4524a0a..b5bd883 100644 --- a/test/index.js +++ b/test/index.js @@ -144,7 +144,7 @@ test('Windows absolute path aliasing', t => { 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'),