2022-05-14 07:10:20 +00:00
|
|
|
/**
|
|
|
|
* ts-node/esmローダーに投げる前にpath mappingを解決する
|
|
|
|
* 参考
|
|
|
|
* - https://github.com/TypeStrong/ts-node/discussions/1450#discussioncomment-1806115
|
|
|
|
* - https://nodejs.org/api/esm.html#loaders
|
|
|
|
* ※ https://github.com/TypeStrong/ts-node/pull/1585 が取り込まれたらこのカスタムローダーは必要なくなる
|
|
|
|
*/
|
2022-02-27 02:07:39 +00:00
|
|
|
|
2023-04-07 01:56:46 +00:00
|
|
|
import { resolve as resolveTs, load } from "ts-node/esm";
|
|
|
|
import { loadConfig, createMatchPath } from "tsconfig-paths";
|
|
|
|
import { pathToFileURL } from "url";
|
2022-02-27 02:07:39 +00:00
|
|
|
|
2022-05-14 07:10:20 +00:00
|
|
|
const tsconfig = loadConfig();
|
|
|
|
const matchPath = createMatchPath(tsconfig.absoluteBaseUrl, tsconfig.paths);
|
2022-02-27 02:07:39 +00:00
|
|
|
|
2022-05-14 07:10:20 +00:00
|
|
|
export function resolve(specifier, ctx, defaultResolve) {
|
|
|
|
let resolvedSpecifier;
|
2023-04-07 01:56:46 +00:00
|
|
|
if (specifier.endsWith(".js")) {
|
2022-05-14 07:10:20 +00:00
|
|
|
// maybe transpiled
|
2023-04-07 01:56:46 +00:00
|
|
|
const specifierWithoutExtension = specifier.substring(
|
|
|
|
0,
|
|
|
|
specifier.length - ".js".length,
|
|
|
|
);
|
2022-05-14 07:10:20 +00:00
|
|
|
const matchedSpecifier = matchPath(specifierWithoutExtension);
|
|
|
|
if (matchedSpecifier) {
|
|
|
|
resolvedSpecifier = pathToFileURL(`${matchedSpecifier}.js`).href;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const matchedSpecifier = matchPath(specifier);
|
|
|
|
if (matchedSpecifier) {
|
|
|
|
resolvedSpecifier = pathToFileURL(matchedSpecifier).href;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return resolveTs(resolvedSpecifier ?? specifier, ctx, defaultResolve);
|
2022-02-27 02:07:39 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 07:10:20 +00:00
|
|
|
export { load };
|