From 1ae2ca1ca5c370d292336b18c29c2ef4e30b9bbb Mon Sep 17 00:00:00 2001 From: xjchengo Date: Tue, 2 May 2017 18:00:20 +0800 Subject: [PATCH 1/5] Adjust test for Windows --- test/index.js | 91 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 54 insertions(+), 37 deletions(-) diff --git a/test/index.js b/test/index.js index 01cecb9..2dd8c4c 100644 --- a/test/index.js +++ b/test/index.js @@ -1,10 +1,10 @@ import test from 'ava'; -import { posix as path } from 'path'; +import 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 normalizeId = id => slash(id); test(t => { t.is(typeof alias, 'function'); @@ -29,9 +29,9 @@ test('Simple aliasing', t => { './local': 'global', }); - const resolved = result.resolveId('foo', '/src/importer.js'); - const resolved2 = result.resolveId('pony', '/src/importer.js'); - const resolved3 = result.resolveId('./local', '/src/importer.js'); + const resolved = result.resolveId('foo', path.resolve('/src/importer.js')); + const resolved2 = result.resolveId('pony', path.resolve('/src/importer.js')); + const resolved3 = result.resolveId('./local', path.resolve('/src/importer.js')); t.is(resolved, 'bar'); t.is(resolved2, 'paradise'); @@ -44,47 +44,64 @@ test('Will not confuse modules with similar names', t => { './foo': 'bar', }); - const resolved = result.resolveId('foo2', '/src/importer.js'); - const resolved2 = result.resolveId('./fooze/bar', '/src/importer.js'); - const resolved3 = result.resolveId('./someFile.foo', '/src/importer.js'); + const resolved = result.resolveId('foo2', path.resolve('/src/importer.js')); + const resolved2 = result.resolveId('./fooze/bar', path.resolve('/src/importer.js')); + const resolved3 = result.resolveId('./someFile.foo', path.resolve('/src/importer.js')); t.is(resolved, null); t.is(resolved2, null); t.is(resolved3, null); }); -test('Local aliasing', t => { +test('Local aliasing with POSIX path', t => { const result = alias({ foo: './bar', pony: './par/a/di/se', }); - const resolved = result.resolveId('foo', '/src/importer.js'); - const resolved2 = result.resolveId('foo/baz', '/src/importer.js'); - const resolved3 = result.resolveId('foo/baz.js', '/src/importer.js'); - const resolved4 = result.resolveId('pony', '/src/highly/nested/importer.js'); + const resolved = result.resolveId('foo', path.resolve('/src/importer.js')); + const resolved2 = result.resolveId('foo/baz', path.resolve('/src/importer.js')); + const resolved3 = result.resolveId('foo/baz.js', path.resolve('/src/importer.js')); + const resolved4 = result.resolveId('pony', path.resolve('/src/highly/nested/importer.js')); - t.is(resolved, '/src/bar.js'); - t.is(resolved2, '/src/bar/baz.js'); - t.is(resolved3, '/src/bar/baz.js'); - t.is(resolved4, '/src/highly/nested/par/a/di/se.js'); + t.is(resolved, normalizeId(path.resolve('/src/bar.js'))); + t.is(resolved2, normalizeId(path.resolve('/src/bar/baz.js'))); + t.is(resolved3, normalizeId(path.resolve('/src/bar/baz.js'))); + t.is(resolved4, normalizeId(path.resolve('/src/highly/nested/par/a/di/se.js'))); +}); + +test('Local aliasing with Windows path', t => { + const result = alias({ + foo: '.\\bar', + pony: '.\\par\\a\\di\\se', + }); + + const resolved = result.resolveId('foo', path.resolve('/src/importer.js')); + const resolved2 = result.resolveId('foo/baz', path.resolve('/src/importer.js')); + const resolved3 = result.resolveId('foo/baz.js', path.resolve('/src/importer.js')); + const resolved4 = result.resolveId('pony', path.resolve('/src/highly/nested/importer.js')); + + t.is(resolved, normalizeId(path.resolve('/src/bar.js'))); + t.is(resolved2, normalizeId(path.resolve('/src/bar/baz.js'))); + t.is(resolved3, normalizeId(path.resolve('/src/bar/baz.js'))); + t.is(resolved4, normalizeId(path.resolve('/src/highly/nested/par/a/di/se.js'))); }); test('Absolute local aliasing', t => { const result = alias({ - foo: '/bar', - pony: '/par/a/di/se.js', + foo: path.resolve('/bar'), + pony: path.resolve('/par/a/di/se.js'), }); - const resolved = result.resolveId('foo', '/src/importer.js'); - const resolved2 = result.resolveId('foo/baz', '/src/importer.js'); - const resolved3 = result.resolveId('foo/baz.js', '/src/importer.js'); - const resolved4 = result.resolveId('pony', '/src/highly/nested/importer.js'); + const resolved = result.resolveId('foo', path.resolve('/src/importer.js')); + const resolved2 = result.resolveId('foo/baz', path.resolve('/src/importer.js')); + const resolved3 = result.resolveId('foo/baz.js', path.resolve('/src/importer.js')); + const resolved4 = result.resolveId('pony', path.resolve('/src/highly/nested/importer.js')); - t.is(resolved, '/bar.js'); - t.is(resolved2, '/bar/baz.js'); - t.is(resolved3, '/bar/baz.js'); - t.is(resolved4, '/par/a/di/se.js'); + t.is(resolved, normalizeId(path.resolve('/bar.js'))); + t.is(resolved2, normalizeId(path.resolve('/bar/baz.js'))); + t.is(resolved3, normalizeId(path.resolve('/bar/baz.js'))); + t.is(resolved4, normalizeId(path.resolve('/par/a/di/se.js'))); }); test('Test for the resolve property', t => { @@ -93,9 +110,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', path.resolve(__dirname, './files/index.js')); - t.is(resolved, path.resolve(DIRNAME, './files/folder/hipster.jsx')); + t.is(resolved, normalizeId(path.resolve(__dirname, './files/folder/hipster.jsx'))); }); test(t => { @@ -103,7 +120,7 @@ test(t => { resolve: 'i/am/a/file', }); - const resolved = result.resolveId('resolve', '/src/import.js'); + const resolved = result.resolveId('resolve', path.resolve('/src/import.js')); t.is(resolved, 'i/am/a/file'); }); @@ -113,9 +130,9 @@ test(t => { resolve: './i/am/a/local/file', }); - const resolved = result.resolveId('resolve', path.resolve(DIRNAME, './files/index.js')); + const resolved = result.resolveId('resolve', path.resolve(__dirname, './files/index.js')); - t.is(resolved, path.resolve(DIRNAME, './files/i/am/a/local/file.js')); + t.is(resolved, normalizeId(path.resolve(__dirname, './files/i/am/a/local/file.js'))); }); // Tests in Rollup @@ -129,11 +146,11 @@ test(t => './numberFolder': './folder', })], }).then(stats => { - t.is(stats.modules[0].id.endsWith('/files/nonAliased.js'), true); - t.is(stats.modules[1].id.endsWith('/files/aliasMe.js'), true); - t.is(stats.modules[2].id.endsWith('/files/localAliasMe.js'), true); - t.is(stats.modules[3].id.endsWith('/files/folder/anotherNumber.js'), true); - t.is(stats.modules[4].id.endsWith('/files/index.js'), true); + t.is(stats.modules[0].id, normalizeId(path.resolve('./files/nonAliased.js'))); + t.is(stats.modules[1].id, normalizeId(path.resolve('./files/aliasMe.js'))); + t.is(stats.modules[2].id, normalizeId(path.resolve('./files/localAliasMe.js'))); + t.is(stats.modules[3].id, normalizeId(path.resolve('./files/folder/anotherNumber.js'))); + t.is(stats.modules[4].id, normalizeId(path.resolve('./files/index.js'))); t.is(stats.modules.length, 5); }) ); From e89e607d8bade3cec6981d0407c8e13bff3cab42 Mon Sep 17 00:00:00 2001 From: xjchengo Date: Tue, 2 May 2017 18:01:25 +0800 Subject: [PATCH 2/5] Fix path issue on Windows --- src/index.js | 41 ++++++++++++++--------------------------- 1 file changed, 14 insertions(+), 27 deletions(-) diff --git a/src/index.js b/src/index.js index d789813..d61538f 100644 --- a/src/index.js +++ b/src/index.js @@ -1,12 +1,7 @@ -import { posix as path } from 'path'; -import { platform } from 'os'; +import path from 'path'; import fs from 'fs'; - import slash from 'slash'; -const VOLUME = /^([A-Z]:)/; -const IS_WINDOWS = platform() === 'win32'; - // Helper functions const noop = () => null; const matches = (key, importee) => { @@ -21,7 +16,8 @@ const matches = (key, importee) => { return importeeStartsWithKey && importeeHasSlashAfterKey; }; const endsWith = (needle, haystack) => haystack.slice(-needle.length) === needle; -const isFilePath = id => /^\.?\//.test(id); +const isAbsolutePath = id => /^(?:\/|(?:[A-Za-z]:)?[\\\/])/.test(id); +const isRelativePath = id => /^\.?\.[\\\/]/.test(id); const exists = uri => { try { return fs.statSync(uri).isFile(); @@ -29,14 +25,7 @@ const exists = uri => { return false; } }; - -const normalizeId = id => { - if (IS_WINDOWS && typeof id === 'string') { - return slash(id.replace(VOLUME, '')); - } - - return id; -}; +const normalizeId = id => slash(id); export default function alias(options = {}) { const hasResolve = Array.isArray(options.resolve); @@ -53,8 +42,8 @@ export default function alias(options = {}) { return { resolveId(importee, importer) { - const importeeId = normalizeId(importee); - const importerId = normalizeId(importer); + const importeeId = importee; + const importerId = importer; // First match is supposed to be the correct one const toReplace = aliasKeys.find(key => matches(key, importeeId)); @@ -67,25 +56,23 @@ export default function alias(options = {}) { const updatedId = importeeId.replace(toReplace, entry); - if (isFilePath(updatedId)) { + if (isRelativePath(updatedId) || isAbsolutePath(updatedId)) { const directory = path.dirname(importerId); // Resolve file names - const filePath = path.resolve(directory, updatedId); + let filePath = path.resolve(directory, updatedId); const match = resolve.map(ext => `${filePath}${ext}`) .find(exists); if (match) { - return match; - } - - // To keep the previous behaviour we simply return the file path - // with extension - if (endsWith('.js', filePath)) { - return filePath; + filePath = match; + } else if (!endsWith('.js', filePath)) { + // To keep the previous behaviour we simply return the file path + // with extension + filePath = filePath + '.js'; } - return filePath + '.js'; + return normalizeId(filePath); } return updatedId; From 7a63bea52a72a4f4e61b2e576658613f59e34983 Mon Sep 17 00:00:00 2001 From: xjchengo Date: Tue, 2 May 2017 18:45:16 +0800 Subject: [PATCH 3/5] Normalize importee and importer --- src/index.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index d61538f..e313a5f 100644 --- a/src/index.js +++ b/src/index.js @@ -25,7 +25,13 @@ const exists = uri => { return false; } }; -const normalizeId = id => slash(id); +const normalizeId = id => { + if (typeof id === 'string') { + return slash(id); + } + + return id; +}; export default function alias(options = {}) { const hasResolve = Array.isArray(options.resolve); @@ -42,8 +48,8 @@ export default function alias(options = {}) { return { resolveId(importee, importer) { - const importeeId = importee; - const importerId = importer; + const importeeId = normalizeId(importee); + const importerId = normalizeId(importer); // First match is supposed to be the correct one const toReplace = aliasKeys.find(key => matches(key, importeeId)); From 42e11727764d6ba0c2895452a4df82376f07909f Mon Sep 17 00:00:00 2001 From: xjchengo Date: Tue, 2 May 2017 19:08:51 +0800 Subject: [PATCH 4/5] Normalize entry --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index e313a5f..3185319 100644 --- a/src/index.js +++ b/src/index.js @@ -58,7 +58,7 @@ export default function alias(options = {}) { return null; } - const entry = options[toReplace]; + const entry = normalizeId(options[toReplace]); const updatedId = importeeId.replace(toReplace, entry); From 01973bbfc420faf7db12906d76fa4d071e28d255 Mon Sep 17 00:00:00 2001 From: xjchengo Date: Tue, 2 May 2017 19:55:53 +0800 Subject: [PATCH 5/5] Deal with alias in rollup entry --- src/index.js | 14 +++++++++++--- test/index.js | 26 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 3185319..596fa59 100644 --- a/src/index.js +++ b/src/index.js @@ -63,10 +63,18 @@ export default function alias(options = {}) { const updatedId = importeeId.replace(toReplace, entry); if (isRelativePath(updatedId) || isAbsolutePath(updatedId)) { - const directory = path.dirname(importerId); + let filePath; + if (isRelativePath(updatedId) && importer === undefined) { + // Cannot handle this, so return null + return null; + } else if (isRelativePath(updatedId) && importer !== undefined) { + // Resolve file names + filePath = path.resolve(path.dirname(importerId), updatedId); + } else { + // Absolute path doesn't need resolve + filePath = updatedId; + } - // Resolve file names - let filePath = path.resolve(directory, updatedId); const match = resolve.map(ext => `${filePath}${ext}`) .find(exists); diff --git a/test/index.js b/test/index.js index 2dd8c4c..94b0dc5 100644 --- a/test/index.js +++ b/test/index.js @@ -154,3 +154,29 @@ test(t => t.is(stats.modules.length, 5); }) ); + +test('Relative local aliasing in rollup entry', t => + rollup({ + entry: 'src/aliasMe.js', + plugins: [alias({ + src: './files', + })], + }).then(() => { + t.fail(); + }).catch(() => { + t.pass(); + }) +); + +test('Absolute local aliasing in rollup entry', t => + rollup({ + entry: 'src/aliasMe.js', + plugins: [alias({ + src: path.resolve('./files'), + })], + }).then(() => { + t.pass(); + }).catch(() => { + t.fail(); + }) +);