1 |
|
2 | import {createRequire as __cjsCompatRequire} from 'module';
|
3 | const require = __cjsCompatRequire(import.meta.url);
|
4 | const __ESM_IMPORT_META_URL__ = import.meta.url;
|
5 |
|
6 | import {
|
7 | __spreadValues
|
8 | } from "./chunk-GMSUYBZP.js";
|
9 |
|
10 |
|
11 | import mapHelpers from "convert-source-map";
|
12 | import { decode, encode } from "sourcemap-codec";
|
13 |
|
14 |
|
15 | function compareSegments(a, b) {
|
16 | return a.position - b.position;
|
17 | }
|
18 | function offsetSegment(startOfLinePositions, marker, offset) {
|
19 | if (offset === 0) {
|
20 | return marker;
|
21 | }
|
22 | let line = marker.line;
|
23 | const position = marker.position + offset;
|
24 | while (line < startOfLinePositions.length - 1 && startOfLinePositions[line + 1] <= position) {
|
25 | line++;
|
26 | }
|
27 | while (line > 0 && startOfLinePositions[line] > position) {
|
28 | line--;
|
29 | }
|
30 | const column = position - startOfLinePositions[line];
|
31 | return { line, column, position, next: void 0 };
|
32 | }
|
33 |
|
34 |
|
35 | function removeSourceMapComments(contents) {
|
36 | return mapHelpers.removeMapFileComments(mapHelpers.removeComments(contents)).replace(/\n\n$/, "\n");
|
37 | }
|
38 | var SourceFile = class {
|
39 | constructor(sourcePath, contents, rawMap, sources, fs) {
|
40 | this.sourcePath = sourcePath;
|
41 | this.contents = contents;
|
42 | this.rawMap = rawMap;
|
43 | this.sources = sources;
|
44 | this.fs = fs;
|
45 | this.contents = removeSourceMapComments(contents);
|
46 | this.startOfLinePositions = computeStartOfLinePositions(this.contents);
|
47 | this.flattenedMappings = this.flattenMappings();
|
48 | }
|
49 | renderFlattenedSourceMap() {
|
50 | const sources = new IndexedMap();
|
51 | const names = new IndexedSet();
|
52 | const mappings = [];
|
53 | const sourcePathDir = this.fs.dirname(this.sourcePath);
|
54 | const relativeSourcePathCache = new Cache((input) => this.fs.relative(sourcePathDir, input));
|
55 | for (const mapping of this.flattenedMappings) {
|
56 | const sourceIndex = sources.set(relativeSourcePathCache.get(mapping.originalSource.sourcePath), mapping.originalSource.contents);
|
57 | const mappingArray = [
|
58 | mapping.generatedSegment.column,
|
59 | sourceIndex,
|
60 | mapping.originalSegment.line,
|
61 | mapping.originalSegment.column
|
62 | ];
|
63 | if (mapping.name !== void 0) {
|
64 | const nameIndex = names.add(mapping.name);
|
65 | mappingArray.push(nameIndex);
|
66 | }
|
67 | const line = mapping.generatedSegment.line;
|
68 | while (line >= mappings.length) {
|
69 | mappings.push([]);
|
70 | }
|
71 | mappings[line].push(mappingArray);
|
72 | }
|
73 | const sourceMap = {
|
74 | version: 3,
|
75 | file: this.fs.relative(sourcePathDir, this.sourcePath),
|
76 | sources: sources.keys,
|
77 | names: names.values,
|
78 | mappings: encode(mappings),
|
79 | sourcesContent: sources.values
|
80 | };
|
81 | return sourceMap;
|
82 | }
|
83 | getOriginalLocation(line, column) {
|
84 | if (this.flattenedMappings.length === 0) {
|
85 | return null;
|
86 | }
|
87 | let position;
|
88 | if (line < this.startOfLinePositions.length) {
|
89 | position = this.startOfLinePositions[line] + column;
|
90 | } else {
|
91 | position = this.contents.length;
|
92 | }
|
93 | const locationSegment = { line, column, position, next: void 0 };
|
94 | let mappingIndex = findLastMappingIndexBefore(this.flattenedMappings, locationSegment, false, 0);
|
95 | if (mappingIndex < 0) {
|
96 | mappingIndex = 0;
|
97 | }
|
98 | const { originalSegment, originalSource, generatedSegment } = this.flattenedMappings[mappingIndex];
|
99 | const offset = locationSegment.position - generatedSegment.position;
|
100 | const offsetOriginalSegment = offsetSegment(originalSource.startOfLinePositions, originalSegment, offset);
|
101 | return {
|
102 | file: originalSource.sourcePath,
|
103 | line: offsetOriginalSegment.line,
|
104 | column: offsetOriginalSegment.column
|
105 | };
|
106 | }
|
107 | flattenMappings() {
|
108 | const mappings = parseMappings(this.rawMap && this.rawMap.map, this.sources, this.startOfLinePositions);
|
109 | ensureOriginalSegmentLinks(mappings);
|
110 | const flattenedMappings = [];
|
111 | for (let mappingIndex = 0; mappingIndex < mappings.length; mappingIndex++) {
|
112 | const aToBmapping = mappings[mappingIndex];
|
113 | const bSource = aToBmapping.originalSource;
|
114 | if (bSource.flattenedMappings.length === 0) {
|
115 | flattenedMappings.push(aToBmapping);
|
116 | continue;
|
117 | }
|
118 | const incomingStart = aToBmapping.originalSegment;
|
119 | const incomingEnd = incomingStart.next;
|
120 | let outgoingStartIndex = findLastMappingIndexBefore(bSource.flattenedMappings, incomingStart, false, 0);
|
121 | if (outgoingStartIndex < 0) {
|
122 | outgoingStartIndex = 0;
|
123 | }
|
124 | const outgoingEndIndex = incomingEnd !== void 0 ? findLastMappingIndexBefore(bSource.flattenedMappings, incomingEnd, true, outgoingStartIndex) : bSource.flattenedMappings.length - 1;
|
125 | for (let bToCmappingIndex = outgoingStartIndex; bToCmappingIndex <= outgoingEndIndex; bToCmappingIndex++) {
|
126 | const bToCmapping = bSource.flattenedMappings[bToCmappingIndex];
|
127 | flattenedMappings.push(mergeMappings(this, aToBmapping, bToCmapping));
|
128 | }
|
129 | }
|
130 | return flattenedMappings;
|
131 | }
|
132 | };
|
133 | function findLastMappingIndexBefore(mappings, marker, exclusive, lowerIndex) {
|
134 | let upperIndex = mappings.length - 1;
|
135 | const test = exclusive ? -1 : 0;
|
136 | if (compareSegments(mappings[lowerIndex].generatedSegment, marker) > test) {
|
137 | return -1;
|
138 | }
|
139 | let matchingIndex = -1;
|
140 | while (lowerIndex <= upperIndex) {
|
141 | const index = upperIndex + lowerIndex >> 1;
|
142 | if (compareSegments(mappings[index].generatedSegment, marker) <= test) {
|
143 | matchingIndex = index;
|
144 | lowerIndex = index + 1;
|
145 | } else {
|
146 | upperIndex = index - 1;
|
147 | }
|
148 | }
|
149 | return matchingIndex;
|
150 | }
|
151 | function mergeMappings(generatedSource, ab, bc) {
|
152 | const name = bc.name || ab.name;
|
153 | const diff = compareSegments(bc.generatedSegment, ab.originalSegment);
|
154 | if (diff > 0) {
|
155 | return {
|
156 | name,
|
157 | generatedSegment: offsetSegment(generatedSource.startOfLinePositions, ab.generatedSegment, diff),
|
158 | originalSource: bc.originalSource,
|
159 | originalSegment: bc.originalSegment
|
160 | };
|
161 | } else {
|
162 | return {
|
163 | name,
|
164 | generatedSegment: ab.generatedSegment,
|
165 | originalSource: bc.originalSource,
|
166 | originalSegment: offsetSegment(bc.originalSource.startOfLinePositions, bc.originalSegment, -diff)
|
167 | };
|
168 | }
|
169 | }
|
170 | function parseMappings(rawMap, sources, generatedSourceStartOfLinePositions) {
|
171 | if (rawMap === null) {
|
172 | return [];
|
173 | }
|
174 | const rawMappings = decode(rawMap.mappings);
|
175 | if (rawMappings === null) {
|
176 | return [];
|
177 | }
|
178 | const mappings = [];
|
179 | for (let generatedLine = 0; generatedLine < rawMappings.length; generatedLine++) {
|
180 | const generatedLineMappings = rawMappings[generatedLine];
|
181 | for (const rawMapping of generatedLineMappings) {
|
182 | if (rawMapping.length >= 4) {
|
183 | const originalSource = sources[rawMapping[1]];
|
184 | if (originalSource === null || originalSource === void 0) {
|
185 | continue;
|
186 | }
|
187 | const generatedColumn = rawMapping[0];
|
188 | const name = rawMapping.length === 5 ? rawMap.names[rawMapping[4]] : void 0;
|
189 | const line = rawMapping[2];
|
190 | const column = rawMapping[3];
|
191 | const generatedSegment = {
|
192 | line: generatedLine,
|
193 | column: generatedColumn,
|
194 | position: generatedSourceStartOfLinePositions[generatedLine] + generatedColumn,
|
195 | next: void 0
|
196 | };
|
197 | const originalSegment = {
|
198 | line,
|
199 | column,
|
200 | position: originalSource.startOfLinePositions[line] + column,
|
201 | next: void 0
|
202 | };
|
203 | mappings.push({ name, generatedSegment, originalSegment, originalSource });
|
204 | }
|
205 | }
|
206 | }
|
207 | return mappings;
|
208 | }
|
209 | function extractOriginalSegments(mappings) {
|
210 | const originalSegments = new Map();
|
211 | for (const mapping of mappings) {
|
212 | const originalSource = mapping.originalSource;
|
213 | if (!originalSegments.has(originalSource)) {
|
214 | originalSegments.set(originalSource, []);
|
215 | }
|
216 | const segments = originalSegments.get(originalSource);
|
217 | segments.push(mapping.originalSegment);
|
218 | }
|
219 | originalSegments.forEach((segmentMarkers) => segmentMarkers.sort(compareSegments));
|
220 | return originalSegments;
|
221 | }
|
222 | function ensureOriginalSegmentLinks(mappings) {
|
223 | const segmentsBySource = extractOriginalSegments(mappings);
|
224 | segmentsBySource.forEach((markers) => {
|
225 | for (let i = 0; i < markers.length - 1; i++) {
|
226 | markers[i].next = markers[i + 1];
|
227 | }
|
228 | });
|
229 | }
|
230 | function computeStartOfLinePositions(str) {
|
231 | const NEWLINE_MARKER_OFFSET = 1;
|
232 | const lineLengths = computeLineLengths(str);
|
233 | const startPositions = [0];
|
234 | for (let i = 0; i < lineLengths.length - 1; i++) {
|
235 | startPositions.push(startPositions[i] + lineLengths[i] + NEWLINE_MARKER_OFFSET);
|
236 | }
|
237 | return startPositions;
|
238 | }
|
239 | function computeLineLengths(str) {
|
240 | return str.split(/\n/).map((s) => s.length);
|
241 | }
|
242 | var IndexedMap = class {
|
243 | constructor() {
|
244 | this.map = new Map();
|
245 | this.keys = [];
|
246 | this.values = [];
|
247 | }
|
248 | set(key, value) {
|
249 | if (this.map.has(key)) {
|
250 | return this.map.get(key);
|
251 | }
|
252 | const index = this.values.push(value) - 1;
|
253 | this.keys.push(key);
|
254 | this.map.set(key, index);
|
255 | return index;
|
256 | }
|
257 | };
|
258 | var IndexedSet = class {
|
259 | constructor() {
|
260 | this.map = new Map();
|
261 | this.values = [];
|
262 | }
|
263 | add(value) {
|
264 | if (this.map.has(value)) {
|
265 | return this.map.get(value);
|
266 | }
|
267 | const index = this.values.push(value) - 1;
|
268 | this.map.set(value, index);
|
269 | return index;
|
270 | }
|
271 | };
|
272 | var Cache = class {
|
273 | constructor(computeFn) {
|
274 | this.computeFn = computeFn;
|
275 | this.map = new Map();
|
276 | }
|
277 | get(input) {
|
278 | if (!this.map.has(input)) {
|
279 | this.map.set(input, this.computeFn(input));
|
280 | }
|
281 | return this.map.get(input);
|
282 | }
|
283 | };
|
284 |
|
285 |
|
286 | import mapHelpers2 from "convert-source-map";
|
287 |
|
288 |
|
289 | var ContentOrigin;
|
290 | (function(ContentOrigin2) {
|
291 | ContentOrigin2[ContentOrigin2["Provided"] = 0] = "Provided";
|
292 | ContentOrigin2[ContentOrigin2["Inline"] = 1] = "Inline";
|
293 | ContentOrigin2[ContentOrigin2["FileSystem"] = 2] = "FileSystem";
|
294 | })(ContentOrigin || (ContentOrigin = {}));
|
295 |
|
296 |
|
297 | var SCHEME_MATCHER = /^([a-z][a-z0-9.-]*):\/\//i;
|
298 | var SourceFileLoader = class {
|
299 | constructor(fs, logger, schemeMap) {
|
300 | this.fs = fs;
|
301 | this.logger = logger;
|
302 | this.schemeMap = schemeMap;
|
303 | this.currentPaths = [];
|
304 | }
|
305 | loadSourceFile(sourcePath, contents = null, mapAndPath = null) {
|
306 | const contentsOrigin = contents !== null ? ContentOrigin.Provided : ContentOrigin.FileSystem;
|
307 | const sourceMapInfo = mapAndPath && __spreadValues({ origin: ContentOrigin.Provided }, mapAndPath);
|
308 | return this.loadSourceFileInternal(sourcePath, contents, contentsOrigin, sourceMapInfo);
|
309 | }
|
310 | loadSourceFileInternal(sourcePath, contents, sourceOrigin, sourceMapInfo) {
|
311 | const previousPaths = this.currentPaths.slice();
|
312 | try {
|
313 | if (contents === null) {
|
314 | if (!this.fs.exists(sourcePath)) {
|
315 | return null;
|
316 | }
|
317 | contents = this.readSourceFile(sourcePath);
|
318 | }
|
319 | if (sourceMapInfo === null) {
|
320 | sourceMapInfo = this.loadSourceMap(sourcePath, contents, sourceOrigin);
|
321 | }
|
322 | let sources = [];
|
323 | if (sourceMapInfo !== null) {
|
324 | const basePath = sourceMapInfo.mapPath || sourcePath;
|
325 | sources = this.processSources(basePath, sourceMapInfo);
|
326 | }
|
327 | return new SourceFile(sourcePath, contents, sourceMapInfo, sources, this.fs);
|
328 | } catch (e) {
|
329 | this.logger.warn(`Unable to fully load ${sourcePath} for source-map flattening: ${e.message}`);
|
330 | return null;
|
331 | } finally {
|
332 | this.currentPaths = previousPaths;
|
333 | }
|
334 | }
|
335 | loadSourceMap(sourcePath, sourceContents, sourceOrigin) {
|
336 | const lastLine = this.getLastNonEmptyLine(sourceContents);
|
337 | const inline = mapHelpers2.commentRegex.exec(lastLine);
|
338 | if (inline !== null) {
|
339 | return {
|
340 | map: mapHelpers2.fromComment(inline.pop()).sourcemap,
|
341 | mapPath: null,
|
342 | origin: ContentOrigin.Inline
|
343 | };
|
344 | }
|
345 | if (sourceOrigin === ContentOrigin.Inline) {
|
346 | return null;
|
347 | }
|
348 | const external = mapHelpers2.mapFileCommentRegex.exec(lastLine);
|
349 | if (external) {
|
350 | try {
|
351 | const fileName = external[1] || external[2];
|
352 | const externalMapPath = this.fs.resolve(this.fs.dirname(sourcePath), fileName);
|
353 | return {
|
354 | map: this.readRawSourceMap(externalMapPath),
|
355 | mapPath: externalMapPath,
|
356 | origin: ContentOrigin.FileSystem
|
357 | };
|
358 | } catch (e) {
|
359 | this.logger.warn(`Unable to fully load ${sourcePath} for source-map flattening: ${e.message}`);
|
360 | return null;
|
361 | }
|
362 | }
|
363 | const impliedMapPath = this.fs.resolve(sourcePath + ".map");
|
364 | if (this.fs.exists(impliedMapPath)) {
|
365 | return {
|
366 | map: this.readRawSourceMap(impliedMapPath),
|
367 | mapPath: impliedMapPath,
|
368 | origin: ContentOrigin.FileSystem
|
369 | };
|
370 | }
|
371 | return null;
|
372 | }
|
373 | processSources(basePath, { map, origin: sourceMapOrigin }) {
|
374 | const sourceRoot = this.fs.resolve(this.fs.dirname(basePath), this.replaceSchemeWithPath(map.sourceRoot || ""));
|
375 | return map.sources.map((source, index) => {
|
376 | const path = this.fs.resolve(sourceRoot, this.replaceSchemeWithPath(source));
|
377 | const content = map.sourcesContent && map.sourcesContent[index] || null;
|
378 | const sourceOrigin = content !== null && sourceMapOrigin !== ContentOrigin.Provided ? ContentOrigin.Inline : ContentOrigin.FileSystem;
|
379 | return this.loadSourceFileInternal(path, content, sourceOrigin, null);
|
380 | });
|
381 | }
|
382 | readSourceFile(sourcePath) {
|
383 | this.trackPath(sourcePath);
|
384 | return this.fs.readFile(sourcePath);
|
385 | }
|
386 | readRawSourceMap(mapPath) {
|
387 | this.trackPath(mapPath);
|
388 | return JSON.parse(this.fs.readFile(mapPath));
|
389 | }
|
390 | trackPath(path) {
|
391 | if (this.currentPaths.includes(path)) {
|
392 | throw new Error(`Circular source file mapping dependency: ${this.currentPaths.join(" -> ")} -> ${path}`);
|
393 | }
|
394 | this.currentPaths.push(path);
|
395 | }
|
396 | getLastNonEmptyLine(contents) {
|
397 | let trailingWhitespaceIndex = contents.length - 1;
|
398 | while (trailingWhitespaceIndex > 0 && (contents[trailingWhitespaceIndex] === "\n" || contents[trailingWhitespaceIndex] === "\r")) {
|
399 | trailingWhitespaceIndex--;
|
400 | }
|
401 | let lastRealLineIndex = contents.lastIndexOf("\n", trailingWhitespaceIndex - 1);
|
402 | if (lastRealLineIndex === -1) {
|
403 | lastRealLineIndex = 0;
|
404 | }
|
405 | return contents.substr(lastRealLineIndex + 1);
|
406 | }
|
407 | replaceSchemeWithPath(path) {
|
408 | return path.replace(SCHEME_MATCHER, (_, scheme) => this.schemeMap[scheme.toLowerCase()] || "");
|
409 | }
|
410 | };
|
411 |
|
412 | export {
|
413 | ContentOrigin,
|
414 | SourceFile,
|
415 | SourceFileLoader
|
416 | };
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|