1 | (function (global, factory) {
|
---|
2 | typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('@jridgewell/trace-mapping'), require('@jridgewell/gen-mapping')) :
|
---|
3 | typeof define === 'function' && define.amd ? define(['@jridgewell/trace-mapping', '@jridgewell/gen-mapping'], factory) :
|
---|
4 | (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.remapping = factory(global.traceMapping, global.genMapping));
|
---|
5 | })(this, (function (traceMapping, genMapping) { 'use strict';
|
---|
6 |
|
---|
7 | const SOURCELESS_MAPPING = /* #__PURE__ */ SegmentObject('', -1, -1, '', null, false);
|
---|
8 | const 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 | /**
|
---|
22 | * MapSource represents a single sourcemap, with the ability to trace mappings into its child nodes
|
---|
23 | * (which may themselves be SourceMapTrees).
|
---|
24 | */
|
---|
25 | function MapSource(map, sources) {
|
---|
26 | return Source(map, sources, '', null, false);
|
---|
27 | }
|
---|
28 | /**
|
---|
29 | * A "leaf" node in the sourcemap tree, representing an original, unmodified source file. Recursive
|
---|
30 | * segment tracing ends at the `OriginalSource`.
|
---|
31 | */
|
---|
32 | function OriginalSource(source, content, ignore) {
|
---|
33 | return Source(null, EMPTY_SOURCES, source, content, ignore);
|
---|
34 | }
|
---|
35 | /**
|
---|
36 | * traceMappings is only called on the root level SourceMapTree, and begins the process of
|
---|
37 | * resolving each mapping in terms of the original source files.
|
---|
38 | */
|
---|
39 | function traceMappings(tree) {
|
---|
40 | // TODO: Eventually support sourceRoot, which has to be removed because the sources are already
|
---|
41 | // fully resolved. We'll need to make sources relative to the sourceRoot before adding them.
|
---|
42 | const gen = new genMapping.GenMapping({ file: tree.map.file });
|
---|
43 | const { sources: rootSources, map } = tree;
|
---|
44 | const rootNames = map.names;
|
---|
45 | const rootMappings = traceMapping.decodedMappings(map);
|
---|
46 | for (let i = 0; i < rootMappings.length; i++) {
|
---|
47 | const segments = rootMappings[i];
|
---|
48 | for (let j = 0; j < segments.length; j++) {
|
---|
49 | const segment = segments[j];
|
---|
50 | const genCol = segment[0];
|
---|
51 | let traced = SOURCELESS_MAPPING;
|
---|
52 | // 1-length segments only move the current generated column, there's no source information
|
---|
53 | // to gather from it.
|
---|
54 | if (segment.length !== 1) {
|
---|
55 | const source = rootSources[segment[1]];
|
---|
56 | traced = originalPositionFor(source, segment[2], segment[3], segment.length === 5 ? rootNames[segment[4]] : '');
|
---|
57 | // If the trace is invalid, then the trace ran into a sourcemap that doesn't contain a
|
---|
58 | // respective segment into an original source.
|
---|
59 | if (traced == null)
|
---|
60 | continue;
|
---|
61 | }
|
---|
62 | const { column, line, name, content, source, ignore } = traced;
|
---|
63 | genMapping.maybeAddSegment(gen, i, genCol, source, line, column, name);
|
---|
64 | if (source && content != null)
|
---|
65 | genMapping.setSourceContent(gen, source, content);
|
---|
66 | if (ignore)
|
---|
67 | genMapping.setIgnore(gen, source, true);
|
---|
68 | }
|
---|
69 | }
|
---|
70 | return gen;
|
---|
71 | }
|
---|
72 | /**
|
---|
73 | * originalPositionFor is only called on children SourceMapTrees. It recurses down into its own
|
---|
74 | * child SourceMapTrees, until we find the original source map.
|
---|
75 | */
|
---|
76 | function originalPositionFor(source, line, column, name) {
|
---|
77 | if (!source.map) {
|
---|
78 | return SegmentObject(source.source, line, column, name, source.content, source.ignore);
|
---|
79 | }
|
---|
80 | const segment = traceMapping.traceSegment(source.map, line, column);
|
---|
81 | // If we couldn't find a segment, then this doesn't exist in the sourcemap.
|
---|
82 | if (segment == null)
|
---|
83 | return null;
|
---|
84 | // 1-length segments only move the current generated column, there's no source information
|
---|
85 | // to gather from it.
|
---|
86 | if (segment.length === 1)
|
---|
87 | return SOURCELESS_MAPPING;
|
---|
88 | return originalPositionFor(source.sources[segment[1]], segment[2], segment[3], segment.length === 5 ? source.map.names[segment[4]] : name);
|
---|
89 | }
|
---|
90 |
|
---|
91 | function asArray(value) {
|
---|
92 | if (Array.isArray(value))
|
---|
93 | return value;
|
---|
94 | return [value];
|
---|
95 | }
|
---|
96 | /**
|
---|
97 | * Recursively builds a tree structure out of sourcemap files, with each node
|
---|
98 | * being either an `OriginalSource` "leaf" or a `SourceMapTree` composed of
|
---|
99 | * `OriginalSource`s and `SourceMapTree`s.
|
---|
100 | *
|
---|
101 | * Every sourcemap is composed of a collection of source files and mappings
|
---|
102 | * into locations of those source files. When we generate a `SourceMapTree` for
|
---|
103 | * the sourcemap, we attempt to load each source file's own sourcemap. If it
|
---|
104 | * does not have an associated sourcemap, it is considered an original,
|
---|
105 | * unmodified source file.
|
---|
106 | */
|
---|
107 | function buildSourceMapTree(input, loader) {
|
---|
108 | const maps = asArray(input).map((m) => new traceMapping.TraceMap(m, ''));
|
---|
109 | const map = maps.pop();
|
---|
110 | for (let i = 0; i < maps.length; i++) {
|
---|
111 | if (maps[i].sources.length > 1) {
|
---|
112 | throw new Error(`Transformation map ${i} must have exactly one source file.\n` +
|
---|
113 | 'Did you specify these with the most recent transformation maps first?');
|
---|
114 | }
|
---|
115 | }
|
---|
116 | let tree = build(map, loader, '', 0);
|
---|
117 | for (let i = maps.length - 1; i >= 0; i--) {
|
---|
118 | tree = MapSource(maps[i], [tree]);
|
---|
119 | }
|
---|
120 | return tree;
|
---|
121 | }
|
---|
122 | function build(map, loader, importer, importerDepth) {
|
---|
123 | const { resolvedSources, sourcesContent, ignoreList } = map;
|
---|
124 | const depth = importerDepth + 1;
|
---|
125 | const children = resolvedSources.map((sourceFile, i) => {
|
---|
126 | // The loading context gives the loader more information about why this file is being loaded
|
---|
127 | // (eg, from which importer). It also allows the loader to override the location of the loaded
|
---|
128 | // sourcemap/original source, or to override the content in the sourcesContent field if it's
|
---|
129 | // an unmodified source file.
|
---|
130 | const ctx = {
|
---|
131 | importer,
|
---|
132 | depth,
|
---|
133 | source: sourceFile || '',
|
---|
134 | content: undefined,
|
---|
135 | ignore: undefined,
|
---|
136 | };
|
---|
137 | // Use the provided loader callback to retrieve the file's sourcemap.
|
---|
138 | // TODO: We should eventually support async loading of sourcemap files.
|
---|
139 | const sourceMap = loader(ctx.source, ctx);
|
---|
140 | const { source, content, ignore } = ctx;
|
---|
141 | // If there is a sourcemap, then we need to recurse into it to load its source files.
|
---|
142 | if (sourceMap)
|
---|
143 | return build(new traceMapping.TraceMap(sourceMap, source), loader, source, depth);
|
---|
144 | // Else, it's an unmodified source file.
|
---|
145 | // The contents of this unmodified source file can be overridden via the loader context,
|
---|
146 | // allowing it to be explicitly null or a string. If it remains undefined, we fall back to
|
---|
147 | // the importing sourcemap's `sourcesContent` field.
|
---|
148 | const sourceContent = content !== undefined ? content : sourcesContent ? sourcesContent[i] : null;
|
---|
149 | const ignored = ignore !== undefined ? ignore : ignoreList ? ignoreList.includes(i) : false;
|
---|
150 | return OriginalSource(source, sourceContent, ignored);
|
---|
151 | });
|
---|
152 | return MapSource(map, children);
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * A SourceMap v3 compatible sourcemap, which only includes fields that were
|
---|
157 | * provided to it.
|
---|
158 | */
|
---|
159 | class SourceMap {
|
---|
160 | constructor(map, options) {
|
---|
161 | const out = options.decodedMappings ? genMapping.toDecodedMap(map) : genMapping.toEncodedMap(map);
|
---|
162 | this.version = out.version; // SourceMap spec says this should be first.
|
---|
163 | this.file = out.file;
|
---|
164 | this.mappings = out.mappings;
|
---|
165 | this.names = out.names;
|
---|
166 | this.ignoreList = out.ignoreList;
|
---|
167 | this.sourceRoot = out.sourceRoot;
|
---|
168 | this.sources = out.sources;
|
---|
169 | if (!options.excludeContent) {
|
---|
170 | this.sourcesContent = out.sourcesContent;
|
---|
171 | }
|
---|
172 | }
|
---|
173 | toString() {
|
---|
174 | return JSON.stringify(this);
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Traces through all the mappings in the root sourcemap, through the sources
|
---|
180 | * (and their sourcemaps), all the way back to the original source location.
|
---|
181 | *
|
---|
182 | * `loader` will be called every time we encounter a source file. If it returns
|
---|
183 | * a sourcemap, we will recurse into that sourcemap to continue the trace. If
|
---|
184 | * it returns a falsey value, that source file is treated as an original,
|
---|
185 | * unmodified source file.
|
---|
186 | *
|
---|
187 | * Pass `excludeContent` to exclude any self-containing source file content
|
---|
188 | * from the output sourcemap.
|
---|
189 | *
|
---|
190 | * Pass `decodedMappings` to receive a SourceMap with decoded (instead of
|
---|
191 | * VLQ encoded) mappings.
|
---|
192 | */
|
---|
193 | function remapping(input, loader, options) {
|
---|
194 | const opts = typeof options === 'object' ? options : { excludeContent: !!options, decodedMappings: false };
|
---|
195 | const tree = buildSourceMapTree(input, loader);
|
---|
196 | return new SourceMap(traceMappings(tree), opts);
|
---|
197 | }
|
---|
198 |
|
---|
199 | return remapping;
|
---|
200 |
|
---|
201 | }));
|
---|
202 | //# sourceMappingURL=remapping.umd.js.map
|
---|