Skip to content

Commit 2940474

Browse files
committed
Add support for partial aliases
1 parent b318193 commit 2940474

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

src/index.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,26 @@ export default function alias(options = {}) {
88
}
99

1010
const aliasKeys = Object.keys(options);
11-
const aliasIndex = aliasKeys.indexOf(importee);
11+
// TODO: We shouldn't have a case of double aliases. But may need to handle that better
12+
const filteredAlias = aliasKeys.filter(value => importee.indexOf(value) === 0)[0];
1213

13-
if (aliasIndex >= 0) {
14-
const entry = options[aliasKeys[aliasIndex]];
14+
if (!filteredAlias) {
15+
return null;
16+
}
17+
18+
const entry = options[filteredAlias];
1519

16-
if (entry.indexOf('./') === 0) {
17-
const basename = path.basename(importer);
18-
const directory = importer.split(basename)[0];
20+
const updatedId = importee.replace(filteredAlias, entry);
1921

20-
// TODO: Is there a way not to have the extension being defined explicitly?
21-
return path.resolve(directory, entry) + '.js';
22-
}
22+
if (updatedId.indexOf('./') === 0) {
23+
const basename = path.basename(importer);
24+
const directory = importer.split(basename)[0];
2325

24-
return entry;
26+
// TODO: Is there a way not to have the extension being defined explicitly?
27+
return path.resolve(directory, updatedId) + '.js';
2528
}
2629

27-
return null;
30+
return updatedId;
2831
},
2932
};
3033
}

test/files/folder/anotherNumber.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 33;

test/files/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import nonAliased from './nonAliased';
22
import fancyNumber from 'fancyNumber';
33
import anotherFancyNumber from './anotherFancyNumber';
4+
import anotherNumber from './numberFolder/anotherNumber';
5+
import moreNumbers from 'numberFolder/anotherNumber';
46

5-
export default fancyNumber + anotherFancyNumber + nonAliased;
7+
export default fancyNumber + anotherFancyNumber + nonAliased + anotherNumber + moreNumbers;

test/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ test(t =>
1111
plugins: [alias({
1212
fancyNumber: './aliasMe',
1313
'./anotherFancyNumber': './localAliasMe',
14+
numberFolder: './folder',
15+
'./numberFolder': './folder',
1416
})],
1517
}).then(stats => {
1618
t.is(path.basename(stats.modules[0].id), 'nonAliased.js');
1719
t.is(path.basename(stats.modules[1].id), 'aliasMe.js');
1820
t.is(path.basename(stats.modules[2].id), 'localAliasMe.js');
19-
t.is(path.basename(stats.modules[3].id), 'index.js');
20-
t.is(stats.modules.length, 4);
21+
t.is(path.basename(stats.modules[3].id), 'anotherNumber.js');
22+
t.is(path.basename(stats.modules[4].id), 'index.js');
23+
t.is(stats.modules.length, 5);
2124
})
2225
);

0 commit comments

Comments
 (0)