-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaliasLoader.mjs
110 lines (79 loc) · 2.43 KB
/
aliasLoader.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
https://nodejs.org/api/esm.html#esm_loaders
Create custom ESM loader to resolve path aliases in `package.json`.
This should be used when a script is run with node but imports files
using path aliases.
How to use:
`node --no-warnings --loader {path_to_this_file} {path_to_target_script}`
`--no-warnings` is optional.
*/
// #region Imports
import path from "path"
import npmPackage from "./package.json" with { type: "json" }
// #endregion
// #region getAbsoluteAliases()
/*
Subroutine to get the absolute path of the file / directory
that the aliases in `package.json` point to.
- Get the current working directory.
- Get the string to prefix paths based on the platform
this process is is running on.
- Get all the aliases defined in `package.json`.
- Get the absolute path for each alias
- Return an object with each alias and absolute path
as key-value pairs.
*/
const getAbsoluteAliases = () => {
const base = process.cwd()
const platformPrefix = process.platform === "win32" ? "file://" : ""
const aliases = npmPackage.aliases || {}
const absoluteAliases = Object.entries(aliases).reduce((aliasesObject, [alias, path_]) => {
return {
...aliasesObject,
[alias]:
// If alias is set to an absolute path
path_ === '/' ?
path_ :
path.join(platformPrefix, base, path_)
}
}, {})
return absoluteAliases
}
// #endregion
// #region pathStartsWithAlias()
/*
Subroutine to check if a path starts with an alias.
*/
const pathStartsWithAlias = (path, alias) => {
return (
path.indexOf(alias) === 0 &&
// Make sure "$alias" doesn't match "$alias2"
(
// "$alias" or "$alias/..."
path.length === alias.length ||
path[alias.length] === '/'
)
)
}
// #endregion
// #region Export ESM Loader
/*
- Get the absolute aliases.
- Get the alias that a path starts with
*/
// Absolute aliases
const aliases = getAbsoluteAliases()
// Runs on every file import
export const resolve = (specifier, parentModuleURL, defaultResolve) => {
const alias = Object.keys(aliases).find((key) => { return pathStartsWithAlias(specifier, key) })
const newSpecifier = alias === undefined ?
// If path starts with no alias, use original path
specifier :
/*
Otherwise join the absolute path for the matching
alias with the rest of the path.
*/
path.join(aliases[alias], specifier.substr(alias.length))
return defaultResolve(newSpecifier, parentModuleURL)
}
// #endregion