vite-node 0.25.7 → 0.26.0

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/cli.cjs CHANGED
@@ -17,6 +17,7 @@ require('mlly');
17
17
  require('url');
18
18
  require('source-map-support');
19
19
  require('module');
20
+ require('path');
20
21
  require('vm');
21
22
 
22
23
  function toArr(any) {
@@ -632,7 +633,7 @@ class CAC extends events.EventEmitter {
632
633
 
633
634
  const cac = (name = "") => new CAC(name);
634
635
 
635
- var version = "0.25.7";
636
+ var version = "0.26.0";
636
637
 
637
638
  const cli = cac("vite-node");
638
639
  cli.version(version).option("-r, --root <path>", "Use specified root directory").option("-c, --config <path>", "Use specified config file").option("-w, --watch", 'Restart on file changes, similar to "nodemon"').option("--options <options>", "Use specified Vite server options").help();
package/dist/cli.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { e as ViteNodeServerOptions } from './types-c9a1a6b2.js';
1
+ import { e as ViteNodeServerOptions } from './types-aaf02e1c.js';
2
2
 
3
3
  interface CliOptions {
4
4
  root?: string;
@@ -7,10 +7,10 @@ interface CliOptions {
7
7
  options?: ViteNodeServerOptionsCLI;
8
8
  '--'?: string[];
9
9
  }
10
- declare type Optional<T> = T | undefined;
11
- declare type ComputeViteNodeServerOptionsCLI<T extends Record<string, any>> = {
10
+ type Optional<T> = T | undefined;
11
+ type ComputeViteNodeServerOptionsCLI<T extends Record<string, any>> = {
12
12
  [K in keyof T]: T[K] extends Optional<RegExp[]> ? string | string[] : T[K] extends Optional<(string | RegExp)[]> ? string | string[] : T[K] extends Optional<(string | RegExp)[] | true> ? string | string[] | true : T[K] extends Optional<Record<string, any>> ? ComputeViteNodeServerOptionsCLI<T[K]> : T[K];
13
13
  };
14
- declare type ViteNodeServerOptionsCLI = ComputeViteNodeServerOptionsCLI<ViteNodeServerOptions>;
14
+ type ViteNodeServerOptionsCLI = ComputeViteNodeServerOptionsCLI<ViteNodeServerOptions>;
15
15
 
16
16
  export { CliOptions, ViteNodeServerOptionsCLI };
package/dist/cli.mjs CHANGED
@@ -15,6 +15,7 @@ import 'mlly';
15
15
  import 'url';
16
16
  import 'source-map-support';
17
17
  import 'module';
18
+ import 'path';
18
19
  import 'vm';
19
20
 
20
21
  function toArr(any) {
@@ -630,7 +631,7 @@ class CAC extends EventEmitter {
630
631
 
631
632
  const cac = (name = "") => new CAC(name);
632
633
 
633
- var version = "0.25.7";
634
+ var version = "0.26.0";
634
635
 
635
636
  const cli = cac("vite-node");
636
637
  cli.version(version).option("-r, --root <path>", "Use specified root directory").option("-c, --config <path>", "Use specified config file").option("-w, --watch", 'Restart on file changes, similar to "nodemon"').option("--options <options>", "Use specified Vite server options").help();
package/dist/client.cjs CHANGED
@@ -3,10 +3,10 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var module$1 = require('module');
6
+ var path = require('path');
6
7
  var url = require('url');
7
8
  var vm = require('vm');
8
9
  var pathe = require('pathe');
9
- var mlly = require('mlly');
10
10
  var createDebug = require('debug');
11
11
  var utils = require('./utils.cjs');
12
12
  var sourceMap = require('./source-map.cjs');
@@ -146,19 +146,19 @@ class ViteNodeRunner {
146
146
  this.debug = options.debug ?? (typeof process !== "undefined" ? !!process.env.VITE_NODE_DEBUG_RUNNER : false);
147
147
  }
148
148
  async executeFile(file) {
149
- return await this.cachedRequest(`/@fs/${utils.slash(pathe.resolve(file))}`, []);
149
+ const url = `/@fs/${utils.slash(pathe.resolve(file))}`;
150
+ return await this.cachedRequest(url, url, []);
150
151
  }
151
- async executeId(id) {
152
- return await this.cachedRequest(id, []);
152
+ async executeId(rawId) {
153
+ const [id, url] = await this.resolveUrl(rawId);
154
+ return await this.cachedRequest(id, url, []);
153
155
  }
154
156
  getSourceMap(id) {
155
157
  return this.moduleCache.getSourceMap(id);
156
158
  }
157
- async cachedRequest(rawId, callstack) {
158
- const id = utils.normalizeRequestId(rawId, this.options.base);
159
- const fsPath = utils.toFilePath(id, this.root);
160
- const mod = this.moduleCache.get(fsPath);
159
+ async cachedRequest(id, fsPath, callstack) {
161
160
  const importee = callstack[callstack.length - 1];
161
+ const mod = this.moduleCache.get(fsPath);
162
162
  if (!mod.importers)
163
163
  mod.importers = /* @__PURE__ */ new Set();
164
164
  if (importee)
@@ -169,68 +169,60 @@ class ViteNodeRunner {
169
169
  return mod.promise;
170
170
  const promise = this.directRequest(id, fsPath, callstack);
171
171
  Object.assign(mod, { promise, evaluated: false });
172
- promise.finally(() => {
172
+ try {
173
+ return await promise;
174
+ } finally {
173
175
  mod.evaluated = true;
174
- });
175
- return await promise;
176
+ }
176
177
  }
177
- async directRequest(id, fsPath, _callstack) {
178
- const callstack = [..._callstack, fsPath];
179
- let mod = this.moduleCache.get(fsPath);
180
- const request = async (dep2) => {
181
- var _a;
182
- const depFsPath = utils.toFilePath(utils.normalizeRequestId(dep2, this.options.base), this.root);
183
- const getStack = () => {
184
- return `stack:
185
- ${[...callstack, depFsPath].reverse().map((p) => `- ${p}`).join("\n")}`;
186
- };
187
- let debugTimer;
188
- if (this.debug)
189
- debugTimer = setTimeout(() => console.warn(() => `module ${depFsPath} takes over 2s to load.
190
- ${getStack()}`), 2e3);
191
- try {
192
- if (callstack.includes(depFsPath)) {
193
- const depExports = (_a = this.moduleCache.get(depFsPath)) == null ? void 0 : _a.exports;
194
- if (depExports)
195
- return depExports;
196
- throw new Error(`[vite-node] Failed to resolve circular dependency, ${getStack()}`);
197
- }
198
- return await this.cachedRequest(dep2, callstack);
199
- } finally {
200
- if (debugTimer)
201
- clearTimeout(debugTimer);
202
- }
178
+ async resolveUrl(id, importee) {
179
+ if (utils.isInternalRequest(id))
180
+ return [id, id];
181
+ if (importee && id.startsWith(utils.VALID_ID_PREFIX))
182
+ importee = void 0;
183
+ id = utils.normalizeRequestId(id, this.options.base);
184
+ if (!this.options.resolveId)
185
+ return [id, utils.toFilePath(id, this.root)];
186
+ const resolved = await this.options.resolveId(id, importee);
187
+ const resolvedId = resolved ? utils.normalizeRequestId(resolved.id, this.options.base) : id;
188
+ const fsPath = resolved ? resolvedId : utils.toFilePath(id, this.root);
189
+ return [resolvedId, fsPath];
190
+ }
191
+ async dependencyRequest(id, fsPath, callstack) {
192
+ var _a;
193
+ const getStack = () => {
194
+ return `stack:
195
+ ${[...callstack, fsPath].reverse().map((p) => `- ${p}`).join("\n")}`;
203
196
  };
204
- Object.defineProperty(request, "callstack", { get: () => callstack });
205
- const resolveId = async (dep2, callstackPosition = 1) => {
206
- if (this.options.resolveId && this.shouldResolveId(dep2)) {
207
- let importer = callstack[callstack.length - callstackPosition];
208
- if (importer && !dep2.startsWith("."))
209
- importer = void 0;
210
- if (importer && importer.startsWith("mock:"))
211
- importer = importer.slice(5);
212
- const resolved = await this.options.resolveId(utils.normalizeRequestId(dep2), importer);
213
- return [dep2, resolved == null ? void 0 : resolved.id];
197
+ let debugTimer;
198
+ if (this.debug)
199
+ debugTimer = setTimeout(() => console.warn(() => `module ${fsPath} takes over 2s to load.
200
+ ${getStack()}`), 2e3);
201
+ try {
202
+ if (callstack.includes(fsPath)) {
203
+ const depExports = (_a = this.moduleCache.get(fsPath)) == null ? void 0 : _a.exports;
204
+ if (depExports)
205
+ return depExports;
206
+ throw new Error(`[vite-node] Failed to resolve circular dependency, ${getStack()}`);
214
207
  }
215
- return [dep2, void 0];
208
+ return await this.cachedRequest(id, fsPath, callstack);
209
+ } finally {
210
+ if (debugTimer)
211
+ clearTimeout(debugTimer);
212
+ }
213
+ }
214
+ async directRequest(id, fsPath, _callstack) {
215
+ const moduleId = utils.normalizeModuleId(fsPath);
216
+ const callstack = [..._callstack, moduleId];
217
+ const mod = this.moduleCache.get(fsPath);
218
+ const request = async (dep) => {
219
+ const [id2, depFsPath] = await this.resolveUrl(dep, fsPath);
220
+ return this.dependencyRequest(id2, depFsPath, callstack);
216
221
  };
217
- const [dep, resolvedId] = await resolveId(id, 2);
218
222
  const requestStubs = this.options.requestStubs || DEFAULT_REQUEST_STUBS;
219
223
  if (id in requestStubs)
220
224
  return requestStubs[id];
221
- let { code: transformed, externalize } = await this.options.fetchModule(resolvedId || dep);
222
- if (resolvedId && !fsPath.includes("?") && fsPath !== resolvedId) {
223
- if (this.moduleCache.has(resolvedId)) {
224
- mod = this.moduleCache.get(resolvedId);
225
- this.moduleCache.set(fsPath, mod);
226
- if (mod.promise)
227
- return mod.promise;
228
- if (mod.exports)
229
- return mod.exports;
230
- } else {
231
- this.moduleCache.set(resolvedId, mod);
232
- }
233
- }
225
+ let { code: transformed, externalize } = await this.options.fetchModule(id);
234
226
  if (externalize) {
235
227
  debugNative(externalize);
236
228
  const exports2 = await this.interopedImport(externalize);
@@ -238,10 +230,10 @@ ${getStack()}`), 2e3);
238
230
  return exports2;
239
231
  }
240
232
  if (transformed == null)
241
- throw new Error(`[vite-node] Failed to load ${id}`);
242
- const file = utils.cleanUrl(resolvedId || fsPath);
243
- const url$1 = url.pathToFileURL(file).href;
244
- const meta = { url: url$1 };
233
+ throw new Error(`[vite-node] Failed to load "${id}" imported from ${callstack[callstack.length - 2]}`);
234
+ const modulePath = utils.cleanUrl(moduleId);
235
+ const href = url.pathToFileURL(modulePath).href;
236
+ const meta = { url: href };
245
237
  const exports = /* @__PURE__ */ Object.create(null);
246
238
  Object.defineProperty(exports, Symbol.toStringTag, {
247
239
  value: "Module",
@@ -249,7 +241,12 @@ ${getStack()}`), 2e3);
249
241
  configurable: false
250
242
  });
251
243
  const cjsExports = new Proxy(exports, {
252
- set(_, p, value) {
244
+ set: (_, p, value) => {
245
+ if (p === "default" && this.shouldInterop(modulePath, { default: value })) {
246
+ exportAll(cjsExports, value);
247
+ exports.default = value;
248
+ return true;
249
+ }
253
250
  if (!Reflect.has(exports, "default"))
254
251
  exports.default = {};
255
252
  if (utils.isPrimitive(exports.default)) {
@@ -263,7 +260,7 @@ ${getStack()}`), 2e3);
263
260
  }
264
261
  });
265
262
  Object.assign(mod, { code: transformed, exports });
266
- const __filename = url.fileURLToPath(url$1);
263
+ const __filename = url.fileURLToPath(href);
267
264
  const moduleProxy = {
268
265
  set exports(value) {
269
266
  exportAll(cjsExports, value);
@@ -281,6 +278,9 @@ ${getStack()}`), 2e3);
281
278
  var _a, _b;
282
279
  hotContext || (hotContext = (_b = (_a = this.options).createHotContext) == null ? void 0 : _b.call(_a, this, `/@fs/${fsPath}`));
283
280
  return hotContext;
281
+ },
282
+ set: (value) => {
283
+ hotContext = value;
284
284
  }
285
285
  });
286
286
  }
@@ -290,12 +290,11 @@ ${getStack()}`), 2e3);
290
290
  __vite_ssr_exports__: exports,
291
291
  __vite_ssr_exportAll__: (obj) => exportAll(exports, obj),
292
292
  __vite_ssr_import_meta__: meta,
293
- __vitest_resolve_id__: resolveId,
294
- require: module$1.createRequire(url$1),
293
+ require: module$1.createRequire(href),
295
294
  exports: cjsExports,
296
295
  module: moduleProxy,
297
296
  __filename,
298
- __dirname: pathe.dirname(__filename)
297
+ __dirname: path.dirname(__filename)
299
298
  });
300
299
  debugExecute(__filename);
301
300
  if (transformed[0] === "#")
@@ -314,42 +313,44 @@ ${getStack()}`), 2e3);
314
313
  prepareContext(context) {
315
314
  return context;
316
315
  }
317
- shouldResolveId(dep) {
318
- if (mlly.isNodeBuiltin(dep) || dep in (this.options.requestStubs || DEFAULT_REQUEST_STUBS) || dep.startsWith("/@vite"))
319
- return false;
320
- return !pathe.isAbsolute(dep) || !pathe.extname(dep);
321
- }
322
316
  shouldInterop(path, mod) {
323
317
  if (this.options.interopDefault === false)
324
318
  return false;
325
319
  return !path.endsWith(".mjs") && "default" in mod;
326
320
  }
327
321
  async interopedImport(path) {
328
- const mod = await (function (t) { return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(t)); }); })(path);
329
- if (this.shouldInterop(path, mod)) {
330
- const tryDefault = this.hasNestedDefault(mod);
331
- return new Proxy(mod, {
332
- get: proxyMethod("get", tryDefault),
333
- set: proxyMethod("set", tryDefault),
334
- has: proxyMethod("has", tryDefault),
335
- deleteProperty: proxyMethod("deleteProperty", tryDefault)
336
- });
337
- }
338
- return mod;
339
- }
340
- hasNestedDefault(target) {
341
- return "__esModule" in target && target.__esModule && "default" in target.default;
322
+ const importedModule = await (function (t) { return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(t)); }); })(path);
323
+ if (!this.shouldInterop(path, importedModule))
324
+ return importedModule;
325
+ const { mod, defaultExport } = interopModule(importedModule);
326
+ return new Proxy(mod, {
327
+ get(mod2, prop) {
328
+ if (prop === "default")
329
+ return defaultExport;
330
+ return mod2[prop] ?? (defaultExport == null ? void 0 : defaultExport[prop]);
331
+ },
332
+ has(mod2, prop) {
333
+ if (prop === "default")
334
+ return defaultExport !== void 0;
335
+ return prop in mod2 || defaultExport && prop in defaultExport;
336
+ }
337
+ });
342
338
  }
343
339
  }
344
- function proxyMethod(name, tryDefault) {
345
- return function(target, key, ...args) {
346
- const result = Reflect[name](target, key, ...args);
347
- if (utils.isPrimitive(target.default))
348
- return result;
349
- if (tryDefault && key === "default" || typeof result === "undefined")
350
- return Reflect[name](target.default, key, ...args);
351
- return result;
352
- };
340
+ function interopModule(mod) {
341
+ if (utils.isPrimitive(mod)) {
342
+ return {
343
+ mod: { default: mod },
344
+ defaultExport: mod
345
+ };
346
+ }
347
+ let defaultExport = "default" in mod ? mod.default : mod;
348
+ if (!utils.isPrimitive(defaultExport) && "__esModule" in defaultExport) {
349
+ mod = defaultExport;
350
+ if ("default" in defaultExport)
351
+ defaultExport = defaultExport.default;
352
+ }
353
+ return { mod, defaultExport };
353
354
  }
354
355
  function defineExport(exports, key, value) {
355
356
  Object.defineProperty(exports, key, {
package/dist/client.d.ts CHANGED
@@ -1 +1 @@
1
- export { j as DEFAULT_REQUEST_STUBS, M as ModuleCacheMap, h as ViteNodeRunner } from './types-c9a1a6b2.js';
1
+ export { j as DEFAULT_REQUEST_STUBS, M as ModuleCacheMap, h as ViteNodeRunner } from './types-aaf02e1c.js';
package/dist/client.mjs CHANGED
@@ -1,10 +1,10 @@
1
1
  import { createRequire } from 'module';
2
+ import { dirname } from 'path';
2
3
  import { pathToFileURL, fileURLToPath } from 'url';
3
4
  import vm from 'vm';
4
- import { resolve, dirname, isAbsolute, extname } from 'pathe';
5
- import { isNodeBuiltin } from 'mlly';
5
+ import { resolve } from 'pathe';
6
6
  import createDebug from 'debug';
7
- import { normalizeModuleId, slash, normalizeRequestId, toFilePath, cleanUrl, isPrimitive } from './utils.mjs';
7
+ import { normalizeModuleId, slash, isInternalRequest, VALID_ID_PREFIX, normalizeRequestId, toFilePath, cleanUrl, isPrimitive } from './utils.mjs';
8
8
  import { extractSourceMap } from './source-map.mjs';
9
9
  import 'fs';
10
10
  import 'source-map-support';
@@ -119,19 +119,19 @@ class ViteNodeRunner {
119
119
  this.debug = options.debug ?? (typeof process !== "undefined" ? !!process.env.VITE_NODE_DEBUG_RUNNER : false);
120
120
  }
121
121
  async executeFile(file) {
122
- return await this.cachedRequest(`/@fs/${slash(resolve(file))}`, []);
122
+ const url = `/@fs/${slash(resolve(file))}`;
123
+ return await this.cachedRequest(url, url, []);
123
124
  }
124
- async executeId(id) {
125
- return await this.cachedRequest(id, []);
125
+ async executeId(rawId) {
126
+ const [id, url] = await this.resolveUrl(rawId);
127
+ return await this.cachedRequest(id, url, []);
126
128
  }
127
129
  getSourceMap(id) {
128
130
  return this.moduleCache.getSourceMap(id);
129
131
  }
130
- async cachedRequest(rawId, callstack) {
131
- const id = normalizeRequestId(rawId, this.options.base);
132
- const fsPath = toFilePath(id, this.root);
133
- const mod = this.moduleCache.get(fsPath);
132
+ async cachedRequest(id, fsPath, callstack) {
134
133
  const importee = callstack[callstack.length - 1];
134
+ const mod = this.moduleCache.get(fsPath);
135
135
  if (!mod.importers)
136
136
  mod.importers = /* @__PURE__ */ new Set();
137
137
  if (importee)
@@ -142,68 +142,60 @@ class ViteNodeRunner {
142
142
  return mod.promise;
143
143
  const promise = this.directRequest(id, fsPath, callstack);
144
144
  Object.assign(mod, { promise, evaluated: false });
145
- promise.finally(() => {
145
+ try {
146
+ return await promise;
147
+ } finally {
146
148
  mod.evaluated = true;
147
- });
148
- return await promise;
149
+ }
149
150
  }
150
- async directRequest(id, fsPath, _callstack) {
151
- const callstack = [..._callstack, fsPath];
152
- let mod = this.moduleCache.get(fsPath);
153
- const request = async (dep2) => {
154
- var _a;
155
- const depFsPath = toFilePath(normalizeRequestId(dep2, this.options.base), this.root);
156
- const getStack = () => {
157
- return `stack:
158
- ${[...callstack, depFsPath].reverse().map((p) => `- ${p}`).join("\n")}`;
159
- };
160
- let debugTimer;
161
- if (this.debug)
162
- debugTimer = setTimeout(() => console.warn(() => `module ${depFsPath} takes over 2s to load.
163
- ${getStack()}`), 2e3);
164
- try {
165
- if (callstack.includes(depFsPath)) {
166
- const depExports = (_a = this.moduleCache.get(depFsPath)) == null ? void 0 : _a.exports;
167
- if (depExports)
168
- return depExports;
169
- throw new Error(`[vite-node] Failed to resolve circular dependency, ${getStack()}`);
170
- }
171
- return await this.cachedRequest(dep2, callstack);
172
- } finally {
173
- if (debugTimer)
174
- clearTimeout(debugTimer);
175
- }
151
+ async resolveUrl(id, importee) {
152
+ if (isInternalRequest(id))
153
+ return [id, id];
154
+ if (importee && id.startsWith(VALID_ID_PREFIX))
155
+ importee = void 0;
156
+ id = normalizeRequestId(id, this.options.base);
157
+ if (!this.options.resolveId)
158
+ return [id, toFilePath(id, this.root)];
159
+ const resolved = await this.options.resolveId(id, importee);
160
+ const resolvedId = resolved ? normalizeRequestId(resolved.id, this.options.base) : id;
161
+ const fsPath = resolved ? resolvedId : toFilePath(id, this.root);
162
+ return [resolvedId, fsPath];
163
+ }
164
+ async dependencyRequest(id, fsPath, callstack) {
165
+ var _a;
166
+ const getStack = () => {
167
+ return `stack:
168
+ ${[...callstack, fsPath].reverse().map((p) => `- ${p}`).join("\n")}`;
176
169
  };
177
- Object.defineProperty(request, "callstack", { get: () => callstack });
178
- const resolveId = async (dep2, callstackPosition = 1) => {
179
- if (this.options.resolveId && this.shouldResolveId(dep2)) {
180
- let importer = callstack[callstack.length - callstackPosition];
181
- if (importer && !dep2.startsWith("."))
182
- importer = void 0;
183
- if (importer && importer.startsWith("mock:"))
184
- importer = importer.slice(5);
185
- const resolved = await this.options.resolveId(normalizeRequestId(dep2), importer);
186
- return [dep2, resolved == null ? void 0 : resolved.id];
170
+ let debugTimer;
171
+ if (this.debug)
172
+ debugTimer = setTimeout(() => console.warn(() => `module ${fsPath} takes over 2s to load.
173
+ ${getStack()}`), 2e3);
174
+ try {
175
+ if (callstack.includes(fsPath)) {
176
+ const depExports = (_a = this.moduleCache.get(fsPath)) == null ? void 0 : _a.exports;
177
+ if (depExports)
178
+ return depExports;
179
+ throw new Error(`[vite-node] Failed to resolve circular dependency, ${getStack()}`);
187
180
  }
188
- return [dep2, void 0];
181
+ return await this.cachedRequest(id, fsPath, callstack);
182
+ } finally {
183
+ if (debugTimer)
184
+ clearTimeout(debugTimer);
185
+ }
186
+ }
187
+ async directRequest(id, fsPath, _callstack) {
188
+ const moduleId = normalizeModuleId(fsPath);
189
+ const callstack = [..._callstack, moduleId];
190
+ const mod = this.moduleCache.get(fsPath);
191
+ const request = async (dep) => {
192
+ const [id2, depFsPath] = await this.resolveUrl(dep, fsPath);
193
+ return this.dependencyRequest(id2, depFsPath, callstack);
189
194
  };
190
- const [dep, resolvedId] = await resolveId(id, 2);
191
195
  const requestStubs = this.options.requestStubs || DEFAULT_REQUEST_STUBS;
192
196
  if (id in requestStubs)
193
197
  return requestStubs[id];
194
- let { code: transformed, externalize } = await this.options.fetchModule(resolvedId || dep);
195
- if (resolvedId && !fsPath.includes("?") && fsPath !== resolvedId) {
196
- if (this.moduleCache.has(resolvedId)) {
197
- mod = this.moduleCache.get(resolvedId);
198
- this.moduleCache.set(fsPath, mod);
199
- if (mod.promise)
200
- return mod.promise;
201
- if (mod.exports)
202
- return mod.exports;
203
- } else {
204
- this.moduleCache.set(resolvedId, mod);
205
- }
206
- }
198
+ let { code: transformed, externalize } = await this.options.fetchModule(id);
207
199
  if (externalize) {
208
200
  debugNative(externalize);
209
201
  const exports2 = await this.interopedImport(externalize);
@@ -211,10 +203,10 @@ ${getStack()}`), 2e3);
211
203
  return exports2;
212
204
  }
213
205
  if (transformed == null)
214
- throw new Error(`[vite-node] Failed to load ${id}`);
215
- const file = cleanUrl(resolvedId || fsPath);
216
- const url = pathToFileURL(file).href;
217
- const meta = { url };
206
+ throw new Error(`[vite-node] Failed to load "${id}" imported from ${callstack[callstack.length - 2]}`);
207
+ const modulePath = cleanUrl(moduleId);
208
+ const href = pathToFileURL(modulePath).href;
209
+ const meta = { url: href };
218
210
  const exports = /* @__PURE__ */ Object.create(null);
219
211
  Object.defineProperty(exports, Symbol.toStringTag, {
220
212
  value: "Module",
@@ -222,7 +214,12 @@ ${getStack()}`), 2e3);
222
214
  configurable: false
223
215
  });
224
216
  const cjsExports = new Proxy(exports, {
225
- set(_, p, value) {
217
+ set: (_, p, value) => {
218
+ if (p === "default" && this.shouldInterop(modulePath, { default: value })) {
219
+ exportAll(cjsExports, value);
220
+ exports.default = value;
221
+ return true;
222
+ }
226
223
  if (!Reflect.has(exports, "default"))
227
224
  exports.default = {};
228
225
  if (isPrimitive(exports.default)) {
@@ -236,7 +233,7 @@ ${getStack()}`), 2e3);
236
233
  }
237
234
  });
238
235
  Object.assign(mod, { code: transformed, exports });
239
- const __filename = fileURLToPath(url);
236
+ const __filename = fileURLToPath(href);
240
237
  const moduleProxy = {
241
238
  set exports(value) {
242
239
  exportAll(cjsExports, value);
@@ -254,6 +251,9 @@ ${getStack()}`), 2e3);
254
251
  var _a, _b;
255
252
  hotContext || (hotContext = (_b = (_a = this.options).createHotContext) == null ? void 0 : _b.call(_a, this, `/@fs/${fsPath}`));
256
253
  return hotContext;
254
+ },
255
+ set: (value) => {
256
+ hotContext = value;
257
257
  }
258
258
  });
259
259
  }
@@ -263,8 +263,7 @@ ${getStack()}`), 2e3);
263
263
  __vite_ssr_exports__: exports,
264
264
  __vite_ssr_exportAll__: (obj) => exportAll(exports, obj),
265
265
  __vite_ssr_import_meta__: meta,
266
- __vitest_resolve_id__: resolveId,
267
- require: createRequire(url),
266
+ require: createRequire(href),
268
267
  exports: cjsExports,
269
268
  module: moduleProxy,
270
269
  __filename,
@@ -287,42 +286,44 @@ ${getStack()}`), 2e3);
287
286
  prepareContext(context) {
288
287
  return context;
289
288
  }
290
- shouldResolveId(dep) {
291
- if (isNodeBuiltin(dep) || dep in (this.options.requestStubs || DEFAULT_REQUEST_STUBS) || dep.startsWith("/@vite"))
292
- return false;
293
- return !isAbsolute(dep) || !extname(dep);
294
- }
295
289
  shouldInterop(path, mod) {
296
290
  if (this.options.interopDefault === false)
297
291
  return false;
298
292
  return !path.endsWith(".mjs") && "default" in mod;
299
293
  }
300
294
  async interopedImport(path) {
301
- const mod = await import(path);
302
- if (this.shouldInterop(path, mod)) {
303
- const tryDefault = this.hasNestedDefault(mod);
304
- return new Proxy(mod, {
305
- get: proxyMethod("get", tryDefault),
306
- set: proxyMethod("set", tryDefault),
307
- has: proxyMethod("has", tryDefault),
308
- deleteProperty: proxyMethod("deleteProperty", tryDefault)
309
- });
310
- }
311
- return mod;
312
- }
313
- hasNestedDefault(target) {
314
- return "__esModule" in target && target.__esModule && "default" in target.default;
295
+ const importedModule = await import(path);
296
+ if (!this.shouldInterop(path, importedModule))
297
+ return importedModule;
298
+ const { mod, defaultExport } = interopModule(importedModule);
299
+ return new Proxy(mod, {
300
+ get(mod2, prop) {
301
+ if (prop === "default")
302
+ return defaultExport;
303
+ return mod2[prop] ?? (defaultExport == null ? void 0 : defaultExport[prop]);
304
+ },
305
+ has(mod2, prop) {
306
+ if (prop === "default")
307
+ return defaultExport !== void 0;
308
+ return prop in mod2 || defaultExport && prop in defaultExport;
309
+ }
310
+ });
315
311
  }
316
312
  }
317
- function proxyMethod(name, tryDefault) {
318
- return function(target, key, ...args) {
319
- const result = Reflect[name](target, key, ...args);
320
- if (isPrimitive(target.default))
321
- return result;
322
- if (tryDefault && key === "default" || typeof result === "undefined")
323
- return Reflect[name](target.default, key, ...args);
324
- return result;
325
- };
313
+ function interopModule(mod) {
314
+ if (isPrimitive(mod)) {
315
+ return {
316
+ mod: { default: mod },
317
+ defaultExport: mod
318
+ };
319
+ }
320
+ let defaultExport = "default" in mod ? mod.default : mod;
321
+ if (!isPrimitive(defaultExport) && "__esModule" in defaultExport) {
322
+ mod = defaultExport;
323
+ if ("default" in defaultExport)
324
+ defaultExport = defaultExport.default;
325
+ }
326
+ return { mod, defaultExport };
326
327
  }
327
328
  function defineExport(exports, key, value) {
328
329
  Object.defineProperty(exports, key, {
package/dist/hmr.d.ts CHANGED
@@ -1,16 +1,16 @@
1
1
  import { EventEmitter } from 'events';
2
2
  import { HMRPayload, Plugin } from 'vite';
3
- import { g as CustomEventMap, h as ViteNodeRunner, i as HMRPayload$1, H as HotContext } from './types-c9a1a6b2.js';
3
+ import { g as CustomEventMap, h as ViteNodeRunner, i as HMRPayload$1, H as HotContext } from './types-aaf02e1c.js';
4
4
 
5
- declare type EventType = string | symbol;
6
- declare type Handler<T = unknown> = (event: T) => void;
5
+ type EventType = string | symbol;
6
+ type Handler<T = unknown> = (event: T) => void;
7
7
  interface Emitter<Events extends Record<EventType, unknown>> {
8
8
  on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): void;
9
9
  off<Key extends keyof Events>(type: Key, handler?: Handler<Events[Key]>): void;
10
10
  emit<Key extends keyof Events>(type: Key, event: Events[Key]): void;
11
11
  emit<Key extends keyof Events>(type: undefined extends Events[Key] ? Key : never): void;
12
12
  }
13
- declare type HMREmitter = Emitter<{
13
+ type HMREmitter = Emitter<{
14
14
  'message': HMRPayload;
15
15
  }> & EventEmitter;
16
16
  declare module 'vite' {
@@ -21,7 +21,7 @@ declare module 'vite' {
21
21
  declare function createHmrEmitter(): HMREmitter;
22
22
  declare function viteNodeHmrPlugin(): Plugin;
23
23
 
24
- declare type InferCustomEventPayload<T extends string> = T extends keyof CustomEventMap ? CustomEventMap[T] : any;
24
+ type InferCustomEventPayload<T extends string> = T extends keyof CustomEventMap ? CustomEventMap[T] : any;
25
25
  interface HotModule {
26
26
  id: string;
27
27
  callbacks: HotCallback[];
package/dist/index.d.ts CHANGED
@@ -1 +1 @@
1
- export { A as Arrayable, C as CreateHotContextFunction, f as DebuggerOptions, D as DepsHandlingOptions, a as FetchFunction, F as FetchResult, H as HotContext, c as ModuleCache, M as ModuleCacheMap, N as Nullable, R as RawSourceMap, b as ResolveIdFunction, S as StartOfSourceMap, d as ViteNodeResolveId, V as ViteNodeRunnerOptions, e as ViteNodeServerOptions } from './types-c9a1a6b2.js';
1
+ export { A as Arrayable, C as CreateHotContextFunction, f as DebuggerOptions, D as DepsHandlingOptions, a as FetchFunction, F as FetchResult, H as HotContext, c as ModuleCache, M as ModuleCacheMap, N as Nullable, R as RawSourceMap, b as ResolveIdFunction, S as StartOfSourceMap, d as ViteNodeResolveId, V as ViteNodeRunnerOptions, e as ViteNodeServerOptions } from './types-aaf02e1c.js';
package/dist/server.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { TransformResult, ViteDevServer } from 'vite';
2
- import { f as DebuggerOptions, D as DepsHandlingOptions, e as ViteNodeServerOptions, F as FetchResult, d as ViteNodeResolveId, R as RawSourceMap } from './types-c9a1a6b2.js';
2
+ import { f as DebuggerOptions, D as DepsHandlingOptions, e as ViteNodeServerOptions, F as FetchResult, d as ViteNodeResolveId, R as RawSourceMap } from './types-aaf02e1c.js';
3
3
 
4
4
  declare class Debugger {
5
5
  options: DebuggerOptions;
@@ -1,5 +1,5 @@
1
1
  import { TransformResult } from 'vite';
2
- import { R as RawSourceMap } from './types-c9a1a6b2.js';
2
+ import { R as RawSourceMap } from './types-aaf02e1c.js';
3
3
 
4
4
  interface InstallSourceMapSupportOptions {
5
5
  getSourceMap: (source: string) => RawSourceMap | null | undefined;
@@ -155,14 +155,16 @@ declare class ViteNodeRunner {
155
155
  moduleCache: ModuleCacheMap;
156
156
  constructor(options: ViteNodeRunnerOptions);
157
157
  executeFile(file: string): Promise<any>;
158
- executeId(id: string): Promise<any>;
158
+ executeId(rawId: string): Promise<any>;
159
159
  getSourceMap(id: string): RawSourceMap | null;
160
160
  /** @internal */
161
- cachedRequest(rawId: string, callstack: string[]): Promise<any>;
161
+ cachedRequest(id: string, fsPath: string, callstack: string[]): Promise<any>;
162
+ resolveUrl(id: string, importee?: string): Promise<[url: string, fsPath: string]>;
163
+ /** @internal */
164
+ dependencyRequest(id: string, fsPath: string, callstack: string[]): Promise<any>;
162
165
  /** @internal */
163
166
  directRequest(id: string, fsPath: string, _callstack: string[]): Promise<any>;
164
167
  prepareContext(context: Record<string, any>): Record<string, any>;
165
- shouldResolveId(dep: string): boolean;
166
168
  /**
167
169
  * Define if a module should be interop-ed
168
170
  * This function mostly for the ability to override by subclass
@@ -172,11 +174,10 @@ declare class ViteNodeRunner {
172
174
  * Import a module and interop it
173
175
  */
174
176
  interopedImport(path: string): Promise<any>;
175
- hasNestedDefault(target: any): any;
176
177
  }
177
178
 
178
- declare type Nullable<T> = T | null | undefined;
179
- declare type Arrayable<T> = T | Array<T>;
179
+ type Nullable<T> = T | null | undefined;
180
+ type Arrayable<T> = T | Array<T>;
180
181
  interface DepsHandlingOptions {
181
182
  external?: (string | RegExp)[];
182
183
  inline?: (string | RegExp)[] | true;
@@ -202,10 +203,10 @@ interface FetchResult {
202
203
  externalize?: string;
203
204
  map?: RawSourceMap;
204
205
  }
205
- declare type HotContext = Omit<ViteHotContext, 'acceptDeps' | 'decline'>;
206
- declare type FetchFunction = (id: string) => Promise<FetchResult>;
207
- declare type ResolveIdFunction = (id: string, importer?: string) => Promise<ViteNodeResolveId | null>;
208
- declare type CreateHotContextFunction = (runner: ViteNodeRunner, url: string) => HotContext;
206
+ type HotContext = Omit<ViteHotContext, 'acceptDeps' | 'decline'>;
207
+ type FetchFunction = (id: string) => Promise<FetchResult>;
208
+ type ResolveIdFunction = (id: string, importer?: string) => Promise<ViteNodeResolveId | null>;
209
+ type CreateHotContextFunction = (runner: ViteNodeRunner, url: string) => HotContext;
209
210
  interface ModuleCache {
210
211
  promise?: Promise<any>;
211
212
  exports?: any;
package/dist/types.d.ts CHANGED
@@ -1 +1 @@
1
- export { A as Arrayable, C as CreateHotContextFunction, f as DebuggerOptions, D as DepsHandlingOptions, a as FetchFunction, F as FetchResult, H as HotContext, c as ModuleCache, M as ModuleCacheMap, N as Nullable, R as RawSourceMap, b as ResolveIdFunction, S as StartOfSourceMap, d as ViteNodeResolveId, V as ViteNodeRunnerOptions, e as ViteNodeServerOptions } from './types-c9a1a6b2.js';
1
+ export { A as Arrayable, C as CreateHotContextFunction, f as DebuggerOptions, D as DepsHandlingOptions, a as FetchFunction, F as FetchResult, H as HotContext, c as ModuleCache, M as ModuleCacheMap, N as Nullable, R as RawSourceMap, b as ResolveIdFunction, S as StartOfSourceMap, d as ViteNodeResolveId, V as ViteNodeRunnerOptions, e as ViteNodeServerOptions } from './types-aaf02e1c.js';
package/dist/utils.cjs CHANGED
@@ -5,7 +5,6 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var url = require('url');
6
6
  var fs = require('fs');
7
7
  var pathe = require('pathe');
8
- var mlly = require('mlly');
9
8
 
10
9
  const isWindows = process.platform === "win32";
11
10
  function slash(str) {
@@ -14,6 +13,7 @@ function slash(str) {
14
13
  function mergeSlashes(str) {
15
14
  return str.replace(/\/\//g, "/");
16
15
  }
16
+ const VALID_ID_PREFIX = "/@id/";
17
17
  function normalizeRequestId(id, base) {
18
18
  if (base && id.startsWith(base))
19
19
  id = `/${id.slice(base.length)}`;
@@ -22,23 +22,15 @@ function normalizeRequestId(id, base) {
22
22
  const queryRE = /\?.*$/s;
23
23
  const hashRE = /#.*$/s;
24
24
  const cleanUrl = (url) => url.replace(hashRE, "").replace(queryRE, "");
25
+ const isInternalRequest = (id) => {
26
+ return id.startsWith("/@vite/");
27
+ };
25
28
  function normalizeModuleId(id) {
26
- return id.replace(/\\/g, "/").replace(/^\/@fs\//, "/").replace(/^file:\//, "/").replace(/^\/+/, "/");
29
+ return id.replace(/\\/g, "/").replace(/^\/@fs\//, isWindows ? "" : "/").replace(/^file:\//, "/").replace(/^\/+/, "/");
27
30
  }
28
31
  function isPrimitive(v) {
29
32
  return v !== Object(v);
30
33
  }
31
- function pathFromRoot(root, filename) {
32
- if (mlly.isNodeBuiltin(filename))
33
- return filename;
34
- filename = filename.replace(/^\/@fs\//, isWindows ? "" : "/");
35
- if (!filename.startsWith(root))
36
- return filename;
37
- const relativePath = pathe.relative(root, filename);
38
- const segments = relativePath.split("/");
39
- const startIndex = segments.findIndex((segment) => segment !== ".." && segment !== ".");
40
- return `/${segments.slice(startIndex).join("/")}`;
41
- }
42
34
  function toFilePath(id, root) {
43
35
  let absolute = (() => {
44
36
  if (id.startsWith("/@fs/"))
@@ -62,14 +54,15 @@ function toArray(array) {
62
54
  return [array];
63
55
  }
64
56
 
57
+ exports.VALID_ID_PREFIX = VALID_ID_PREFIX;
65
58
  exports.cleanUrl = cleanUrl;
66
59
  exports.hashRE = hashRE;
60
+ exports.isInternalRequest = isInternalRequest;
67
61
  exports.isPrimitive = isPrimitive;
68
62
  exports.isWindows = isWindows;
69
63
  exports.mergeSlashes = mergeSlashes;
70
64
  exports.normalizeModuleId = normalizeModuleId;
71
65
  exports.normalizeRequestId = normalizeRequestId;
72
- exports.pathFromRoot = pathFromRoot;
73
66
  exports.queryRE = queryRE;
74
67
  exports.slash = slash;
75
68
  exports.toArray = toArray;
package/dist/utils.d.ts CHANGED
@@ -1,15 +1,16 @@
1
- import { N as Nullable, A as Arrayable } from './types-c9a1a6b2.js';
1
+ import { N as Nullable, A as Arrayable } from './types-aaf02e1c.js';
2
2
 
3
3
  declare const isWindows: boolean;
4
4
  declare function slash(str: string): string;
5
5
  declare function mergeSlashes(str: string): string;
6
+ declare const VALID_ID_PREFIX = "/@id/";
6
7
  declare function normalizeRequestId(id: string, base?: string): string;
7
8
  declare const queryRE: RegExp;
8
9
  declare const hashRE: RegExp;
9
10
  declare const cleanUrl: (url: string) => string;
11
+ declare const isInternalRequest: (id: string) => boolean;
10
12
  declare function normalizeModuleId(id: string): string;
11
13
  declare function isPrimitive(v: any): boolean;
12
- declare function pathFromRoot(root: string, filename: string): string;
13
14
  declare function toFilePath(id: string, root: string): string;
14
15
  /**
15
16
  * Convert `Arrayable<T>` to `Array<T>`
@@ -18,4 +19,4 @@ declare function toFilePath(id: string, root: string): string;
18
19
  */
19
20
  declare function toArray<T>(array?: Nullable<Arrayable<T>>): Array<T>;
20
21
 
21
- export { cleanUrl, hashRE, isPrimitive, isWindows, mergeSlashes, normalizeModuleId, normalizeRequestId, pathFromRoot, queryRE, slash, toArray, toFilePath };
22
+ export { VALID_ID_PREFIX, cleanUrl, hashRE, isInternalRequest, isPrimitive, isWindows, mergeSlashes, normalizeModuleId, normalizeRequestId, queryRE, slash, toArray, toFilePath };
package/dist/utils.mjs CHANGED
@@ -1,7 +1,6 @@
1
1
  import { fileURLToPath, pathToFileURL } from 'url';
2
2
  import { existsSync } from 'fs';
3
- import { relative, resolve } from 'pathe';
4
- import { isNodeBuiltin } from 'mlly';
3
+ import { resolve } from 'pathe';
5
4
 
6
5
  const isWindows = process.platform === "win32";
7
6
  function slash(str) {
@@ -10,6 +9,7 @@ function slash(str) {
10
9
  function mergeSlashes(str) {
11
10
  return str.replace(/\/\//g, "/");
12
11
  }
12
+ const VALID_ID_PREFIX = "/@id/";
13
13
  function normalizeRequestId(id, base) {
14
14
  if (base && id.startsWith(base))
15
15
  id = `/${id.slice(base.length)}`;
@@ -18,23 +18,15 @@ function normalizeRequestId(id, base) {
18
18
  const queryRE = /\?.*$/s;
19
19
  const hashRE = /#.*$/s;
20
20
  const cleanUrl = (url) => url.replace(hashRE, "").replace(queryRE, "");
21
+ const isInternalRequest = (id) => {
22
+ return id.startsWith("/@vite/");
23
+ };
21
24
  function normalizeModuleId(id) {
22
- return id.replace(/\\/g, "/").replace(/^\/@fs\//, "/").replace(/^file:\//, "/").replace(/^\/+/, "/");
25
+ return id.replace(/\\/g, "/").replace(/^\/@fs\//, isWindows ? "" : "/").replace(/^file:\//, "/").replace(/^\/+/, "/");
23
26
  }
24
27
  function isPrimitive(v) {
25
28
  return v !== Object(v);
26
29
  }
27
- function pathFromRoot(root, filename) {
28
- if (isNodeBuiltin(filename))
29
- return filename;
30
- filename = filename.replace(/^\/@fs\//, isWindows ? "" : "/");
31
- if (!filename.startsWith(root))
32
- return filename;
33
- const relativePath = relative(root, filename);
34
- const segments = relativePath.split("/");
35
- const startIndex = segments.findIndex((segment) => segment !== ".." && segment !== ".");
36
- return `/${segments.slice(startIndex).join("/")}`;
37
- }
38
30
  function toFilePath(id, root) {
39
31
  let absolute = (() => {
40
32
  if (id.startsWith("/@fs/"))
@@ -58,4 +50,4 @@ function toArray(array) {
58
50
  return [array];
59
51
  }
60
52
 
61
- export { cleanUrl, hashRE, isPrimitive, isWindows, mergeSlashes, normalizeModuleId, normalizeRequestId, pathFromRoot, queryRE, slash, toArray, toFilePath };
53
+ export { VALID_ID_PREFIX, cleanUrl, hashRE, isInternalRequest, isPrimitive, isWindows, mergeSlashes, normalizeModuleId, normalizeRequestId, queryRE, slash, toArray, toFilePath };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-node",
3
- "version": "0.25.7",
3
+ "version": "0.26.0",
4
4
  "description": "Vite as Node.js runtime",
5
5
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
6
6
  "license": "MIT",
@@ -86,7 +86,7 @@
86
86
  },
87
87
  "scripts": {
88
88
  "build": "rimraf dist && rollup -c",
89
- "dev": "rollup -c --watch --watch.include=src -m inline",
89
+ "dev": "rollup -c --watch --watch.include 'src/**' -m inline",
90
90
  "typecheck": "tsc --noEmit"
91
91
  }
92
92
  }