MCPcopy
hub / github.com/webpack/webpack / extractSourceAndMap

Function extractSourceAndMap

lib/SourceMapDevToolPlugin.js:123–158  ·  lib/SourceMapDevToolPlugin.js::extractSourceAndMap
(file, asset, options, registry)

Source from the content-addressed store, hash-verified

121 * @returns {{ source: string, sourceMap: RawSourceMap, mapSource: Source } | undefined} extracted pair or `undefined` when no map is recoverable
122 */
123const extractSourceAndMap = (file, asset, options, registry) => {
124 /** @type {string | Buffer} */
125 let source;
126 /** @type {null | RawSourceMap} */
127 let sourceMap;
128 if (asset.sourceAndMap) {
129 const sourceAndMap = asset.sourceAndMap(options);
130 source = sourceAndMap.source;
131 sourceMap = sourceAndMap.map;
132 } else {
133 source = asset.source();
134 sourceMap = asset.map(options);
135 }
136 class="cm">// Bail before touching the registry if we can't return a usable string
137 class="cm">// source — pinning a non-string-producing asset would only waste the slot.
138 if (typeof source !== class="st">"string") return;
139 if (sourceMap) {
140 class="cm">// The current asset still owns the original map — pin a reference so
141 class="cm">// that a later plugin instance (which will see a rewrapped asset
142 class="cm">// without a map) can recover it on demand.
143 if (!registry.has(file)) registry.set(file, asset);
144 return { source, sourceMap, mapSource: asset };
145 }
146 class="cm">// The current asset (typically a `RawSource` left by an earlier
147 class="cm">// SourceMapDevToolPlugin instance) has no internal map. Re-extract
148 class="cm">// the map from the original Source we pinned earlier. We keep using
149 class="cm">// `source` from the current asset so that any prior wrappers (e.g.
150 class="cm">// appended sourceMappingURL comments) are preserved.
151 const original = registry.get(file);
152 if (!original) return;
153 sourceMap = original.sourceAndMap
154 ? original.sourceAndMap(options).map
155 : original.map(options);
156 if (!sourceMap) return;
157 return { source, sourceMap, mapSource: original };
158};
159
160/**
161 * Creating {@link SourceMapTask} for given file

Callers 1

getTaskForFileFunction · 0.85

Calls 4

sourceMethod · 0.80
hasMethod · 0.45
setMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected