-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
37 lines (25 loc) · 1.05 KB
/
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
31
32
33
34
35
36
import path from 'node:path';
export default function generateAliasesResolver(aliasesToAdd, options) {
const getAliases = () => {
const base = process.cwd();
const windowsSupportString = process.platform === 'win32' ? 'file://' : '';
const absoluteAliases = Object.keys(aliasesToAdd).reduce((acc, key) =>
aliasesToAdd[key][0] === '/'
? acc
: { ...acc, [key]: path.join(windowsSupportString, base, aliasesToAdd[key]) },
aliasesToAdd);
return absoluteAliases;
}
const isAliasInSpecifier = options?.matcher ?? ((path, alias) => {
return path.indexOf(alias) === 0
&& (path.length === alias.length || path[alias.length] === '/');
});
const aliases = getAliases();
return (specifier, parentModuleURL, defaultResolve) => {
const alias = Object.keys(aliases).find((key) => isAliasInSpecifier(specifier, key));
const newSpecifier = alias === undefined
? specifier
: path.join(aliases[alias], specifier.substr(alias.length));
return defaultResolve(newSpecifier, parentModuleURL);
};
}