Repository navigation
Expand file tree
/
Copy pathjiti.ts
More file actions
173 lines (159 loc) · 4.53 KB
/
Copy pathjiti.ts
File metadata and controls
173 lines (159 loc) · 4.53 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import type {
Jiti,
TransformOptions,
JitiOptions,
Context,
EvalModuleOptions,
JitiResolveOptions,
} from "./types";
import { platform } from "node:os";
import { pathToFileURL } from "mlly";
import { join } from "pathe";
import escapeStringRegexp from "escape-string-regexp";
import { normalizeAliases } from "pathe/utils";
import pkg from "../package.json";
import { debug, isDir } from "./utils";
import { resolveJitiOptions } from "./options";
import { jitiResolve } from "./resolve";
import { evalModule } from "./eval";
import { transform } from "./transform";
import { jitiRequire } from "./require";
import { prepareCacheDir } from "./cache";
const isWindows = platform() === "win32";
export default function createJiti(
filename: string,
userOptions: JitiOptions = {},
parentContext: Pick<
Context,
| "parentModule"
| "parentCache"
| "nativeImport"
| "onError"
| "createRequire"
>,
isNested = false,
): Jiti {
// Resolve options
const opts = isNested ? userOptions : resolveJitiOptions(userOptions);
// Normalize aliases (and disable if non given)
const alias =
opts.alias && Object.keys(opts.alias).length > 0
? normalizeAliases(opts.alias || {})
: undefined;
// List of modules to force transform or native
const nativeModules = ["typescript", "jiti", ...(opts.nativeModules || [])];
const isNativeRe = new RegExp(
`node_modules/(${nativeModules
.map((m) => escapeStringRegexp(m))
.join("|")})/`,
);
const transformModules = [...(opts.transformModules || [])];
const isTransformRe = new RegExp(
`node_modules/(${transformModules
.map((m) => escapeStringRegexp(m))
.join("|")})/`,
);
// If filename is dir, createRequire goes with parent directory, so we need fakepath
if (!filename) {
filename = process.cwd();
}
if (!isNest
8000
ed && isDir(filename)) {
filename = join(filename, "_index.js");
}
const url = pathToFileURL(filename);
const additionalExts = [...(opts.extensions as string[])].filter(
(ext) => ext !== ".js",
);
const nativeRequire = parentContext.createRequire(
isWindows
? filename.replace(/\//g, "\\") // Import maps does not work with normalized paths!
: filename,
);
// Create shared context
const ctx: Context = {
filename,
url,
opts,
alias,
nativeModules,
transformModules,
isNativeRe,
isTransformRe,
additionalExts,
nativeRequire,
onError: parentContext.onError,
parentModule: parentContext.parentModule,
parentCache: parentContext.parentCache,
nativeImport: parentContext.nativeImport,
createRequire: parentContext.createRequire,
};
// Debug
if (!isNested) {
debug(
ctx,
"[init]",
...[
["version:", pkg.version],
["module-cache:", opts.moduleCache],
["fs-cache:", opts.fsCache],
["interop-defaults:", opts.interopDefault],
].flat(),
);
}
// Prepare cache dir
if (!isNested) {
prepareCacheDir(ctx);
}
// Create jiti instance
const jiti: Jiti = Object.assign(
function jiti(id: string) {
return jitiRequire(ctx, id, { async: false });
},
{
cache: opts.moduleCache ? nativeRequire.cache : Object.create(null),
extensions: nativeRequire.extensions,
main: nativeRequire.main,
options: opts,
resolve: Object.assign(
function resolve(path: string) {
return jitiResolve(ctx, path, { async: false });
},
{
paths: nativeRequire.resolve.paths,
},
),
transform(opts: TransformOptions) {
return transform(ctx, opts);
},
evalModule(source: string, options?: EvalModuleOptions) {
return evalModule(ctx, source, options);
},
async import<T = unknown>(
id: string,
opts?: JitiResolveOptions & { default?: true },
): Promise<T> {
const mod = await jitiRequire(ctx, id, { ...opts, async: true });
return opts?.default ? (mod?.default ?? mod) : mod;
},
esmResolve(id: string, opts?: string | JitiResolveOptions): string {
if (typeof opts === "string") {
opts = { parentURL: opts };
}
const resolved = jitiResolve(ctx, id, {
parentURL: url as any,
...opts,
async: true,
});
if (
!resolved ||
typeof resolved !== "string" ||
resolved.startsWith("file://")
) {
return resolved;
}
return pathToFileURL(resolved);
},
},
) as Jiti;
return jiti;
}