source: imaps-frontend/node_modules/@ampproject/remapping/dist/remapping.mjs

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

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