vite-node 4.0.0-beta.5 → 4.0.0-beta.6

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.cjs CHANGED
@@ -10,10 +10,7 @@ var utils = require('./utils.cjs');
10
10
  require('pathe');
11
11
  require('node:fs');
12
12
 
13
- const { setTimeout, clearTimeout } = globalThis;
14
- const debugExecute = createDebug("vite-node:client:execute");
15
- const debugNative = createDebug("vite-node:client:native");
16
- const clientStub = {
13
+ const { setTimeout, clearTimeout } = globalThis, debugExecute = createDebug("vite-node:client:execute"), debugNative = createDebug("vite-node:client:native"), clientStub = {
17
14
  injectQuery: (id) => id,
18
15
  createHotContext: () => {
19
16
  return {
@@ -28,8 +25,7 @@ const clientStub = {
28
25
  },
29
26
  updateStyle: () => {},
30
27
  removeStyle: () => {}
31
- };
32
- const env = utils.createImportMetaEnvProxy();
28
+ }, env = utils.createImportMetaEnvProxy();
33
29
  const DEFAULT_REQUEST_STUBS = {
34
30
  "/@vite/client": clientStub,
35
31
  "@vite/client": clientStub
@@ -42,8 +38,7 @@ class ModuleCacheMap extends Map {
42
38
  * Assign partial data to the map
43
39
  */
44
40
  update(fsPath, mod) {
45
- fsPath = this.normalizePath(fsPath);
46
- if (!super.has(fsPath)) this.setByModuleId(fsPath, mod);
41
+ if (fsPath = this.normalizePath(fsPath), !super.has(fsPath)) this.setByModuleId(fsPath, mod);
47
42
  else Object.assign(super.get(fsPath), mod);
48
43
  return this;
49
44
  }
@@ -73,13 +68,7 @@ class ModuleCacheMap extends Map {
73
68
  }
74
69
  invalidateModule(mod) {
75
70
  var _mod$importers, _mod$imports;
76
- delete mod.evaluated;
77
- delete mod.resolving;
78
- delete mod.promise;
79
- delete mod.exports;
80
- (_mod$importers = mod.importers) === null || _mod$importers === void 0 || _mod$importers.clear();
81
- (_mod$imports = mod.imports) === null || _mod$imports === void 0 || _mod$imports.clear();
82
- return true;
71
+ return delete mod.evaluated, delete mod.resolving, delete mod.promise, delete mod.exports, (_mod$importers = mod.importers) === null || _mod$importers === void 0 || _mod$importers.clear(), (_mod$imports = mod.imports) === null || _mod$imports === void 0 || _mod$imports.clear(), true;
83
72
  }
84
73
  /**
85
74
  * Invalidate modules that dependent on the given modules, up to the main entry
@@ -119,11 +108,7 @@ class ModuleCacheMap extends Map {
119
108
  const cache = this.get(id);
120
109
  if (cache.map) return cache.map;
121
110
  const map = cache.code && sourceMap.extractSourceMap(cache.code);
122
- if (map) {
123
- cache.map = map;
124
- return map;
125
- }
126
- return null;
111
+ return map ? (cache.map = map, map) : null;
127
112
  }
128
113
  }
129
114
  class ViteNodeRunner {
@@ -154,10 +139,7 @@ class ViteNodeRunner {
154
139
  // `performance` can be mocked, so make sure we're using the original function
155
140
  performanceNow = performance.now.bind(performance);
156
141
  constructor(options) {
157
- this.options = options;
158
- this.root = options.root ?? process.cwd();
159
- this.moduleCache = options.moduleCache ?? new ModuleCacheMap();
160
- this.debug = options.debug ?? (typeof process !== "undefined" ? !!process.env.VITE_NODE_DEBUG_RUNNER : false);
142
+ this.options = options, this.root = options.root ?? process.cwd(), this.moduleCache = options.moduleCache ?? new ModuleCacheMap(), this.debug = options.debug ?? (typeof process !== "undefined" ? !!process.env.VITE_NODE_DEBUG_RUNNER : false);
161
143
  }
162
144
  async executeFile(file) {
163
145
  const url = `/@fs/${utils.slash(path.resolve(file))}`;
@@ -169,9 +151,7 @@ class ViteNodeRunner {
169
151
  }
170
152
  /** @internal */
171
153
  async cachedRequest(id, fsPath, callstack) {
172
- const importee = callstack[callstack.length - 1];
173
- const mod = this.moduleCache.get(fsPath);
174
- const { imports, importers } = mod;
154
+ const importee = callstack[callstack.length - 1], mod = this.moduleCache.get(fsPath), { imports, importers } = mod;
175
155
  if (importee) importers.add(importee);
176
156
  const getStack = () => `stack:\n${[...callstack, fsPath].reverse().map((p) => ` - ${p}`).join("\n")}`;
177
157
  // check circular dependency
@@ -184,14 +164,12 @@ class ViteNodeRunner {
184
164
  // cached module
185
165
  if (mod.promise) return await mod.promise;
186
166
  const promise = this.directRequest(id, fsPath, callstack);
187
- Object.assign(mod, {
167
+ return Object.assign(mod, {
188
168
  promise,
189
169
  evaluated: false
190
- });
191
- return await promise;
170
+ }), await promise;
192
171
  } finally {
193
- mod.evaluated = true;
194
- if (debugTimer) clearTimeout(debugTimer);
172
+ if (mod.evaluated = true, debugTimer) clearTimeout(debugTimer);
195
173
  }
196
174
  }
197
175
  shouldResolveId(id, _importee) {
@@ -210,18 +188,16 @@ class ViteNodeRunner {
210
188
 
211
189
  - If you rely on tsconfig.json's "paths" to resolve modules, please install "vite-tsconfig-paths" plugin to handle module resolution.
212
190
  - Make sure you don't have relative aliases in your Vitest config. Use absolute paths instead. Read more: https://vitest.dev/guide/common-errors`);
213
- Object.defineProperty(error, "code", {
191
+ throw Object.defineProperty(error, "code", {
214
192
  value: "ERR_MODULE_NOT_FOUND",
215
193
  enumerable: true
216
- });
217
- Object.defineProperty(error, Symbol.for("vitest.error.not_found.data"), {
194
+ }), Object.defineProperty(error, Symbol.for("vitest.error.not_found.data"), {
218
195
  value: {
219
196
  id: dep,
220
197
  importer
221
198
  },
222
199
  enumerable: false
223
- });
224
- throw error;
200
+ }), error;
225
201
  }
226
202
  const resolvedId = resolved ? utils.normalizeRequestId(resolved.id, this.options.base) : dep;
227
203
  return [resolvedId, resolvedId];
@@ -247,47 +223,31 @@ class ViteNodeRunner {
247
223
  // rethrow vite error if it cannot load the module because it's not resolved
248
224
  if (typeof cause === "object" && cause.code === "ERR_LOAD_URL" || typeof (cause === null || cause === void 0 ? void 0 : cause.message) === "string" && cause.message.includes("Failed to load url")) {
249
225
  const error = new Error(`Cannot find ${utils.isBareImport(id) ? "package" : "module"} '${id}'${importer ? ` imported from '${importer}'` : ""}`, { cause });
250
- error.code = "ERR_MODULE_NOT_FOUND";
251
- throw error;
226
+ throw error.code = "ERR_MODULE_NOT_FOUND", error;
252
227
  }
253
228
  throw cause;
254
229
  }
255
230
  }
256
231
  /** @internal */
257
232
  async directRequest(id, fsPath, _callstack) {
258
- const moduleId = utils.normalizeModuleId(fsPath);
259
- const callstack = [..._callstack, moduleId];
260
- const mod = this.moduleCache.getByModuleId(moduleId);
261
- const request = async (dep) => {
262
- const [id, depFsPath] = await this.resolveUrl(String(dep), fsPath);
263
- const depMod = this.moduleCache.getByModuleId(depFsPath);
264
- depMod.importers.add(moduleId);
265
- mod.imports.add(depFsPath);
266
- return this.dependencyRequest(id, depFsPath, callstack);
267
- };
268
- const requestStubs = this.options.requestStubs || DEFAULT_REQUEST_STUBS;
233
+ const moduleId = utils.normalizeModuleId(fsPath), callstack = [..._callstack, moduleId], mod = this.moduleCache.getByModuleId(moduleId), request = async (dep) => {
234
+ const [id, depFsPath] = await this.resolveUrl(String(dep), fsPath), depMod = this.moduleCache.getByModuleId(depFsPath);
235
+ return depMod.importers.add(moduleId), mod.imports.add(depFsPath), this.dependencyRequest(id, depFsPath, callstack);
236
+ }, requestStubs = this.options.requestStubs || DEFAULT_REQUEST_STUBS;
269
237
  if (id in requestStubs) return requestStubs[id];
270
238
  let { code: transformed, externalize } = await this._fetchModule(id, callstack[callstack.length - 2]);
271
239
  if (externalize) {
272
240
  debugNative(externalize);
273
241
  const exports = await this.interopedImport(externalize);
274
- mod.exports = exports;
275
- return exports;
242
+ return mod.exports = exports, exports;
276
243
  }
277
244
  if (transformed == null) throw new Error(`[vite-node] Failed to load "${id}" imported from ${callstack[callstack.length - 2]}`);
278
- const { Object, Reflect, Symbol } = this.getContextPrimitives();
279
- const modulePath = utils.cleanUrl(moduleId);
280
- // disambiguate the `<UNIT>:/` on windows: see nodejs/node#31710
281
- const href = node_url.pathToFileURL(modulePath).href;
282
- const __filename = node_url.fileURLToPath(href);
283
- const __dirname = path.dirname(__filename);
284
- const meta = {
245
+ const { Object, Reflect, Symbol } = this.getContextPrimitives(), modulePath = utils.cleanUrl(moduleId), href = node_url.pathToFileURL(modulePath).href, __filename = node_url.fileURLToPath(href), __dirname = path.dirname(__filename), meta = {
285
246
  url: href,
286
247
  env,
287
248
  filename: __filename,
288
249
  dirname: __dirname
289
- };
290
- const exports = Object.create(null);
250
+ }, exports = Object.create(null);
291
251
  Object.defineProperty(exports, Symbol.toStringTag, {
292
252
  value: "Module",
293
253
  enumerable: false,
@@ -299,25 +259,17 @@ class ViteNodeRunner {
299
259
  // inside the module itself. imported module is always "exports"
300
260
  const cjsExports = new Proxy(exports, {
301
261
  get: (target, p, receiver) => {
302
- if (Reflect.has(target, p)) return Reflect.get(target, p, receiver);
303
- return Reflect.get(Object.prototype, p, receiver);
262
+ return Reflect.has(target, p) ? Reflect.get(target, p, receiver) : Reflect.get(Object.prototype, p, receiver);
304
263
  },
305
264
  getPrototypeOf: () => Object.prototype,
306
265
  set: (_, p, value) => {
307
266
  // treat "module.exports =" the same as "exports.default =" to not have nested "default.default",
308
267
  // so "exports.default" becomes the actual module
309
- if (p === "default" && this.shouldInterop(modulePath, { default: value }) && cjsExports !== value) {
310
- exportAll(cjsExports, value);
311
- exports.default = value;
312
- return true;
313
- }
268
+ if (p === "default" && this.shouldInterop(modulePath, { default: value }) && cjsExports !== value) return exportAll(cjsExports, value), exports.default = value, true;
314
269
  if (!Reflect.has(exports, "default")) exports.default = {};
315
270
  // returns undefined, when accessing named exports, if default is not an object
316
271
  // but is still present inside hasOwnKeys, this is Node behaviour for CJS
317
- if (moduleExports !== SYMBOL_NOT_DEFINED && utils.isPrimitive(moduleExports)) {
318
- defineExport(exports, p, () => void 0);
319
- return true;
320
- }
272
+ if (moduleExports !== SYMBOL_NOT_DEFINED && utils.isPrimitive(moduleExports)) return defineExport(exports, p, () => void 0), true;
321
273
  if (!utils.isPrimitive(exports.default)) exports.default[p] = value;
322
274
  if (p !== "default") defineExport(exports, p, () => value);
323
275
  return true;
@@ -329,9 +281,7 @@ class ViteNodeRunner {
329
281
  });
330
282
  const moduleProxy = {
331
283
  set exports(value) {
332
- exportAll(cjsExports, value);
333
- exports.default = value;
334
- moduleExports = value;
284
+ exportAll(cjsExports, value), exports.default = value, moduleExports = value;
335
285
  },
336
286
  get exports() {
337
287
  return cjsExports;
@@ -343,8 +293,7 @@ class ViteNodeRunner {
343
293
  enumerable: true,
344
294
  get: () => {
345
295
  var _this$options$createH, _this$options;
346
- 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));
347
- return hotContext;
296
+ return 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)), hotContext;
348
297
  },
349
298
  set: (value) => {
350
299
  hotContext = value;
@@ -371,11 +320,9 @@ class ViteNodeRunner {
371
320
  __filename,
372
321
  __dirname
373
322
  });
374
- debugExecute(__filename);
375
323
  // remove shebang
376
- if (transformed[0] === "#") transformed = transformed.replace(/^#!.*/, (s) => " ".repeat(s.length));
377
- await this.runModule(context, transformed);
378
- return exports;
324
+ if (debugExecute(__filename), transformed[0] === "#") transformed = transformed.replace(/^#!.*/, (s) => " ".repeat(s.length));
325
+ return await this.runModule(context, transformed), exports;
379
326
  }
380
327
  getContextPrimitives() {
381
328
  return {
@@ -386,14 +333,11 @@ class ViteNodeRunner {
386
333
  }
387
334
  async runModule(context, transformed) {
388
335
  // add 'use strict' since ESM enables it by default
389
- const codeDefinition = `'use strict';async (${Object.keys(context).join(",")})=>{{`;
390
- const code = `${codeDefinition}${transformed}\n}}`;
391
- const options = {
336
+ const codeDefinition = `'use strict';async (${Object.keys(context).join(",")})=>{{`, code = `${codeDefinition}${transformed}\n}}`, options = {
392
337
  filename: context.__filename,
393
338
  lineOffset: 0,
394
339
  columnOffset: -codeDefinition.length
395
- };
396
- const finishModuleExecutionInfo = this.startCalculateModuleExecutionInfo(options.filename, codeDefinition.length);
340
+ }, finishModuleExecutionInfo = this.startCalculateModuleExecutionInfo(options.filename, codeDefinition.length);
397
341
  try {
398
342
  const fn = vm.runInThisContext(code, options);
399
343
  await fn(...Object.values(context));
@@ -408,14 +352,12 @@ class ViteNodeRunner {
408
352
  */
409
353
  startCalculateModuleExecutionInfo(filename, startOffset) {
410
354
  const startTime = this.performanceNow();
411
- this.executionStack.push({
355
+ return this.executionStack.push({
412
356
  filename,
413
357
  startTime,
414
358
  subImportTime: 0
415
- });
416
- return () => {
417
- const duration = this.performanceNow() - startTime;
418
- const currentExecution = this.executionStack.pop();
359
+ }), () => {
360
+ const duration = this.performanceNow() - startTime, currentExecution = this.executionStack.pop();
419
361
  if (currentExecution == null) throw new Error("Execution stack is empty, this should never happen");
420
362
  const selfTime = duration - currentExecution.subImportTime;
421
363
  if (this.executionStack.length > 0) this.executionStack.at(-1).subImportTime += duration;
@@ -434,10 +376,9 @@ class ViteNodeRunner {
434
376
  * This function mostly for the ability to override by subclass
435
377
  */
436
378
  shouldInterop(path, mod) {
437
- if (this.options.interopDefault === false) return false;
438
379
  // never interop ESM modules
439
380
  // TODO: should also skip for `.js` with `type="module"`
440
- return !path.endsWith(".mjs") && "default" in mod;
381
+ return this.options.interopDefault === false ? false : !path.endsWith(".mjs") && "default" in mod;
441
382
  }
442
383
  importExternalModule(path) {
443
384
  return import(
@@ -454,12 +395,10 @@ class ViteNodeRunner {
454
395
  const { mod, defaultExport } = interopModule(importedModule);
455
396
  return new Proxy(mod, {
456
397
  get(mod, prop) {
457
- if (prop === "default") return defaultExport;
458
- return mod[prop] ?? (defaultExport === null || defaultExport === void 0 ? void 0 : defaultExport[prop]);
398
+ return prop === "default" ? defaultExport : mod[prop] ?? (defaultExport === null || defaultExport === void 0 ? void 0 : defaultExport[prop]);
459
399
  },
460
400
  has(mod, prop) {
461
- if (prop === "default") return defaultExport !== void 0;
462
- return prop in mod || defaultExport && prop in defaultExport;
401
+ return prop === "default" ? defaultExport !== void 0 : prop in mod || defaultExport && prop in defaultExport;
463
402
  },
464
403
  getOwnPropertyDescriptor(mod, prop) {
465
404
  const descriptor = Reflect.getOwnPropertyDescriptor(mod, prop);
@@ -480,8 +419,7 @@ function interopModule(mod) {
480
419
  };
481
420
  let defaultExport = "default" in mod ? mod.default : mod;
482
421
  if (!utils.isPrimitive(defaultExport) && "__esModule" in defaultExport) {
483
- mod = defaultExport;
484
- if ("default" in defaultExport) defaultExport = defaultExport.default;
422
+ if (mod = defaultExport, "default" in defaultExport) defaultExport = defaultExport.default;
485
423
  }
486
424
  return {
487
425
  mod,
@@ -497,13 +435,11 @@ function defineExport(exports, key, value) {
497
435
  });
498
436
  }
499
437
  function exportAll(exports, sourceModule) {
500
- // #1120 when a module exports itself it causes
501
- // call stack error
502
- if (exports === sourceModule) return;
503
- if (utils.isPrimitive(sourceModule) || Array.isArray(sourceModule) || sourceModule instanceof Promise) return;
504
- for (const key in sourceModule) if (key !== "default" && !(key in exports)) try {
505
- defineExport(exports, key, () => sourceModule[key]);
506
- } catch {}
438
+ if (exports !== sourceModule && !(utils.isPrimitive(sourceModule) || Array.isArray(sourceModule) || sourceModule instanceof Promise)) {
439
+ for (const key in sourceModule) if (key !== "default" && !(key in exports)) try {
440
+ defineExport(exports, key, () => sourceModule[key]);
441
+ } catch {}
442
+ }
507
443
  }
508
444
 
509
445
  exports.DEFAULT_REQUEST_STUBS = DEFAULT_REQUEST_STUBS;
package/dist/client.mjs CHANGED
@@ -8,10 +8,7 @@ import { createImportMetaEnvProxy, slash, isInternalRequest, isNodeBuiltin, norm
8
8
  import 'pathe';
9
9
  import 'node:fs';
10
10
 
11
- const { setTimeout, clearTimeout } = globalThis;
12
- const debugExecute = createDebug("vite-node:client:execute");
13
- const debugNative = createDebug("vite-node:client:native");
14
- const clientStub = {
11
+ const { setTimeout, clearTimeout } = globalThis, debugExecute = createDebug("vite-node:client:execute"), debugNative = createDebug("vite-node:client:native"), clientStub = {
15
12
  injectQuery: (id) => id,
16
13
  createHotContext: () => {
17
14
  return {
@@ -26,8 +23,7 @@ const clientStub = {
26
23
  },
27
24
  updateStyle: () => {},
28
25
  removeStyle: () => {}
29
- };
30
- const env = createImportMetaEnvProxy();
26
+ }, env = createImportMetaEnvProxy();
31
27
  const DEFAULT_REQUEST_STUBS = {
32
28
  "/@vite/client": clientStub,
33
29
  "@vite/client": clientStub
@@ -40,8 +36,7 @@ class ModuleCacheMap extends Map {
40
36
  * Assign partial data to the map
41
37
  */
42
38
  update(fsPath, mod) {
43
- fsPath = this.normalizePath(fsPath);
44
- if (!super.has(fsPath)) this.setByModuleId(fsPath, mod);
39
+ if (fsPath = this.normalizePath(fsPath), !super.has(fsPath)) this.setByModuleId(fsPath, mod);
45
40
  else Object.assign(super.get(fsPath), mod);
46
41
  return this;
47
42
  }
@@ -71,13 +66,7 @@ class ModuleCacheMap extends Map {
71
66
  }
72
67
  invalidateModule(mod) {
73
68
  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;
69
+ return delete mod.evaluated, delete mod.resolving, delete mod.promise, delete mod.exports, (_mod$importers = mod.importers) === null || _mod$importers === void 0 || _mod$importers.clear(), (_mod$imports = mod.imports) === null || _mod$imports === void 0 || _mod$imports.clear(), true;
81
70
  }
82
71
  /**
83
72
  * Invalidate modules that dependent on the given modules, up to the main entry
@@ -117,11 +106,7 @@ class ModuleCacheMap extends Map {
117
106
  const cache = this.get(id);
118
107
  if (cache.map) return cache.map;
119
108
  const map = cache.code && extractSourceMap(cache.code);
120
- if (map) {
121
- cache.map = map;
122
- return map;
123
- }
124
- return null;
109
+ return map ? (cache.map = map, map) : null;
125
110
  }
126
111
  }
127
112
  class ViteNodeRunner {
@@ -152,10 +137,7 @@ class ViteNodeRunner {
152
137
  // `performance` can be mocked, so make sure we're using the original function
153
138
  performanceNow = performance.now.bind(performance);
154
139
  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);
140
+ this.options = options, this.root = options.root ?? process.cwd(), this.moduleCache = options.moduleCache ?? new ModuleCacheMap(), this.debug = options.debug ?? (typeof process !== "undefined" ? !!process.env.VITE_NODE_DEBUG_RUNNER : false);
159
141
  }
160
142
  async executeFile(file) {
161
143
  const url = `/@fs/${slash(resolve(file))}`;
@@ -167,9 +149,7 @@ class ViteNodeRunner {
167
149
  }
168
150
  /** @internal */
169
151
  async cachedRequest(id, fsPath, callstack) {
170
- const importee = callstack[callstack.length - 1];
171
- const mod = this.moduleCache.get(fsPath);
172
- const { imports, importers } = mod;
152
+ const importee = callstack[callstack.length - 1], mod = this.moduleCache.get(fsPath), { imports, importers } = mod;
173
153
  if (importee) importers.add(importee);
174
154
  const getStack = () => `stack:\n${[...callstack, fsPath].reverse().map((p) => ` - ${p}`).join("\n")}`;
175
155
  // check circular dependency
@@ -182,14 +162,12 @@ class ViteNodeRunner {
182
162
  // cached module
183
163
  if (mod.promise) return await mod.promise;
184
164
  const promise = this.directRequest(id, fsPath, callstack);
185
- Object.assign(mod, {
165
+ return Object.assign(mod, {
186
166
  promise,
187
167
  evaluated: false
188
- });
189
- return await promise;
168
+ }), await promise;
190
169
  } finally {
191
- mod.evaluated = true;
192
- if (debugTimer) clearTimeout(debugTimer);
170
+ if (mod.evaluated = true, debugTimer) clearTimeout(debugTimer);
193
171
  }
194
172
  }
195
173
  shouldResolveId(id, _importee) {
@@ -208,18 +186,16 @@ class ViteNodeRunner {
208
186
 
209
187
  - If you rely on tsconfig.json's "paths" to resolve modules, please install "vite-tsconfig-paths" plugin to handle module resolution.
210
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`);
211
- Object.defineProperty(error, "code", {
189
+ throw Object.defineProperty(error, "code", {
212
190
  value: "ERR_MODULE_NOT_FOUND",
213
191
  enumerable: true
214
- });
215
- Object.defineProperty(error, Symbol.for("vitest.error.not_found.data"), {
192
+ }), Object.defineProperty(error, Symbol.for("vitest.error.not_found.data"), {
216
193
  value: {
217
194
  id: dep,
218
195
  importer
219
196
  },
220
197
  enumerable: false
221
- });
222
- throw error;
198
+ }), error;
223
199
  }
224
200
  const resolvedId = resolved ? normalizeRequestId(resolved.id, this.options.base) : dep;
225
201
  return [resolvedId, resolvedId];
@@ -245,47 +221,31 @@ class ViteNodeRunner {
245
221
  // rethrow vite error if it cannot load the module because it's not resolved
246
222
  if (typeof cause === "object" && cause.code === "ERR_LOAD_URL" || typeof (cause === null || cause === void 0 ? void 0 : cause.message) === "string" && cause.message.includes("Failed to load url")) {
247
223
  const error = new Error(`Cannot find ${isBareImport(id) ? "package" : "module"} '${id}'${importer ? ` imported from '${importer}'` : ""}`, { cause });
248
- error.code = "ERR_MODULE_NOT_FOUND";
249
- throw error;
224
+ throw error.code = "ERR_MODULE_NOT_FOUND", error;
250
225
  }
251
226
  throw cause;
252
227
  }
253
228
  }
254
229
  /** @internal */
255
230
  async directRequest(id, fsPath, _callstack) {
256
- const moduleId = normalizeModuleId(fsPath);
257
- const callstack = [..._callstack, moduleId];
258
- const mod = this.moduleCache.getByModuleId(moduleId);
259
- const request = async (dep) => {
260
- const [id, depFsPath] = await this.resolveUrl(String(dep), fsPath);
261
- const depMod = this.moduleCache.getByModuleId(depFsPath);
262
- depMod.importers.add(moduleId);
263
- mod.imports.add(depFsPath);
264
- return this.dependencyRequest(id, depFsPath, callstack);
265
- };
266
- const requestStubs = this.options.requestStubs || DEFAULT_REQUEST_STUBS;
231
+ const moduleId = normalizeModuleId(fsPath), callstack = [..._callstack, moduleId], mod = this.moduleCache.getByModuleId(moduleId), request = async (dep) => {
232
+ const [id, depFsPath] = await this.resolveUrl(String(dep), fsPath), depMod = this.moduleCache.getByModuleId(depFsPath);
233
+ return depMod.importers.add(moduleId), mod.imports.add(depFsPath), this.dependencyRequest(id, depFsPath, callstack);
234
+ }, requestStubs = this.options.requestStubs || DEFAULT_REQUEST_STUBS;
267
235
  if (id in requestStubs) return requestStubs[id];
268
236
  let { code: transformed, externalize } = await this._fetchModule(id, callstack[callstack.length - 2]);
269
237
  if (externalize) {
270
238
  debugNative(externalize);
271
239
  const exports = await this.interopedImport(externalize);
272
- mod.exports = exports;
273
- return exports;
240
+ return mod.exports = exports, exports;
274
241
  }
275
242
  if (transformed == null) throw new Error(`[vite-node] Failed to load "${id}" imported from ${callstack[callstack.length - 2]}`);
276
- const { Object, Reflect, Symbol } = this.getContextPrimitives();
277
- const modulePath = cleanUrl(moduleId);
278
- // disambiguate the `<UNIT>:/` on windows: see nodejs/node#31710
279
- const href = pathToFileURL(modulePath).href;
280
- const __filename = fileURLToPath(href);
281
- const __dirname = dirname(__filename);
282
- const meta = {
243
+ const { Object, Reflect, Symbol } = this.getContextPrimitives(), modulePath = cleanUrl(moduleId), href = pathToFileURL(modulePath).href, __filename = fileURLToPath(href), __dirname = dirname(__filename), meta = {
283
244
  url: href,
284
245
  env,
285
246
  filename: __filename,
286
247
  dirname: __dirname
287
- };
288
- const exports = Object.create(null);
248
+ }, exports = Object.create(null);
289
249
  Object.defineProperty(exports, Symbol.toStringTag, {
290
250
  value: "Module",
291
251
  enumerable: false,
@@ -297,25 +257,17 @@ class ViteNodeRunner {
297
257
  // inside the module itself. imported module is always "exports"
298
258
  const cjsExports = new Proxy(exports, {
299
259
  get: (target, p, receiver) => {
300
- if (Reflect.has(target, p)) return Reflect.get(target, p, receiver);
301
- return Reflect.get(Object.prototype, p, receiver);
260
+ return Reflect.has(target, p) ? Reflect.get(target, p, receiver) : Reflect.get(Object.prototype, p, receiver);
302
261
  },
303
262
  getPrototypeOf: () => Object.prototype,
304
263
  set: (_, p, value) => {
305
264
  // treat "module.exports =" the same as "exports.default =" to not have nested "default.default",
306
265
  // so "exports.default" becomes the actual module
307
- if (p === "default" && this.shouldInterop(modulePath, { default: value }) && cjsExports !== value) {
308
- exportAll(cjsExports, value);
309
- exports.default = value;
310
- return true;
311
- }
266
+ if (p === "default" && this.shouldInterop(modulePath, { default: value }) && cjsExports !== value) return exportAll(cjsExports, value), exports.default = value, true;
312
267
  if (!Reflect.has(exports, "default")) exports.default = {};
313
268
  // returns undefined, when accessing named exports, if default is not an object
314
269
  // but is still present inside hasOwnKeys, this is Node behaviour for CJS
315
- if (moduleExports !== SYMBOL_NOT_DEFINED && isPrimitive(moduleExports)) {
316
- defineExport(exports, p, () => void 0);
317
- return true;
318
- }
270
+ if (moduleExports !== SYMBOL_NOT_DEFINED && isPrimitive(moduleExports)) return defineExport(exports, p, () => void 0), true;
319
271
  if (!isPrimitive(exports.default)) exports.default[p] = value;
320
272
  if (p !== "default") defineExport(exports, p, () => value);
321
273
  return true;
@@ -327,9 +279,7 @@ class ViteNodeRunner {
327
279
  });
328
280
  const moduleProxy = {
329
281
  set exports(value) {
330
- exportAll(cjsExports, value);
331
- exports.default = value;
332
- moduleExports = value;
282
+ exportAll(cjsExports, value), exports.default = value, moduleExports = value;
333
283
  },
334
284
  get exports() {
335
285
  return cjsExports;
@@ -341,8 +291,7 @@ class ViteNodeRunner {
341
291
  enumerable: true,
342
292
  get: () => {
343
293
  var _this$options$createH, _this$options;
344
- 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));
345
- return hotContext;
294
+ return 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)), hotContext;
346
295
  },
347
296
  set: (value) => {
348
297
  hotContext = value;
@@ -369,11 +318,9 @@ class ViteNodeRunner {
369
318
  __filename,
370
319
  __dirname
371
320
  });
372
- debugExecute(__filename);
373
321
  // remove shebang
374
- if (transformed[0] === "#") transformed = transformed.replace(/^#!.*/, (s) => " ".repeat(s.length));
375
- await this.runModule(context, transformed);
376
- return exports;
322
+ if (debugExecute(__filename), transformed[0] === "#") transformed = transformed.replace(/^#!.*/, (s) => " ".repeat(s.length));
323
+ return await this.runModule(context, transformed), exports;
377
324
  }
378
325
  getContextPrimitives() {
379
326
  return {
@@ -384,14 +331,11 @@ class ViteNodeRunner {
384
331
  }
385
332
  async runModule(context, transformed) {
386
333
  // add 'use strict' since ESM enables it by default
387
- const codeDefinition = `'use strict';async (${Object.keys(context).join(",")})=>{{`;
388
- const code = `${codeDefinition}${transformed}\n}}`;
389
- const options = {
334
+ const codeDefinition = `'use strict';async (${Object.keys(context).join(",")})=>{{`, code = `${codeDefinition}${transformed}\n}}`, options = {
390
335
  filename: context.__filename,
391
336
  lineOffset: 0,
392
337
  columnOffset: -codeDefinition.length
393
- };
394
- const finishModuleExecutionInfo = this.startCalculateModuleExecutionInfo(options.filename, codeDefinition.length);
338
+ }, finishModuleExecutionInfo = this.startCalculateModuleExecutionInfo(options.filename, codeDefinition.length);
395
339
  try {
396
340
  const fn = vm.runInThisContext(code, options);
397
341
  await fn(...Object.values(context));
@@ -406,14 +350,12 @@ class ViteNodeRunner {
406
350
  */
407
351
  startCalculateModuleExecutionInfo(filename, startOffset) {
408
352
  const startTime = this.performanceNow();
409
- this.executionStack.push({
353
+ return this.executionStack.push({
410
354
  filename,
411
355
  startTime,
412
356
  subImportTime: 0
413
- });
414
- return () => {
415
- const duration = this.performanceNow() - startTime;
416
- const currentExecution = this.executionStack.pop();
357
+ }), () => {
358
+ const duration = this.performanceNow() - startTime, currentExecution = this.executionStack.pop();
417
359
  if (currentExecution == null) throw new Error("Execution stack is empty, this should never happen");
418
360
  const selfTime = duration - currentExecution.subImportTime;
419
361
  if (this.executionStack.length > 0) this.executionStack.at(-1).subImportTime += duration;
@@ -432,10 +374,9 @@ class ViteNodeRunner {
432
374
  * This function mostly for the ability to override by subclass
433
375
  */
434
376
  shouldInterop(path, mod) {
435
- if (this.options.interopDefault === false) return false;
436
377
  // never interop ESM modules
437
378
  // TODO: should also skip for `.js` with `type="module"`
438
- return !path.endsWith(".mjs") && "default" in mod;
379
+ return this.options.interopDefault === false ? false : !path.endsWith(".mjs") && "default" in mod;
439
380
  }
440
381
  importExternalModule(path) {
441
382
  return import(
@@ -452,12 +393,10 @@ class ViteNodeRunner {
452
393
  const { mod, defaultExport } = interopModule(importedModule);
453
394
  return new Proxy(mod, {
454
395
  get(mod, prop) {
455
- if (prop === "default") return defaultExport;
456
- return mod[prop] ?? (defaultExport === null || defaultExport === void 0 ? void 0 : defaultExport[prop]);
396
+ return prop === "default" ? defaultExport : mod[prop] ?? (defaultExport === null || defaultExport === void 0 ? void 0 : defaultExport[prop]);
457
397
  },
458
398
  has(mod, prop) {
459
- if (prop === "default") return defaultExport !== void 0;
460
- return prop in mod || defaultExport && prop in defaultExport;
399
+ return prop === "default" ? defaultExport !== void 0 : prop in mod || defaultExport && prop in defaultExport;
461
400
  },
462
401
  getOwnPropertyDescriptor(mod, prop) {
463
402
  const descriptor = Reflect.getOwnPropertyDescriptor(mod, prop);
@@ -478,8 +417,7 @@ function interopModule(mod) {
478
417
  };
479
418
  let defaultExport = "default" in mod ? mod.default : mod;
480
419
  if (!isPrimitive(defaultExport) && "__esModule" in defaultExport) {
481
- mod = defaultExport;
482
- if ("default" in defaultExport) defaultExport = defaultExport.default;
420
+ if (mod = defaultExport, "default" in defaultExport) defaultExport = defaultExport.default;
483
421
  }
484
422
  return {
485
423
  mod,
@@ -495,13 +433,11 @@ function defineExport(exports, key, value) {
495
433
  });
496
434
  }
497
435
  function exportAll(exports, sourceModule) {
498
- // #1120 when a module exports itself it causes
499
- // call stack error
500
- if (exports === sourceModule) return;
501
- if (isPrimitive(sourceModule) || Array.isArray(sourceModule) || sourceModule instanceof Promise) return;
502
- for (const key in sourceModule) if (key !== "default" && !(key in exports)) try {
503
- defineExport(exports, key, () => sourceModule[key]);
504
- } catch {}
436
+ if (exports !== sourceModule && !(isPrimitive(sourceModule) || Array.isArray(sourceModule) || sourceModule instanceof Promise)) {
437
+ for (const key in sourceModule) if (key !== "default" && !(key in exports)) try {
438
+ defineExport(exports, key, () => sourceModule[key]);
439
+ } catch {}
440
+ }
505
441
  }
506
442
 
507
443
  export { DEFAULT_REQUEST_STUBS, ModuleCacheMap, ViteNodeRunner };