| 1 | // src/build-source-map-tree.ts
|
|---|
| 2 | import { TraceMap } from "@jridgewell/trace-mapping";
|
|---|
| 3 |
|
|---|
| 4 | // src/source-map-tree.ts
|
|---|
| 5 | import { GenMapping, maybeAddSegment, setIgnore, setSourceContent } from "@jridgewell/gen-mapping";
|
|---|
| 6 | import { traceSegment, decodedMappings } from "@jridgewell/trace-mapping";
|
|---|
| 7 | var SOURCELESS_MAPPING = /* @__PURE__ */ SegmentObject("", -1, -1, "", null, false);
|
|---|
| 8 | var EMPTY_SOURCES = [];
|
|---|
| 9 | function SegmentObject(source, line, column, name, content, ignore) {
|
|---|
| 10 | return { source, line, column, name, content, ignore };
|
|---|
| 11 | }
|
|---|
| 12 | function Source(map, sources, source, content, ignore) {
|
|---|
| 13 | return {
|
|---|
| 14 | map,
|
|---|
| 15 | sources,
|
|---|
| 16 | source,
|
|---|
| 17 | content,
|
|---|
| 18 | ignore
|
|---|
| 19 | };
|
|---|
| 20 | }
|
|---|
| 21 | function MapSource(map, sources) {
|
|---|
| 22 | return Source(map, sources, "", null, false);
|
|---|
| 23 | }
|
|---|
| 24 | function OriginalSource(source, content, ignore) {
|
|---|
| 25 | return Source(null, EMPTY_SOURCES, source, content, ignore);
|
|---|
| 26 | }
|
|---|
| 27 | function traceMappings(tree) {
|
|---|
| 28 | const gen = new GenMapping({ file: tree.map.file });
|
|---|
| 29 | const { sources: rootSources, map } = tree;
|
|---|
| 30 | const rootNames = map.names;
|
|---|
| 31 | const rootMappings = decodedMappings(map);
|
|---|
| 32 | for (let i = 0; i < rootMappings.length; i++) {
|
|---|
| 33 | const segments = rootMappings[i];
|
|---|
| 34 | for (let j = 0; j < segments.length; j++) {
|
|---|
| 35 | const segment = segments[j];
|
|---|
| 36 | const genCol = segment[0];
|
|---|
| 37 | let traced = SOURCELESS_MAPPING;
|
|---|
| 38 | if (segment.length !== 1) {
|
|---|
| 39 | const source2 = rootSources[segment[1]];
|
|---|
| 40 | traced = originalPositionFor(
|
|---|
| 41 | source2,
|
|---|
| 42 | segment[2],
|
|---|
| 43 | segment[3],
|
|---|
| 44 | segment.length === 5 ? rootNames[segment[4]] : ""
|
|---|
| 45 | );
|
|---|
| 46 | if (traced == null) continue;
|
|---|
| 47 | }
|
|---|
| 48 | const { column, line, name, content, source, ignore } = traced;
|
|---|
| 49 | maybeAddSegment(gen, i, genCol, source, line, column, name);
|
|---|
| 50 | if (source && content != null) setSourceContent(gen, source, content);
|
|---|
| 51 | if (ignore) setIgnore(gen, source, true);
|
|---|
| 52 | }
|
|---|
| 53 | }
|
|---|
| 54 | return gen;
|
|---|
| 55 | }
|
|---|
| 56 | function originalPositionFor(source, line, column, name) {
|
|---|
| 57 | if (!source.map) {
|
|---|
| 58 | return SegmentObject(source.source, line, column, name, source.content, source.ignore);
|
|---|
| 59 | }
|
|---|
| 60 | const segment = traceSegment(source.map, line, column);
|
|---|
| 61 | if (segment == null) return null;
|
|---|
| 62 | if (segment.length === 1) return SOURCELESS_MAPPING;
|
|---|
| 63 | return originalPositionFor(
|
|---|
| 64 | source.sources[segment[1]],
|
|---|
| 65 | segment[2],
|
|---|
| 66 | segment[3],
|
|---|
| 67 | segment.length === 5 ? source.map.names[segment[4]] : name
|
|---|
| 68 | );
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | // src/build-source-map-tree.ts
|
|---|
| 72 | function asArray(value) {
|
|---|
| 73 | if (Array.isArray(value)) return value;
|
|---|
| 74 | return [value];
|
|---|
| 75 | }
|
|---|
| 76 | function buildSourceMapTree(input, loader) {
|
|---|
| 77 | const maps = asArray(input).map((m) => new TraceMap(m, ""));
|
|---|
| 78 | const map = maps.pop();
|
|---|
| 79 | for (let i = 0; i < maps.length; i++) {
|
|---|
| 80 | if (maps[i].sources.length > 1) {
|
|---|
| 81 | throw new Error(
|
|---|
| 82 | `Transformation map ${i} must have exactly one source file.
|
|---|
| 83 | Did you specify these with the most recent transformation maps first?`
|
|---|
| 84 | );
|
|---|
| 85 | }
|
|---|
| 86 | }
|
|---|
| 87 | let tree = build(map, loader, "", 0);
|
|---|
| 88 | for (let i = maps.length - 1; i >= 0; i--) {
|
|---|
| 89 | tree = MapSource(maps[i], [tree]);
|
|---|
| 90 | }
|
|---|
| 91 | return tree;
|
|---|
| 92 | }
|
|---|
| 93 | function build(map, loader, importer, importerDepth) {
|
|---|
| 94 | const { resolvedSources, sourcesContent, ignoreList } = map;
|
|---|
| 95 | const depth = importerDepth + 1;
|
|---|
| 96 | const children = resolvedSources.map((sourceFile, i) => {
|
|---|
| 97 | const ctx = {
|
|---|
| 98 | importer,
|
|---|
| 99 | depth,
|
|---|
| 100 | source: sourceFile || "",
|
|---|
| 101 | content: void 0,
|
|---|
| 102 | ignore: void 0
|
|---|
| 103 | };
|
|---|
| 104 | const sourceMap = loader(ctx.source, ctx);
|
|---|
| 105 | const { source, content, ignore } = ctx;
|
|---|
| 106 | if (sourceMap) return build(new TraceMap(sourceMap, source), loader, source, depth);
|
|---|
| 107 | const sourceContent = content !== void 0 ? content : sourcesContent ? sourcesContent[i] : null;
|
|---|
| 108 | const ignored = ignore !== void 0 ? ignore : ignoreList ? ignoreList.includes(i) : false;
|
|---|
| 109 | return OriginalSource(source, sourceContent, ignored);
|
|---|
| 110 | });
|
|---|
| 111 | return MapSource(map, children);
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | // src/source-map.ts
|
|---|
| 115 | import { toDecodedMap, toEncodedMap } from "@jridgewell/gen-mapping";
|
|---|
| 116 | var SourceMap = class {
|
|---|
| 117 | constructor(map, options) {
|
|---|
| 118 | const out = options.decodedMappings ? toDecodedMap(map) : toEncodedMap(map);
|
|---|
| 119 | this.version = out.version;
|
|---|
| 120 | this.file = out.file;
|
|---|
| 121 | this.mappings = out.mappings;
|
|---|
| 122 | this.names = out.names;
|
|---|
| 123 | this.ignoreList = out.ignoreList;
|
|---|
| 124 | this.sourceRoot = out.sourceRoot;
|
|---|
| 125 | this.sources = out.sources;
|
|---|
| 126 | if (!options.excludeContent) {
|
|---|
| 127 | this.sourcesContent = out.sourcesContent;
|
|---|
| 128 | }
|
|---|
| 129 | }
|
|---|
| 130 | toString() {
|
|---|
| 131 | return JSON.stringify(this);
|
|---|
| 132 | }
|
|---|
| 133 | };
|
|---|
| 134 |
|
|---|
| 135 | // src/remapping.ts
|
|---|
| 136 | function remapping(input, loader, options) {
|
|---|
| 137 | const opts = typeof options === "object" ? options : { excludeContent: !!options, decodedMappings: false };
|
|---|
| 138 | const tree = buildSourceMapTree(input, loader);
|
|---|
| 139 | return new SourceMap(traceMappings(tree), opts);
|
|---|
| 140 | }
|
|---|
| 141 | export {
|
|---|
| 142 | remapping as default
|
|---|
| 143 | };
|
|---|
| 144 | //# sourceMappingURL=remapping.mjs.map
|
|---|