vite-node 3.0.9 → 3.1.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/client.mjs CHANGED
@@ -12,489 +12,408 @@ const { setTimeout, clearTimeout } = globalThis;
12
12
  const debugExecute = createDebug("vite-node:client:execute");
13
13
  const debugNative = createDebug("vite-node:client:native");
14
14
  const clientStub = {
15
- injectQuery: (id) => id,
16
- createHotContext: () => {
17
- return {
18
- accept: () => {
19
- },
20
- prune: () => {
21
- },
22
- dispose: () => {
23
- },
24
- decline: () => {
25
- },
26
- invalidate: () => {
27
- },
28
- on: () => {
29
- },
30
- send: () => {
31
- }
32
- };
33
- },
34
- updateStyle: () => {
35
- },
36
- removeStyle: () => {
37
- }
15
+ injectQuery: (id) => id,
16
+ createHotContext: () => {
17
+ return {
18
+ accept: () => {},
19
+ prune: () => {},
20
+ dispose: () => {},
21
+ decline: () => {},
22
+ invalidate: () => {},
23
+ on: () => {},
24
+ send: () => {}
25
+ };
26
+ },
27
+ updateStyle: () => {},
28
+ removeStyle: () => {}
38
29
  };
39
30
  const env = createImportMetaEnvProxy();
40
31
  const DEFAULT_REQUEST_STUBS = {
41
- "/@vite/client": clientStub,
42
- "@vite/client": clientStub
32
+ "/@vite/client": clientStub,
33
+ "@vite/client": clientStub
43
34
  };
