-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.mjs
41 lines (29 loc) · 1.04 KB
/
loader.mjs
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
37
38
39
40
41
import path from 'node:path';
export default function loadAliases(aliasesToAdd) {
const getAliases = () => {
// eslint-disable-next-line no-undef
const base = process.cwd();
const absoluteAliases = Object.keys(aliasesToAdd).reduce((acc, key) =>
aliasesToAdd[key][0] === '/'
? acc
: { ...acc, [key]: path.join(base, aliasesToAdd[key]) },
aliasesToAdd)
return absoluteAliases;
}
const isAliasInSpecifier = (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);
}
}
export const resolve = loadAliases({
"@server": "./build",
"@config": "./build/config"
});