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

Function mapSourcePosition

packages/vite/src/module-runner/sourcemap/interceptor.ts:173–235  ·  view source on GitHub ↗
(position: OriginalMapping)

Source from the content-addressed store, hash-verified

171}
172
173function mapSourcePosition(position: OriginalMapping) {
174 if (!position.source) return position
175 let sourceMap = getRunnerSourceMap(position)
176 if (!sourceMap) sourceMap = sourceMapCache[position.source]
177 if (!sourceMap) {
178 // Call the (overridable) retrieveSourceMap function to get the source map.
179 const urlAndMap = retrieveSourceMap(position.source)
180 if (urlAndMap && urlAndMap.map) {
181 const url = urlAndMap.url
182 sourceMap = sourceMapCache[position.source] = {
183 url,
184 map: new DecodedMap(
185 typeof urlAndMap.map === 'string'
186 ? JSON.parse(urlAndMap.map)
187 : urlAndMap.map,
188 url,
189 ),
190 }
191
192 const contents = sourceMap.map?.map.sourcesContent
193 // Load all sources stored inline with the source map into the file cache
194 // to pretend like they are already loaded. They may not exist on disk.
195 if (sourceMap.map && contents) {
196 sourceMap.map.resolvedSources.forEach((source, i) => {
197 const content = contents[i]
198 if (content && source && url) {
199 const contentUrl = supportRelativeURL(url, source)
200 fileContentsCache[contentUrl] = content
201 }
202 })
203 }
204 } else {
205 sourceMap = sourceMapCache[position.source] = {
206 url: null,
207 map: null,
208 }
209 }
210 }
211
212 // Resolve the source URL relative to the URL of the source map
213 if (sourceMap.map && sourceMap.url) {
214 const originalPosition = getOriginalPosition(sourceMap.map, position)
215
216 // Only return the original position if a matching line was found. If no
217 // matching line is found then we return position instead, which will cause
218 // the stack trace to print the path and line for the compiled file. It is
219 // better to give a precise location in the compiled file than a vague
220 // location in the original file.
221 if (originalPosition && originalPosition.source != null) {
222 originalPosition.source = supportRelativeURL(
223 sourceMap.url,
224 originalPosition.source,
225 )
226 if (sourceMap.vite) {
227 // @ts-expect-error vite is not defined
228 originalPosition._vite = true
229 }
230 return originalPosition

Callers 2

mapEvalOriginFunction · 0.85
wrapCallSiteFunction · 0.85

Calls 5

getOriginalPositionFunction · 0.90
getRunnerSourceMapFunction · 0.85
retrieveSourceMapFunction · 0.85
supportRelativeURLFunction · 0.85
parseMethod · 0.80

Tested by

no test coverage detected