44
35
  class ModuleCacheMap extends Map {
45
- normalizePath(fsPath) {
46
- return normalizeModuleId(fsPath);
47
- }
48
- /**
49
- * Assign partial data to the map
50
- */
51
- update(fsPath, mod) {
52
- fsPath = this.normalizePath(fsPath);
53
- if (!super.has(fsPath)) {
54
- this.setByModuleId(fsPath, mod);
55
- } else {
56
- Object.assign(super.get(fsPath), mod);
57
- }
58
- return this;
59
- }
60
- setByModuleId(modulePath, mod) {
61
- return super.set(modulePath, mod);
62
- }
63
- set(fsPath, mod) {
64
- return this.setByModuleId(this.normalizePath(fsPath), mod);
65
- }
66
- getByModuleId(modulePath) {
67
- if (!super.has(modulePath)) {
68
- this.setByModuleId(modulePath, {});
69
- }
70
- const mod = super.get(modulePath);
71
- if (!mod.imports) {
72
- Object.assign(mod, {
73
- imports: /* @__PURE__ */ new Set(),
74
- importers: /* @__PURE__ */ new Set()
75
- });
76
- }
77
- return mod;
78
- }
79
- get(fsPath) {
80
- return this.getByModuleId(this.normalizePath(fsPath));
81
- }
82
- deleteByModuleId(modulePath) {
83
- return super.delete(modulePath);
84
- }
85
- delete(fsPath) {
86
- return this.deleteByModuleId(this.normalizePath(fsPath));
87
- }
88
- invalidateModule(mod) {
89
- var _a, _b;
90
- delete mod.evaluated;
91
- delete mod.resolving;
92
- delete mod.promise;
93
- delete mod.exports;
94
- (_a = mod.importers) == null ? void 0 : _a.clear();
95
- (_b = mod.imports) == null ? void 0 : _b.clear();
96
- return true;
97
- }
98
- /**
99
- * Invalidate modules that dependent on the given modules, up to the main entry
100
- */
101
- invalidateDepTree(ids, invalidated = /* @__PURE__ */ new Set()) {
102
- for (const _id of ids) {
103
- const id = this.normalizePath(_id);
104
- if (invalidated.has(id)) {
105
- continue;
106
- }
107
- invalidated.add(id);
108
- const mod = super.get(id);
109
- if (mod == null ? void 0 : mod.importers) {
110
- this.invalidateDepTree(mod.importers, invalidated);
111
- }
112
- super.delete(id);
113
- }
114
- return invalidated;
115
- }
116
- /**
117
- * Invalidate dependency modules of the given modules, down to the bottom-level dependencies
118
- */
119
- invalidateSubDepTree(ids, invalidated = /* @__PURE__ */ new Set()) {
120
- for (const _id of ids) {
121
- const id = this.normalizePath(_id);
122
- if (invalidated.has(id)) {
123
- continue;
124
- }
125
- invalidated.add(id);
126
- const subIds = Array.from(super.entries()).filter(([, mod]) => {
127
- var _a;
128
- return (_a = mod.importers) == null ? void 0 : _a.has(id);
129
- }).map(([key]) => key);
130
- if (subIds.length) {
131
- this.invalidateSubDepTree(subIds, invalidated);
132
- }
133
- super.delete(id);
134
- }
135
- return invalidated;
136
- }
137
- /**
138
- * Return parsed source map based on inlined source map of the module
139
- */
140
- getSourceMap(id) {
141
- const cache = this.get(id);
142
- if (cache.map) {
143
- return cache.map;
144
- }
145
- const map = cache.code && extractSourceMap(cache.code);
146
- if (map) {
147
- cache.map = map;
148
- return map;
149
- }
150
- return null;
151
- }
36
+ normalizePath(fsPath) {
37
+ return normalizeModuleId(fsPath);
38
+ }
39
+ /**
40
+ * Assign partial data to the map
41
+ */
42
+ update(fsPath, mod) {
43
+ fsPath = this.normalizePath(fsPath);
44
+ if (!super.has(fsPath)) this.setByModuleId(fsPath, mod);
45
+ else Object.assign(super.get(fsPath), mod);
46
+ return this;
47
+ }
48
+ setByModuleId(modulePath, mod) {
49
+ return super.set(modulePath, mod);
50
+ }
51
+ set(fsPath, mod) {
52
+ return this.setByModuleId(this.normalizePath(fsPath), mod);
53
+ }
54
+ getByModuleId(modulePath) {
55
+ if (!super.has(modulePath)) this.setByModuleId(modulePath, {});
56
+ const mod = super.get(modulePath);
57
+ if (!mod.imports) Object.assign(mod, {
58
+ imports: new Set(),
59
+ importers: new Set()
60
+ });
61
+ return mod;
62
+ }
63
+ get(fsPath) {
64
+ return this.getByModuleId(this.normalizePath(fsPath));
65
+ }
66
+ deleteByModuleId(modulePath) {
67
+ return super.delete(modulePath);
68
+ }
69
+ delete(fsPath) {
70
+ return this.deleteByModuleId(this.normalizePath(fsPath));
71
+ }
72
+ invalidateModule(mod) {
73
+ var _mod$importers, _mod$imports;
74
+ delete mod.evaluated;
75
+ delete mod.resolving;
76
+ delete mod.promise;
77
+ delete mod.exports;
78
+ (_mod$importers = mod.importers) === null || _mod$importers === void 0 || _mod$importers.clear();
79
+ (_mod$imports = mod.imports) === null || _mod$imports === void 0 || _mod$imports.clear();
80
+ return true;
81
+ }
82
+ /**
83
+ * Invalidate modules that dependent on the given modules, up to the main entry
84
+ */
85
+ invalidateDepTree(ids, invalidated = new Set()) {
86
+ for (const _id of ids) {
87
+ const id = this.normalizePath(_id);
88
+ if (invalidated.has(id)) continue;
89
+ invalidated.add(id);
90
+ const mod = super.get(id);
91
+ if (mod === null || mod === void 0 ? void 0 : mod.importers) this.invalidateDepTree(mod.importers, invalidated);
92
+ super.delete(id);
93
+ }
94
+ return invalidated;
95
+ }
96
+ /**
97
+ * Invalidate dependency modules of the given modules, down to the bottom-level dependencies
98
+ */
99
+ invalidateSubDepTree(ids, invalidated = new Set()) {
100
+ for (const _id of ids) {
101
+ const id = this.normalizePath(_id);
102
+ if (invalidated.has(id)) continue;
103
+ invalidated.add(id);
104
+ const subIds = Array.from(super.entries()).filter(([, mod]) => {
105
+ var _mod$importers2;
106
+ return (_mod$importers2 = mod.importers) === null || _mod$importers2 === void 0 ? void 0 : _mod$importers2.has(id);
107
+ }).map(([key]) => key);
108
+ if (subIds.length) this.invalidateSubDepTree(subIds, invalidated);
109
+ super.delete(id);
110
+ }
111
+ return invalidated;
112
+ }
113
+ /**
114
+ * Return parsed source map based on inlined source map of the module
115
+ */
116
+ getSourceMap(id) {
117
+ const cache = this.get(id);
118
+ if (cache.map) return cache.map;
119
+ const map = cache.code && extractSourceMap(cache.code);
120
+ if (map) {
121
+ cache.map = map;
122
+ return map;
123
+ }
124
+ return null;
125
+ }
152
126
  }
