vite-node 0.1.20 → 0.1.24

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/client.d.ts CHANGED
@@ -14,7 +14,7 @@ interface ViteNodeRunnerOptions {
14
14
  root: string;
15
15
  base?: string;
16
16
  moduleCache?: Map<string, ModuleCache>;
17
- interpretDefault?: boolean;
17
+ interopDefault?: boolean;
18
18
  requestStubs?: Record<string, any>;
19
19
  }
20
20
  interface ViteNodeResolveId {
@@ -25,10 +25,19 @@ interface ViteNodeResolveId {
25
25
  syntheticNamedExports?: boolean | string | null;
26
26
  }
27
27
 
28
+ declare const DEFAULT_REQUEST_STUBS: {
29
+ '/@vite/client': {
30
+ injectQuery: (id: string) => string;
31
+ createHotContext(): {
32
+ accept: () => void;
33
+ prune: () => void;
34
+ };
35
+ updateStyle(): void;
36
+ };
37
+ };
28
38
  declare class ViteNodeRunner {
29
39
  options: ViteNodeRunnerOptions;
30
40
  root: string;
31
- externalCache: Map<string, string | Promise<false | string>>;
32
41
  moduleCache: Map<string, ModuleCache>;
33
42
  constructor(options: ViteNodeRunnerOptions);
34
43
  executeFile(file: string): Promise<any>;
@@ -37,6 +46,16 @@ declare class ViteNodeRunner {
37
46
  directRequest(id: string, fsPath: string, callstack: string[]): Promise<any>;
38
47
  prepareContext(context: Record<string, any>): Record<string, any>;
39
48
  setCache(id: string, mod: Partial<ModuleCache>): void;
49
+ /**
50
+ * Define if a module should be interop-ed
51
+ * This function mostly for the ability to override by subclass
52
+ */
53
+ shouldInterop(path: string, mod: any): boolean;
54
+ /**
55
+ * Import a module and interop it
56
+ */
57
+ interopedImport(path: string): Promise<any>;
58
+ hasNestedDefault(target: any): any;
40
59
  }
41
60
 
42
- export { ViteNodeRunner };
61
+ export { DEFAULT_REQUEST_STUBS, ViteNodeRunner };
package/dist/cli.cjs CHANGED
@@ -202,13 +202,26 @@ async function withInlineSourcemap(result) {
202
202
  return result;
203
203
  }
204
204
 
205
+ const DEFAULT_REQUEST_STUBS = {
206
+ "/@vite/client": {
207
+ injectQuery: (id) => id,
208
+ createHotContext() {
209
+ return {
210
+ accept: () => {
211
+ },
212
+ prune: () => {
213
+ }
214
+ };
215
+ },
216
+ updateStyle() {
217
+ }
218
+ }
219
+ };
205
220
  class ViteNodeRunner {
206
221
  constructor(options) {
207
222
  this.options = options;
208
223
  this.root = options.root || process.cwd();
209
224
  this.moduleCache = options.moduleCache || new Map();
210
- this.externalCache = new Map();
211
- module$1.builtinModules.forEach((m) => this.externalCache.set(m, m));
212
225
  }
213
226
  async executeFile(file) {
214
227
  return await this.cachedRequest(`/@fs/${slash(pathe.resolve(file))}`, []);
@@ -219,11 +232,11 @@ class ViteNodeRunner {
219
232
  async cachedRequest(rawId, callstack) {
220
233
  var _a, _b;
221
234
  const id = normalizeId(rawId, this.options.base);
235
+ if ((_a = this.moduleCache.get(id)) == null ? void 0 : _a.promise)
236
+ return (_b = this.moduleCache.get(id)) == null ? void 0 : _b.promise;
222
237
  const fsPath = toFilePath(id, this.root);
223
- if ((_a = this.moduleCache.get(fsPath)) == null ? void 0 : _a.promise)
224
- return (_b = this.moduleCache.get(fsPath)) == null ? void 0 : _b.promise;
225
238
  const promise = this.directRequest(id, fsPath, callstack);
226
- this.setCache(fsPath, { promise });
239
+ this.setCache(id, { promise });
227
240
  return await promise;
228
241
  }
229
242
  async directRequest(id, fsPath, callstack) {
@@ -231,28 +244,28 @@ class ViteNodeRunner {
231
244
  const request = async (dep) => {
232
245
  var _a;
233
246
  if (callstack.includes(dep)) {
234
- const cacheKey = toFilePath(dep, this.root);
235
- if (!((_a = this.moduleCache.get(cacheKey)) == null ? void 0 : _a.exports))
236
- throw new Error(`Circular dependency detected
247
+ if (!((_a = this.moduleCache.get(dep)) == null ? void 0 : _a.exports))
248
+ throw new Error(`[vite-node] Circular dependency detected
237
249
  Stack:
238
250
  ${[...callstack, dep].reverse().map((p) => `- ${p}`).join("\n")}`);
239
- return this.moduleCache.get(cacheKey).exports;
251
+ return this.moduleCache.get(dep).exports;
240
252
  }
241
253
  return this.cachedRequest(dep, callstack);
242
254
  };
243
- if (this.options.requestStubs && id in this.options.requestStubs)
244
- return this.options.requestStubs[id];
255
+ const requestStubs = this.options.requestStubs || DEFAULT_REQUEST_STUBS;
256
+ if (id in requestStubs)
257
+ return requestStubs[id];
245
258
  const { code: transformed, externalize } = await this.options.fetchModule(id);
246
259
  if (externalize) {
247
- const mod = await interpretedImport(externalize, this.options.interpretDefault ?? true);
248
- this.setCache(fsPath, { exports: mod });
260
+ const mod = await this.interopedImport(externalize);
261
+ this.setCache(id, { exports: mod });
249
262
  return mod;
250
263
  }
251
264
  if (transformed == null)
252
- throw new Error(`failed to load ${id}`);
265
+ throw new Error(`[vite-node] Failed to load ${id}`);
253
266
  const url$1 = url.pathToFileURL(fsPath).href;
254
267
  const exports = {};
255
- this.setCache(fsPath, { code: transformed, exports });
268
+ this.setCache(id, { code: transformed, exports });
256
269
  const __filename = url.fileURLToPath(url$1);
257
270
  const moduleProxy = {
258
271
  set exports(value) {
@@ -292,9 +305,27 @@ ${[...callstack, dep].reverse().map((p) => `- ${p}`).join("\n")}`);
292
305
  else
293
306
  Object.assign(this.moduleCache.get(id), mod);
294
307
  }
295
- }
296
- function hasNestedDefault(target) {
297
- return "__esModule" in target && target.__esModule && "default" in target.default;
308
+ shouldInterop(path, mod) {
309
+ if (this.options.interopDefault === false)
310
+ return false;
311
+ return !path.endsWith(".mjs") && "default" in mod;
312
+ }
313
+ async interopedImport(path) {
314
+ const mod = await (function (t) { return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(t)); }); })(path);
315
+ if (this.shouldInterop(path, mod)) {
316
+ const tryDefault = this.hasNestedDefault(mod);
317
+ return new Proxy(mod, {
318
+ get: proxyMethod("get", tryDefault),
319
+ set: proxyMethod("set", tryDefault),
320
+ has: proxyMethod("has", tryDefault),
321
+ deleteProperty: proxyMethod("deleteProperty", tryDefault)
322
+ });
323
+ }
324
+ return mod;
325
+ }
326
+ hasNestedDefault(target) {
327
+ return "__esModule" in target && target.__esModule && "default" in target.default;
328
+ }
298
329
  }
299
330
  function proxyMethod(name, tryDefault) {
300
331
  return function(target, key, ...args) {
@@ -306,19 +337,6 @@ function proxyMethod(name, tryDefault) {
306
337
  return result;
307
338
  };
308
339
  }
309
- async function interpretedImport(path, interpretDefault) {
310
- const mod = await (function (t) { return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(t)); }); })(path);
311
- if (interpretDefault && "default" in mod) {
312
- const tryDefault = hasNestedDefault(mod);
313
- return new Proxy(mod, {
314
- get: proxyMethod("get", tryDefault),
315
- set: proxyMethod("set", tryDefault),
316
- has: proxyMethod("has", tryDefault),
317
- deleteProperty: proxyMethod("deleteProperty", tryDefault)
318
- });
319
- }
320
- return mod;
321
- }
322
340
  function exportAll(exports, sourceModule) {
323
341
  for (const key in sourceModule) {
324
342
  if (key !== "default") {
package/dist/cli.js CHANGED
@@ -5,7 +5,7 @@ import { existsSync } from 'fs';
5
5
  import { isNodeBuiltin, isValidNodeImport } from 'mlly';
6
6
  import { fileURLToPath, pathToFileURL } from 'url';
7
7
  import { dirname, resolve } from 'pathe';
8
- import { builtinModules, createRequire } from 'module';
8
+ import { createRequire } from 'module';
9
9
  import vm from 'vm';
10
10
 
11
11
  const isWindows = process.platform === "win32";
@@ -177,13 +177,26 @@ async function withInlineSourcemap(result) {
177
177
  return result;
178
178
  }
179
179
 
180
+ const DEFAULT_REQUEST_STUBS = {
181
+ "/@vite/client": {
182
+ injectQuery: (id) => id,
183
+ createHotContext() {
184
+ return {
185
+ accept: () => {
186
+ },
187
+ prune: () => {
188
+ }
189
+ };
190
+ },
191
+ updateStyle() {
192
+ }
193
+ }
194
+ };
180
195
  class ViteNodeRunner {
181
196
  constructor(options) {
182
197
  this.options = options;
183
198
  this.root = options.root || process.cwd();
184
199
  this.moduleCache = options.moduleCache || new Map();
185
- this.externalCache = new Map();
186
- builtinModules.forEach((m) => this.externalCache.set(m, m));
187
200
  }
188
201
  async executeFile(file) {
189
202
  return await this.cachedRequest(`/@fs/${slash(resolve(file))}`, []);
@@ -194,11 +207,11 @@ class ViteNodeRunner {
194
207
  async cachedRequest(rawId, callstack) {
195
208
  var _a, _b;
196
209
  const id = normalizeId(rawId, this.options.base);
210
+ if ((_a = this.moduleCache.get(id)) == null ? void 0 : _a.promise)
211
+ return (_b = this.moduleCache.get(id)) == null ? void 0 : _b.promise;
197
212
  const fsPath = toFilePath(id, this.root);
198
- if ((_a = this.moduleCache.get(fsPath)) == null ? void 0 : _a.promise)
199
- return (_b = this.moduleCache.get(fsPath)) == null ? void 0 : _b.promise;
200
213
  const promise = this.directRequest(id, fsPath, callstack);
201
- this.setCache(fsPath, { promise });
214
+ this.setCache(id, { promise });
202
215
  return await promise;
203
216
  }
204
217
  async directRequest(id, fsPath, callstack) {
@@ -206,28 +219,28 @@ class ViteNodeRunner {
206
219
  const request = async (dep) => {
207
220
  var _a;
208
221
  if (callstack.includes(dep)) {
209
- const cacheKey = toFilePath(dep, this.root);
210
- if (!((_a = this.moduleCache.get(cacheKey)) == null ? void 0 : _a.exports))
211
- throw new Error(`Circular dependency detected
222
+ if (!((_a = this.moduleCache.get(dep)) == null ? void 0 : _a.exports))
223
+ throw new Error(`[vite-node] Circular dependency detected
212
224
  Stack:
213
225
  ${[...callstack, dep].reverse().map((p) => `- ${p}`).join("\n")}`);
214
- return this.moduleCache.get(cacheKey).exports;
226
+ return this.moduleCache.get(dep).exports;
215
227
  }
216
228
  return this.cachedRequest(dep, callstack);
217
229
  };
218
- if (this.options.requestStubs && id in this.options.requestStubs)
219
- return this.options.requestStubs[id];
230
+ const requestStubs = this.options.requestStubs || DEFAULT_REQUEST_STUBS;
231
+ if (id in requestStubs)
232
+ return requestStubs[id];
220
233
  const { code: transformed, externalize } = await this.options.fetchModule(id);
221
234
  if (externalize) {
222
- const mod = await interpretedImport(externalize, this.options.interpretDefault ?? true);
223
- this.setCache(fsPath, { exports: mod });
235
+ const mod = await this.interopedImport(externalize);
236
+ this.setCache(id, { exports: mod });
224
237
  return mod;
225
238
  }
226
239
  if (transformed == null)
227
- throw new Error(`failed to load ${id}`);
240
+ throw new Error(`[vite-node] Failed to load ${id}`);
228
241
  const url = pathToFileURL(fsPath).href;
229
242
  const exports = {};
230
- this.setCache(fsPath, { code: transformed, exports });
243
+ this.setCache(id, { code: transformed, exports });
231
244
  const __filename = fileURLToPath(url);
232
245
  const moduleProxy = {
233
246
  set exports(value) {
@@ -267,9 +280,27 @@ ${[...callstack, dep].reverse().map((p) => `- ${p}`).join("\n")}`);
267
280
  else
268
281
  Object.assign(this.moduleCache.get(id), mod);
269
282
  }
270
- }
271
- function hasNestedDefault(target) {
272
- return "__esModule" in target && target.__esModule && "default" in target.default;
283
+ shouldInterop(path, mod) {
284
+ if (this.options.interopDefault === false)
285
+ return false;
286
+ return !path.endsWith(".mjs") && "default" in mod;
287
+ }
288
+ async interopedImport(path) {
289
+ const mod = await import(path);
290
+ if (this.shouldInterop(path, mod)) {
291
+ const tryDefault = this.hasNestedDefault(mod);
292
+ return new Proxy(mod, {
293
+ get: proxyMethod("get", tryDefault),
294
+ set: proxyMethod("set", tryDefault),
295
+ has: proxyMethod("has", tryDefault),
296
+ deleteProperty: proxyMethod("deleteProperty", tryDefault)
297
+ });
298
+ }
299
+ return mod;
300
+ }
301
+ hasNestedDefault(target) {
302
+ return "__esModule" in target && target.__esModule && "default" in target.default;
303
+ }
273
304
  }
274
305
  function proxyMethod(name, tryDefault) {
275
306
  return function(target, key, ...args) {
@@ -281,19 +312,6 @@ function proxyMethod(name, tryDefault) {
281
312
  return result;
282
313
  };
283
314
  }
284
- async function interpretedImport(path, interpretDefault) {
285
- const mod = await import(path);
286
- if (interpretDefault && "default" in mod) {
287
- const tryDefault = hasNestedDefault(mod);
288
- return new Proxy(mod, {
289
- get: proxyMethod("get", tryDefault),
290
- set: proxyMethod("set", tryDefault),
291
- has: proxyMethod("has", tryDefault),
292
- deleteProperty: proxyMethod("deleteProperty", tryDefault)
293
- });
294
- }
295
- return mod;
296
- }
297
315
  function exportAll(exports, sourceModule) {
298
316
  for (const key in sourceModule) {
299
317
  if (key !== "default") {
package/dist/client.cjs CHANGED
@@ -48,13 +48,26 @@ function toFilePath(id, root) {
48
48
  return isWindows && absolute.startsWith("/") ? url.fileURLToPath(url.pathToFileURL(absolute.slice(1)).href) : absolute;
49
49
  }
50
50
 
51
+ const DEFAULT_REQUEST_STUBS = {
52
+ "/@vite/client": {
53
+ injectQuery: (id) => id,
54
+ createHotContext() {
55
+ return {
56
+ accept: () => {
57
+ },
58
+ prune: () => {
59
+ }
60
+ };
61
+ },
62
+ updateStyle() {
63
+ }
64
+ }
65
+ };
51
66
  class ViteNodeRunner {
52
67
  constructor(options) {
53
68
  this.options = options;
54
69
  this.root = options.root || process.cwd();
55
70
  this.moduleCache = options.moduleCache || new Map();
56
- this.externalCache = new Map();
57
- module$1.builtinModules.forEach((m) => this.externalCache.set(m, m));
58
71
  }
59
72
  async executeFile(file) {
60
73
  return await this.cachedRequest(`/@fs/${slash(pathe.resolve(file))}`, []);
@@ -65,11 +78,11 @@ class ViteNodeRunner {
65
78
  async cachedRequest(rawId, callstack) {
66
79
  var _a, _b;
67
80
  const id = normalizeId(rawId, this.options.base);
81
+ if ((_a = this.moduleCache.get(id)) == null ? void 0 : _a.promise)
82
+ return (_b = this.moduleCache.get(id)) == null ? void 0 : _b.promise;
68
83
  const fsPath = toFilePath(id, this.root);
69
- if ((_a = this.moduleCache.get(fsPath)) == null ? void 0 : _a.promise)
70
- return (_b = this.moduleCache.get(fsPath)) == null ? void 0 : _b.promise;
71
84
  const promise = this.directRequest(id, fsPath, callstack);
72
- this.setCache(fsPath, { promise });
85
+ this.setCache(id, { promise });
73
86
  return await promise;
74
87
  }
75
88
  async directRequest(id, fsPath, callstack) {
@@ -77,28 +90,28 @@ class ViteNodeRunner {
77
90
  const request = async (dep) => {
78
91
  var _a;
79
92
  if (callstack.includes(dep)) {
80
- const cacheKey = toFilePath(dep, this.root);
81
- if (!((_a = this.moduleCache.get(cacheKey)) == null ? void 0 : _a.exports))
82
- throw new Error(`Circular dependency detected
93
+ if (!((_a = this.moduleCache.get(dep)) == null ? void 0 : _a.exports))
94
+ throw new Error(`[vite-node] Circular dependency detected
83
95
  Stack:
84
96
  ${[...callstack, dep].reverse().map((p) => `- ${p}`).join("\n")}`);
85
- return this.moduleCache.get(cacheKey).exports;
97
+ return this.moduleCache.get(dep).exports;
86
98
  }
87
99
  return this.cachedRequest(dep, callstack);
88
100
  };
89
- if (this.options.requestStubs && id in this.options.requestStubs)
90
- return this.options.requestStubs[id];
101
+ const requestStubs = this.options.requestStubs || DEFAULT_REQUEST_STUBS;
102
+ if (id in requestStubs)
103
+ return requestStubs[id];
91
104
  const { code: transformed, externalize } = await this.options.fetchModule(id);
92
105
  if (externalize) {
93
- const mod = await interpretedImport(externalize, this.options.interpretDefault ?? true);
94
- this.setCache(fsPath, { exports: mod });
106
+ const mod = await this.interopedImport(externalize);
107
+ this.setCache(id, { exports: mod });
95
108
  return mod;
96
109
  }
97
110
  if (transformed == null)
98
- throw new Error(`failed to load ${id}`);
111
+ throw new Error(`[vite-node] Failed to load ${id}`);
99
112
  const url$1 = url.pathToFileURL(fsPath).href;
100
113
  const exports = {};
101
- this.setCache(fsPath, { code: transformed, exports });
114
+ this.setCache(id, { code: transformed, exports });
102
115
  const __filename = url.fileURLToPath(url$1);
103
116
  const moduleProxy = {
104
117
  set exports(value) {
@@ -138,9 +151,27 @@ ${[...callstack, dep].reverse().map((p) => `- ${p}`).join("\n")}`);
138
151
  else
139
152
  Object.assign(this.moduleCache.get(id), mod);
140
153
  }
141
- }
142
- function hasNestedDefault(target) {
143
- return "__esModule" in target && target.__esModule && "default" in target.default;
154
+ shouldInterop(path, mod) {
155
+ if (this.options.interopDefault === false)
156
+ return false;
157
+ return !path.endsWith(".mjs") && "default" in mod;
158
+ }
159
+ async interopedImport(path) {
160
+ const mod = await (function (t) { return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(t)); }); })(path);
161
+ if (this.shouldInterop(path, mod)) {
162
+ const tryDefault = this.hasNestedDefault(mod);
163
+ return new Proxy(mod, {
164
+ get: proxyMethod("get", tryDefault),
165
+ set: proxyMethod("set", tryDefault),
166
+ has: proxyMethod("has", tryDefault),
167
+ deleteProperty: proxyMethod("deleteProperty", tryDefault)
168
+ });
169
+ }
170
+ return mod;
171
+ }
172
+ hasNestedDefault(target) {
173
+ return "__esModule" in target && target.__esModule && "default" in target.default;
174
+ }
144
175
  }
145
176
  function proxyMethod(name, tryDefault) {
146
177
  return function(target, key, ...args) {
@@ -152,19 +183,6 @@ function proxyMethod(name, tryDefault) {
152
183
  return result;
153
184
  };
154
185
  }
155
- async function interpretedImport(path, interpretDefault) {
156
- const mod = await (function (t) { return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(t)); }); })(path);
157
- if (interpretDefault && "default" in mod) {
158
- const tryDefault = hasNestedDefault(mod);
159
- return new Proxy(mod, {
160
- get: proxyMethod("get", tryDefault),
161
- set: proxyMethod("set", tryDefault),
162
- has: proxyMethod("has", tryDefault),
163
- deleteProperty: proxyMethod("deleteProperty", tryDefault)
164
- });
165
- }
166
- return mod;
167
- }
168
186
  function exportAll(exports, sourceModule) {
169
187
  for (const key in sourceModule) {
170
188
  if (key !== "default") {
@@ -182,4 +200,5 @@ function exportAll(exports, sourceModule) {
182
200
  }
183
201
  }
184
202
 
203
+ exports.DEFAULT_REQUEST_STUBS = DEFAULT_REQUEST_STUBS;
185
204
  exports.ViteNodeRunner = ViteNodeRunner;
package/dist/client.js CHANGED
@@ -1,4 +1,4 @@
1
- import { builtinModules, createRequire } from 'module';
1
+ import { createRequire } from 'module';
2
2
  import { fileURLToPath, pathToFileURL } from 'url';
3
3
  import vm from 'vm';
4
4
  import { dirname, resolve } from 'pathe';
@@ -22,13 +22,26 @@ function toFilePath(id, root) {
22
22
  return isWindows && absolute.startsWith("/") ? fileURLToPath(pathToFileURL(absolute.slice(1)).href) : absolute;
23
23
  }
24
24
 
25
+ const DEFAULT_REQUEST_STUBS = {
26
+ "/@vite/client": {
27
+ injectQuery: (id) => id,
28
+ createHotContext() {
29
+ return {
30
+ accept: () => {
31
+ },
32
+ prune: () => {
33
+ }
34
+ };
35
+ },
36
+ updateStyle() {
37
+ }
38
+ }
39
+ };
25
40
  class ViteNodeRunner {
26
41
  constructor(options) {
27
42
  this.options = options;
28
43
  this.root = options.root || process.cwd();
29
44
  this.moduleCache = options.moduleCache || new Map();
30
- this.externalCache = new Map();
31
- builtinModules.forEach((m) => this.externalCache.set(m, m));
32
45
  }
33
46
  async executeFile(file) {
34
47
  return await this.cachedRequest(`/@fs/${slash(resolve(file))}`, []);
@@ -39,11 +52,11 @@ class ViteNodeRunner {
39
52
  async cachedRequest(rawId, callstack) {
40
53
  var _a, _b;
41
54
  const id = normalizeId(rawId, this.options.base);
55
+ if ((_a = this.moduleCache.get(id)) == null ? void 0 : _a.promise)
56
+ return (_b = this.moduleCache.get(id)) == null ? void 0 : _b.promise;
42
57
  const fsPath = toFilePath(id, this.root);
43
- if ((_a = this.moduleCache.get(fsPath)) == null ? void 0 : _a.promise)
44
- return (_b = this.moduleCache.get(fsPath)) == null ? void 0 : _b.promise;
45
58
  const promise = this.directRequest(id, fsPath, callstack);
46
- this.setCache(fsPath, { promise });
59
+ this.setCache(id, { promise });
47
60
  return await promise;
48
61
  }
49
62
  async directRequest(id, fsPath, callstack) {
@@ -51,28 +64,28 @@ class ViteNodeRunner {
51
64
  const request = async (dep) => {
52
65
  var _a;
53
66
  if (callstack.includes(dep)) {
54
- const cacheKey = toFilePath(dep, this.root);
55
- if (!((_a = this.moduleCache.get(cacheKey)) == null ? void 0 : _a.exports))
56
- throw new Error(`Circular dependency detected
67
+ if (!((_a = this.moduleCache.get(dep)) == null ? void 0 : _a.exports))
68
+ throw new Error(`[vite-node] Circular dependency detected
57
69
  Stack:
58
70
  ${[...callstack, dep].reverse().map((p) => `- ${p}`).join("\n")}`);
59
- return this.moduleCache.get(cacheKey).exports;
71
+ return this.moduleCache.get(dep).exports;
60
72
  }
61
73
  return this.cachedRequest(dep, callstack);
62
74
  };
63
- if (this.options.requestStubs && id in this.options.requestStubs)
64
- return this.options.requestStubs[id];
75
+ const requestStubs = this.options.requestStubs || DEFAULT_REQUEST_STUBS;
76
+ if (id in requestStubs)
77
+ return requestStubs[id];
65
78
  const { code: transformed, externalize } = await this.options.fetchModule(id);
66
79
  if (externalize) {
67
- const mod = await interpretedImport(externalize, this.options.interpretDefault ?? true);
68
- this.setCache(fsPath, { exports: mod });
80
+ const mod = await this.interopedImport(externalize);
81
+ this.setCache(id, { exports: mod });
69
82
  return mod;
70
83
  }
71
84
  if (transformed == null)
72
- throw new Error(`failed to load ${id}`);
85
+ throw new Error(`[vite-node] Failed to load ${id}`);
73
86
  const url = pathToFileURL(fsPath).href;
74
87
  const exports = {};
75
- this.setCache(fsPath, { code: transformed, exports });
88
+ this.setCache(id, { code: transformed, exports });
76
89
  const __filename = fileURLToPath(url);
77
90
  const moduleProxy = {
78
91
  set exports(value) {
@@ -112,9 +125,27 @@ ${[...callstack, dep].reverse().map((p) => `- ${p}`).join("\n")}`);
112
125
  else
113
126
  Object.assign(this.moduleCache.get(id), mod);
114
127
  }
115
- }
116
- function hasNestedDefault(target) {
117
- return "__esModule" in target && target.__esModule && "default" in target.default;
128
+ shouldInterop(path, mod) {
129
+ if (this.options.interopDefault === false)
130
+ return false;
131
+ return !path.endsWith(".mjs") && "default" in mod;
132
+ }
133
+ async interopedImport(path) {
134
+ const mod = await import(path);
135
+ if (this.shouldInterop(path, mod)) {
136
+ const tryDefault = this.hasNestedDefault(mod);
137
+ return new Proxy(mod, {
138
+ get: proxyMethod("get", tryDefault),
139
+ set: proxyMethod("set", tryDefault),
140
+ has: proxyMethod("has", tryDefault),
141
+ deleteProperty: proxyMethod("deleteProperty", tryDefault)
142
+ });
143
+ }
144
+ return mod;
145
+ }
146
+ hasNestedDefault(target) {
147
+ return "__esModule" in target && target.__esModule && "default" in target.default;
148
+ }
118
149
  }
119
150
  function proxyMethod(name, tryDefault) {
120
151
  return function(target, key, ...args) {
@@ -126,19 +157,6 @@ function proxyMethod(name, tryDefault) {
126
157
  return result;
127
158
  };
128
159
  }
129
- async function interpretedImport(path, interpretDefault) {
130
- const mod = await import(path);
131
- if (interpretDefault && "default" in mod) {
132
- const tryDefault = hasNestedDefault(mod);
133
- return new Proxy(mod, {
134
- get: proxyMethod("get", tryDefault),
135
- set: proxyMethod("set", tryDefault),
136
- has: proxyMethod("has", tryDefault),
137
- deleteProperty: proxyMethod("deleteProperty", tryDefault)
138
- });
139
- }
140
- return mod;
141
- }
142
160
  function exportAll(exports, sourceModule) {
143
161
  for (const key in sourceModule) {
144
162
  if (key !== "default") {
@@ -156,4 +174,4 @@ function exportAll(exports, sourceModule) {
156
174
  }
157
175
  }
158
176
 
159
- export { ViteNodeRunner };
177
+ export { DEFAULT_REQUEST_STUBS, ViteNodeRunner };
package/index.d.ts CHANGED
@@ -23,7 +23,7 @@ interface ViteNodeRunnerOptions {
23
23
  root: string;
24
24
  base?: string;
25
25
  moduleCache?: Map<string, ModuleCache>;
26
- interpretDefault?: boolean;
26
+ interopDefault?: boolean;
27
27
  requestStubs?: Record<string, any>;
28
28
  }
29
29
  interface ViteNodeResolveId {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-node",
3
- "version": "0.1.20",
3
+ "version": "0.1.24",
4
4
  "description": "Vite as Node.js runtime",
5
5
  "homepage": "https://github.com/vitest-dev/vitest#readme",
6
6
  "bugs": {
@@ -51,13 +51,13 @@
51
51
  "dependencies": {
52
52
  "kolorist": "^1.5.1",
53
53
  "minimist": "^1.2.5",
54
- "mlly": "^0.3.17",
54
+ "mlly": "^0.3.19",
55
55
  "pathe": "^0.2.0",
56
- "vite": "^2.7.12"
56
+ "vite": "^2.7.13"
57
57
  },
58
58
  "devDependencies": {
59
59
  "@types/minimist": "^1.2.2",
60
- "rollup": "^2.63.0"
60
+ "rollup": "^2.64.0"
61
61
  },
62
62
  "engines": {
63
63
  "node": ">=14.14.0"