vite-node 4.0.0-beta.4 → 4.0.0-beta.6

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.
@@ -552,17 +552,11 @@ function traceSegmentInternal(segments, memo, line, column, bias) {
552
552
  // Only install once if called multiple times
553
553
  let errorFormatterInstalled = false;
554
554
  // Maps a file path to a string containing the file contents
555
- const fileContentsCache = {};
556
- // Maps a file path to a source map for that file
557
- const sourceMapCache = {};
558
- // Regex for detecting source maps
559
- const reSourceMap = /^data:application\/json[^,]+base64,/;
555
+ const fileContentsCache = {}, sourceMapCache = {}, reSourceMap = /^data:application\/json[^,]+base64,/;
560
556
  // Priority list of retrieve handlers
561
- let retrieveFileHandlers = [];
562
- let retrieveMapHandlers = [];
557
+ let retrieveFileHandlers = [], retrieveMapHandlers = [];
563
558
  function globalProcessVersion() {
564
- if (typeof process === "object" && process !== null) return process.version;
565
- else return "";
559
+ return typeof process === "object" && process !== null ? process.version : "";
566
560
  }
567
561
  function handlerExec(list) {
568
562
  return function(arg) {
@@ -575,9 +569,7 @@ function handlerExec(list) {
575
569
  }
576
570
  let retrieveFile = handlerExec(retrieveFileHandlers);
577
571
  retrieveFileHandlers.push((path) => {
578
- // Trim the path to make sure there is no extra whitespace.
579
- path = path.trim();
580
- if (path.startsWith("file:"))
572
+ if (path = path.trim(), path.startsWith("file:"))
581
573
  // existsSync/readFileSync can't handle file protocol, but once stripped, it works
582
574
  path = path.replace(/file:\/\/\/(\w:)?/, (protocol, drive) => {
583
575
  return drive ? "" : "/";
@@ -592,16 +584,10 @@ retrieveFileHandlers.push((path) => {
592
584
  // Support URLs relative to a directory, but be careful about a protocol prefix
593
585
  function supportRelativeURL(file, url) {
594
586
  if (!file) return url;
595
- const dir = path.dirname(file);
596
- const match = /^\w+:\/\/[^/]*/.exec(dir);
587
+ const dir = path.dirname(file), match = /^\w+:\/\/[^/]*/.exec(dir);
597
588
  let protocol = match ? match[0] : "";
598
589
  const startPath = dir.slice(protocol.length);
599
- if (protocol && /^\/\w:/.test(startPath)) {
600
- // handle file:///C:/ paths
601
- protocol += "/";
602
- return protocol + path.resolve(dir.slice(protocol.length), url).replace(/\\/g, "/");
603
- }
604
- return protocol + path.resolve(dir.slice(protocol.length), url);
590
+ return protocol && /^\/\w:/.test(startPath) ? (protocol += "/", protocol + path.resolve(dir.slice(protocol.length), url).replace(/\\/g, "/")) : protocol + path.resolve(dir.slice(protocol.length), url);
605
591
  }
606
592
  function retrieveSourceMapURL(source) {
607
593
  // Get the URL of the source map
@@ -613,8 +599,7 @@ function retrieveSourceMapURL(source) {
613
599
  let lastMatch, match;
614
600
  // eslint-disable-next-line no-cond-assign
615
601
  while (match = re.exec(fileData)) lastMatch = match;
616
- if (!lastMatch) return null;
617
- return lastMatch[1];
602
+ return lastMatch ? lastMatch[1] : null;
618
603
  }
619
604
  // Can be overridden by the retrieveSourceMap option to install. Takes a
620
605
  // generated source filename; returns a {map, optional url} object, or null if
@@ -630,18 +615,12 @@ retrieveMapHandlers.push((source) => {
630
615
  if (reSourceMap.test(sourceMappingURL)) {
631
616
  // Support source map URL as a data url
632
617
  const rawData = sourceMappingURL.slice(sourceMappingURL.indexOf(",") + 1);
633
- sourceMapData = Buffer.from(rawData, "base64").toString();
634
- sourceMappingURL = source;
635
- } else {
636
- // Support source map URLs relative to the source URL
637
- sourceMappingURL = supportRelativeURL(source, sourceMappingURL);
638
- sourceMapData = retrieveFile(sourceMappingURL);
639
- }
640
- if (!sourceMapData) return null;
641
- return {
618
+ sourceMapData = Buffer.from(rawData, "base64").toString(), sourceMappingURL = source;
619
+ } else sourceMappingURL = supportRelativeURL(source, sourceMappingURL), sourceMapData = retrieveFile(sourceMappingURL);
620
+ return sourceMapData ? {
642
621
  url: sourceMappingURL,
643
622
  map: sourceMapData
644
- };
623
+ } : null;
645
624
  });
646
625
  // interface Position {
647
626
  // source: string
@@ -653,17 +632,15 @@ function mapSourcePosition(position) {
653
632
  let sourceMap = sourceMapCache[position.source];
654
633
  if (!sourceMap) {
655
634
  // Call the (overridable) retrieveSourceMap function to get the source map.
656
- const urlAndMap = retrieveSourceMap(position.source);
657
- const map = urlAndMap && urlAndMap.map;
635
+ const urlAndMap = retrieveSourceMap(position.source), map = urlAndMap && urlAndMap.map;
658
636
  if (map && !(typeof map === "object" && "mappings" in map && map.mappings === "")) {
659
637
  var _sourceMap$map;
660
- sourceMap = sourceMapCache[position.source] = {
661
- url: urlAndMap.url,
662
- map: new TraceMap(map)
663
- };
664
638
  // Load all sources stored inline with the source map into the file cache
665
639
  // to pretend like they are already loaded. They may not exist on disk.
666
- if ((_sourceMap$map = sourceMap.map) === null || _sourceMap$map === void 0 ? void 0 : _sourceMap$map.sourcesContent) sourceMap.map.sources.forEach((source, i) => {
640
+ if (sourceMap = sourceMapCache[position.source] = {
641
+ url: urlAndMap.url,
642
+ map: new TraceMap(map)
643
+ }, (_sourceMap$map = sourceMap.map) === null || _sourceMap$map === void 0 ? void 0 : _sourceMap$map.sourcesContent) sourceMap.map.sources.forEach((source, i) => {
667
644
  var _sourceMap$map2;
668
645
  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];
669
646
  if (contents && source && sourceMap.url) {
@@ -684,10 +661,7 @@ function mapSourcePosition(position) {
684
661
  // the stack trace to print the path and line for the compiled file. It is
685
662
  // better to give a precise location in the compiled file than a vague
686
663
  // location in the original file.
687
- if (originalPosition.source !== null) {
688
- originalPosition.source = supportRelativeURL(sourceMap.url, originalPosition.source);
689
- return originalPosition;
690
- }
664
+ if (originalPosition.source !== null) return originalPosition.source = supportRelativeURL(sourceMap.url, originalPosition.source), originalPosition;
691
665
  }
692
666
  return position;
693
667
  }
@@ -705,11 +679,8 @@ function mapEvalOrigin(origin) {
705
679
  });
706
680
  return `eval at ${match[1]} (${position.source}:${position.line}:${position.column + 1})`;
707
681
  }
708
- // Parse nested eval() calls using recursion
709
- match = /^eval at ([^(]+) \((.+)\)$/.exec(origin);
710
- if (match) return `eval at ${match[1]} (${mapEvalOrigin(match[2])})`;
711
682
  // Make sure we still return useful information if we didn't find anything
712
- return origin;
683
+ return match = /^eval at ([^(]+) \((.+)\)$/.exec(origin), match ? `eval at ${match[1]} (${mapEvalOrigin(match[2])})` : origin;
713
684
  }
714
685
  // This is copied almost verbatim from the V8 source code at
715
686
  // https://code.google.com/p/v8/source/browse/trunk/src/messages.js. The
@@ -718,15 +689,10 @@ function mapEvalOrigin(origin) {
718
689
  // did something to the prototype chain and broke the shim. The only fix I
719
690
  // could find was copy/paste.
720
691
  function CallSiteToString() {
721
- let fileName;
722
- let fileLocation = "";
692
+ let fileName, fileLocation = "";
723
693
  if (this.isNative()) fileLocation = "native";
724
694
  else {
725
- fileName = this.getScriptNameOrSourceURL();
726
- if (!fileName && this.isEval()) {
727
- fileLocation = this.getEvalOrigin();
728
- fileLocation += ", ";
729
- }
695
+ if (fileName = this.getScriptNameOrSourceURL(), !fileName && this.isEval()) fileLocation = this.getEvalOrigin(), fileLocation += ", ";
730
696
  if (fileName) fileLocation += fileName;
731
697
  else
732
698
  // Source code does not originate from a file and is not native, but we
@@ -743,8 +709,7 @@ function CallSiteToString() {
743
709
  let line = "";
744
710
  const functionName = this.getFunctionName();
745
711
  let addSuffix = true;
746
- const isConstructor = this.isConstructor();
747
- const isMethodCall = !(this.isToplevel() || isConstructor);
712
+ const isConstructor = this.isConstructor(), isMethodCall = !(this.isToplevel() || isConstructor);
748
713
  if (isMethodCall) {
749
714
  let typeName = this.getTypeName();
750
715
  // Fixes shim to be backward compatible with Node v0 to v4
@@ -752,30 +717,24 @@ function CallSiteToString() {
752
717
  const methodName = this.getMethodName();
753
718
  if (functionName) {
754
719
  if (typeName && functionName.indexOf(typeName) !== 0) line += `${typeName}.`;
755
- line += functionName;
756
- if (methodName && functionName.indexOf(`.${methodName}`) !== functionName.length - methodName.length - 1) line += ` [as ${methodName}]`;
720
+ if (line += functionName, methodName && functionName.indexOf(`.${methodName}`) !== functionName.length - methodName.length - 1) line += ` [as ${methodName}]`;
757
721
  } else line += `${typeName}.${methodName || "<anonymous>"}`;
758
722
  } else if (isConstructor) line += `new ${functionName || "<anonymous>"}`;
759
723
  else if (functionName) line += functionName;
760
- else {
761
- line += fileLocation;
762
- addSuffix = false;
763
- }
724
+ else line += fileLocation, addSuffix = false;
764
725
  if (addSuffix) line += ` (${fileLocation})`;
765
726
  return line;
766
727
  }
767
728
  function cloneCallSite(frame) {
768
729
  const object = {};
769
- Object.getOwnPropertyNames(Object.getPrototypeOf(frame)).forEach((name) => {
730
+ return Object.getOwnPropertyNames(Object.getPrototypeOf(frame)).forEach((name) => {
770
731
  const key = name;
771
732
  // @ts-expect-error difficult to type
772
733
  object[key] = /^(?:is|get)/.test(name) ? function() {
773
734
  // eslint-disable-next-line no-useless-call
774
735
  return frame[key].call(frame);
775
736
  } : frame[key];
776
- });
777
- object.toString = CallSiteToString;
778
- return object;
737
+ }), object.toString = CallSiteToString, object;
779
738
  }
780
739
  function wrapCallSite(frame, state) {
781
740
  // provides interface backward compatibility
@@ -783,10 +742,7 @@ function wrapCallSite(frame, state) {
783
742
  nextPosition: null,
784
743
  curPosition: null
785
744
  };
786
- if (frame.isNative()) {
787
- state.curPosition = null;
788
- return frame;
789
- }
745
+ if (frame.isNative()) return state.curPosition = null, frame;
790
746
  // Most call sites will return the source file from getFileName(), but code
791
747
  // passed to eval() ending in "//# sourceURL=..." will return the source file
792
748
  // from getScriptNameOrSourceURL() instead
@@ -799,8 +755,7 @@ function wrapCallSite(frame, state) {
799
755
  // Header removed in node at ^10.16 || >=11.11.0
800
756
  // v11 is not an LTS candidate, we can just test the one version with it.
801
757
  // Test node versions for: 10.16-19, 10.20+, 12-19, 20-99, 100+, or 11.11
802
- 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)/;
803
- const headerLength = noHeader.test(globalProcessVersion()) ? 0 : 62;
758
+ 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;
804
759
  if (line === 1 && column > headerLength && !frame.isEval()) column -= headerLength;
805
760
  const position = mapSourcePosition({
806
761
  name: null,
@@ -808,65 +763,42 @@ function wrapCallSite(frame, state) {
808
763
  line,
809
764
  column
810
765
  });
811
- state.curPosition = position;
812
- frame = cloneCallSite(frame);
766
+ state.curPosition = position, frame = cloneCallSite(frame);
813
767
  const originalFunctionName = frame.getFunctionName;
814
- frame.getFunctionName = function() {
815
- if (state.nextPosition == null) return originalFunctionName();
816
- return state.nextPosition.name || originalFunctionName();
817
- };
818
- frame.getFileName = function() {
768
+ return frame.getFunctionName = function() {
769
+ return state.nextPosition == null ? originalFunctionName() : state.nextPosition.name || originalFunctionName();
770
+ }, frame.getFileName = function() {
819
771
  return position.source ?? null;
820
- };
821
- frame.getLineNumber = function() {
772
+ }, frame.getLineNumber = function() {
822
773
  return position.line;
823
- };
824
- frame.getColumnNumber = function() {
774
+ }, frame.getColumnNumber = function() {
825
775
  return position.column + 1;
826
- };
827
- frame.getScriptNameOrSourceURL = function() {
776
+ }, frame.getScriptNameOrSourceURL = function() {
828
777
  return position.source;
829
- };
830
- return frame;
778
+ }, frame;
831
779
  }
832
780
  // Code called using eval() needs special handling
833
781
  let origin = frame.isEval() && frame.getEvalOrigin();
834
- if (origin) {
835
- origin = mapEvalOrigin(origin);
836
- frame = cloneCallSite(frame);
837
- frame.getEvalOrigin = function() {
838
- return origin || void 0;
839
- };
840
- return frame;
841
- }
842
782
  // If we get here then we were unable to change the source position
843
- return frame;
783
+ return origin ? (origin = mapEvalOrigin(origin), frame = cloneCallSite(frame), frame.getEvalOrigin = function() {
784
+ return origin || void 0;
785
+ }, frame) : frame;
844
786
  }
845
787
  // This function is part of the V8 stack trace API, for more info see:
846
788
  // https://v8.dev/docs/stack-trace-api
847
789
  function prepareStackTrace(error, stack) {
848
- const name = error.name || "Error";
849
- const message = error.message || "";
850
- const errorString = `${name}: ${message}`;
851
- const state = {
790
+ const name = error.name || "Error", message = error.message || "", errorString = `${name}: ${message}`, state = {
852
791
  nextPosition: null,
853
792
  curPosition: null
854
- };
855
- const processedStack = [];
856
- for (let i = stack.length - 1; i >= 0; i--) {
857
- processedStack.push(`\n at ${wrapCallSite(stack[i], state)}`);
858
- state.nextPosition = state.curPosition;
859
- }
860
- state.curPosition = state.nextPosition = null;
861
- return errorString + processedStack.reverse().join("");
793
+ }, processedStack = [];
794
+ for (let i = stack.length - 1; i >= 0; i--) processedStack.push(`\n at ${wrapCallSite(stack[i], state)}`), state.nextPosition = state.curPosition;
795
+ return state.curPosition = state.nextPosition = null, errorString + processedStack.reverse().join("");
862
796
  }
863
- retrieveFileHandlers.slice(0);
864
- retrieveMapHandlers.slice(0);
797
+ retrieveFileHandlers.slice(0); retrieveMapHandlers.slice(0);
865
798
  function install(options) {
866
- options = options || {};
867
799
  // Allow sources to be found by methods other than reading the files
868
800
  // directly from disk.
869
- if (options.retrieveFile) {
801
+ if (options = options || {}, options.retrieveFile) {
870
802
  if (options.overrideRetrieveFile) retrieveFileHandlers.length = 0;
871
803
  retrieveFileHandlers.unshift(options.retrieveFile);
872
804
  }
@@ -877,16 +809,12 @@ function install(options) {
877
809
  retrieveMapHandlers.unshift(options.retrieveSourceMap);
878
810
  }
879
811
  // Install the error reformatter
880
- if (!errorFormatterInstalled) {
881
- errorFormatterInstalled = true;
882
- Error.prepareStackTrace = prepareStackTrace;
883
- }
812
+ if (!errorFormatterInstalled) errorFormatterInstalled = true, Error.prepareStackTrace = prepareStackTrace;
884
813
  }
885
814
 
886
815
  let SOURCEMAPPING_URL = "sourceMa";
887
816
  SOURCEMAPPING_URL += "ppingURL";
888
- const VITE_NODE_SOURCEMAPPING_SOURCE = "//# sourceMappingSource=vite-node";
889
- const VITE_NODE_SOURCEMAPPING_URL = `${SOURCEMAPPING_URL}=data:application/json;charset=utf-8`;
817
+ const VITE_NODE_SOURCEMAPPING_SOURCE = "//# sourceMappingSource=vite-node", VITE_NODE_SOURCEMAPPING_URL = `${SOURCEMAPPING_URL}=data:application/json;charset=utf-8`;
890
818
  function withInlineSourcemap(result, options) {
891
819
  const map = result.map;
892
820
  let code = result.code;
@@ -915,26 +843,22 @@ function withInlineSourcemap(result, options) {
915
843
  // so we don't need to add this mapping anymore.
916
844
  if (!options.noFirstLineMapping && map.mappings.startsWith(";")) map.mappings = `AAAA,CAAA${map.mappings}`;
917
845
  const sourceMap = Buffer.from(JSON.stringify(map), "utf-8").toString("base64");
918
- result.code = `${code.trimEnd()}\n\n${VITE_NODE_SOURCEMAPPING_SOURCE}\n//# ${VITE_NODE_SOURCEMAPPING_URL};base64,${sourceMap}\n`;
919
- return result;
846
+ return result.code = `${code.trimEnd()}\n\n${VITE_NODE_SOURCEMAPPING_SOURCE}\n//# ${VITE_NODE_SOURCEMAPPING_URL};base64,${sourceMap}\n`, result;
920
847
  }
921
848
  function extractSourceMap(code) {
922
849
  const regexp = new RegExp(`//# ${VITE_NODE_SOURCEMAPPING_URL};base64,(.+)`, "gm");
923
850
  let lastMatch, match;
924
851
  // eslint-disable-next-line no-cond-assign
925
852
  while (match = regexp.exec(code)) lastMatch = match;
926
- // pick only the last source map keeping user strings that look like maps
927
- if (lastMatch) return JSON.parse(Buffer.from(lastMatch[1], "base64").toString("utf-8"));
928
- return null;
853
+ return lastMatch ? JSON.parse(Buffer.from(lastMatch[1], "base64").toString("utf-8")) : null;
929
854
  }
930
855
  function installSourcemapsSupport(options) {
931
856
  install({ retrieveSourceMap(source) {
932
857
  const map = options.getSourceMap(source);
933
- if (map) return {
858
+ return map ? {
934
859
  url: source,
935
860
  map
936
- };
937
- return null;
861
+ } : null;
938
862
  } });
939
863
  }
940
864