153
127
  class ViteNodeRunner {
154
- constructor(options) {
155
- this.options = options;
156
- this.root = options.root ?? process.cwd();
157
- this.moduleCache = options.moduleCache ?? new ModuleCacheMap();
158
- this.debug = options.debug ?? (typeof process !== "undefined" ? !!process.env.VITE_NODE_DEBUG_RUNNER : false);
159
- }
160
- root;
161
- debug;
162
- /**
163
- * Holds the cache of modules
164
- * Keys of the map are filepaths, or plain package names
165
- */
166
- moduleCache;
167
- async executeFile(file) {
168
- const url = `/@fs/${slash(resolve(file))}`;
169
- return await this.cachedRequest(url, url, []);
170
- }
171
- async executeId(rawId) {
172
- const [id, url] = await this.resolveUrl(rawId);
173
- return await this.cachedRequest(id, url, []);
174
- }
175
- /** @internal */
176
- async cachedRequest(id, fsPath, callstack) {
177
- const importee = callstack[callstack.length - 1];
178
- const mod = this.moduleCache.get(fsPath);
179
- const { imports, importers } = mod;
180
- if (importee) {
181
- importers.add(importee);
182
- }
183
- const getStack = () => `stack:
184
- ${[...callstack, fsPath].reverse().map((p) => ` - ${p}`).join("\n")}`;
185
- if (callstack.includes(fsPath) || Array.from(imports.values()).some((i) => importers.has(i))) {
186
- if (mod.exports) {
187
- return mod.exports;
188
- }
189
- }
190
- let debugTimer;
191
- if (this.debug) {
192
- debugTimer = setTimeout(
193
- () => console.warn(
194
- `[vite-node] module ${fsPath} takes over 2s to load.
195
- ${getStack()}`
196
- ),
197
- 2e3
198
- );
199
- }
200
- try {
201
- if (mod.promise) {
202
- return await mod.promise;
203
- }
204
- const promise = this.directRequest(id, fsPath, callstack);
205
- Object.assign(mod, { promise, evaluated: false });
206
- return await promise;
207
- } finally {
208
- mod.evaluated = true;
209
- if (debugTimer) {
210
- clearTimeout(debugTimer);
211
- }
212
- }
213
- }
214
- shouldResolveId(id, _importee) {
215
- return !isInternalRequest(id) && !isNodeBuiltin(id) && !id.startsWith("data:");
216
- }
217
- async _resolveUrl(id, importer) {
218
- var _a, _b;
219
- const dep = normalizeRequestId(id, this.options.base);
220
- if (!this.shouldResolveId(dep)) {
221
- return [dep, dep];
222
- }
223
- const { path, exists } = toFilePath(dep, this.root);
224
- if (!this.options.resolveId || exists) {
225
- return [dep, path];
226
- }
227
- const resolved = await this.options.resolveId(dep, importer);
228
- if ((_b = (_a = resolved == null ? void 0 : resolved.meta) == null ? void 0 : _a["vite:alias"]) == null ? void 0 : _b.noResolved) {
229
- const error = new Error(
230
- `Cannot find module '${id}'${importer ? ` imported from '${importer}'` : ""}.
128
+ root;
129
+ debug;
130
+ /**
131
+ * Holds the cache of modules
132
+ * Keys of the map are filepaths, or plain package names
133
+ */
134
+ moduleCache;
135
+ constructor(options) {
136
+ this.options = options;
137
+ this.root = options.root ?? process.cwd();
138
+ this.moduleCache = options.moduleCache ?? new ModuleCacheMap();
139
+ this.debug = options.debug ?? (typeof process !== "undefined" ? !!process.env.VITE_NODE_DEBUG_RUNNER : false);
140
+ }
141
+ async executeFile(file) {
142
+ const url = `/@fs/${slash(resolve(file))}`;
143
+ return await this.cachedRequest(url, url, []);
144
+ }
145
+ async executeId(rawId) {
146
+ const [id, url] = await this.resolveUrl(rawId);
147
+ return await this.cachedRequest(id, url, []);
148
+ }
149
+ /** @internal */
150
+ async cachedRequest(id, fsPath, callstack) {
151
+ const importee = callstack[callstack.length - 1];
152
+ const mod = this.moduleCache.get(fsPath);
153
+ const { imports, importers } = mod;
154
+ if (importee) importers.add(importee);
155
+ const getStack = () => `stack:\n${[...callstack, fsPath].reverse().map((p) => ` - ${p}`).join("\n")}`;
156
+ if (callstack.includes(fsPath) || Array.from(imports.values()).some((i) => importers.has(i))) {
157
+ if (mod.exports) return mod.exports;
158
+ }
159
+ let debugTimer;
160
+ if (this.debug) debugTimer = setTimeout(() => console.warn(`[vite-node] module ${fsPath} takes over 2s to load.\n${getStack()}`), 2e3);
161
+ try {
162
+ if (mod.promise) return await mod.promise;
163
+ const promise = this.directRequest(id, fsPath, callstack);
164
+ Object.assign(mod, {
165
+ promise,
166
+ evaluated: false
167
+ });
168
+ return await promise;
169
+ } finally {
170
+ mod.evaluated = true;
171
+ if (debugTimer) clearTimeout(debugTimer);
172
+ }
173
+ }
174
+ shouldResolveId(id, _importee) {
175
+ return !isInternalRequest(id) && !isNodeBuiltin(id) && !id.startsWith("data:");
176
+ }
177
+ async _resolveUrl(id, importer) {
178
+ var _resolved$meta;
179
+ const dep = normalizeRequestId(id, this.options.base);
180
+ if (!this.shouldResolveId(dep)) return [dep, dep];
181
+ const { path, exists } = toFilePath(dep, this.root);
182
+ if (!this.options.resolveId || exists) return [dep, path];
183
+ const resolved = await this.options.resolveId(dep, importer);
184
+ if (resolved === null || resolved === void 0 || (_resolved$meta = resolved.meta) === null || _resolved$meta === void 0 || (_resolved$meta = _resolved$meta["vite:alias"]) === null || _resolved$meta === void 0 ? void 0 : _resolved$meta.noResolved) {
185
+ const error = new Error(`Cannot find module '${id}'${importer ? ` imported from '${importer}'` : ""}.
231
186
 
232
187
  - If you rely on tsconfig.json's "paths" to resolve modules, please install "vite-tsconfig-paths" plugin to handle module resolution.
233
- - Make sure you don't have relative aliases in your Vitest config. Use absolute paths instead. Read more: https://vitest.dev/guide/common-errors`
234
- );
235
- Object.defineProperty(error, "code", {
236
- value: "ERR_MODULE_NOT_FOUND",
237
- enumerable: true
238
- });
239
- Object.defineProperty(error, Symbol.for("vitest.error.not_found.data"), {
240
- value: { id: dep, importer },
241
- enumerable: false
242
- });
243
- throw error;
244
- }
245
- const resolvedId = resolved ? normalizeRequestId(resolved.id, this.options.base) : dep;
246
- return [resolvedId, resolvedId];
247
- }
248
- async resolveUrl(id, importee) {
249
- const resolveKey = `resolve:${id}`;
250
- this.moduleCache.setByModuleId(resolveKey, { resolving: true });
251
- try {
252
- return await this._resolveUrl(id, importee);
253
- } finally {
254
- this.moduleCache.deleteByModuleId(resolveKey);
255
- }
256
- }
257
- /** @internal */
258
- async dependencyRequest(id, fsPath, callstack) {
259
- return await this.cachedRequest(id, fsPath, callstack);
260
- }
261
- /** @internal */
262
- async directRequest(id, fsPath, _callstack) {
263
- const moduleId = normalizeModuleId(fsPath);
264
- const callstack = [..._callstack, moduleId];
265
- const mod = this.moduleCache.getByModuleId(moduleId);
266
- const request = async (dep) => {
267
- const [id2, depFsPath] = await this.resolveUrl(String(dep), fsPath);
268
- const depMod = this.moduleCache.getByModuleId(depFsPath);
269
- depMod.importers.add(moduleId);
270
- mod.imports.add(depFsPath);
271
- return this.dependencyRequest(id2, depFsPath, callstack);
272
- };
273
- const requestStubs = this.options.requestStubs || DEFAULT_REQUEST_STUBS;
274
- if (id in requestStubs) {
275
- return requestStubs[id];
276
- }
277
- let { code: transformed, externalize } = await this.options.fetchModule(id);
278
- if (externalize) {
279
- debugNative(externalize);
280
- const exports2 = await this.interopedImport(externalize);
281
- mod.exports = exports2;
282
- return exports2;
283
- }
284
- if (transformed == null) {
285
- throw new Error(
286
- `[vite-node] Failed to load "${id}" imported from ${callstack[callstack.length - 2]}`
287
- );
288
- }
289
- const { Object: Object2, Reflect: Reflect2, Symbol: Symbol2 } = this.getContextPrimitives();
290
- const modulePath = cleanUrl(moduleId);
291
- const href = pathToFileURL(modulePath).href;
292
- const __filename = fileURLToPath(href);
293
- const __dirname = dirname(__filename);
294
- const meta = {
295
- url: href,
296
- env,
297
- filename: __filename,
298
- dirname: __dirname
299
- };
300
- const exports = Object2.create(null);
301
- Object2.defineProperty(exports, Symbol2.toStringTag, {
302
- value: "Module",
303
- enumerable: false,
304
- configurable: false
305
- });
306
- const SYMBOL_NOT_DEFINED = Symbol2("not defined");
307
- let moduleExports = SYMBOL_NOT_DEFINED;
308
- const cjsExports = new Proxy(exports, {
309
- get: (target, p, receiver) => {
310
- if (Reflect2.has(target, p)) {
311
- return Reflect2.get(target, p, receiver);
312
- }
313
- return Reflect2.get(Object2.prototype, p, receiver);
314
- },
315
- getPrototypeOf: () => Object2.prototype,
316
- set: (_, p, value) => {
317
- if (p === "default" && this.shouldInterop(modulePath, { default: value }) && cjsExports !== value) {
318
- exportAll(cjsExports, value);
319
- exports.default = value;
320
- return true;
321
- }
322
- if (!Reflect2.has(exports, "default")) {
323
- exports.default = {};
324
- }
325
- if (moduleExports !== SYMBOL_NOT_DEFINED && isPrimitive(moduleExports)) {
326
- defineExport(exports, p, () => void 0);
327
- return true;
328
- }
329
- if (!isPrimitive(exports.default)) {
330
- exports.default[p] = value;
331
- }
332
- if (p !== "default") {
333
- defineExport(exports, p, () => value);
334
- }
335
- return true;
336
- }
337
- });
338
- Object2.assign(mod, { code: transformed, exports });
339
- const moduleProxy = {
340
- set exports(value) {
341
- exportAll(cjsExports, value);
342
- exports.default = value;
343
- moduleExports = value;
344
- },
345
- get exports() {
346
- return cjsExports;
347
- }
348
- };
349
- let hotContext;
350
- if (this.options.createHotContext) {
351
- Object2.defineProperty(meta, "hot", {
352
- enumerable: true,
353
- get: () => {
354
- var _a, _b;
355
- hotContext || (hotContext = (_b = (_a = this.options).createHotContext) == null ? void 0 : _b.call(_a, this, moduleId));
356
- return hotContext;
357
- },
358
- set: (value) => {
359
- hotContext = value;
360
- }
361
- });
362
- }
363
- const context = this.prepareContext({
364
- // esm transformed by Vite
365
- __vite_ssr_import__: request,
366
- __vite_ssr_dynamic_import__: request,
367
- __vite_ssr_exports__: exports,
368
- __vite_ssr_exportAll__: (obj) => exportAll(exports, obj),
369
- __vite_ssr_import_meta__: meta,
370
- // cjs compact
371
- require: createRequire(href),
372
- exports: cjsExports,
373
- module: moduleProxy,
374
- __filename,
375
- __dirname
376
- });
377
- debugExecute(__filename);
378
- if (transformed[0] === "#") {
379
- transformed = transformed.replace(/^#!.*/, (s) => " ".repeat(s.length));
380
- }
381
- await this.runModule(context, transformed);
382
- return exports;
383
- }
384
- getContextPrimitives() {
385
- return { Object, Reflect, Symbol };
386
- }
387
- async runModule(context, transformed) {
388
- var _a;
389
- const codeDefinition = `'use strict';async (${Object.keys(context).join(
390
- ","
391
- )})=>{{`;
392
- const code = `${codeDefinition}${transformed}
393
- }}`;
394
- const options = {
395
- filename: context.__filename,
396
- lineOffset: 0,
397
- columnOffset: -codeDefinition.length
398
- };
399
- (_a = this.options.moduleExecutionInfo) == null ? void 0 : _a.set(options.filename, { startOffset: codeDefinition.length });
400
- const fn = vm.runInThisContext(code, options);
401
- await fn(...Object.values(context));
402
- }
403
- prepareContext(context) {
404
- return context;
405
- }
406
- /**
407
- * Define if a module should be interop-ed
408
- * This function mostly for the ability to override by subclass
409
- */
410
- shouldInterop(path, mod) {
411
- if (this.options.interopDefault === false) {
412
- return false;
413
- }
414
- return !path.endsWith(".mjs") && "default" in mod;
415
- }
416
- importExternalModule(path) {
417
- return import(
418
- /* @vite-ignore */
419
- path
420
- );
421
- }
422
- /**
423
- * Import a module and interop it
424
- */
425
- async interopedImport(path) {
426
- const importedModule = await this.importExternalModule(path);
427
- if (!this.shouldInterop(path, importedModule)) {
428
- return importedModule;
429
- }
430
- const { mod, defaultExport } = interopModule(importedModule);
431
- return new Proxy(mod, {
432
- get(mod2, prop) {
433
- if (prop === "default") {
434
- return defaultExport;
435
- }
436
- return mod2[prop] ?? (defaultExport == null ? void 0 : defaultExport[prop]);
437
- },
438
- has(mod2, prop) {
439
- if (prop === "default") {
440
- return defaultExport !== void 0;
441
- }
442
- return prop in mod2 || defaultExport && prop in defaultExport;
443
- },
444
- getOwnPropertyDescriptor(mod2, prop) {
445
- const descriptor = Reflect.getOwnPropertyDescriptor(mod2, prop);
446
- if (descriptor) {
447
- return descriptor;
448
- }
449
- if (prop === "default" && defaultExport !== void 0) {
450
- return {
451
- value: defaultExport,
452
- enumerable: true,
453
- configurable: true
454
- };
455
- }
456
- }
457
- });
458
- }
188
+ - Make sure you don't have relative aliases in your Vitest config. Use absolute paths instead. Read more: https://vitest.dev/guide/common-errors`);
189
+ Object.defineProperty(error, "code", {
190
+ value: "ERR_MODULE_NOT_FOUND",
191
+ enumerable: true
192
+ });
193
+ Object.defineProperty(error, Symbol.for("vitest.error.not_found.data"), {
194
+ value: {
195
+ id: dep,
196
+ importer
197
+ },
198
+ enumerable: false
199
+ });
200
+ throw error;
201
+ }
202
+ const resolvedId = resolved ? normalizeRequestId(resolved.id, this.options.base) : dep;
203
+ return [resolvedId, resolvedId];
204
+ }
205
+ async resolveUrl(id, importee) {
206
+ const resolveKey = `resolve:${id}`;
207
+ this.moduleCache.setByModuleId(resolveKey, { resolving: true });
208
+ try {
209
+ return await this._resolveUrl(id, importee);
210
+ } finally {
211
+ this.moduleCache.deleteByModuleId(resolveKey);
212
+ }
213
+ }
214
+ /** @internal */
215
+ async dependencyRequest(id, fsPath, callstack) {
216
+ return await this.cachedRequest(id, fsPath, callstack);
217
+ }
218
+ /** @internal */
219
+ async directRequest(id, fsPath, _callstack) {
220
+ const moduleId = normalizeModuleId(fsPath);
221
+ const callstack = [..._callstack, moduleId];
222
+ const mod = this.moduleCache.getByModuleId(moduleId);
223
+ const request = async (dep) => {
224
+ const [id, depFsPath] = await this.resolveUrl(String(dep), fsPath);
225
+ const depMod = this.moduleCache.getByModuleId(depFsPath);
226
+ depMod.importers.add(moduleId);
227
+ mod.imports.add(depFsPath);
228
+ return this.dependencyRequest(id, depFsPath, callstack);
229
+ };
230
+ const requestStubs = this.options.requestStubs || DEFAULT_REQUEST_STUBS;
231
+ if (id in requestStubs) return requestStubs[id];
232
+ let { code: transformed, externalize } = await this.options.fetchModule(id);
233
+ if (externalize) {
234
+ debugNative(externalize);
235
+ const exports = await this.interopedImport(externalize);
236
+ mod.exports = exports;
237
+ return exports;
238
+ }
239
+ if (transformed == null) throw new Error(`[vite-node] Failed to load "${id}" imported from ${callstack[callstack.length - 2]}`);
240
+ const { Object, Reflect, Symbol } = this.getContextPrimitives();
241
+ const modulePath = cleanUrl(moduleId);
242
+ const href = pathToFileURL(modulePath).href;
243
+ const __filename = fileURLToPath(href);
244
+ const __dirname = dirname(__filename);
245
+ const meta = {
246
+ url: href,
247
+ env,
248
+ filename: __filename,
249
+ dirname: __dirname
250
+ };
251
+ const exports = Object.create(null);
252
+ Object.defineProperty(exports, Symbol.toStringTag, {
253
+ value: "Module",
254
+ enumerable: false,
255
+ configurable: false
256
+ });
257
+ const SYMBOL_NOT_DEFINED = Symbol("not defined");
258
+ let moduleExports = SYMBOL_NOT_DEFINED;
259
+ const cjsExports = new Proxy(exports, {
260
+ get: (target, p, receiver) => {
261
+ if (Reflect.has(target, p)) return Reflect.get(target, p, receiver);
262
+ return Reflect.get(Object.prototype, p, receiver);
263
+ },
264
+ getPrototypeOf: () => Object.prototype,
265
+ set: (_, p, value) => {
266
+ if (p === "default" && this.shouldInterop(modulePath, { default: value }) && cjsExports !== value) {
267
+ exportAll(cjsExports, value);
268
+ exports.default = value;
269
+ return true;
270
+ }
271
+ if (!Reflect.has(exports, "default")) exports.default = {};
272
+ if (moduleExports !== SYMBOL_NOT_DEFINED && isPrimitive(moduleExports)) {
273
+ defineExport(exports, p, () => void 0);
274
+ return true;
275
+ }
276
+ if (!isPrimitive(exports.default)) exports.default[p] = value;
277
+ if (p !== "default") defineExport(exports, p, () => value);
278
+ return true;
279
+ }
280
+ });
281
+ Object.assign(mod, {
282
+ code: transformed,
283
+ exports
284
+ });
285
+ const moduleProxy = {
286
+ set exports(value) {
287
+ exportAll(cjsExports, value);
288
+ exports.default = value;
289
+ moduleExports = value;
290
+ },
291
+ get exports() {
292
+ return cjsExports;
293
+ }
294
+ };
295
+ let hotContext;
296
+ if (this.options.createHotContext) Object.defineProperty(meta, "hot", {
297
+ enumerable: true,
298
+ get: () => {
299
+ var _this$options$createH, _this$options;
300
+ hotContext || (hotContext = (_this$options$createH = (_this$options = this.options).createHotContext) === null || _this$options$createH === void 0 ? void 0 : _this$options$createH.call(_this$options, this, moduleId));
301
+ return hotContext;
302
+ },
303
+ set: (value) => {
304
+ hotContext = value;
305
+ }
306
+ });
307
+ const context = this.prepareContext({
308
+ __vite_ssr_import__: request,
309
+ __vite_ssr_dynamic_import__: request,
310
+ __vite_ssr_exports__: exports,
311
+ __vite_ssr_exportAll__: (obj) => exportAll(exports, obj),
312
+ __vite_ssr_import_meta__: meta,
313
+ require: createRequire(href),
314
+ exports: cjsExports,
315
+ module: moduleProxy,
316
+ __filename,
317
+ __dirname
318
+ });
319
+ debugExecute(__filename);
320
+ if (transformed[0] === "#") transformed = transformed.replace(/^#!.*/, (s) => " ".repeat(s.length));
321
+ await this.runModule(context, transformed);
322
+ return exports;
323
+ }
324
+ getContextPrimitives() {
325
+ return {
326
+ Object,
327
+ Reflect,
328
+ Symbol
329
+ };
330
+ }
331
+ async runModule(context, transformed) {
332
+ var _this$options$moduleE;
333
+ const codeDefinition = `'use strict';async (${Object.keys(context).join(",")})=>{{`;
334
+ const code = `${codeDefinition}${transformed}\n}}`;
335
+ const options = {
336
+ filename: context.__filename,
337
+ lineOffset: 0,
338
+ columnOffset: -codeDefinition.length
339
+ };
340
+ (_this$options$moduleE = this.options.moduleExecutionInfo) === null || _this$options$moduleE === void 0 || _this$options$moduleE.set(options.filename, { startOffset: codeDefinition.length });
341
+ const fn = vm.runInThisContext(code, options);
342
+ await fn(...Object.values(context));
343
+ }
344
+ prepareContext(context) {
345
+ return context;
346
+ }
347
+ /**
348
+ * Define if a module should be interop-ed
349
+ * This function mostly for the ability to override by subclass
350
+ */
351
+ shouldInterop(path, mod) {
352
+ if (this.options.interopDefault === false) return false;
353
+ return !path.endsWith(".mjs") && "default" in mod;
354
+ }
355
+ importExternalModule(path) {
356
+ return import(
357
+ /* @vite-ignore */
358
+ path
359
+ );
360
+ }
361
+ /**
362
+ * Import a module and interop it
363
+ */
364
+ async interopedImport(path) {
365
+ const importedModule = await this.importExternalModule(path);
366
+ if (!this.shouldInterop(path, importedModule)) return importedModule;
367
+ const { mod, defaultExport } = interopModule(importedModule);
368
+ return new Proxy(mod, {
369
+ get(mod, prop) {
370
+ if (prop === "default") return defaultExport;
371
+ return mod[prop] ?? (defaultExport === null || defaultExport === void 0 ? void 0 : defaultExport[prop]);
372
+ },
373
+ has(mod, prop) {
374
+ if (prop === "default") return defaultExport !== void 0;
375
+ return prop in mod || defaultExport && prop in defaultExport;
376
+ },
377
+ getOwnPropertyDescriptor(mod, prop) {
378
+ const descriptor = Reflect.getOwnPropertyDescriptor(mod, prop);
379
+ if (descriptor) return descriptor;
380
+ if (prop === "default" && defaultExport !== void 0) return {
381
+ value: defaultExport,
382
+ enumerable: true,
383
+ configurable: true
384
+ };
385
+ }
386
+ });
387
+ }
459
388
  }
