-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
30 lines (28 loc) · 979 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const path = require('path').posix;
function getAlias(moduleSpecifier, aliases) {
const longestMatchingKey = Object.keys(aliases).reduce((matchingKey, key) => {
if(moduleSpecifier.startsWith(key)) {
if(key.length > matchingKey.length) {
matchingKey = key;
}
}
return matchingKey;
}, '');
if(longestMatchingKey !== '') {
return path.resolve(moduleSpecifier.replace(longestMatchingKey, aliases[longestMatchingKey]));
}
}
exports['default'] = function aliasModuleSpecifiers(aliases) {
return {
name: 'alias-module-specifier',
resolveId(importee, importer) {
const currentDir = importer ? path.dirname(importer) : path.resolve();
const alias = getAlias(importee, aliases);
if(alias) {
return path.resolve(currentDir, alias);
}
return null;
}
};
};
module.exports = exports['default'];