vite-node 0.28.4 → 0.29.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/README.md CHANGED
@@ -48,6 +48,32 @@ npx vite-node --options.deps.inline="module-name" --options.deps.external="/modu
48
48
 
49
49
  Note that for options supporting RegExps, strings passed to the CLI must start _and_ end with a `/`;
50
50
 
51
+ ### Hashbang
52
+
53
+ If you prefer to write scripts that don't need to be passed into Vite Node, you can declare it in the [hashbang](https://bash.cyberciti.biz/guide/Shebang).
54
+
55
+ Simply add `#!/usr/bin/env vite-node --script` at the top of your file:
56
+
57
+ _file.ts_
58
+ ```ts
59
+ #!/usr/bin/env vite-node --script
60
+
61
+ console.log('argv:', process.argv.slice(2))
62
+ ```
63
+
64
+ And make the file executable:
65
+ ```sh
66
+ chmod +x ./file.ts
67
+ ```
68
+
69
+ Now, you can run the file without passing it into Vite Node:
70
+ ```sh
71
+ $ ./file.ts hello
72
+ argv: [ 'hello' ]
73
+ ```
74
+
75
+ Note that when using the `--script` option, Vite Node forwards every argument and option to the script to execute, even the one supported by Vite Node itself.
76
+
51
77
  ## Programmatic Usage
52
78
 
53
79
  In Vite Node, the server and runner (client) are separated, so you can integrate them in different contexts (workers, cross-process, or remote) if needed. The demo below shows a simple example of having both (server and runner) running in the same context
package/dist/cli.cjs CHANGED
@@ -6,17 +6,16 @@ var vite = require('vite');
6
6
  var server = require('./server.cjs');
7
7
  var client = require('./client.cjs');
8
8
  var utils = require('./utils.cjs');
9
- var hmr = require('./chunk-hmr.cjs');
10
9
  var sourceMap = require('./source-map.cjs');
10
+ var hmr = require('./chunk-hmr.cjs');
11
11
  require('perf_hooks');
12
+ require('fs');
12
13
  require('pathe');
13
14
  require('debug');
14
- require('fs');
15
15
  require('mlly');
16
16
  require('node:url');
17
- require('source-map-support');
18
- require('module');
19
17
  require('path');
18
+ require('module');
20
19
  require('vm');
21
20
  require('node:events');
22
21
 
@@ -25,25 +24,32 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
25
24
  var cac__default = /*#__PURE__*/_interopDefaultLegacy(cac);
26
25
  var c__default = /*#__PURE__*/_interopDefaultLegacy(c);
27
26
 
28
- var version = "0.28.4";
27
+ var version = "0.29.0";
29
28
 
30
29
  const cli = cac__default["default"]("vite-node");
31
- 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();
32
- cli.command("[...files]").action(run);
30
+ cli.version(version).option("-r, --root <path>", "Use specified root directory").option("-c, --config <path>", "Use specified config file").option("-m, --mode <mode>", "Set env mode").option("-w, --watch", 'Restart on file changes, similar to "nodemon"').option("--script", "Use vite-node as a script runner").option("--options <options>", "Use specified Vite server options").help();
31
+ cli.command("[...files]").allowUnknownOptions().action(run);
33
32
  cli.parse();
