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

Commit 38e8d3a

Browse files
szwaczfrostney
authored andcommitted
Don't confuse modules with similar names (#9)
* Don't confuse modules with similar names * More bugfixes for similar module names * Even more improvements for similar module names
1 parent 7e33d7c commit 38e8d3a

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

src/index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,17 @@ import fs from 'fs';
33

44
// Helper functions
55
const noop = () => null;
6-
const startsWith = (needle, haystack) => ! haystack.indexOf(needle);
6+
const matches = (key, importee) => {
7+
if (importee.length < key.length) {
8+
return false;
9+
}
10+
if (importee === key) {
11+
return true;
12+
}
13+
const importeeStartsWithKey = (importee.indexOf(key) === 0);
14+
const importeeHasSlashAfterKey = (importee.substring(key.length)[0] === '/');
15+
return importeeStartsWithKey && importeeHasSlashAfterKey;
16+
};
717
const endsWith = (needle, haystack) => haystack.slice(-needle.length) === needle;
818
const isFilePath = id => /^\.?\//.test(id);
919
const exists = uri => {
@@ -30,7 +40,7 @@ export default function alias(options = {}) {
3040
return {
3141
resolveId(importee, importer) {
3242
// First match is supposed to be the correct one
33-
const toReplace = aliasKeys.find(key => startsWith(key, importee));
43+
const toReplace = aliasKeys.find(key => matches(key, importee));
3444

3545
if (!toReplace) {
3646
return null;

test/index.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,38 @@ test(t => {
1919
t.is(typeof result.resolveId, 'function');
2020
});
2121

22-
// Simple aliasing
23-
test(t => {
22+
test('Simple aliasing', t => {
2423
const result = alias({
2524
foo: 'bar',
2625
pony: 'paradise',
26+
'./local': 'global',
2727
});
2828

2929
const resolved = result.resolveId('foo', '/src/importer.js');
3030
const resolved2 = result.resolveId('pony', '/src/importer.js');
31+
const resolved3 = result.resolveId('./local', '/src/importer.js');
3132

3233
t.is(resolved, 'bar');
3334
t.is(resolved2, 'paradise');
35+
t.is(resolved3, 'global');
3436
});
3537

36-
// Local aliasing
37-
test(t => {
38+
test('Will not confuse modules with similar names', t => {
39+
const result = alias({
40+
foo: 'bar',
41+
'./foo': 'bar',
42+
});
43+
44+
const resolved = result.resolveId('foo2', '/src/importer.js');
45+
const resolved2 = result.resolveId('./fooze/bar', '/src/importer.js');
46+
const resolved3 = result.resolveId('./someFile.foo', '/src/importer.js');
47+
48+
t.is(resolved, null);
49+
t.is(resolved2, null);
50+
t.is(resolved3, null);
51+
});
52+
53+
test('Local aliasing', t => {
3854
const result = alias({
3955
foo: './bar',
4056
pony: './par/a/di/se',
@@ -51,8 +67,7 @@ test(t => {
5167
t.is(resolved4, '/src/highly/nested/par/a/di/se.js');
5268
});
5369

54-
// Absolute local aliasing
55-
test(t => {
70+
test('Absolute local aliasing', t => {
5671
const result = alias({
5772
foo: '/bar',
5873
pony: '/par/a/di/se.js',
@@ -69,8 +84,7 @@ test(t => {
6984
t.is(resolved4, '/par/a/di/se.js');
7085
});
7186

72-
// Test for the resolve property
73-
test(t => {
87+
test('Test for the resolve property', t => {
7488
const result = alias({
7589
ember: './folder/hipster',
7690
resolve: ['.js', '.jsx'],

0 commit comments

Comments
 (0)