-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
44 lines (41 loc) · 1.62 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
37
38
39
40
41
42
43
44
const { sync: glob } = require("fast-glob");
const path = require("path");
const fs = require("fs");
const normalizePath = process.platform === "win32" ? require("normalize-path") : (x) => x;
function stripJsonComments(data) {
return data.replace(/\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g, (m, g) => (g ? "" : m));
}
module.exports = (relativeTsconfigPath = "./tsconfig.json") => {
const absTsconfigPath = path.resolve(process.cwd(), relativeTsconfigPath);
let tsconfigData = fs.readFileSync(absTsconfigPath, "utf8");
tsconfigData = stripJsonComments(tsconfigData);
const { compilerOptions } = JSON.parse(tsconfigData);
const pathKeys = Object.keys(compilerOptions.paths);
const re = new RegExp(`^(${pathKeys.join("|")})`);
return {
name: "esbuild-ts-paths",
setup(build) {
build.onResolve({ filter: re }, (args) => {
const pathKey = pathKeys.find((pkey) => new RegExp(`^${pkey}`).test(args.path));
const [pathDir] = pathKey.split("*");
let file = args.path.replace(pathDir, "");
if (file === args.path) {
// if importing from root of alias
file = "";
}
for (const dir of compilerOptions.paths[pathKey]) {
const fileDir = normalizePath(path.resolve(process.cwd(), dir).replace("*", file));
let [matchedFile] = glob(`${fileDir}.*`);
if (!matchedFile) {
const [matchIndexFile] = glob(`${fileDir}/index.*`);
matchedFile = matchIndexFile;
}
if (matchedFile) {
return { path: matchedFile };
}
}
return { path: args.path };
});
},
};
};