vite-node 0.24.5 → 0.25.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
@@ -56,6 +56,7 @@ In Vite Node, the server and runner (client) are separated, so you can integrate
56
56
  import { createServer } from 'vite'
57
57
  import { ViteNodeServer } from 'vite-node/server'
58
58
  import { ViteNodeRunner } from 'vite-node/client'
59
+ import { installSourcemapsSupport } from 'vite-node/source-map'
59
60
 
60
61
  // create vite server
61
62
  const server = await createServer({
@@ -70,6 +71,11 @@ await server.pluginContainer.buildStart({})
70
71
  // create vite-node server
71
72
  const node = new ViteNodeServer(server)
72
73
 
74
+ // fixes stacktraces in Errors
75
+ installSourcemapsSupport({
76
+ getSourceMap: source => node.getSourceMap(source),
77
+ })
78
+
73
79
  // create vite-node runner
74
80
  const runner = new ViteNodeRunner({
75
81
  root: server.config.root,
package/dist/cli.cjs CHANGED
@@ -7,6 +7,7 @@ var server = require('./server.cjs');
7
7
  var client = require('./client.cjs');
8
8
  var utils = require('./utils.cjs');
9
9
  var hmr = require('./chunk-hmr.cjs');
10
+ var sourceMap = require('./source-map.cjs');
10
11
  require('tty');
11
12
  require('perf_hooks');
12
13
  require('pathe');
@@ -16,6 +17,7 @@ require('mlly');
16
17
  require('url');
17
18
  require('module');
18
19
  require('vm');
20
+ require('source-map-support');
19
21
 
20
22
  function toArr(any) {
21
23
  return any == null ? [] : Array.isArray(any) ? any : [any];
@@ -630,7 +632,7 @@ class CAC extends events.EventEmitter {
630
632
 
631
633
  const cac = (name = "") => new CAC(name);
632
634
 
633
- var version = "0.24.5";
635
+ var version = "0.25.0";
634
636
 
635
637
  const cli = cac("vite-node");
636
638
  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();
@@ -655,6 +657,9 @@ async function run(files, options = {}) {
655
657
  });
656
658
  await server$1.pluginContainer.buildStart({});
657
659
  const node = new server.ViteNodeServer(server$1, serverOptions);
660
+ sourceMap.installSourcemapsSupport({
661
+ getSourceMap: (source) => node.getSourceMap(source)
662
+ });
658
663
  const runner = new client.ViteNodeRunner({
659
664
  root: server$1.config.root,
660
665
  base: server$1.config.base,
package/dist/cli.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { e as ViteNodeServerOptions } from './types-ab8db104.js';
1
+ import { e as ViteNodeServerOptions } from './types-257c4f87.js';
2
2
 
3
3
  interface CliOptions {
4
4
  root?: string;
package/dist/cli.mjs CHANGED
@@ -5,6 +5,7 @@ import { ViteNodeServer } from './server.mjs';
5
5
  import { ViteNodeRunner } from './client.mjs';
6
6
  import { toArray } from './utils.mjs';
7
7
  import { v as viteNodeHmrPlugin, c as createHotContext, h as handleMessage } from './chunk-hmr.mjs';
8
+ import { installSourcemapsSupport } from './source-map.mjs';
8
9
  import 'tty';
9
10
  import 'perf_hooks';
10
11
  import 'pathe';
@@ -14,6 +15,7 @@ import 'mlly';
14
15
  import 'url';
15
16
  import 'module';
16
17
  import 'vm';
18
+ import 'source-map-support';
17
19
 
18
20
  function toArr(any) {
19
21
  return any == null ? [] : Array.isArray(any) ? any : [any];
@@ -628,7 +630,7 @@ class CAC extends EventEmitter {
628
630
 
629
631
  const cac = (name = "") => new CAC(name);
630
632
 
631
- var version = "0.24.5";
633
+ var version = "0.25.0";
632
634
 
633
635
  const cli = cac("vite-node");
634
636
  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();
@@ -653,6 +655,9 @@ async function run(files, options = {}) {
653
655
  });
654
656
  await server.pluginContainer.buildStart({});
655
657
  const node = new ViteNodeServer(server, serverOptions);
658
+ installSourcemapsSupport({
659
+ getSourceMap: (source) => node.getSourceMap(source)
660
+ });
656
661
  const runner = new ViteNodeRunner({
657
662
  root: server.config.root,
658
663
  base: server.config.base,
package/dist/client.cjs CHANGED
@@ -124,6 +124,20 @@ class ModuleCacheMap extends Map {
124
124
  }
125
125
  return invalidated;
126
126
  }
127
+ getSourceMap(id) {
128
+ var _a, _b;
129
+ const fsPath = this.normalizePath(id);
130
+ const cache = this.get(fsPath);
131
+ if (cache.map)
132
+ return cache.map;
133
+ const mapString = (_b = (_a = cache == null ? void 0 : cache.code) == null ? void 0 : _a.match(/\/\/# sourceMappingURL=data:application\/json;charset=utf-8;base64,(.+)/)) == null ? void 0 : _b[1];
134
+ if (mapString) {
135
+ const map = JSON.parse(Buffer.from(mapString, "base64").toString("utf-8"));
136
+ cache.map = map;
137
+ return map;
138
+ }
139
+ return null;
140
+ }
127
141
  }
128
142
  class ViteNodeRunner {
129
143
  constructor(options) {
@@ -138,6 +152,9 @@ class ViteNodeRunner {
138
152
  async executeId(id) {
139
153
  return await this.cachedRequest(id, []);
140
154
  }
155
+ getSourceMap(id) {
156
+ return this.moduleCache.getSourceMap(id);
157
+ }
141
158
  async cachedRequest(rawId, callstack) {
142
159
  const id = utils.normalizeRequestId(rawId, this.options.base);
143
160
  const fsPath = utils.toFilePath(id, this.root);
@@ -269,10 +286,13 @@ ${getStack()}`), 2e3);
269
286
  debugExecute(__filename);
270
287
  if (transformed[0] === "#")
271
288
  transformed = transformed.replace(/^\#\!.*/, (s) => " ".repeat(s.length));
272
- const fn = vm__default["default"].runInThisContext(`'use strict';async (${Object.keys(context).join(",")})=>{{${transformed}
273
- }}`, {
289
+ const codeDefinition = `'use strict';async (${Object.keys(context).join(",")})=>{{`;
290
+ const code = `${codeDefinition}${transformed}
291
+ }}`;
292
+ const fn = vm__default["default"].runInThisContext(code, {
274
293
  filename: fsPath,
275
- lineOffset: 0
294
+ lineOffset: 0,
295
+ columnOffset: -codeDefinition.length
276
296
  });
277
297
  await fn(...Object.values(context));
278
298
  return exports;
package/dist/client.d.ts CHANGED
@@ -1 +1 @@
1
- export { j as DEFAULT_REQUEST_STUBS, M as ModuleCacheMap, h as ViteNodeRunner } from './types-ab8db104.js';
1
+ export { j as DEFAULT_REQUEST_STUBS, M as ModuleCacheMap, h as ViteNodeRunner } from './types-257c4f87.js';
package/dist/client.mjs CHANGED
@@ -97,6 +97,20 @@ class ModuleCacheMap extends Map {
97
97
  }
98
98
  return invalidated;
99
99
  }
100
+ getSourceMap(id) {
101
+ var _a, _b;
102
+ const fsPath = this.normalizePath(id);
103
+ const cache = this.get(fsPath);
104
+ if (cache.map)
105
+ return cache.map;
106
+ const mapString = (_b = (_a = cache == null ? void 0 : cache.code) == null ? void 0 : _a.match(/\/\/# sourceMappingURL=data:application\/json;charset=utf-8;base64,(.+)/)) == null ? void 0 : _b[1];
107
+ if (mapString) {
108
+ const map = JSON.parse(Buffer.from(mapString, "base64").toString("utf-8"));
109
+ cache.map = map;
110
+ return map;
111
+ }
112
+ return null;
113
+ }
100
114
  }
101
115
  class ViteNodeRunner {
102
116
  constructor(options) {
@@ -111,6 +125,9 @@ class ViteNodeRunner {
111
125
  async executeId(id) {
112
126
  return await this.cachedRequest(id, []);
113
127
  }
128
+ getSourceMap(id) {
129
+ return this.moduleCache.getSourceMap(id);
130
+ }
114
131
  async cachedRequest(rawId, callstack) {
115
132
  const id = normalizeRequestId(rawId, this.options.base);
116
133
  const fsPath = toFilePath(id, this.root);
@@ -242,10 +259,13 @@ ${getStack()}`), 2e3);
242
259
  debugExecute(__filename);
243
260
  if (transformed[0] === "#")
244
261
  transformed = transformed.replace(/^\#\!.*/, (s) => " ".repeat(s.length));
245
- const fn = vm.runInThisContext(`'use strict';async (${Object.keys(context).join(",")})=>{{${transformed}
246
- }}`, {
262
+ const codeDefinition = `'use strict';async (${Object.keys(context).join(",")})=>{{`;
263
+ const code = `${codeDefinition}${transformed}
264
+ }}`;
265
+ const fn = vm.runInThisContext(code, {
247
266
  filename: fsPath,
248
- lineOffset: 0
267
+ lineOffset: 0,
268
+ columnOffset: -codeDefinition.length
249
269
  });
250
270
  await fn(...Object.values(context));
251
271
  return exports;
package/dist/hmr.d.ts CHANGED
@@ -1,6 +1,6 @@
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-ab8db104.js';
3
+ import { g as CustomEventMap, h as ViteNodeRunner, i as HMRPayload$1, H as HotContext } from './types-257c4f87.js';
4
4
 
5
5
  declare type EventType = string | symbol;
6
6
  declare type Handler<T = unknown> = (event: T) => void;
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-ab8db104.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-257c4f87.js';
package/dist/server.cjs CHANGED
@@ -211,6 +211,14 @@ class ViteNodeServer {
211
211
  const mode = importer && this.getTransformMode(importer) || "ssr";
212
212
  return this.server.pluginContainer.resolveId(id, importer, { ssr: mode === "ssr" });
213
213
  }
214
+ getSourceMap(source) {
215
+ var _a, _b;
216
+ const fetchResult = (_a = this.fetchCache.get(source)) == null ? void 0 : _a.result;
217
+ if (fetchResult == null ? void 0 : fetchResult.map)
218
+ return fetchResult.map;
219
+ const ssrTransformResult = (_b = this.server.moduleGraph.getModuleById(source)) == null ? void 0 : _b.ssrTransformResult;
220
+ return (ssrTransformResult == null ? void 0 : ssrTransformResult.map) || null;
221
+ }
214
222
  async fetchModule(id) {
215
223
  if (!this.fetchPromiseMap.has(id)) {
216
224
  this.fetchPromiseMap.set(
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 } from './types-ab8db104.js';
2
+ import { f as DebuggerOptions, D as DepsHandlingOptions, e as ViteNodeServerOptions, F as FetchResult, d as ViteNodeResolveId, R as RawSourceMap } from './types-257c4f87.js';
3
3
 
4
4
  declare class Debugger {
5
5
  options: DebuggerOptions;
@@ -33,6 +33,7 @@ declare class ViteNodeServer {
33
33
  constructor(server: ViteDevServer, options?: ViteNodeServerOptions);
34
34
  shouldExternalize(id: string): Promise<string | false>;
35
35
  resolveId(id: string, importer?: string): Promise<ViteNodeResolveId | null>;
36
+ getSourceMap(source: string): RawSourceMap | null;
36
37
  fetchModule(id: string): Promise<FetchResult>;
37
38
  transformRequest(id: string): Promise<TransformResult | null | undefined>;
38
39
  getTransformMode(id: string): "web" | "ssr";
package/dist/server.mjs CHANGED
@@ -203,6 +203,14 @@ class ViteNodeServer {
203
203
  const mode = importer && this.getTransformMode(importer) || "ssr";
204
204
  return this.server.pluginContainer.resolveId(id, importer, { ssr: mode === "ssr" });
205
205
  }
206
+ getSourceMap(source) {
207
+ var _a, _b;
208
+ const fetchResult = (_a = this.fetchCache.get(source)) == null ? void 0 : _a.result;
209
+ if (fetchResult == null ? void 0 : fetchResult.map)
210
+ return fetchResult.map;
211
+ const ssrTransformResult = (_b = this.server.moduleGraph.getModuleById(source)) == null ? void 0 : _b.ssrTransformResult;
212
+ return (ssrTransformResult == null ? void 0 : ssrTransformResult.map) || null;
213
+ }
206
214
  async fetchModule(id) {
207
215
  if (!this.fetchPromiseMap.has(id)) {
208
216
  this.fetchPromiseMap.set(
@@ -0,0 +1,24 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var sourceMapSupport = require('source-map-support');
6
+
7
+ function installSourcemapsSupport(options) {
8
+ sourceMapSupport.install({
9
+ environment: "node",
10
+ handleUncaughtExceptions: false,
11
+ retrieveSourceMap(source) {
12
+ const map = options.getSourceMap(source);
13
+ if (map) {
14
+ return {
15
+ url: source,
16
+ map
17
+ };
18
+ }
19
+ return null;
20
+ }
21
+ });
22
+ }
23
+
24
+ exports.installSourcemapsSupport = installSourcemapsSupport;
@@ -0,0 +1,8 @@
1
+ import { R as RawSourceMap } from './types-257c4f87.js';
2
+
3
+ interface InstallSourceMapSupportOptions {
4
+ getSourceMap: (source: string) => RawSourceMap | null | undefined;
5
+ }
6
+ declare function installSourcemapsSupport(options: InstallSourceMapSupportOptions): void;
7
+
8
+ export { installSourcemapsSupport };
@@ -0,0 +1,20 @@
1
+ import { install } from 'source-map-support';
2
+
3
+ function installSourcemapsSupport(options) {
4
+ install({
5
+ environment: "node",
6
+ handleUncaughtExceptions: false,
7
+ retrieveSourceMap(source) {
8
+ const map = options.getSourceMap(source);
9
+ if (map) {
10
+ return {
11
+ url: source,
12
+ map
13
+ };
14
+ }
15
+ return null;
16
+ }
17
+ });
18
+ }
19
+
20
+ export { installSourcemapsSupport };
@@ -62,6 +62,7 @@ interface ErrorPayload {
62
62
 
63
63
  interface CustomEventMap {
64
64
  'vite:beforeUpdate': UpdatePayload
65
+ 'vite:afterUpdate': UpdatePayload
65
66
  'vite:beforePrune': PrunePayload
66
67
  'vite:beforeFullReload': FullReloadPayload
67
68
  'vite:error': ErrorPayload
@@ -138,6 +139,10 @@ declare class ModuleCacheMap extends Map<string, ModuleCache> {
138
139
  * Invalidate dependency modules of the given modules, down to the bottom-level dependencies
139
140
  */
140
141
  invalidateSubDepTree(ids: string[] | Set<string>, invalidated?: Set<string>): Set<string>;
142
+ /**
143
+ * Return parsed source map based on inlined source map of the module
144
+ */
145
+ getSourceMap(id: string): any;
141
146
  }
142
147
  declare class ViteNodeRunner {
143
148
  options: ViteNodeRunnerOptions;
@@ -151,6 +156,7 @@ declare class ViteNodeRunner {
151
156
  constructor(options: ViteNodeRunnerOptions);
152
157
  executeFile(file: string): Promise<any>;
153
158
  executeId(id: string): Promise<any>;
159
+ getSourceMap(id: string): any;
154
160
  /** @internal */
155
161
  cachedRequest(rawId: string, callstack: string[]): Promise<any>;
156
162
  /** @internal */
@@ -204,6 +210,7 @@ interface ModuleCache {
204
210
  promise?: Promise<any>;
205
211
  exports?: any;
206
212
  code?: string;
213
+ map?: RawSourceMap;
207
214
  /**
208
215
  * Module ids that imports this module
209
216
  */
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-ab8db104.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-257c4f87.js';
package/dist/utils.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { TransformResult } from 'vite';
2
- import { N as Nullable, A as Arrayable } from './types-ab8db104.js';
2
+ import { N as Nullable, A as Arrayable } from './types-257c4f87.js';
3
3
 
4
4
  declare const isWindows: boolean;
5
5
  declare function slash(str: string): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-node",
3
- "version": "0.24.5",
3
+ "version": "0.25.0",
4
4
  "description": "Vite as Node.js runtime",
5
5
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
6
6
  "license": "MIT",
@@ -39,6 +39,11 @@
39
39
  "types": "./dist/hmr.d.ts",
40
40
  "require": "./dist/hmr.cjs",
41
41
  "import": "./dist/hmr.mjs"
42
+ },
43
+ "./source-map": {
44
+ "types": "./dist/source-map.d.ts",
45
+ "require": "./dist/source-map.cjs",
46
+ "import": "./dist/source-map.mjs"
42
47
  }
43
48
  },
44
49
  "main": "./dist/index.mjs",
@@ -67,10 +72,14 @@
67
72
  "debug": "^4.3.4",
68
73
  "mlly": "^0.5.16",
69
74
  "pathe": "^0.2.0",
75
+ "source-map": "^0.6.1",
76
+ "source-map-support": "^0.5.21",
70
77
  "vite": "^3.0.0"
71
78
  },
72
79
  "devDependencies": {
73
80
  "@types/debug": "^4.1.7",
81
+ "@types/source-map": "^0.5.7",
82
+ "@types/source-map-support": "^0.5.6",
74
83
  "cac": "^6.7.14",
75
84
  "picocolors": "^1.0.0",
76
85
  "rollup": "^2.79.1"