MCPcopy
hub / github.com/webpack/webpack / extractSourceMap

Function extractSourceMap

lib/util/extractSourceMap.js:222–316  ·  view source on GitHub ↗

* Extract source map from code content * @param {StringOrBuffer} stringOrBuffer The input code content as string or buffer * @param {string} resourcePath The path to the resource file * @param {ReadResource} readResource The read resource function * @returns {Promise<{ source: StringOrBuffer, so

(stringOrBuffer, resourcePath, readResource)

Source from the content-addressed store, hash-verified

220 * @returns {Promise<{ source: StringOrBuffer, sourceMap: string | RawSourceMap | undefined }>} Promise resolving to extracted source map information
221 */
222async function extractSourceMap(stringOrBuffer, resourcePath, readResource) {
223 const input =
224 typeof stringOrBuffer === "string"
225 ? stringOrBuffer
226 : stringOrBuffer.toString("utf8");
227 const inputSourceMap = undefined;
228 const output = {
229 source: stringOrBuffer,
230 sourceMap: inputSourceMap
231 };
232 const { sourceMappingURL, replacementString } = getSourceMappingURL(input);
233
234 if (!sourceMappingURL) {
235 return output;
236 }
237
238 const baseContext = path.dirname(resourcePath);
239
240 const { sourceURL, sourceContent } = await fetchFromURL(
241 readResource,
242 baseContext,
243 sourceMappingURL
244 );
245
246 if (!sourceContent) {
247 return output;
248 }
249
250 /** @type {RawSourceMap} */
251 const map = JSON.parse(
252 sourceContent.toString("utf8").replace(/^\)\]\}'/, "")
253 );
254
255 const context = sourceURL ? path.dirname(sourceURL) : baseContext;
256
257 const resolvedSources = await Promise.all(
258 map.sources.map(
259 async (/** @type {string} */ source, /** @type {number} */ i) => {
260 const originalSourceContent =
261 map.sourcesContent &&
262 typeof map.sourcesContent[i] !== "undefined" &&
263 map.sourcesContent[i] !== null
264 ? map.sourcesContent[i]
265 : undefined;
266 const skipReading = typeof originalSourceContent !== "undefined";
267 // We do not skipReading here, because we need absolute paths in sources.
268 // This is necessary so that for sourceMaps with the same file structure in sources, name collisions do not occur.
269 // https://github.com/webpack-contrib/source-map-loader/issues/51
270 let { sourceURL, sourceContent } = await fetchFromURL(
271 readResource,
272 context,
273 source,
274 map.sourceRoot,
275 skipReading
276 );
277
278 if (skipReading) {
279 sourceContent = originalSourceContent;

Callers

nothing calls this directly

Calls 6

getSourceMappingURLFunction · 0.85
fetchFromURLFunction · 0.85
toStringMethod · 0.45
parseMethod · 0.45
replaceMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected