This repository was archived by the owner on May 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader-esm.mjs
More file actions
62 lines (59 loc) · 2.03 KB
/
loader-esm.mjs
File metadata and controls
62 lines (59 loc) · 2.03 KB
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
import { fileURLToPath, pathToFileURL } from 'url';
import fs from 'fs';
import path from 'path';
/**
* Resolve the entry point for a package in ts_modules.
* First, it checks for a package.json with a "ts:main" field.
* If not found, it falls back to the "main" field.
* Otherwise, it tries index.ts or index.js.
*/
function resolveEntry(tsModulePath) {
const pkgJsonPath = path.join(tsModulePath, 'package.json');
if (fs.existsSync(pkgJsonPath)) {
try {
const pkg = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf8'));
if (pkg['ts:main']) {
return path.join(tsModulePath, pkg['ts:main']);
}
if (pkg.main) {
return path.join(tsModulePath, pkg.main);
}
} catch (err) {
// If package.json can't be parsed, we continue to fallback.
console.error(`Error parsing package.json in ${tsModulePath}: ${err.message}`);
}
}
// Fallback: try index.ts then index.js
const indexTs = path.join(tsModulePath, 'index.ts');
if (fs.existsSync(indexTs)) {
return indexTs;
}
const indexJs = path.join(tsModulePath, 'index.js');
if (fs.existsSync(indexJs)) {
return indexJs;
}
throw new Error(`Cannot resolve entry point for module at ${tsModulePath}`);
}
export async function resolve(specifier, context, defaultResolve) {
// Only intercept bare specifiers.
if (
!specifier.startsWith('.') &&
!specifier.startsWith('/') &&
!/^[A-Za-z]:\\/.test(specifier)
) {
const projectRoot = process.cwd();
const tsModulePath = path.join(projectRoot, 'ts_modules', specifier);
if (fs.existsSync(tsModulePath)) {
let resolvedPath = tsModulePath;
const stats = fs.statSync(tsModulePath);
if (stats.isDirectory()) {
// Resolve the directory to its actual entry file.
resolvedPath = resolveEntry(tsModulePath);
}
const resolvedUrl = pathToFileURL(resolvedPath).href;
return { url: resolvedUrl, shortCircuit: true };
}
}
// Fall back to default resolution.
return defaultResolve(specifier, context, defaultResolve);
}