MCPcopy
hub / github.com/vitejs/vite / combineSourcemaps

Function combineSourcemaps

packages/vite/src/node/utils.ts:857–911  ·  view source on GitHub ↗
(
  filename: string,
  sourcemapList: Array<DecodedSourceMap | RawSourceMap>,
)

Source from the content-addressed store, hash-verified

855 * Note that the length of sourcemapList must be 2.
856 */
857export function combineSourcemaps(
858 filename: string,
859 sourcemapList: Array<DecodedSourceMap | RawSourceMap>,
860): RawSourceMap {
861 if (
862 sourcemapList.length === 0 ||
863 sourcemapList.every((m) => m.sources.length === 0)
864 ) {
865 return { ...nullSourceMap }
866 }
867
868 // hack for parse broken with normalized absolute paths on windows (C:/path/to/something).
869 // escape them to linux like paths
870 // also avoid mutation here to prevent breaking plugin's using cache to generate sourcemaps like vue (see #7442)
871 sourcemapList = sourcemapList.map((sourcemap) => {
872 const newSourcemaps = { ...sourcemap }
873 newSourcemaps.sources = sourcemap.sources.map((source) =>
874 source ? escapeToLinuxLikePath(source) : null,
875 )
876 if (sourcemap.sourceRoot) {
877 newSourcemaps.sourceRoot = escapeToLinuxLikePath(sourcemap.sourceRoot)
878 }
879 return newSourcemaps
880 })
881 const escapedFilename = escapeToLinuxLikePath(filename)
882
883 // We don't declare type here so we can convert/fake/map as RawSourceMap
884 let map //: SourceMap
885 let mapIndex = 1
886 const useArrayInterface =
887 sourcemapList.slice(0, -1).find((m) => m.sources.length !== 1) === undefined
888 if (useArrayInterface) {
889 map = remapping(sourcemapList, () => null)
890 } else {
891 map = remapping(sourcemapList[0], function loader(sourcefile) {
892 // this line assumes that the length of the sourcemapList is 2
893 if (sourcefile === escapedFilename && sourcemapList[mapIndex]) {
894 return sourcemapList[mapIndex++]
895 } else {
896 return null
897 }
898 })
899 }
900 if (!map.file) {
901 delete map.file
902 }
903
904 // unescape the previous hack
905 map.sources = map.sources.map((source) =>
906 source ? unescapeToLinuxLikePath(source) : source,
907 )
908 map.file = filename
909
910 return map as RawSourceMap
911}
912
913export function unique<T>(arr: T[]): T[] {
914 return Array.from(new Set(arr))

Callers 6

ssrTransformScriptFunction · 0.90
_getCombinedSourcemapMethod · 0.90
generateBundleFunction · 0.90
transformWithEsbuildFunction · 0.90
utils.spec.tsFile · 0.90

Calls 2

escapeToLinuxLikePathFunction · 0.85
unescapeToLinuxLikePathFunction · 0.85

Tested by

no test coverage detected