vite-node 4.0.0-beta.8 → 5.0.0-beta.2

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.
@@ -1,110 +1,109 @@
1
+ import { Buffer } from 'node:buffer';
1
2
  import { isAbsolute, resolve as resolve$1, relative, dirname } from 'pathe';
2
3
  import fs from 'node:fs';
3
4
  import path from 'node:path';
4
- import { withTrailingSlash } from './utils.mjs';
5
+ import process from 'node:process';
6
+ import { withTrailingSlash } from './utils.js';
5
7
  import 'node:module';
6
8
  import 'node:url';
7
9
 
8
- const comma = ','.charCodeAt(0);
9
- const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
10
- const intToChar = new Uint8Array(64); // 64 possible chars.
11
- const charToInt = new Uint8Array(128); // z is 122 in ASCII
10
+ // src/vlq.ts
11
+ var comma = ",".charCodeAt(0);
12
+ var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
13
+ var intToChar = new Uint8Array(64);
14
+ var charToInt = new Uint8Array(128);
12
15
  for (let i = 0; i < chars.length; i++) {
13
- const c = chars.charCodeAt(i);
14
- intToChar[i] = c;
15
- charToInt[c] = i;
16
+ const c = chars.charCodeAt(i);
17
+ intToChar[i] = c;
18
+ charToInt[c] = i;
16
19
  }
17
20
  function decodeInteger(reader, relative) {
18
- let value = 0;
19
- let shift = 0;
20
- let integer = 0;
21
- do {
22
- const c = reader.next();
23
- integer = charToInt[c];
24
- value |= (integer & 31) << shift;
25
- shift += 5;
26
- } while (integer & 32);
27
- const shouldNegate = value & 1;
28
- value >>>= 1;
29
- if (shouldNegate) {
30
- value = -2147483648 | -value;
31
- }
32
- return relative + value;
21
+ let value = 0;
22
+ let shift = 0;
23
+ let integer = 0;
24
+ do {
25
+ const c = reader.next();
26
+ integer = charToInt[c];
27
+ value |= (integer & 31) << shift;
28
+ shift += 5;
29
+ } while (integer & 32);
30
+ const shouldNegate = value & 1;
31
+ value >>>= 1;
32
+ if (shouldNegate) {
33
+ value = -2147483648 | -value;
34
+ }
35
+ return relative + value;
33
36
  }
34
37
  function hasMoreVlq(reader, max) {
35
- if (reader.pos >= max)
36
- return false;
37
- return reader.peek() !== comma;
38
- }
39
- class StringReader {
40
- constructor(buffer) {
41
- this.pos = 0;
42
- this.buffer = buffer;
43
- }
44
- next() {
45
- return this.buffer.charCodeAt(this.pos++);
46
- }
47
- peek() {
48
- return this.buffer.charCodeAt(this.pos);
49
- }
50
- indexOf(char) {
51
- const { buffer, pos } = this;
52
- const idx = buffer.indexOf(char, pos);
53
- return idx === -1 ? buffer.length : idx;
54
- }
38
+ if (reader.pos >= max) return false;
39
+ return reader.peek() !== comma;
55
40
  }
41
+ var StringReader = class {
42
+ constructor(buffer) {
43
+ this.pos = 0;
44
+ this.buffer = buffer;
45
+ }
46
+ next() {
47
+ return this.buffer.charCodeAt(this.pos++);
48
+ }
49
+ peek() {
50
+ return this.buffer.charCodeAt(this.pos);
51
+ }
52
+ indexOf(char) {
53
+ const { buffer, pos } = this;
54
+ const idx = buffer.indexOf(char, pos);
55
+ return idx === -1 ? buffer.length : idx;
56
+ }
57
+ };
56
58
 