460
389
  function interopModule(mod) {
461
- if (isPrimitive(mod)) {
462
- return {
463
- mod: { default: mod },
464
- defaultExport: mod
465
- };
466
- }
467
- let defaultExport = "default" in mod ? mod.default : mod;
468
- if (!isPrimitive(defaultExport) && "__esModule" in defaultExport) {
469
- mod = defaultExport;
470
- if ("default" in defaultExport) {
471
- defaultExport = defaultExport.default;
472
- }
473
- }
474
- return { mod, defaultExport };
390
+ if (isPrimitive(mod)) return {
391
+ mod: { default: mod },
392
+ defaultExport: mod
393
+ };
394
+ let defaultExport = "default" in mod ? mod.default : mod;
395
+ if (!isPrimitive(defaultExport) && "__esModule" in defaultExport) {
396
+ mod = defaultExport;
397
+ if ("default" in defaultExport) defaultExport = defaultExport.default;
398
+ }
399
+ return {
400
+ mod,
401
+ defaultExport
402
+ };
475
403
  }
476
404
  function defineExport(exports, key, value) {
477
- Object.defineProperty(exports, key, {
478
- enumerable: true,
479
- configurable: true,
480
- get: value
481
- });
405
+ Object.defineProperty(exports, key, {
406
+ enumerable: true,
407
+ configurable: true,
408
+ get: value
409
+ });
482
410
  }
483
411
  function exportAll(exports, sourceModule) {
484
- if (exports === sourceModule) {
485
- return;
486
- }
487
- if (isPrimitive(sourceModule) || Array.isArray(sourceModule) || sourceModule instanceof Promise) {
488
- return;
489
- }
490
- for (const key in sourceModule) {
491
- if (key !== "default") {
492
- try {
493
- defineExport(exports, key, () => sourceModule[key]);
494
- } catch {
495
- }
496
- }
497
- }
412
+ if (exports === sourceModule) return;
413
+ if (isPrimitive(sourceModule) || Array.isArray(sourceModule) || sourceModule instanceof Promise) return;
414
+ for (const key in sourceModule) if (key !== "default") try {
415
+ defineExport(exports, key, () => sourceModule[key]);
416
+ } catch {}
498
417
  }
499
418
 
500
419
  export { DEFAULT_REQUEST_STUBS, ModuleCacheMap, ViteNodeRunner };