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

Commit

Permalink
Don't confuse modules with similar names (#9)
Browse files Browse the repository at this point in the history
* Don't confuse modules with similar names

* More bugfixes for similar module names

* Even more improvements for similar module names
  • Loading branch information
szwacz authored and frostney committed Mar 7, 2017
1 parent 7e33d7c commit 38e8d3a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
14 changes: 12 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@ import fs from 'fs';

// Helper functions
const noop = () => null;
const startsWith = (needle, haystack) => ! haystack.indexOf(needle);
const matches = (key, importee) => {
if (importee.length < key.length) {
return false;
}
if (importee === key) {
return true;
}
const importeeStartsWithKey = (importee.indexOf(key) === 0);
const importeeHasSlashAfterKey = (importee.substring(key.length)[0] === '/');
return importeeStartsWithKey && importeeHasSlashAfterKey;
};
const endsWith = (needle, haystack) => haystack.slice(-needle.length) === needle;
const isFilePath = id => /^\.?\//.test(id);
const exists = uri => {
Expand All @@ -30,7 +40,7 @@ export default function alias(options = {}) {
return {
resolveId(importee, importer) {
// First match is supposed to be the correct one
const toReplace = aliasKeys.find(key => startsWith(key, importee));
const toReplace = aliasKeys.find(key => matches(key, importee));

if (!toReplace) {
return null;
Expand Down
30 changes: 22 additions & 8 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,38 @@ test(t => {
t.is(typeof result.resolveId, 'function');
});

// Simple aliasing
test(t => {
test('Simple aliasing', t => {
const result = alias({
foo: 'bar',
pony: 'paradise',
'./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');

t.is(resolved, 'bar');
t.is(resolved2, 'paradise');
t.is(resolved3, 'global');
});

// Local aliasing
test(t => {
test('Will not confuse modules with similar names', t => {
const result = alias({
foo: 'bar',
'./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');

t.is(resolved, null);
t.is(resolved2, null);
t.is(resolved3, null);
});

test('Local aliasing', t => {
const result = alias({
foo: './bar',
pony: './par/a/di/se',
Expand All @@ -51,8 +67,7 @@ test(t => {
t.is(resolved4, '/src/highly/nested/par/a/di/se.js');
});

// Absolute local aliasing
test(t => {
test('Absolute local aliasing', t => {
const result = alias({
foo: '/bar',
pony: '/par/a/di/se.js',
Expand All @@ -69,8 +84,7 @@ test(t => {
t.is(resolved4, '/par/a/di/se.js');
});

// Test for the resolve property
test(t => {
test('Test for the resolve property', t => {
const result = alias({
ember: './folder/hipster',
resolve: ['.js', '.jsx'],
Expand Down

0 comments on commit 38e8d3a

Please sign in to comment.