59
+ // src/sourcemap-codec.ts
57
60
  function decode(mappings) {
58
- const { length } = mappings;
59
- const reader = new StringReader(mappings);
60
- const decoded = [];
61
- let genColumn = 0;
62
- let sourcesIndex = 0;
63
- let sourceLine = 0;
64
- let sourceColumn = 0;
65
- let namesIndex = 0;
66
- do {
67
- const semi = reader.indexOf(';');
68
- const line = [];
69
- let sorted = true;
70
- let lastCol = 0;
71
- genColumn = 0;
72
- while (reader.pos < semi) {
73
- let seg;
74
- genColumn = decodeInteger(reader, genColumn);
75
- if (genColumn < lastCol)
76
- sorted = false;
77
- lastCol = genColumn;
78
- if (hasMoreVlq(reader, semi)) {
79
- sourcesIndex = decodeInteger(reader, sourcesIndex);
80
- sourceLine = decodeInteger(reader, sourceLine);
81
- sourceColumn = decodeInteger(reader, sourceColumn);
82
- if (hasMoreVlq(reader, semi)) {
83
- namesIndex = decodeInteger(reader, namesIndex);
84
- seg = [genColumn, sourcesIndex, sourceLine, sourceColumn, namesIndex];
85
- }
86
- else {
87
- seg = [genColumn, sourcesIndex, sourceLine, sourceColumn];
88
- }
89
- }
90
- else {
91
- seg = [genColumn];
92
- }
93
- line.push(seg);
94
- reader.pos++;
61
+ const { length } = mappings;
62
+ const reader = new StringReader(mappings);
63
+ const decoded = [];
64
+ let genColumn = 0;
65
+ let sourcesIndex = 0;
66
+ let sourceLine = 0;
67
+ let sourceColumn = 0;
68
+ let namesIndex = 0;
69
+ do {
70
+ const semi = reader.indexOf(";");
71
+ const line = [];
72
+ let sorted = true;
73
+ let lastCol = 0;
74
+ genColumn = 0;
75
+ while (reader.pos < semi) {
76
+ let seg;
77
+ genColumn = decodeInteger(reader, genColumn);
78
+ if (genColumn < lastCol) sorted = false;
79
+ lastCol = genColumn;
80
+ if (hasMoreVlq(reader, semi)) {
81
+ sourcesIndex = decodeInteger(reader, sourcesIndex);
82
+ sourceLine = decodeInteger(reader, sourceLine);
83
+ sourceColumn = decodeInteger(reader, sourceColumn);
84
+ if (hasMoreVlq(reader, semi)) {
85
+ namesIndex = decodeInteger(reader, namesIndex);
86
+ seg = [genColumn, sourcesIndex, sourceLine, sourceColumn, namesIndex];
87
+ } else {
88
+ seg = [genColumn, sourcesIndex, sourceLine, sourceColumn];
95
89
  }
96
- if (!sorted)
97
- sort(line);
98
- decoded.push(line);
99
- reader.pos = semi + 1;
100
- } while (reader.pos <= length);
101
- return decoded;
90
+ } else {
91
+ seg = [genColumn];
92
+ }
93
+ line.push(seg);
94
+ reader.pos++;
95
+ }
96
+ if (!sorted) sort(line);
97
+ decoded.push(line);
98
+ reader.pos = semi + 1;
99
+ } while (reader.pos <= length);
100
+ return decoded;
102
101
  }
103
102
  function sort(line) {
104
- line.sort(sortComparator$1);
103
+ line.sort(sortComparator$1);
105
104
  }
106
105
  function sortComparator$1(a, b) {
107
- return a[0] - b[0];
106
+ return a[0] - b[0];
108
107
  }
109
108
 
110
109
  // Matches the scheme of a URL, eg "http://"
@@ -130,16 +129,6 @@ const urlRegex = /^([\w+.-]+:)\/\/([^@/#?]*@)?([^:/#?]*)(:\d+)?(\/[^#?]*)?(\?[^#
130
129
  * 4. Hash, including "#", optional.
131
130
  */
132
131
  const fileRegex = /^file:(?:\/\/((?![a-z]:)[^/#?]*)?)?(\/?[^#?]*)(\?[^#]*)?(#.*)?/i;
133
- var UrlType;
134
- (function (UrlType) {
135
- UrlType[UrlType["Empty"] = 1] = "Empty";
136
- UrlType[UrlType["Hash"] = 2] = "Hash";
137
- UrlType[UrlType["Query"] = 3] = "Query";
138
- UrlType[UrlType["RelativePath"] = 4] = "RelativePath";
139
- UrlType[UrlType["AbsolutePath"] = 5] = "AbsolutePath";
140
- UrlType[UrlType["SchemeRelative"] = 6] = "SchemeRelative";
141
- UrlType[UrlType["Absolute"] = 7] = "Absolute";
142
- })(UrlType || (UrlType = {}));
143
132
  function isAbsoluteUrl(input) {
144
133
  return schemeRegex.test(input);
145
134
  }
@@ -173,21 +162,21 @@ function makeUrl(scheme, user, host, port, path, query, hash) {
173
162
  path,
174
163
  query,
175
164
  hash,
176
- type: UrlType.Absolute,
165
+ type: 7 /* Absolute */,
177
166
  };
178
167
  }
179
168
  function parseUrl(input) {
180
169
  if (isSchemeRelativeUrl(input)) {
181
170
  const url = parseAbsoluteUrl('http:' + input);
182
171
  url.scheme = '';
183
- url.type = UrlType.SchemeRelative;
172
+ url.type = 6 /* SchemeRelative */;
184
173
  return url;
185
174
  }
186
175
  if (isAbsolutePath(input)) {
187
176
  const url = parseAbsoluteUrl('http://foo.com' + input);
188
177
  url.scheme = '';
189
178
  url.host = '';
190
- url.type = UrlType.AbsolutePath;
179
+ url.type = 5 /* AbsolutePath */;
191
180
  return url;
192
181
  }
193
182
  if (isFileUrl(input))
@@ -199,11 +188,11 @@ function parseUrl(input) {
199
188
  url.host = '';
200
189
  url.type = input
201
190
  ? input.startsWith('?')
202
- ? UrlType.Query
191
+ ? 3 /* Query */
203
192
  : input.startsWith('#')
204
- ? UrlType.Hash
205
- : UrlType.RelativePath
206
- : UrlType.Empty;
193
+ ? 2 /* Hash */
194
+ : 4 /* RelativePath */
195
+ : 1 /* Empty */;
207
196
  return url;
208
197
  }
209
198
  function stripPathFilename(path) {
@@ -231,7 +220,7 @@ function mergePaths(url, base) {
231
220
  * "foo/.". We need to normalize to a standard representation.
232
221
  */
233
222
  function normalizePath(url, type) {
234
- const rel = type <= UrlType.RelativePath;
223
+ const rel = type <= 4 /* RelativePath */;
235
224
  const pieces = url.path.split('/');
236
225
  // We need to preserve the first piece always, so that we output a leading slash. The item at
237
226
  // pieces[0] is an empty string.
@@ -292,27 +281,27 @@ function resolve(input, base) {
292
281
  return '';
293
282
  const url = parseUrl(input);
294
283
  let inputType = url.type;
295
- if (base && inputType !== UrlType.Absolute) {
284
+ if (base && inputType !== 7 /* Absolute */) {
296
285
  const baseUrl = parseUrl(base);
297
286
  const baseType = baseUrl.type;
298
287
  switch (inputType) {
299
- case UrlType.Empty:
288
+ case 1 /* Empty */:
300
289
  url.hash = baseUrl.hash;
301
290
  // fall through
302
- case UrlType.Hash:
291
+ case 2 /* Hash */:
303
292
  url.query = baseUrl.query;
304
293
  // fall through
305
- case UrlType.Query:
306
- case UrlType.RelativePath:
294
+ case 3 /* Query */:
295
+ case 4 /* RelativePath */:
307
296
  mergePaths(url, baseUrl);
308
297
  // fall through
309
- case UrlType.AbsolutePath:
298
+ case 5 /* AbsolutePath */:
310
299
  // The host, user, and port are joined, you can't copy one without the others.
311
300
  url.user = baseUrl.user;
312
301
  url.host = baseUrl.host;
313
302
  url.port = baseUrl.port;
314
303
  // fall through
315
- case UrlType.SchemeRelative:
304
+ case 6 /* SchemeRelative */:
316
305
  // The input doesn't have a schema at least, so we need to copy at least that over.
317
306
  url.scheme = baseUrl.scheme;
318
307
  }
@@ -324,10 +313,10 @@ function resolve(input, base) {
324
313
  switch (inputType) {
325
314
  // This is impossible, because of the empty checks at the start of the function.
326
315
  // case UrlType.Empty:
327
- case UrlType.Hash:
328
- case UrlType.Query:
316
+ case 2 /* Hash */:
317
+ case 3 /* Query */:
329
318
  return queryHash;
330
- case UrlType.RelativePath: {
319
+ case 4 /* RelativePath */: {
331
320
  // The first char is always a "/", and we need it to be relative.
332
321
  const path = url.path.slice(1);
333
322
  if (!path)
@@ -340,7 +329,7 @@ function resolve(input, base) {
340
329
  }
341
330
  return path + queryHash;
342
331
  }
343
- case UrlType.AbsolutePath:
332
+ case 5 /* AbsolutePath */:
344
333
  return url.path + queryHash;
345
334
  default:
346
335
  return url.scheme + '//' + url.user + url.host + url.port + url.path + queryHash;
@@ -550,11 +539,17 @@ function traceSegmentInternal(segments, memo, line, column, bias) {
550
539
  // Only install once if called multiple times
551
540
  let errorFormatterInstalled = false;
552
541
  // Maps a file path to a string containing the file contents
553
- const fileContentsCache = {}, sourceMapCache = {}, reSourceMap = /^data:application\/json[^,]+base64,/;
542
+ const fileContentsCache = {};
543
+ // Maps a file path to a source map for that file
544
+ const sourceMapCache = {};
545
+ // Regex for detecting source maps
546
+ const reSourceMap = /^data:application\/json[^,]+base64,/;
554
547
  // Priority list of retrieve handlers
555
- let retrieveFileHandlers = [], retrieveMapHandlers = [];
548
+ let retrieveFileHandlers = [];
549
+ let retrieveMapHandlers = [];
556
550
  function globalProcessVersion() {
557
- return typeof process === "object" && process !== null ? process.version : "";
551
+ if (typeof process === "object" && process !== null) return process.version;
552
+ else return "";
558
553
  }
559
554
  function handlerExec(list) {
560
555
  return function(arg) {
@@ -567,7 +562,9 @@ function handlerExec(list) {
567
562
  }
568
563
  let retrieveFile = handlerExec(retrieveFileHandlers);
569
564
  retrieveFileHandlers.push((path) => {
570
- if (path = path.trim(), path.startsWith("file:"))
565
+ // Trim the path to make sure there is no extra whitespace.
566
+ path = path.trim();
567
+ if (path.startsWith("file:"))
571
568
  // existsSync/readFileSync can't handle file protocol, but once stripped, it works
572
569
  path = path.replace(/file:\/\/\/(\w:)?/, (protocol, drive) => {
573
570
  return drive ? "" : "/";
@@ -582,10 +579,16 @@ retrieveFileHandlers.push((path) => {
582
579
  // Support URLs relative to a directory, but be careful about a protocol prefix
583
580
  function supportRelativeURL(file, url) {
584
581
  if (!file) return url;
585
- const dir = path.dirname(file), match = /^\w+:\/\/[^/]*/.exec(dir);
582
+ const dir = path.dirname(file);
583
+ const match = /^\w+:\/\/[^/]*/.exec(dir);
586
584
  let protocol = match ? match[0] : "";
587
585
  const startPath = dir.slice(protocol.length);
588
- return protocol && /^\/\w:/.test(startPath) ? (protocol += "/", protocol + path.resolve(dir.slice(protocol.length), url).replace(/\\/g, "/")) : protocol + path.resolve(dir.slice(protocol.length), url);
586
+ if (protocol && /^\/\w:/.test(startPath)) {
587
+ // handle file:///C:/ paths
588
+ protocol += "/";
589
+ return protocol + path.resolve(dir.slice(protocol.length), url).replace(/\\/g, "/");
590
+ }
591
+ return protocol + path.resolve(dir.slice(protocol.length), url);
589
592
  }
590
593
  function retrieveSourceMapURL(source) {
591
594
  // Get the URL of the source map
@@ -597,7 +600,8 @@ function retrieveSourceMapURL(source) {
597
600
  let lastMatch, match;
598
601
  // eslint-disable-next-line no-cond-assign
599
602
  while (match = re.exec(fileData)) lastMatch = match;
600
- return lastMatch ? lastMatch[1] : null;
603
+ if (!lastMatch) return null;
604
+ return lastMatch[1];
601
605
  }
602
606
  // Can be overridden by the retrieveSourceMap option to install. Takes a
603
607
  // generated source filename; returns a {map, optional url} object, or null if
@@ -613,12 +617,18 @@ retrieveMapHandlers.push((source) => {
613
617
  if (reSourceMap.test(sourceMappingURL)) {
614
618
  // Support source map URL as a data url
615
619
  const rawData = sourceMappingURL.slice(sourceMappingURL.indexOf(",") + 1);
616
- sourceMapData = Buffer.from(rawData, "base64").toString(), sourceMappingURL = source;
617
- } else sourceMappingURL = supportRelativeURL(source, sourceMappingURL), sourceMapData = retrieveFile(sourceMappingURL);
618
- return sourceMapData ? {
620
+ sourceMapData = Buffer.from(rawData, "base64").toString();
621
+ sourceMappingURL = source;
622
+ } else {
623
+ // Support source map URLs relative to the source URL
624
+ sourceMappingURL = supportRelativeURL(source, sourceMappingURL);
625
+ sourceMapData = retrieveFile(sourceMappingURL);
626
+ }
627
+ if (!sourceMapData) return null;
628
+ return {
619
629
  url: sourceMappingURL,
620
630
  map: sourceMapData
621
- } : null;
631
+ };
622
632
  });
623
633
  // interface Position {
624
634
  // source: string
@@ -630,15 +640,17 @@ function mapSourcePosition(position) {
630
640
  let sourceMap = sourceMapCache[position.source];
631
641
  if (!sourceMap) {
632
642
  // Call the (overridable) retrieveSourceMap function to get the source map.
633
- const urlAndMap = retrieveSourceMap(position.source), map = urlAndMap && urlAndMap.map;
643
+ const urlAndMap = retrieveSourceMap(position.source);
644
+ const map = urlAndMap && urlAndMap.map;
634
645
  if (map && !(typeof map === "object" && "mappings" in map && map.mappings === "")) {
635
646
  var _sourceMap$map;
636
- // Load all sources stored inline with the source map into the file cache
637
- // to pretend like they are already loaded. They may not exist on disk.
638
- if (sourceMap = sourceMapCache[position.source] = {
647
+ sourceMap = sourceMapCache[position.source] = {
639
648
  url: urlAndMap.url,
640
649
  map: new TraceMap(map)
641
- }, (_sourceMap$map = sourceMap.map) === null || _sourceMap$map === void 0 ? void 0 : _sourceMap$map.sourcesContent) sourceMap.map.sources.forEach((source, i) => {
650
+ };
651
+ // Load all sources stored inline with the source map into the file cache
652
+ // to pretend like they are already loaded. They may not exist on disk.
653
+ if ((_sourceMap$map = sourceMap.map) === null || _sourceMap$map === void 0 ? void 0 : _sourceMap$map.sourcesContent) sourceMap.map.sources.forEach((source, i) => {
642
654
  var _sourceMap$map2;
643
655
  const contents = (_sourceMap$map2 = sourceMap.map) === null || _sourceMap$map2 === void 0 || (_sourceMap$map2 = _sourceMap$map2.sourcesContent) === null || _sourceMap$map2 === void 0 ? void 0 : _sourceMap$map2[i];
644
656
  if (contents && source && sourceMap.url) {
@@ -659,7 +671,10 @@ function mapSourcePosition(position) {
659
671
  // the stack trace to print the path and line for the compiled file. It is
660
672
  // better to give a precise location in the compiled file than a vague
661
673
  // location in the original file.
662
- if (originalPosition.source !== null) return originalPosition.source = supportRelativeURL(sourceMap.url, originalPosition.source), originalPosition;
674
+ if (originalPosition.source !== null) {
675
+ originalPosition.source = supportRelativeURL(sourceMap.url, originalPosition.source);
676
+ return originalPosition;
677
+ }
663
678
  }
664
679
  return position;
665
680
  }
@@ -677,8 +692,11 @@ function mapEvalOrigin(origin) {
677
692
  });
678
693
  return `eval at ${match[1]} (${position.source}:${position.line}:${position.column + 1})`;
679
694
  }
695
+ // Parse nested eval() calls using recursion
696
+ match = /^eval at ([^(]+) \((.+)\)$/.exec(origin);
697
+ if (match) return `eval at ${match[1]} (${mapEvalOrigin(match[2])})`;
680
698
  // Make sure we still return useful information if we didn't find anything
681
- return match = /^eval at ([^(]+) \((.+)\)$/.exec(origin), match ? `eval at ${match[1]} (${mapEvalOrigin(match[2])})` : origin;
699
+ return origin;
682
700
  }
683
701
  // This is copied almost verbatim from the V8 source code at
684
702
  // https://code.google.com/p/v8/source/browse/trunk/src/messages.js. The
@@ -687,10 +705,15 @@ function mapEvalOrigin(origin) {
687
705
  // did something to the prototype chain and broke the shim. The only fix I
688
706
  // could find was copy/paste.
689
707
  function CallSiteToString() {
690
- let fileName, fileLocation = "";
708
+ let fileName;
709
+ let fileLocation = "";
691
710
  if (this.isNative()) fileLocation = "native";
692
711
  else {
693
- if (fileName = this.getScriptNameOrSourceURL(), !fileName && this.isEval()) fileLocation = this.getEvalOrigin(), fileLocation += ", ";
712
+ fileName = this.getScriptNameOrSourceURL();
713
+ if (!fileName && this.isEval()) {
714
+ fileLocation = this.getEvalOrigin();
715
+ fileLocation += ", ";
716
+ }
694
717
  if (fileName) fileLocation += fileName;
695
718
  else
696
719
  // Source code does not originate from a file and is not native, but we
@@ -707,32 +730,38 @@ function CallSiteToString() {
707
730
  let line = "";
708
731
  const functionName = this.getFunctionName();
709
732
  let addSuffix = true;
710
- const isConstructor = this.isConstructor(), isMethodCall = !(this.isToplevel() || isConstructor);
711
- if (isMethodCall) {
733
+ const isConstructor = this.isConstructor();
734
+ if (!(this.isToplevel() || isConstructor)) {
712
735
  let typeName = this.getTypeName();
713
736
  // Fixes shim to be backward compatible with Node v0 to v4
714
737
  if (typeName === "[object Object]") typeName = "null";
715
738
  const methodName = this.getMethodName();
716
739
  if (functionName) {
717
740
  if (typeName && functionName.indexOf(typeName) !== 0) line += `${typeName}.`;
718
- if (line += functionName, methodName && functionName.indexOf(`.${methodName}`) !== functionName.length - methodName.length - 1) line += ` [as ${methodName}]`;
741
+ line += functionName;
742
+ if (methodName && functionName.indexOf(`.${methodName}`) !== functionName.length - methodName.length - 1) line += ` [as ${methodName}]`;
719
743
  } else line += `${typeName}.${methodName || "<anonymous>"}`;
720
744
  } else if (isConstructor) line += `new ${functionName || "<anonymous>"}`;
721
745
  else if (functionName) line += functionName;
722
- else line += fileLocation, addSuffix = false;
746
+ else {
747
+ line += fileLocation;
748
+ addSuffix = false;
749
+ }
723
750
  if (addSuffix) line += ` (${fileLocation})`;
724
751
  return line;
725
752
  }
726
753
  function cloneCallSite(frame) {
727
754
  const object = {};
728
- return Object.getOwnPropertyNames(Object.getPrototypeOf(frame)).forEach((name) => {
755
+ Object.getOwnPropertyNames(Object.getPrototypeOf(frame)).forEach((name) => {
729
756
  const key = name;
730
757
  // @ts-expect-error difficult to type
731
758
  object[key] = /^(?:is|get)/.test(name) ? function() {
732
759
  // eslint-disable-next-line no-useless-call
733
760
  return frame[key].call(frame);
734
761
  } : frame[key];
735
- }), object.toString = CallSiteToString, object;
762
+ });
763
+ object.toString = CallSiteToString;
764
+ return object;
736
765
  }
737
766
  function wrapCallSite(frame, state) {
738
767
  // provides interface backward compatibility
@@ -740,7 +769,10 @@ function wrapCallSite(frame, state) {
740
769
  nextPosition: null,
741
770
  curPosition: null
742
771
  };
743
- if (frame.isNative()) return state.curPosition = null, frame;
772
+ if (frame.isNative()) {
773
+ state.curPosition = null;
774
+ return frame;
775
+ }
744
776
  // Most call sites will return the source file from getFileName(), but code
745
777
  // passed to eval() ending in "//# sourceURL=..." will return the source file
746
778
  // from getScriptNameOrSourceURL() instead
@@ -748,12 +780,7 @@ function wrapCallSite(frame, state) {
748
780
  if (source) {
749
781
  const line = frame.getLineNumber();
750
782
  let column = frame.getColumnNumber() - 1;
751
- // Fix position in Node where some (internal) code is prepended.
752
- // See https://github.com/evanw/node-source-map-support/issues/36
753
- // Header removed in node at ^10.16 || >=11.11.0
754
- // v11 is not an LTS candidate, we can just test the one version with it.
755
- // Test node versions for: 10.16-19, 10.20+, 12-19, 20-99, 100+, or 11.11
756
- const noHeader = /^v(?:10\.1[6-9]|10\.[2-9]\d|10\.\d{3,}|1[2-9]\d*|[2-9]\d|\d{3,}|11\.11)/, headerLength = noHeader.test(globalProcessVersion()) ? 0 : 62;
783
+ const headerLength = /^v(?:10\.1[6-9]|10\.[2-9]\d|10\.\d{3,}|1[2-9]\d*|[2-9]\d|\d{3,}|11\.11)/.test(globalProcessVersion()) ? 0 : 62;
757
784
  if (line === 1 && column > headerLength && !frame.isEval()) column -= headerLength;
758
785
  const position = mapSourcePosition({
759
786
  name: null,
@@ -761,42 +788,63 @@ function wrapCallSite(frame, state) {
761
788
  line,
762
789
  column
763
790
  });
764
- state.curPosition = position, frame = cloneCallSite(frame);
791
+ state.curPosition = position;
792
+ frame = cloneCallSite(frame);
765
793
  const originalFunctionName = frame.getFunctionName;
766
- return frame.getFunctionName = function() {
767
- return state.nextPosition == null ? originalFunctionName() : state.nextPosition.name || originalFunctionName();
768
- }, frame.getFileName = function() {
794
+ frame.getFunctionName = function() {
795
+ if (state.nextPosition == null) return originalFunctionName();
796
+ return state.nextPosition.name || originalFunctionName();
797
+ };
798
+ frame.getFileName = function() {
769
799
  return position.source ?? null;
770
- }, frame.getLineNumber = function() {
800
+ };
801
+ frame.getLineNumber = function() {
771
802
  return position.line;
772
- }, frame.getColumnNumber = function() {
803
+ };
804
+ frame.getColumnNumber = function() {
773
805
  return position.column + 1;
774
- }, frame.getScriptNameOrSourceURL = function() {
806
+ };
807
+ frame.getScriptNameOrSourceURL = function() {
775
808
  return position.source;
776
- }, frame;
809
+ };
810
+ return frame;
777
811
  }
778
812
  // Code called using eval() needs special handling
779
813
  let origin = frame.isEval() && frame.getEvalOrigin();
814
+ if (origin) {
815
+ origin = mapEvalOrigin(origin);
816
+ frame = cloneCallSite(frame);
817
+ frame.getEvalOrigin = function() {
818
+ return origin || void 0;
819
+ };
820
+ return frame;
821
+ }
780
822
  // If we get here then we were unable to change the source position
781
- return origin ? (origin = mapEvalOrigin(origin), frame = cloneCallSite(frame), frame.getEvalOrigin = function() {
782
- return origin || void 0;
783
- }, frame) : frame;
823
+ return frame;
784
824
  }
785
825
  // This function is part of the V8 stack trace API, for more info see:
786
826
  // https://v8.dev/docs/stack-trace-api
787
827
  function prepareStackTrace(error, stack) {
788
- const name = error.name || "Error", message = error.message || "", errorString = `${name}: ${message}`, state = {
828
+ const errorString = `${error.name || "Error"}: ${error.message || ""}`;
829
+ const state = {
789
830
  nextPosition: null,
790
831
  curPosition: null
791
- }, processedStack = [];
792
- for (let i = stack.length - 1; i >= 0; i--) processedStack.push(`\n at ${wrapCallSite(stack[i], state)}`), state.nextPosition = state.curPosition;
793
- return state.curPosition = state.nextPosition = null, errorString + processedStack.reverse().join("");
832
+ };
833
+ const processedStack = [];
834
+ for (let i = stack.length - 1; i >= 0; i--) {
835
+ processedStack.push(`\n at ${wrapCallSite(stack[i], state)}`);
836
+ state.nextPosition = state.curPosition;
837
+ }
838
+ state.curPosition = state.nextPosition = null;
839
+ return errorString + processedStack.reverse().join("");
794
840
  }
795
- retrieveFileHandlers.slice(0); retrieveMapHandlers.slice(0);
841
+ retrieveFileHandlers.slice(0);
842
+ retrieveMapHandlers.slice(0);
796
843
  function install(options) {
844
+ options = options || {};
797
845
  // Allow sources to be found by methods other than reading the files
798
846
  // directly from disk.
799
- if (options = options || {}, options.retrieveFile) {
847
+ if (options.retrieveFile) {
800
848
  if (options.overrideRetrieveFile) retrieveFileHandlers.length = 0;
801
849
  retrieveFileHandlers.unshift(options.retrieveFile);
802
850
  }
@@ -807,12 +855,16 @@ function install(options) {
807
855
  retrieveMapHandlers.unshift(options.retrieveSourceMap);
808
856
  }
809
857
  // Install the error reformatter
810
- if (!errorFormatterInstalled) errorFormatterInstalled = true, Error.prepareStackTrace = prepareStackTrace;
858
+ if (!errorFormatterInstalled) {
859
+ errorFormatterInstalled = true;
860
+ Error.prepareStackTrace = prepareStackTrace;
861
+ }
811
862
  }
812
863
 
813
864
  let SOURCEMAPPING_URL = "sourceMa";
814
865
  SOURCEMAPPING_URL += "ppingURL";
815
- const VITE_NODE_SOURCEMAPPING_SOURCE = "//# sourceMappingSource=vite-node", VITE_NODE_SOURCEMAPPING_URL = `${SOURCEMAPPING_URL}=data:application/json;charset=utf-8`;
866
+ const VITE_NODE_SOURCEMAPPING_SOURCE = "//# sourceMappingSource=vite-node";
867
+ const VITE_NODE_SOURCEMAPPING_URL = `${SOURCEMAPPING_URL}=data:application/json;charset=utf-8`;
816
868
  function withInlineSourcemap(result, options) {
817
869
  const map = result.map;
818
870
  let code = result.code;
@@ -841,22 +893,26 @@ function withInlineSourcemap(result, options) {
841
893
  // so we don't need to add this mapping anymore.
842
894
  if (!options.noFirstLineMapping && map.mappings.startsWith(";")) map.mappings = `AAAA,CAAA${map.mappings}`;
843
895
  const sourceMap = Buffer.from(JSON.stringify(map), "utf-8").toString("base64");
844
- return result.code = `${code.trimEnd()}\n\n${VITE_NODE_SOURCEMAPPING_SOURCE}\n//# ${VITE_NODE_SOURCEMAPPING_URL};base64,${sourceMap}\n`, result;
896
+ result.code = `${code.trimEnd()}\n\n${VITE_NODE_SOURCEMAPPING_SOURCE}\n//# ${VITE_NODE_SOURCEMAPPING_URL};base64,${sourceMap}\n`;
897
+ return result;
845
898
  }
846
899
  function extractSourceMap(code) {
847
900
  const regexp = new RegExp(`//# ${VITE_NODE_SOURCEMAPPING_URL};base64,(.+)`, "gm");
848
901
  let lastMatch, match;
849
902
  // eslint-disable-next-line no-cond-assign
850
903
  while (match = regexp.exec(code)) lastMatch = match;
851
- return lastMatch ? JSON.parse(Buffer.from(lastMatch[1], "base64").toString("utf-8")) : null;
904
+ // pick only the last source map keeping user strings that look like maps
905
+ if (lastMatch) return JSON.parse(Buffer.from(lastMatch[1], "base64").toString("utf-8"));
906
+ return null;
852
907
  }
853
908
  function installSourcemapsSupport(options) {
854
909
  install({ retrieveSourceMap(source) {
855
910
  const map = options.getSourceMap(source);
856
- return map ? {
911
+ if (map) return {
857
912
  url: source,
858
913
  map
859
- } : null;
914
+ };
915
+ return null;
860
916
  } });
861
917
  }
862
918