vite-node 3.2.4 → 4.0.0-beta.10

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.at(-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) {
@@ -206,22 +184,20 @@ class ViteNodeRunner {
206
184
  const resolved = await this.options.resolveId(dep, importer);
207
185
  // supported since Vite 5-beta.19
208
186
  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) {
209
- const error = new Error(`Cannot find module '${id}'${importer ? ` imported from '${importer}'` : ""}.
187
+ const error = /* @__PURE__ */ new Error(`Cannot find module '${id}'${importer ? ` imported from '${importer}'` : ""}.
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.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- import './trace-mapping.d-DLVdEqOp.js';
2
- export { e as DEFAULT_REQUEST_STUBS, M as ModuleCacheMap, f as ModuleExecutionInfo, g as ModuleExecutionInfoEntry, a as ViteNodeRunner } from './index.d-DGmxD2U7.js';
1
+ import './trace-mapping.d-BWFx6tPc.js';
2
+ export { e as DEFAULT_REQUEST_STUBS, M as ModuleCacheMap, f as ModuleExecutionInfo, g as ModuleExecutionInfoEntry, a as ViteNodeRunner } from './index.d-uN06xifv.js';