1 | (function (global, factory) {
|
---|
2 | typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
---|
3 | typeof define === 'function' && define.amd ? define(factory) :
|
---|
4 | (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.remapping = factory());
|
---|
5 | }(this, (function () { 'use strict';
|
---|
6 |
|
---|
7 | var charToInteger = {};
|
---|
8 | var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
|
---|
9 | for (var i = 0; i < chars.length; i++) {
|
---|
10 | charToInteger[chars.charCodeAt(i)] = i;
|
---|
11 | }
|
---|
12 | function decode(mappings) {
|
---|
13 | var decoded = [];
|
---|
14 | var line = [];
|
---|
15 | var segment = [
|
---|
16 | 0,
|
---|
17 | 0,
|
---|
18 | 0,
|
---|
19 | 0,
|
---|
20 | 0,
|
---|
21 | ];
|
---|
22 | var j = 0;
|
---|
23 | for (var i = 0, shift = 0, value = 0; i < mappings.length; i++) {
|
---|
24 | var c = mappings.charCodeAt(i);
|
---|
25 | if (c === 44) { // ","
|
---|
26 | segmentify(line, segment, j);
|
---|
27 | j = 0;
|
---|
28 | }
|
---|
29 | else if (c === 59) { // ";"
|
---|
30 | segmentify(line, segment, j);
|
---|
31 | j = 0;
|
---|
32 | decoded.push(line);
|
---|
33 | line = [];
|
---|
34 | segment[0] = 0;
|
---|
35 | }
|
---|
36 | else {
|
---|
37 | var integer = charToInteger[c];
|
---|
38 | if (integer === undefined) {
|
---|
39 | throw new Error('Invalid character (' + String.fromCharCode(c) + ')');
|
---|
40 | }
|
---|
41 | var hasContinuationBit = integer & 32;
|
---|
42 | integer &= 31;
|
---|
43 | value += integer << shift;
|
---|
44 | if (hasContinuationBit) {
|
---|
45 | shift += 5;
|
---|
46 | }
|
---|
47 | else {
|
---|
48 | var shouldNegate = value & 1;
|
---|
49 | value >>>= 1;
|
---|
50 | if (shouldNegate) {
|
---|
51 | value = value === 0 ? -0x80000000 : -value;
|
---|
52 | }
|
---|
53 | segment[j] += value;
|
---|
54 | j++;
|
---|
55 | value = shift = 0; // reset
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|
59 | segmentify(line, segment, j);
|
---|
60 | decoded.push(line);
|
---|
61 | return decoded;
|
---|
62 | }
|
---|
63 | function segmentify(line, segment, j) {
|
---|
64 | // This looks ugly, but we're creating specialized arrays with a specific
|
---|
65 | // length. This is much faster than creating a new array (which v8 expands to
|
---|
66 | // a capacity of 17 after pushing the first item), or slicing out a subarray
|
---|
67 | // (which is slow). Length 4 is assumed to be the most frequent, followed by
|
---|
68 | // length 5 (since not everything will have an associated name), followed by
|
---|
69 | // length 1 (it's probably rare for a source substring to not have an
|
---|
70 | // associated segment data).
|
---|
71 | if (j === 4)
|
---|
72 | line.push([segment[0], segment[1], segment[2], segment[3]]);
|
---|
73 | else if (j === 5)
|
---|
74 | line.push([segment[0], segment[1], segment[2], segment[3], segment[4]]);
|
---|
75 | else if (j === 1)
|
---|
76 | line.push([segment[0]]);
|
---|
77 | }
|
---|
78 | function encode(decoded) {
|
---|
79 | var sourceFileIndex = 0; // second field
|
---|
80 | var sourceCodeLine = 0; // third field
|
---|
81 | var sourceCodeColumn = 0; // fourth field
|
---|
82 | var nameIndex = 0; // fifth field
|
---|
83 | var mappings = '';
|
---|
84 | for (var i = 0; i < decoded.length; i++) {
|
---|
85 | var line = decoded[i];
|
---|
86 | if (i > 0)
|
---|
87 | mappings += ';';
|
---|
88 | if (line.length === 0)
|
---|
89 | continue;
|
---|
90 | var generatedCodeColumn = 0; // first field
|
---|
91 | var lineMappings = [];
|
---|
92 | for (var _i = 0, line_1 = line; _i < line_1.length; _i++) {
|
---|
93 | var segment = line_1[_i];
|
---|
94 | var segmentMappings = encodeInteger(segment[0] - generatedCodeColumn);
|
---|
95 | generatedCodeColumn = segment[0];
|
---|
96 | if (segment.length > 1) {
|
---|
97 | segmentMappings +=
|
---|
98 | encodeInteger(segment[1] - sourceFileIndex) +
|
---|
99 | encodeInteger(segment[2] - sourceCodeLine) +
|
---|
100 | encodeInteger(segment[3] - sourceCodeColumn);
|
---|
101 | sourceFileIndex = segment[1];
|
---|
102 | sourceCodeLine = segment[2];
|
---|
103 | sourceCodeColumn = segment[3];
|
---|
104 | }
|
---|
105 | if (segment.length === 5) {
|
---|
106 | segmentMappings += encodeInteger(segment[4] - nameIndex);
|
---|
107 | nameIndex = segment[4];
|
---|
108 | }
|
---|
109 | lineMappings.push(segmentMappings);
|
---|
110 | }
|
---|
111 | mappings += lineMappings.join(',');
|
---|
112 | }
|
---|
113 | return mappings;
|
---|
114 | }
|
---|
115 | function encodeInteger(num) {
|
---|
116 | var result = '';
|
---|
117 | num = num < 0 ? (-num << 1) | 1 : num << 1;
|
---|
118 | do {
|
---|
119 | var clamped = num & 31;
|
---|
120 | num >>>= 5;
|
---|
121 | if (num > 0) {
|
---|
122 | clamped |= 32;
|
---|
123 | }
|
---|
124 | result += chars[clamped];
|
---|
125 | } while (num > 0);
|
---|
126 | return result;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Copyright 2019 The AMP HTML Authors. All Rights Reserved.
|
---|
131 | *
|
---|
132 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
133 | * you may not use this file except in compliance with the License.
|
---|
134 | * You may obtain a copy of the License at
|
---|
135 | *
|
---|
136 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
137 | *
|
---|
138 | * Unless required by applicable law or agreed to in writing, software
|
---|
139 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
140 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
141 | * See the License for the specific language governing permissions and
|
---|
142 | * limitations under the License.
|
---|
143 | */
|
---|
144 | /**
|
---|
145 | * Creates a brand new (prototype-less) object with the enumerable-own
|
---|
146 | * properties of `target`. Any enumerable-own properties from `source` which
|
---|
147 | * are not present on `target` will be copied as well.
|
---|
148 | */
|
---|
149 | function defaults(target, source) {
|
---|
150 | return Object.assign(Object.create(null), source, target);
|
---|
151 | }
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Copyright 2019 The AMP HTML Authors. All Rights Reserved.
|
---|
155 | *
|
---|
156 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
157 | * you may not use this file except in compliance with the License.
|
---|
158 | * You may obtain a copy of the License at
|
---|
159 | *
|
---|
160 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
161 | *
|
---|
162 | * Unless required by applicable law or agreed to in writing, software
|
---|
163 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
164 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
165 | * See the License for the specific language governing permissions and
|
---|
166 | * limitations under the License.
|
---|
167 | */
|
---|
168 | /**
|
---|
169 | * Decodes an input sourcemap into a `DecodedSourceMap` sourcemap object.
|
---|
170 | *
|
---|
171 | * Valid input maps include a `DecodedSourceMap`, a `RawSourceMap`, or JSON
|
---|
172 | * representations of either type.
|
---|
173 | */
|
---|
174 | function decodeSourceMap(map) {
|
---|
175 | if (typeof map === 'string') {
|
---|
176 | map = JSON.parse(map);
|
---|
177 | }
|
---|
178 | let { mappings } = map;
|
---|
179 | if (typeof mappings === 'string') {
|
---|
180 | mappings = sortMappings(decode(mappings), true);
|
---|
181 | }
|
---|
182 | else {
|
---|
183 | // Clone the Line so that we can sort it. We don't want to mutate an array
|
---|
184 | // that we don't own directly.
|
---|
185 | mappings = sortMappings(mappings, false);
|
---|
186 | }
|
---|
187 | return defaults({ mappings }, map);
|
---|
188 | }
|
---|
189 | function firstUnsortedSegmentLine(mappings) {
|
---|
190 | for (let i = 0; i < mappings.length; i++) {
|
---|
191 | const segments = mappings[i];
|
---|
192 | for (let j = 1; j < segments.length; j++) {
|
---|
193 | if (segments[j][0] < segments[j - 1][0]) {
|
---|
194 | return i;
|
---|
195 | }
|
---|
196 | }
|
---|
197 | }
|
---|
198 | return mappings.length;
|
---|
199 | }
|
---|
200 | function sortMappings(mappings, owned) {
|
---|
201 | const unosrtedIndex = firstUnsortedSegmentLine(mappings);
|
---|
202 | if (unosrtedIndex === mappings.length)
|
---|
203 | return mappings;
|
---|
204 | if (!owned)
|
---|
205 | mappings = mappings.slice();
|
---|
206 | for (let i = unosrtedIndex; i < mappings.length; i++) {
|
---|
207 | mappings[i] = sortSegments(mappings[i], owned);
|
---|
208 | }
|
---|
209 | return mappings;
|
---|
210 | }
|
---|
211 | function sortSegments(segments, owned) {
|
---|
212 | if (!owned)
|
---|
213 | segments = segments.slice();
|
---|
214 | return segments.sort(segmentComparator);
|
---|
215 | }
|
---|
216 | function segmentComparator(a, b) {
|
---|
217 | return a[0] - b[0];
|
---|
218 | }
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * Copyright 2019 The AMP HTML Authors. All Rights Reserved.
|
---|
222 | *
|
---|
223 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
224 | * you may not use this file except in compliance with the License.
|
---|
225 | * You may obtain a copy of the License at
|
---|
226 | *
|
---|
227 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
228 | *
|
---|
229 | * Unless required by applicable law or agreed to in writing, software
|
---|
230 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
231 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
232 | * See the License for the specific language governing permissions and
|
---|
233 | * limitations under the License.
|
---|
234 | */
|
---|
235 | /**
|
---|
236 | * A "leaf" node in the sourcemap tree, representing an original, unmodified
|
---|
237 | * source file. Recursive segment tracing ends at the `OriginalSource`.
|
---|
238 | */
|
---|
239 | class OriginalSource {
|
---|
240 | constructor(filename, content) {
|
---|
241 | this.filename = filename;
|
---|
242 | this.content = content;
|
---|
243 | }
|
---|
244 | /**
|
---|
245 | * Tracing a `SourceMapSegment` ends when we get to an `OriginalSource`,
|
---|
246 | * meaning this line/column location originated from this source file.
|
---|
247 | */
|
---|
248 | traceSegment(line, column, name) {
|
---|
249 | return { column, line, name, source: this };
|
---|
250 | }
|
---|
251 | }
|
---|
252 |
|
---|
253 | /* istanbul ignore next */
|
---|
254 | const Url = (typeof URL !== 'undefined' ? URL : require('url').URL);
|
---|
255 | // Matches "..", which must be preceeded by "/" or the start of the string, and
|
---|
256 | // must be followed by a "/". We do not eat the following "/", so that the next
|
---|
257 | // iteration can match on it.
|
---|
258 | const parentRegex = /(^|\/)\.\.(?=\/|$)/g;
|
---|
259 | function isAbsoluteUrl(url) {
|
---|
260 | try {
|
---|
261 | return !!new Url(url);
|
---|
262 | }
|
---|
263 | catch (e) {
|
---|
264 | return false;
|
---|
265 | }
|
---|
266 | }
|
---|
267 | /**
|
---|
268 | * Creates a directory name that is guaranteed to not be in `str`.
|
---|
269 | */
|
---|
270 | function uniqInStr(str) {
|
---|
271 | let uniq = String(Math.random()).slice(2);
|
---|
272 | while (str.indexOf(uniq) > -1) {
|
---|
273 | /* istanbul ignore next */
|
---|
274 | uniq += uniq;
|
---|
275 | }
|
---|
276 | return uniq;
|
---|
277 | }
|
---|
278 | /**
|
---|
279 | * Removes the filename from the path (everything trailing the last "/"). This
|
---|
280 | * is only safe to call on a path, never call with an absolute or protocol
|
---|
281 | * relative URL.
|
---|
282 | */
|
---|
283 | function stripPathFilename(path) {
|
---|
284 | path = normalizePath(path);
|
---|
285 | const index = path.lastIndexOf('/');
|
---|
286 | return path.slice(0, index + 1);
|
---|
287 | }
|
---|
288 | /**
|
---|
289 | * Normalizes a protocol-relative URL, but keeps it protocol relative by
|
---|
290 | * stripping out the protocl before returning it.
|
---|
291 | */
|
---|
292 | function normalizeProtocolRelative(input, absoluteBase) {
|
---|
293 | const { href, protocol } = new Url(input, absoluteBase);
|
---|
294 | return href.slice(protocol.length);
|
---|
295 | }
|
---|
296 | /**
|
---|
297 | * Normalizes a simple path (one that has no ".."s, or is absolute so ".."s can
|
---|
298 | * be normalized absolutely).
|
---|
299 | */
|
---|
300 | function normalizeSimplePath(input) {
|
---|
301 | const { href } = new Url(input, 'https://foo.com/');
|
---|
302 | return href.slice('https://foo.com/'.length);
|
---|
303 | }
|
---|
304 | /**
|
---|
305 | * Normalizes a path, ensuring that excess ".."s are preserved for relative
|
---|
306 | * paths in the output.
|
---|
307 | *
|
---|
308 | * If the input is absolute, this will return an absolutey normalized path, but
|
---|
309 | * it will not have a leading "/".
|
---|
310 | *
|
---|
311 | * If the input has a leading "..", the output will have a leading "..".
|
---|
312 | *
|
---|
313 | * If the input has a leading ".", the output will not have a leading "."
|
---|
314 | * unless there are too many ".."s, in which case there will be a leading "..".
|
---|
315 | */
|
---|
316 | function normalizePath(input) {
|
---|
317 | // If there are no ".."s, we can treat this as if it were an absolute path.
|
---|
318 | // The return won't be an absolute path, so it's easy.
|
---|
319 | if (!parentRegex.test(input))
|
---|
320 | return normalizeSimplePath(input);
|
---|
321 | // We already found one "..". Let's see how many there are.
|
---|
322 | let total = 1;
|
---|
323 | while (parentRegex.test(input))
|
---|
324 | total++;
|
---|
325 | // If there are ".."s, we need to prefix the the path with the same number of
|
---|
326 | // unique directories. This is to ensure that we "remember" how many parent
|
---|
327 | // directories we are accessing. Eg, "../../.." must keep 3, and "foo/../.."
|
---|
328 | // must keep 1.
|
---|
329 | const uniqDirectory = `z${uniqInStr(input)}/`;
|
---|
330 | // uniqDirectory is just a "z", followed by numbers, followed by a "/". So
|
---|
331 | // generating a runtime regex from it is safe. We'll use this search regex to
|
---|
332 | // strip out our uniq directory names and insert any needed ".."s.
|
---|
333 | const search = new RegExp(`^(?:${uniqDirectory})*`);
|
---|
334 | // Now we can resolve the total path. If there are excess ".."s, they will
|
---|
335 | // eliminate one or more of the unique directories we prefix with.
|
---|
336 | const relative = normalizeSimplePath(uniqDirectory.repeat(total) + input);
|
---|
337 | // We can now count the number of unique directories that were eliminated. If
|
---|
338 | // there were 3, and 1 was eliminated, we know we only need to add 1 "..". If
|
---|
339 | // 2 were eliminated, we need to insert 2 ".."s. If all 3 were eliminated,
|
---|
340 | // then we need 3, etc. This replace is guranteed to match (it may match 0 or
|
---|
341 | // more times), and we can count the total match to see how many were eliminated.
|
---|
342 | return relative.replace(search, (all) => {
|
---|
343 | const leftover = all.length / uniqDirectory.length;
|
---|
344 | return '../'.repeat(total - leftover);
|
---|
345 | });
|
---|
346 | }
|
---|
347 | /**
|
---|
348 | * Attempts to resolve `input` URL relative to `base`.
|
---|
349 | */
|
---|
350 | function resolve(input, base) {
|
---|
351 | if (!base)
|
---|
352 | base = '';
|
---|
353 | // Absolute URLs are very easy to resolve right.
|
---|
354 | if (isAbsoluteUrl(input))
|
---|
355 | return new Url(input).href;
|
---|
356 | if (base) {
|
---|
357 | // Absolute URLs are easy...
|
---|
358 | if (isAbsoluteUrl(base))
|
---|
359 | return new Url(input, base).href;
|
---|
360 | // If base is protocol relative, we'll resolve with it but keep the result
|
---|
361 | // protocol relative.
|
---|
362 | if (base.startsWith('//'))
|
---|
363 | return normalizeProtocolRelative(input, `https:${base}`);
|
---|
364 | }
|
---|
365 | // Normalize input, but keep it protocol relative. We know base doesn't supply
|
---|
366 | // a protocol, because that would have been handled above.
|
---|
367 | if (input.startsWith('//'))
|
---|
368 | return normalizeProtocolRelative(input, 'https://foo.com/');
|
---|
369 | // We now know that base (if there is one) and input are paths. We've handled
|
---|
370 | // both absolute and protocol-relative variations above.
|
---|
371 | // Absolute paths don't need any special handling, because they cannot have
|
---|
372 | // extra "." or ".."s. That'll all be stripped away. Input takes priority here,
|
---|
373 | // because if input is an absolute path, base path won't affect it in any way.
|
---|
374 | if (input.startsWith('/'))
|
---|
375 | return '/' + normalizeSimplePath(input);
|
---|
376 | // Since input and base are paths, we need to join them to do any further
|
---|
377 | // processing. Paths are joined at the directory level, so we need to remove
|
---|
378 | // the base's filename before joining. We also know that input does not have a
|
---|
379 | // leading slash, and that the stripped base will have a trailing slash if
|
---|
380 | // there are any directories (or it'll be empty).
|
---|
381 | const joined = stripPathFilename(base) + input;
|
---|
382 | // If base is an absolute path, then input will be relative to it.
|
---|
383 | if (base.startsWith('/'))
|
---|
384 | return '/' + normalizeSimplePath(joined);
|
---|
385 | // We now know both base (if there is one) and input are relative paths.
|
---|
386 | const relative = normalizePath(joined);
|
---|
387 | // If base started with a leading ".", or there is no base and input started
|
---|
388 | // with a ".", then we need to ensure that the relative path starts with a
|
---|
389 | // ".". We don't know if relative starts with a "..", though, so check before
|
---|
390 | // prepending.
|
---|
391 | if ((base || input).startsWith('.') && !relative.startsWith('.')) {
|
---|
392 | return './' + relative;
|
---|
393 | }
|
---|
394 | return relative;
|
---|
395 | }
|
---|
396 |
|
---|
397 | /**
|
---|
398 | * Copyright 2019 The AMP HTML Authors. All Rights Reserved.
|
---|
399 | *
|
---|
400 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
401 | * you may not use this file except in compliance with the License.
|
---|
402 | * You may obtain a copy of the License at
|
---|
403 | *
|
---|
404 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
405 | *
|
---|
406 | * Unless required by applicable law or agreed to in writing, software
|
---|
407 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
408 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
409 | * See the License for the specific language governing permissions and
|
---|
410 | * limitations under the License.
|
---|
411 | */
|
---|
412 | function resolve$1(input, base) {
|
---|
413 | // The base is always treated as a directory, if it's not empty.
|
---|
414 | // https://github.com/mozilla/source-map/blob/8cb3ee57/lib/util.js#L327
|
---|
415 | // https://github.com/chromium/chromium/blob/da4adbb3/third_party/blink/renderer/devtools/front_end/sdk/SourceMap.js#L400-L401
|
---|
416 | if (base && !base.endsWith('/'))
|
---|
417 | base += '/';
|
---|
418 | return resolve(input, base);
|
---|
419 | }
|
---|
420 |
|
---|
421 | /**
|
---|
422 | * Copyright 2019 The AMP HTML Authors. All Rights Reserved.
|
---|
423 | *
|
---|
424 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
425 | * you may not use this file except in compliance with the License.
|
---|
426 | * You may obtain a copy of the License at
|
---|
427 | *
|
---|
428 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
429 | *
|
---|
430 | * Unless required by applicable law or agreed to in writing, software
|
---|
431 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
432 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
433 | * See the License for the specific language governing permissions and
|
---|
434 | * limitations under the License.
|
---|
435 | */
|
---|
436 | /**
|
---|
437 | * A binary search implementation that returns the index if a match is found,
|
---|
438 | * or the negated index of where the `needle` should be inserted.
|
---|
439 | *
|
---|
440 | * The `comparator` callback receives both the `item` under comparison and the
|
---|
441 | * needle we are searching for. It must return `0` if the `item` is a match,
|
---|
442 | * any negative number if `item` is too small (and we must search after it), or
|
---|
443 | * any positive number if the `item` is too large (and we must search before
|
---|
444 | * it).
|
---|
445 | *
|
---|
446 | * If no match is found, a negated index of where to insert the `needle` is
|
---|
447 | * returned. This negated index is guaranteed to be less than 0. To insert an
|
---|
448 | * item, negate it (again) and splice:
|
---|
449 | *
|
---|
450 | * ```js
|
---|
451 | * const array = [1, 3];
|
---|
452 | * const needle = 2;
|
---|
453 | * const index = binarySearch(array, needle, (item, needle) => item - needle);
|
---|
454 | *
|
---|
455 | * assert.equal(index, -2);
|
---|
456 | * assert.equal(~index, 1);
|
---|
457 | * array.splice(~index, 0, needle);
|
---|
458 | * assert.deepEqual(array, [1, 2, 3]);
|
---|
459 | * ```
|
---|
460 | */
|
---|
461 | function binarySearch(haystack, needle, comparator, low, high) {
|
---|
462 | low = Math.max(low, 0);
|
---|
463 | while (low <= high) {
|
---|
464 | const mid = low + ((high - low) >> 1);
|
---|
465 | const cmp = comparator(haystack[mid], needle);
|
---|
466 | if (cmp === 0) {
|
---|
467 | return mid;
|
---|
468 | }
|
---|
469 | if (cmp < 0) {
|
---|
470 | low = mid + 1;
|
---|
471 | }
|
---|
472 | else {
|
---|
473 | high = mid - 1;
|
---|
474 | }
|
---|
475 | }
|
---|
476 | return ~low;
|
---|
477 | }
|
---|
478 |
|
---|
479 | /**
|
---|
480 | * Copyright 2019 The AMP HTML Authors. All Rights Reserved.
|
---|
481 | *
|
---|
482 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
483 | * you may not use this file except in compliance with the License.
|
---|
484 | * You may obtain a copy of the License at
|
---|
485 | *
|
---|
486 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
487 | *
|
---|
488 | * Unless required by applicable law or agreed to in writing, software
|
---|
489 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
490 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
491 | * See the License for the specific language governing permissions and
|
---|
492 | * limitations under the License.
|
---|
493 | */
|
---|
494 | /**
|
---|
495 | * FastStringArray acts like a `Set` (allowing only one occurrence of a string
|
---|
496 | * `key`), but provides the index of the `key` in the backing array.
|
---|
497 | *
|
---|
498 | * This is designed to allow synchronizing a second array with the contents of
|
---|
499 | * the backing array, like how `sourcesContent[i]` is the source content
|
---|
500 | * associated with `source[i]`, and there are never duplicates.
|
---|
501 | */
|
---|
502 | class FastStringArray {
|
---|
503 | constructor() {
|
---|
504 | this.indexes = Object.create(null);
|
---|
505 | this.array = [];
|
---|
506 | }
|
---|
507 | /**
|
---|
508 | * Puts `key` into the backing array, if it is not already present. Returns
|
---|
509 | * the index of the `key` in the backing array.
|
---|
510 | */
|
---|
511 | put(key) {
|
---|
512 | const { array, indexes } = this;
|
---|
513 | // The key may or may not be present. If it is present, it's a number.
|
---|
514 | let index = indexes[key];
|
---|
515 | // If it's not yet present, we need to insert it and track the index in the
|
---|
516 | // indexes.
|
---|
517 | if (index === undefined) {
|
---|
518 | index = indexes[key] = array.length;
|
---|
519 | array.push(key);
|
---|
520 | }
|
---|
521 | return index;
|
---|
522 | }
|
---|
523 | }
|
---|
524 |
|
---|
525 | /**
|
---|
526 | * Copyright 2019 The AMP HTML Authors. All Rights Reserved.
|
---|
527 | *
|
---|
528 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
529 | * you may not use this file except in compliance with the License.
|
---|
530 | * You may obtain a copy of the License at
|
---|
531 | *
|
---|
532 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
533 | *
|
---|
534 | * Unless required by applicable law or agreed to in writing, software
|
---|
535 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
536 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
537 | * See the License for the specific language governing permissions and
|
---|
538 | * limitations under the License.
|
---|
539 | */
|
---|
540 | /**
|
---|
541 | * SourceMapTree represents a single sourcemap, with the ability to trace
|
---|
542 | * mappings into its child nodes (which may themselves be SourceMapTrees).
|
---|
543 | */
|
---|
544 | class SourceMapTree {
|
---|
545 | constructor(map, sources) {
|
---|
546 | this.map = map;
|
---|
547 | this.sources = sources;
|
---|
548 | this.lastLine = 0;
|
---|
549 | this.lastColumn = 0;
|
---|
550 | this.lastIndex = 0;
|
---|
551 | }
|
---|
552 | /**
|
---|
553 | * traceMappings is only called on the root level SourceMapTree, and begins
|
---|
554 | * the process of resolving each mapping in terms of the original source
|
---|
555 | * files.
|
---|
556 | */
|
---|
557 | traceMappings() {
|
---|
558 | const mappings = [];
|
---|
559 | const names = new FastStringArray();
|
---|
560 | const sources = new FastStringArray();
|
---|
561 | const sourcesContent = [];
|
---|
562 | const { mappings: rootMappings, names: rootNames } = this.map;
|
---|
563 | for (let i = 0; i < rootMappings.length; i++) {
|
---|
564 | const segments = rootMappings[i];
|
---|
565 | const tracedSegments = [];
|
---|
566 | let lastTraced = undefined;
|
---|
567 | for (let j = 0; j < segments.length; j++) {
|
---|
568 | const segment = segments[j];
|
---|
569 | // 1-length segments only move the current generated column, there's no
|
---|
570 | // source information to gather from it.
|
---|
571 | if (segment.length === 1)
|
---|
572 | continue;
|
---|
573 | const source = this.sources[segment[1]];
|
---|
574 | const traced = source.traceSegment(segment[2], segment[3], segment.length === 5 ? rootNames[segment[4]] : '');
|
---|
575 | if (!traced)
|
---|
576 | continue;
|
---|
577 | // So we traced a segment down into its original source file. Now push a
|
---|
578 | // new segment pointing to this location.
|
---|
579 | const { column, line, name } = traced;
|
---|
580 | const { content, filename } = traced.source;
|
---|
581 | // Store the source location, and ensure we keep sourcesContent up to
|
---|
582 | // date with the sources array.
|
---|
583 | const sourceIndex = sources.put(filename);
|
---|
584 | sourcesContent[sourceIndex] = content;
|
---|
585 | if (lastTraced &&
|
---|
586 | lastTraced[1] === sourceIndex &&
|
---|
587 | lastTraced[2] === line &&
|
---|
588 | lastTraced[3] === column) {
|
---|
589 | // This is a duplicate mapping pointing at the exact same starting point in the source file.
|
---|
590 | // It doesn't provide any new information, and only bloats the sourcemap.
|
---|
591 | continue;
|
---|
592 | }
|
---|
593 | // This looks like unnecessary duplication, but it noticeably increases
|
---|
594 | // performance. If we were to push the nameIndex onto length-4 array, v8
|
---|
595 | // would internally allocate 22 slots! That's 68 wasted bytes! Array
|
---|
596 | // literals have the same capacity as their length, saving memory.
|
---|
597 | if (name) {
|
---|
598 | lastTraced = [segment[0], sourceIndex, line, column, names.put(name)];
|
---|
599 | }
|
---|
600 | else {
|
---|
601 | lastTraced = [segment[0], sourceIndex, line, column];
|
---|
602 | }
|
---|
603 | tracedSegments.push(lastTraced);
|
---|
604 | }
|
---|
605 | mappings.push(tracedSegments);
|
---|
606 | }
|
---|
607 | // TODO: Make all sources relative to the sourceRoot.
|
---|
608 | return defaults({
|
---|
609 | mappings,
|
---|
610 | names: names.array,
|
---|
611 | sources: sources.array,
|
---|
612 | sourcesContent,
|
---|
613 | }, this.map);
|
---|
614 | }
|
---|
615 | /**
|
---|
616 | * traceSegment is only called on children SourceMapTrees. It recurses down
|
---|
617 | * into its own child SourceMapTrees, until we find the original source map.
|
---|
618 | */
|
---|
619 | traceSegment(line, column, name) {
|
---|
620 | const { mappings, names } = this.map;
|
---|
621 | // It's common for parent sourcemaps to have pointers to lines that have no
|
---|
622 | // mapping (like a "//# sourceMappingURL=") at the end of the child file.
|
---|
623 | if (line >= mappings.length)
|
---|
624 | return null;
|
---|
625 | const segments = mappings[line];
|
---|
626 | if (segments.length === 0)
|
---|
627 | return null;
|
---|
628 | let low = 0;
|
---|
629 | let high = segments.length - 1;
|
---|
630 | if (line === this.lastLine) {
|
---|
631 | if (column >= this.lastColumn) {
|
---|
632 | low = this.lastIndex;
|
---|
633 | }
|
---|
634 | else {
|
---|
635 | high = this.lastIndex;
|
---|
636 | }
|
---|
637 | }
|
---|
638 | let index = binarySearch(segments, column, segmentComparator$1, low, high);
|
---|
639 | this.lastLine = line;
|
---|
640 | this.lastColumn = column;
|
---|
641 | if (index === -1) {
|
---|
642 | this.lastIndex = index;
|
---|
643 | return null; // we come before any mapped segment
|
---|
644 | }
|
---|
645 | // If we can't find a segment that lines up to this column, we use the
|
---|
646 | // segment before.
|
---|
647 | if (index < 0) {
|
---|
648 | index = ~index - 1;
|
---|
649 | }
|
---|
650 | this.lastIndex = index;
|
---|
651 | const segment = segments[index];
|
---|
652 | // 1-length segments only move the current generated column, there's no
|
---|
653 | // source information to gather from it.
|
---|
654 | if (segment.length === 1)
|
---|
655 | return null;
|
---|
656 | const source = this.sources[segment[1]];
|
---|
657 | // So now we can recurse down, until we hit the original source file.
|
---|
658 | return source.traceSegment(segment[2], segment[3],
|
---|
659 | // A child map's recorded name for this segment takes precedence over the
|
---|
660 | // parent's mapped name. Imagine a mangler changing the name over, etc.
|
---|
661 | segment.length === 5 ? names[segment[4]] : name);
|
---|
662 | }
|
---|
663 | }
|
---|
664 | function segmentComparator$1(segment, column) {
|
---|
665 | return segment[0] - column;
|
---|
666 | }
|
---|
667 |
|
---|
668 | /**
|
---|
669 | * Copyright 2019 The AMP HTML Authors. All Rights Reserved.
|
---|
670 | *
|
---|
671 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
672 | * you may not use this file except in compliance with the License.
|
---|
673 | * You may obtain a copy of the License at
|
---|
674 | *
|
---|
675 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
676 | *
|
---|
677 | * Unless required by applicable law or agreed to in writing, software
|
---|
678 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
679 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
680 | * See the License for the specific language governing permissions and
|
---|
681 | * limitations under the License.
|
---|
682 | */
|
---|
683 | /**
|
---|
684 | * Removes the filename from a path.
|
---|
685 | */
|
---|
686 | function stripFilename(path) {
|
---|
687 | if (!path)
|
---|
688 | return '';
|
---|
689 | const index = path.lastIndexOf('/');
|
---|
690 | return path.slice(0, index + 1);
|
---|
691 | }
|
---|
692 |
|
---|
693 | /**
|
---|
694 | * Copyright 2019 The AMP HTML Authors. All Rights Reserved.
|
---|
695 | *
|
---|
696 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
697 | * you may not use this file except in compliance with the License.
|
---|
698 | * You may obtain a copy of the License at
|
---|
699 | *
|
---|
700 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
701 | *
|
---|
702 | * Unless required by applicable law or agreed to in writing, software
|
---|
703 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
704 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
705 | * See the License for the specific language governing permissions and
|
---|
706 | * limitations under the License.
|
---|
707 | */
|
---|
708 | function asArray(value) {
|
---|
709 | if (Array.isArray(value))
|
---|
710 | return value;
|
---|
711 | return [value];
|
---|
712 | }
|
---|
713 | /**
|
---|
714 | * Recursively builds a tree structure out of sourcemap files, with each node
|
---|
715 | * being either an `OriginalSource` "leaf" or a `SourceMapTree` composed of
|
---|
716 | * `OriginalSource`s and `SourceMapTree`s.
|
---|
717 | *
|
---|
718 | * Every sourcemap is composed of a collection of source files and mappings
|
---|
719 | * into locations of those source files. When we generate a `SourceMapTree` for
|
---|
720 | * the sourcemap, we attempt to load each source file's own sourcemap. If it
|
---|
721 | * does not have an associated sourcemap, it is considered an original,
|
---|
722 | * unmodified source file.
|
---|
723 | */
|
---|
724 | function buildSourceMapTree(input, loader, relativeRoot) {
|
---|
725 | const maps = asArray(input).map(decodeSourceMap);
|
---|
726 | const map = maps.pop();
|
---|
727 | for (let i = 0; i < maps.length; i++) {
|
---|
728 | if (maps[i].sources.length !== 1) {
|
---|
729 | throw new Error(`Transformation map ${i} must have exactly one source file.\n` +
|
---|
730 | 'Did you specify these with the most recent transformation maps first?');
|
---|
731 | }
|
---|
732 | }
|
---|
733 | const { sourceRoot, sources, sourcesContent } = map;
|
---|
734 | const children = sources.map((sourceFile, i) => {
|
---|
735 | // Each source file is loaded relative to the sourcemap's own sourceRoot,
|
---|
736 | // which is itself relative to the sourcemap's parent.
|
---|
737 | const uri = resolve$1(sourceFile || '', resolve$1(sourceRoot || '', stripFilename(relativeRoot)));
|
---|
738 | // Use the provided loader callback to retrieve the file's sourcemap.
|
---|
739 | // TODO: We should eventually support async loading of sourcemap files.
|
---|
740 | const sourceMap = loader(uri);
|
---|
741 | // If there is no sourcemap, then it is an unmodified source file.
|
---|
742 | if (!sourceMap) {
|
---|
743 | // The source file's actual contents must be included in the sourcemap
|
---|
744 | // (done when generating the sourcemap) for it to be included as a
|
---|
745 | // sourceContent in the output sourcemap.
|
---|
746 | const sourceContent = sourcesContent ? sourcesContent[i] : null;
|
---|
747 | return new OriginalSource(uri, sourceContent);
|
---|
748 | }
|
---|
749 | // Else, it's a real sourcemap, and we need to recurse into it to load its
|
---|
750 | // source files.
|
---|
751 | return buildSourceMapTree(decodeSourceMap(sourceMap), loader, uri);
|
---|
752 | });
|
---|
753 | let tree = new SourceMapTree(map, children);
|
---|
754 | for (let i = maps.length - 1; i >= 0; i--) {
|
---|
755 | tree = new SourceMapTree(maps[i], [tree]);
|
---|
756 | }
|
---|
757 | return tree;
|
---|
758 | }
|
---|
759 |
|
---|
760 | /**
|
---|
761 | * Copyright 2019 The AMP HTML Authors. All Rights Reserved.
|
---|
762 | *
|
---|
763 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
764 | * you may not use this file except in compliance with the License.
|
---|
765 | * You may obtain a copy of the License at
|
---|
766 | *
|
---|
767 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
768 | *
|
---|
769 | * Unless required by applicable law or agreed to in writing, software
|
---|
770 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
771 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
772 | * See the License for the specific language governing permissions and
|
---|
773 | * limitations under the License.
|
---|
774 | */
|
---|
775 | /**
|
---|
776 | * A SourceMap v3 compatible sourcemap, which only includes fields that were
|
---|
777 | * provided to it.
|
---|
778 | */
|
---|
779 | class SourceMap {
|
---|
780 | constructor(map, options) {
|
---|
781 | this.version = 3; // SourceMap spec says this should be first.
|
---|
782 | if ('file' in map)
|
---|
783 | this.file = map.file;
|
---|
784 | this.mappings = options.decodedMappings ? map.mappings : encode(map.mappings);
|
---|
785 | this.names = map.names;
|
---|
786 | // TODO: We first need to make all source URIs relative to the sourceRoot
|
---|
787 | // before we can support a sourceRoot.
|
---|
788 | // if ('sourceRoot' in map) this.sourceRoot = map.sourceRoot;
|
---|
789 | this.sources = map.sources;
|
---|
790 | if (!options.excludeContent && 'sourcesContent' in map) {
|
---|
791 | this.sourcesContent = map.sourcesContent;
|
---|
792 | }
|
---|
793 | }
|
---|
794 | toString() {
|
---|
795 | return JSON.stringify(this);
|
---|
796 | }
|
---|
797 | }
|
---|
798 |
|
---|
799 | /**
|
---|
800 | * Copyright 2019 The AMP HTML Authors. All Rights Reserved.
|
---|
801 | *
|
---|
802 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
803 | * you may not use this file except in compliance with the License.
|
---|
804 | * You may obtain a copy of the License at
|
---|
805 | *
|
---|
806 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
807 | *
|
---|
808 | * Unless required by applicable law or agreed to in writing, software
|
---|
809 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
810 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
811 | * See the License for the specific language governing permissions and
|
---|
812 | * limitations under the License.
|
---|
813 | */
|
---|
814 | /**
|
---|
815 | * Traces through all the mappings in the root sourcemap, through the sources
|
---|
816 | * (and their sourcemaps), all the way back to the original source location.
|
---|
817 | *
|
---|
818 | * `loader` will be called every time we encounter a source file. If it returns
|
---|
819 | * a sourcemap, we will recurse into that sourcemap to continue the trace. If
|
---|
820 | * it returns a falsey value, that source file is treated as an original,
|
---|
821 | * unmodified source file.
|
---|
822 | *
|
---|
823 | * Pass `excludeContent` to exclude any self-containing source file content
|
---|
824 | * from the output sourcemap.
|
---|
825 | *
|
---|
826 | * Pass `decodedMappings` to receive a SourceMap with decoded (instead of
|
---|
827 | * VLQ encoded) mappings.
|
---|
828 | */
|
---|
829 | function remapping(input, loader, options) {
|
---|
830 | const opts = typeof options === 'object' ? options : { excludeContent: !!options, decodedMappings: false };
|
---|
831 | const graph = buildSourceMapTree(input, loader);
|
---|
832 | return new SourceMap(graph.traceMappings(), opts);
|
---|
833 | }
|
---|
834 |
|
---|
835 | return remapping;
|
---|
836 |
|
---|
837 | })));
|
---|
838 | //# sourceMappingURL=remapping.umd.js.map
|
---|