34
33
  async function run(files, options = {}) {
35
34
  var _a;
35
+ if (options.script) {
36
+ files = [files[0]];
37
+ options = {};
38
+ process.argv = [process.argv[0], files[0], ...process.argv.slice(2).filter((arg) => arg !== "--script" && arg !== files[0])];
39
+ } else {
40
+ process.argv = [...process.argv.slice(0, 2), ...options["--"] || []];
41
+ }
36
42
  if (!files.length) {
37
43
  console.error(c__default["default"].red("No files specified."));
38
44
  cli.outputHelp();
39
45
  process.exit(1);
40
46
  }
41
- process.argv = [...process.argv.slice(0, 2), ...options["--"] || []];
42
47
  const serverOptions = options.options ? parseServerOptions(options.options) : {};
43
48
  const server$1 = await vite.createServer({
44
49
  logLevel: "error",
45
50
  configFile: options.config,
46
51
  root: options.root,
52
+ mode: options.mode,
47
53
  plugins: [
48
54
  options.watch && hmr.viteNodeHmrPlugin()
49
55
  ]
package/dist/cli.d.ts CHANGED
@@ -1,8 +1,11 @@
1
- import { e as ViteNodeServerOptions } from './types-63205a44.js';
1
+ import { e as ViteNodeServerOptions } from './types-e288fc62.js';
2
+ import './types.d-1e7e3fdf.js';
2
3
 
3
4
  interface CliOptions {
4
5
  root?: string;
6
+ script?: boolean;
5
7
  config?: string;
8
+ mode?: string;
6
9
  watch?: boolean;
7
10
  options?: ViteNodeServerOptionsCLI;
8
11
  '--'?: string[];
package/dist/cli.mjs CHANGED
@@ -7,36 +7,42 @@ import { toArray } from './utils.mjs';
7
7
  import { installSourcemapsSupport } from './source-map.mjs';
8
8
  import { v as viteNodeHmrPlugin, c as createHotContext, h as handleMessage } from './chunk-hmr.mjs';
9
9
  import 'node:perf_hooks';
10
+ import 'node:fs';
10
11
  import 'pathe';
11
12
  import 'debug';
12
- import 'node:fs';
13
13
  import 'mlly';
14
14
  import 'node:url';
15
- import 'source-map-support';
16
- import 'node:module';
17
15
  import 'node:path';
16
+ import 'node:module';
18
17
  import 'node:vm';
19
18
  import 'node:events';
20
19
 
21
- var version = "0.28.4";
20
+ var version = "0.29.0";
22
21
 
23
22
  const cli = cac("vite-node");
24
- 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();
25
- cli.command("[...files]").action(run);
23
+ cli.version(version).option("-r, --root <path>", "Use specified root directory").option("-c, --config <path>", "Use specified config file").option("-m, --mode <mode>", "Set env mode").option("-w, --watch", 'Restart on file changes, similar to "nodemon"').option("--script", "Use vite-node as a script runner").option("--options <options>", "Use specified Vite server options").help();
24
+ cli.command("[...files]").allowUnknownOptions().action(run);
26
25
  cli.parse();
27
26
  async function run(files, options = {}) {
28
27
  var _a;
28
+ if (options.script) {
29
+ files = [files[0]];
30
+ options = {};
31
+ process.argv = [process.argv[0], files[0], ...process.argv.slice(2).filter((arg) => arg !== "--script" && arg !== files[0])];
32
+ } else {
33
+ process.argv = [...process.argv.slice(0, 2), ...options["--"] || []];
34
+ }
29
35
  if (!files.length) {
30
36
  console.error(c.red("No files specified."));
31
37
  cli.outputHelp();
32
38
  process.exit(1);
33
39
  }
34
- process.argv = [...process.argv.slice(0, 2), ...options["--"] || []];
35
40
  const serverOptions = options.options ? parseServerOptions(options.options) : {};
36
41
  const server = await createServer({
37
42
  logLevel: "error",
38
43
  configFile: options.config,
39
44
  root: options.root,
45
+ mode: options.mode,
40
46
  plugins: [
41
47
  options.watch && viteNodeHmrPlugin()
42
48
  ]
package/dist/client.cjs CHANGED
@@ -12,7 +12,6 @@ var createDebug = require('debug');
12
12
  var utils = require('./utils.cjs');
13
13
  var sourceMap = require('./source-map.cjs');
14
14
  require('fs');
15
- require('source-map-support');
16
15
 
17
16
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
18
17
 
@@ -163,9 +162,6 @@ class ViteNodeRunner {
163
162
  const [id, url] = await this.resolveUrl(rawId);
164
163
  return await this.cachedRequest(id, url, []);
165
164
  }
166
- getSourceMap(id) {
167
- return this.moduleCache.getSourceMap(id);
168
- }
169
165
  async cachedRequest(id, fsPath, callstack) {
170
166
  const importee = callstack[callstack.length - 1];
171
167
  const mod = this.moduleCache.get(fsPath);
@@ -188,16 +184,16 @@ class ViteNodeRunner {
188
184
  shouldResolveId(id, _importee) {
189
185
  return !utils.isInternalRequest(id) && !mlly.isNodeBuiltin(id);
190
186
  }
191
- async _resolveUrl(id, importee) {
192
- if (importee && id.startsWith(utils.VALID_ID_PREFIX))
193
- importee = void 0;
187
+ async _resolveUrl(id, importer) {
188
+ if (importer && id.startsWith(utils.VALID_ID_PREFIX))
189
+ importer = void 0;
194
190
  id = utils.normalizeRequestId(id, this.options.base);
195
191
  if (!this.shouldResolveId(id))
196
192
  return [id, id];
197
193
  const { path, exists } = utils.toFilePath(id, this.root);
198
194
  if (!this.options.resolveId || exists)
199
195
  return [id, path];
200
- const resolved = await this.options.resolveId(id, importee);
196
+ const resolved = await this.options.resolveId(id, importer);
201
197
  const resolvedId = resolved ? utils.normalizeRequestId(resolved.id, this.options.base) : id;
202
198
  const fsPath = resolved ? resolvedId : path;
203
199
  return [resolvedId, fsPath];
package/dist/client.d.ts CHANGED
@@ -1 +1,2 @@
1
- export { j as DEFAULT_REQUEST_STUBS, M as ModuleCacheMap, h as ViteNodeRunner } from './types-63205a44.js';
1
+ import './types.d-1e7e3fdf.js';
2
+ export { j as DEFAULT_REQUEST_STUBS, M as ModuleCacheMap, h as ViteNodeRunner } from './types-e288fc62.js';
package/dist/client.mjs CHANGED
@@ -8,7 +8,6 @@ import createDebug from 'debug';
8
8
  import { normalizeModuleId, slash, isInternalRequest, VALID_ID_PREFIX, normalizeRequestId, toFilePath, cleanUrl, isPrimitive } from './utils.mjs';
9
9
  import { extractSourceMap } from './source-map.mjs';
10
10
  import 'node:fs';
11
- import 'source-map-support';
12
11
 
13
12
  const { setTimeout, clearTimeout } = globalThis;
14
13
  const debugExecute = createDebug("vite-node:client:execute");
@@ -136,9 +135,6 @@ class ViteNodeRunner {
136
135
  const [id, url] = await this.resolveUrl(rawId);
137
136
  return await this.cachedRequest(id, url, []);
138
137
  }
139
- getSourceMap(id) {
140
- return this.moduleCache.getSourceMap(id);
141
- }
142
138
  async cachedRequest(id, fsPath, callstack) {
143
139
  const importee = callstack[callstack.length - 1];
144
140
  const mod = this.moduleCache.get(fsPath);
@@ -161,16 +157,16 @@ class ViteNodeRunner {
161
157
  shouldResolveId(id, _importee) {
162
158
  return !isInternalRequest(id) && !isNodeBuiltin(id);
163
159
  }
164
- async _resolveUrl(id, importee) {
165
- if (importee && id.startsWith(VALID_ID_PREFIX))
166
- importee = void 0;
160
+ async _resolveUrl(id, importer) {
161
+ if (importer && id.startsWith(VALID_ID_PREFIX))
162
+ importer = void 0;
167
163
  id = normalizeRequestId(id, this.options.base);
168
164
  if (!this.shouldResolveId(id))
169
165
  return [id, id];
170
166
  const { path, exists } = toFilePath(id, this.root);
171
167
  if (!this.options.resolveId || exists)
172
168
  return [id, path];
173
- const resolved = await this.options.resolveId(id, importee);
169
+ const resolved = await this.options.resolveId(id, importer);
174
170
  const resolvedId = resolved ? normalizeRequestId(resolved.id, this.options.base) : id;
175
171
  const fsPath = resolved ? resolvedId : path;
176
172
  return [resolvedId, fsPath];
package/dist/hmr.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { EventEmitter } from 'node: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-63205a44.js';
3
+ import { g as CustomEventMap, h as ViteNodeRunner, i as HMRPayload$1, H as HotContext } from './types-e288fc62.js';
4
+ import './types.d-1e7e3fdf.js';
4
5
 
5
6
  type EventType = string | symbol;
6
7
  type Handler<T = unknown> = (event: T) => void;
package/dist/index.d.ts CHANGED
@@ -1 +1,2 @@
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-63205a44.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-e288fc62.js';
2
+ export { D as DecodedSourceMap, E as EncodedSourceMap } from './types.d-1e7e3fdf.js';
package/dist/server.cjs CHANGED
@@ -3,15 +3,15 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var perf_hooks = require('perf_hooks');
6
+ var fs = require('fs');
6
7
  var pathe = require('pathe');
7
8
  var createDebug = require('debug');
8
- var fs = require('fs');
9
9
  var mlly = require('mlly');
10
10
  var utils = require('./utils.cjs');
11
11
  var c = require('picocolors');
12
12
  var sourceMap = require('./source-map.cjs');
13
13
  require('node:url');
14
- require('source-map-support');
14
+ require('path');
15
15
 
16
16
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
17
17
 
@@ -92,6 +92,8 @@ async function _shouldExternalize(id, options) {
92
92
  if (id.startsWith("data:"))
93
93
  return id;
94
94
  id = patchWindowsImportPath(id);
95
+ if ((options == null ? void 0 : options.cacheDir) && id.includes(options.cacheDir))
96
+ return id;
95
97
  if (matchExternalizePattern(id, options == null ? void 0 : options.inline))
96
98
  return false;
97
99
  if (matchExternalizePattern(id, options == null ? void 0 : options.external))
@@ -209,12 +211,14 @@ class ViteNodeServer {
209
211
  this.options = options;
210
212
  this.fetchPromiseMap = /* @__PURE__ */ new Map();
211
213
  this.transformPromiseMap = /* @__PURE__ */ new Map();
214
+ this.existingOptimizedDeps = /* @__PURE__ */ new Set();
212
215
  this.fetchCache = /* @__PURE__ */ new Map();
213
216
  this.externalizeCache = /* @__PURE__ */ new Map();
214
217
  var _a, _b;
215
218
  const ssrOptions = server.config.ssr;
219
+ options.deps ?? (options.deps = {});
220
+ options.deps.cacheDir = pathe.relative(server.config.root, server.config.cacheDir);
216
221
  if (ssrOptions) {
217
- options.deps ?? (options.deps = {});
218
222
  if (ssrOptions.noExternal === true) {
219
223
  (_a = options.deps).inline ?? (_a.inline = true);
220
224
  } else if (options.deps.inline !== true) {
@@ -234,10 +238,25 @@ class ViteNodeServer {
234
238
  shouldExternalize(id) {
235
239
  return shouldExternalize(id, this.options.deps, this.externalizeCache);
236
240
  }
237
- async resolveId(id, importer) {
241
+ async ensureExists(id) {
242
+ if (this.existingOptimizedDeps.has(id))
243
+ return true;
244
+ if (fs.existsSync(id)) {
245
+ this.existingOptimizedDeps.add(id);
246
+ return true;
247
+ }
248
+ return new Promise((resolve2) => {
249
+ setTimeout(() => {
250
+ this.ensureExists(id).then(() => {
251
+ resolve2(true);
252
+ });
253
+ });
254
+ });
255
+ }
256
+ async resolveId(id, importer, transformMode) {
238
257
  if (importer && !importer.startsWith(this.server.config.root))
239
258
  importer = pathe.resolve(this.server.config.root, importer);
240
- const mode = importer && this.getTransformMode(importer) || "ssr";
259
+ const mode = transformMode ?? (importer && this.getTransformMode(importer) || "ssr");
241
260
  return this.server.pluginContainer.resolveId(id, importer, { ssr: mode === "ssr" });
242
261
  }
243
262
  getSourceMap(source) {
@@ -248,12 +267,12 @@ class ViteNodeServer {
248
267
  const ssrTransformResult = (_b = this.server.moduleGraph.getModuleById(source)) == null ? void 0 : _b.ssrTransformResult;
249
268
  return (ssrTransformResult == null ? void 0 : ssrTransformResult.map) || null;
250
269
  }
251
- async fetchModule(id) {
270
+ async fetchModule(id, transformMode) {
252
271
  id = utils.normalizeModuleId(id);
253
272
  if (!this.fetchPromiseMap.has(id)) {
254
273
  this.fetchPromiseMap.set(
255
274
  id,
256
- this._fetchModule(id).then((r) => {
275
+ this._fetchModule(id, transformMode).then((r) => {
257
276
  return this.options.sourcemap !== true ? { ...r, map: void 0 } : r;
258
277
  }).finally(() => {
259
278
  this.fetchPromiseMap.delete(id);
@@ -284,9 +303,18 @@ class ViteNodeServer {
284
303
  return "ssr";
285
304
  return "web";
286
305
  }
287
- async _fetchModule(id) {
288
- var _a;
306
+ async _fetchModule(id, transformMode) {
307
+ var _a, _b;
289
308
  let result;
309
+ const cacheDir = (_a = this.options.deps) == null ? void 0 : _a.cacheDir;
310
+ if (cacheDir && id.includes(cacheDir) && !id.includes(this.server.config.root)) {
311
+ id = pathe.join(this.server.config.root, id);
312
+ const timeout = setTimeout(() => {
313
+ throw new Error(`ViteNodeServer: ${id} not found. This is a bug, please report it.`);
314
+ }, 5e3);
315
+ await this.ensureExists(id);
316
+ clearTimeout(timeout);
317
+ }
290
318
  const { path: filePath } = utils.toFilePath(id, this.server.config.root);
291
319
  const module = this.server.moduleGraph.getModuleById(id);
292
320
  const timestamp = module ? module.lastHMRTimestamp : null;
@@ -298,10 +326,10 @@ class ViteNodeServer {
298
326
  let duration;
299
327
  if (externalize) {
300
328
  result = { externalize };
301
- (_a = this.debugger) == null ? void 0 : _a.recordExternalize(id, externalize);
329
+ (_b = this.debugger) == null ? void 0 : _b.recordExternalize(id, externalize);
302
330
  } else {
303
331
  const start = perf_hooks.performance.now();
304
- const r = await this._transformRequest(id);
332
+ const r = await this._transformRequest(id, transformMode);
305
333
  duration = perf_hooks.performance.now() - start;
306
334
  result = { code: r == null ? void 0 : r.code, map: r == null ? void 0 : r.map };
307
335
  }
@@ -312,7 +340,7 @@ class ViteNodeServer {
312
340
  });
313
341
  return result;
314
342
  }
315
- async _transformRequest(id) {
343
+ async _transformRequest(id, customTransformMode) {
316
344
  var _a, _b, _c, _d;
317
345
  debugRequest(id);
318
346
  let result = null;
@@ -321,7 +349,8 @@ class ViteNodeServer {
321
349
  if (result)
322
350
  return result;
323
351
  }
324
- if (this.getTransformMode(id) === "web") {
352
+ const transformMode = customTransformMode ?? this.getTransformMode(id);
353
+ if (transformMode === "web") {
325
354
  result = await this.server.transformRequest(id);
326
355
  if (result)
327
356
  result = await this.server.ssrTransform(result.code, result.map, id);
package/dist/server.d.ts CHANGED
@@ -1,5 +1,6 @@
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-63205a44.js';
2
+ import { E as EncodedSourceMap } from './types.d-1e7e3fdf.js';
3
+ import { f as DebuggerOptions, D as DepsHandlingOptions, e as ViteNodeServerOptions, F as FetchResult, d as ViteNodeResolveId } from './types-e288fc62.js';
3
4
 
4
5
  declare class Debugger {
5
6
  options: DebuggerOptions;
@@ -23,6 +24,7 @@ declare class ViteNodeServer {
23
24
  options: ViteNodeServerOptions;
24
25
  private fetchPromiseMap;
25
26
  private transformPromiseMap;
27
+ private existingOptimizedDeps;
26
28
  fetchCache: Map<string, {
27
29
  duration?: number | undefined;
28
30
  timestamp: number;
@@ -32,9 +34,10 @@ declare class ViteNodeServer {
32
34
  debugger?: Debugger;
33
35
  constructor(server: ViteDevServer, options?: ViteNodeServerOptions);
34
36
  shouldExternalize(id: string): Promise<string | false>;
35
- resolveId(id: string, importer?: string): Promise<ViteNodeResolveId | null>;
36
- getSourceMap(source: string): RawSourceMap | null;
37
- fetchModule(id: string): Promise<FetchResult>;
37
+ private ensureExists;
38
+ resolveId(id: string, importer?: string, transformMode?: 'web' | 'ssr'): Promise<ViteNodeResolveId | null>;
39
+ getSourceMap(source: string): EncodedSourceMap | null;
40
+ fetchModule(id: string, transformMode?: 'web' | 'ssr'): Promise<FetchResult>;
38
41
  transformRequest(id: string): Promise<TransformResult | null | undefined>;
39
42
  getTransformMode(id: string): "web" | "ssr";
40
43
  private _fetchModule;
package/dist/server.mjs CHANGED
@@ -1,13 +1,13 @@
1
1
  import { performance } from 'node:perf_hooks';
2
- import { resolve, join } from 'pathe';
3
- import createDebug from 'debug';
4
2
  import { existsSync, promises } from 'node:fs';
3
+ import { resolve, join, relative } from 'pathe';
4
+ import createDebug from 'debug';
5
5
  import { isNodeBuiltin, isValidNodeImport } from 'mlly';
6
6
  import { slash, toArray, normalizeModuleId, toFilePath } from './utils.mjs';
7
7
  import c from 'picocolors';
8
8
  import { withInlineSourcemap } from './source-map.mjs';
9
9
  import 'node:url';
10
- import 'source-map-support';
10
+ import 'node:path';
11
11
 
12
12
  const KNOWN_ASSET_TYPES = [
13
13
  "png",
@@ -83,6 +83,8 @@ async function _shouldExternalize(id, options) {
83
83
  if (id.startsWith("data:"))
84
84
  return id;
85
85
  id = patchWindowsImportPath(id);
86
+ if ((options == null ? void 0 : options.cacheDir) && id.includes(options.cacheDir))
87
+ return id;
86
88
  if (matchExternalizePattern(id, options == null ? void 0 : options.inline))
87
89
  return false;
88
90
  if (matchExternalizePattern(id, options == null ? void 0 : options.external))
@@ -200,12 +202,14 @@ class ViteNodeServer {
200
202
  this.options = options;
201
203
  this.fetchPromiseMap = /* @__PURE__ */ new Map();
202
204
  this.transformPromiseMap = /* @__PURE__ */ new Map();
205
+ this.existingOptimizedDeps = /* @__PURE__ */ new Set();
203
206
  this.fetchCache = /* @__PURE__ */ new Map();
204
207
  this.externalizeCache = /* @__PURE__ */ new Map();
205
208
  var _a, _b;
206
209
  const ssrOptions = server.config.ssr;
210
+ options.deps ?? (options.deps = {});
211
+ options.deps.cacheDir = relative(server.config.root, server.config.cacheDir);
207
212
  if (ssrOptions) {
208
- options.deps ?? (options.deps = {});
209
213
  if (ssrOptions.noExternal === true) {
210
214
  (_a = options.deps).inline ?? (_a.inline = true);
211
215
  } else if (options.deps.inline !== true) {
@@ -225,10 +229,25 @@ class ViteNodeServer {
225
229
  shouldExternalize(id) {
226
230
  return shouldExternalize(id, this.options.deps, this.externalizeCache);
227
231
  }
228
- async resolveId(id, importer) {
232
+ async ensureExists(id) {
233
+ if (this.existingOptimizedDeps.has(id))
234
+ return true;
235
+ if (existsSync(id)) {
236
+ this.existingOptimizedDeps.add(id);
237
+ return true;
238
+ }
239
+ return new Promise((resolve2) => {
240
+ setTimeout(() => {
241
+ this.ensureExists(id).then(() => {
242
+ resolve2(true);
243
+ });
244
+ });
245
+ });
246
+ }
247
+ async resolveId(id, importer, transformMode) {
229
248
  if (importer && !importer.startsWith(this.server.config.root))
230
249
  importer = resolve(this.server.config.root, importer);
231
- const mode = importer && this.getTransformMode(importer) || "ssr";
250
+ const mode = transformMode ?? (importer && this.getTransformMode(importer) || "ssr");
232
251
  return this.server.pluginContainer.resolveId(id, importer, { ssr: mode === "ssr" });
233
252
  }
234
253
  getSourceMap(source) {
@@ -239,12 +258,12 @@ class ViteNodeServer {
239
258
  const ssrTransformResult = (_b = this.server.moduleGraph.getModuleById(source)) == null ? void 0 : _b.ssrTransformResult;
240
259
  return (ssrTransformResult == null ? void 0 : ssrTransformResult.map) || null;
241
260
  }
242
- async fetchModule(id) {
261
+ async fetchModule(id, transformMode) {
243
262
  id = normalizeModuleId(id);
244
263
  if (!this.fetchPromiseMap.has(id)) {
245
264
  this.fetchPromiseMap.set(
246
265
  id,
247
- this._fetchModule(id).then((r) => {
266
+ this._fetchModule(id, transformMode).then((r) => {
248
267
  return this.options.sourcemap !== true ? { ...r, map: void 0 } : r;
249
268
  }).finally(() => {
250
269
  this.fetchPromiseMap.delete(id);
@@ -275,9 +294,18 @@ class ViteNodeServer {
275
294
  return "ssr";
276
295
  return "web";
277
296
  }
278
- async _fetchModule(id) {
279
- var _a;
297
+ async _fetchModule(id, transformMode) {
298
+ var _a, _b;
280
299
  let result;
300
+ const cacheDir = (_a = this.options.deps) == null ? void 0 : _a.cacheDir;
301
+ if (cacheDir && id.includes(cacheDir) && !id.includes(this.server.config.root)) {
302
+ id = join(this.server.config.root, id);
303
+ const timeout = setTimeout(() => {
304
+ throw new Error(`ViteNodeServer: ${id} not found. This is a bug, please report it.`);
305
+ }, 5e3);
306
+ await this.ensureExists(id);
307
+ clearTimeout(timeout);
308
+ }
281
309
  const { path: filePath } = toFilePath(id, this.server.config.root);
282
310
  const module = this.server.moduleGraph.getModuleById(id);
283
311
  const timestamp = module ? module.lastHMRTimestamp : null;
@@ -289,10 +317,10 @@ class ViteNodeServer {
289
317
  let duration;
290
318
  if (externalize) {
291
319
  result = { externalize };
292
- (_a = this.debugger) == null ? void 0 : _a.recordExternalize(id, externalize);
320
+ (_b = this.debugger) == null ? void 0 : _b.recordExternalize(id, externalize);
293
321
  } else {
294
322
  const start = performance.now();
295
- const r = await this._transformRequest(id);
323
+ const r = await this._transformRequest(id, transformMode);
296
324
  duration = performance.now() - start;
297
325
  result = { code: r == null ? void 0 : r.code, map: r == null ? void 0 : r.map };
298
326
  }
@@ -303,7 +331,7 @@ class ViteNodeServer {
303
331
  });
304
332
  return result;
305
333
  }
306
- async _transformRequest(id) {
334
+ async _transformRequest(id, customTransformMode) {
307
335
  var _a, _b, _c, _d;
308
336
  debugRequest(id);
309
337
  let result = null;
@@ -312,7 +340,8 @@ class ViteNodeServer {
312
340
  if (result)
313
341
  return result;
314
342
  }
315
- if (this.getTransformMode(id) === "web") {
343
+ const transformMode = customTransformMode ?? this.getTransformMode(id);
344
+ if (transformMode === "web") {
316
345
  result = await this.server.transformRequest(id);
317
346
  if (result)
318
347
  result = await this.server.ssrTransform(result.code, result.map, id);