1 | (function (global, factory) {
|
---|
2 | typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
---|
3 | typeof define === 'function' && define.amd ? define(factory) :
|
---|
4 | (global = global || self, global.MagicString = factory());
|
---|
5 | }(this, function () { 'use strict';
|
---|
6 |
|
---|
7 | var BitSet = function BitSet(arg) {
|
---|
8 | this.bits = arg instanceof BitSet ? arg.bits.slice() : [];
|
---|
9 | };
|
---|
10 |
|
---|
11 | BitSet.prototype.add = function add (n) {
|
---|
12 | this.bits[n >> 5] |= 1 << (n & 31);
|
---|
13 | };
|
---|
14 |
|
---|
15 | BitSet.prototype.has = function has (n) {
|
---|
16 | return !!(this.bits[n >> 5] & (1 << (n & 31)));
|
---|
17 | };
|
---|
18 |
|
---|
19 | var Chunk = function Chunk(start, end, content) {
|
---|
20 | this.start = start;
|
---|
21 | this.end = end;
|
---|
22 | this.original = content;
|
---|
23 |
|
---|
24 | this.intro = '';
|
---|
25 | this.outro = '';
|
---|
26 |
|
---|
27 | this.content = content;
|
---|
28 | this.storeName = false;
|
---|
29 | this.edited = false;
|
---|
30 |
|
---|
31 | // we make these non-enumerable, for sanity while debugging
|
---|
32 | Object.defineProperties(this, {
|
---|
33 | previous: { writable: true, value: null },
|
---|
34 | next: { writable: true, value: null }
|
---|
35 | });
|
---|
36 | };
|
---|
37 |
|
---|
38 | Chunk.prototype.appendLeft = function appendLeft (content) {
|
---|
39 | this.outro += content;
|
---|
40 | };
|
---|
41 |
|
---|
42 | Chunk.prototype.appendRight = function appendRight (content) {
|
---|
43 | this.intro = this.intro + content;
|
---|
44 | };
|
---|
45 |
|
---|
46 | Chunk.prototype.clone = function clone () {
|
---|
47 | var chunk = new Chunk(this.start, this.end, this.original);
|
---|
48 |
|
---|
49 | chunk.intro = this.intro;
|
---|
50 | chunk.outro = this.outro;
|
---|
51 | chunk.content = this.content;
|
---|
52 | chunk.storeName = this.storeName;
|
---|
53 | chunk.edited = this.edited;
|
---|
54 |
|
---|
55 | return chunk;
|
---|
56 | };
|
---|
57 |
|
---|
58 | Chunk.prototype.contains = function contains (index) {
|
---|
59 | return this.start < index && index < this.end;
|
---|
60 | };
|
---|
61 |
|
---|
62 | Chunk.prototype.eachNext = function eachNext (fn) {
|
---|
63 | var chunk = this;
|
---|
64 | while (chunk) {
|
---|
65 | fn(chunk);
|
---|
66 | chunk = chunk.next;
|
---|
67 | }
|
---|
68 | };
|
---|
69 |
|
---|
70 | Chunk.prototype.eachPrevious = function eachPrevious (fn) {
|
---|
71 | var chunk = this;
|
---|
72 | while (chunk) {
|
---|
73 | fn(chunk);
|
---|
74 | chunk = chunk.previous;
|
---|
75 | }
|
---|
76 | };
|
---|
77 |
|
---|
78 | Chunk.prototype.edit = function edit (content, storeName, contentOnly) {
|
---|
79 | this.content = content;
|
---|
80 | if (!contentOnly) {
|
---|
81 | this.intro = '';
|
---|
82 | this.outro = '';
|
---|
83 | }
|
---|
84 | this.storeName = storeName;
|
---|
85 |
|
---|
86 | this.edited = true;
|
---|
87 |
|
---|
88 | return this;
|
---|
89 | };
|
---|
90 |
|
---|
91 | Chunk.prototype.prependLeft = function prependLeft (content) {
|
---|
92 | this.outro = content + this.outro;
|
---|
93 | };
|
---|
94 |
|
---|
95 | Chunk.prototype.prependRight = function prependRight (content) {
|
---|
96 | this.intro = content + this.intro;
|
---|
97 | };
|
---|
98 |
|
---|
99 | Chunk.prototype.split = function split (index) {
|
---|
100 | var sliceIndex = index - this.start;
|
---|
101 |
|
---|
102 | var originalBefore = this.original.slice(0, sliceIndex);
|
---|
103 | var originalAfter = this.original.slice(sliceIndex);
|
---|
104 |
|
---|
105 | this.original = originalBefore;
|
---|
106 |
|
---|
107 | var newChunk = new Chunk(index, this.end, originalAfter);
|
---|
108 | newChunk.outro = this.outro;
|
---|
109 | this.outro = '';
|
---|
110 |
|
---|
111 | this.end = index;
|
---|
112 |
|
---|
113 | if (this.edited) {
|
---|
114 | // TODO is this block necessary?...
|
---|
115 | newChunk.edit('', false);
|
---|
116 | this.content = '';
|
---|
117 | } else {
|
---|
118 | this.content = originalBefore;
|
---|
119 | }
|
---|
120 |
|
---|
121 | newChunk.next = this.next;
|
---|
122 | if (newChunk.next) { newChunk.next.previous = newChunk; }
|
---|
123 | newChunk.previous = this;
|
---|
124 | this.next = newChunk;
|
---|
125 |
|
---|
126 | return newChunk;
|
---|
127 | };
|
---|
128 |
|
---|
129 | Chunk.prototype.toString = function toString () {
|
---|
130 | return this.intro + this.content + this.outro;
|
---|
131 | };
|
---|
132 |
|
---|
133 | Chunk.prototype.trimEnd = function trimEnd (rx) {
|
---|
134 | this.outro = this.outro.replace(rx, '');
|
---|
135 | if (this.outro.length) { return true; }
|
---|
136 |
|
---|
137 | var trimmed = this.content.replace(rx, '');
|
---|
138 |
|
---|
139 | if (trimmed.length) {
|
---|
140 | if (trimmed !== this.content) {
|
---|
141 | this.split(this.start + trimmed.length).edit('', undefined, true);
|
---|
142 | }
|
---|
143 | return true;
|
---|
144 |
|
---|
145 | } else {
|
---|
146 | this.edit('', undefined, true);
|
---|
147 |
|
---|
148 | this.intro = this.intro.replace(rx, '');
|
---|
149 | if (this.intro.length) { return true; }
|
---|
150 | }
|
---|
151 | };
|
---|
152 |
|
---|
153 | Chunk.prototype.trimStart = function trimStart (rx) {
|
---|
154 | this.intro = this.intro.replace(rx, '');
|
---|
155 | if (this.intro.length) { return true; }
|
---|
156 |
|
---|
157 | var trimmed = this.content.replace(rx, '');
|
---|
158 |
|
---|
159 | if (trimmed.length) {
|
---|
160 | if (trimmed !== this.content) {
|
---|
161 | this.split(this.end - trimmed.length);
|
---|
162 | this.edit('', undefined, true);
|
---|
163 | }
|
---|
164 | return true;
|
---|
165 |
|
---|
166 | } else {
|
---|
167 | this.edit('', undefined, true);
|
---|
168 |
|
---|
169 | this.outro = this.outro.replace(rx, '');
|
---|
170 | if (this.outro.length) { return true; }
|
---|
171 | }
|
---|
172 | };
|
---|
173 |
|
---|
174 | var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
|
---|
175 | function encode(decoded) {
|
---|
176 | var sourceFileIndex = 0; // second field
|
---|
177 | var sourceCodeLine = 0; // third field
|
---|
178 | var sourceCodeColumn = 0; // fourth field
|
---|
179 | var nameIndex = 0; // fifth field
|
---|
180 | var mappings = '';
|
---|
181 | for (var i = 0; i < decoded.length; i++) {
|
---|
182 | var line = decoded[i];
|
---|
183 | if (i > 0)
|
---|
184 | mappings += ';';
|
---|
185 | if (line.length === 0)
|
---|
186 | continue;
|
---|
187 | var generatedCodeColumn = 0; // first field
|
---|
188 | var lineMappings = [];
|
---|
189 | for (var _i = 0, line_1 = line; _i < line_1.length; _i++) {
|
---|
190 | var segment = line_1[_i];
|
---|
191 | var segmentMappings = encodeInteger(segment[0] - generatedCodeColumn);
|
---|
192 | generatedCodeColumn = segment[0];
|
---|
193 | if (segment.length > 1) {
|
---|
194 | segmentMappings +=
|
---|
195 | encodeInteger(segment[1] - sourceFileIndex) +
|
---|
196 | encodeInteger(segment[2] - sourceCodeLine) +
|
---|
197 | encodeInteger(segment[3] - sourceCodeColumn);
|
---|
198 | sourceFileIndex = segment[1];
|
---|
199 | sourceCodeLine = segment[2];
|
---|
200 | sourceCodeColumn = segment[3];
|
---|
201 | }
|
---|
202 | if (segment.length === 5) {
|
---|
203 | segmentMappings += encodeInteger(segment[4] - nameIndex);
|
---|
204 | nameIndex = segment[4];
|
---|
205 | }
|
---|
206 | lineMappings.push(segmentMappings);
|
---|
207 | }
|
---|
208 | mappings += lineMappings.join(',');
|
---|
209 | }
|
---|
210 | return mappings;
|
---|
211 | }
|
---|
212 | function encodeInteger(num) {
|
---|
213 | var result = '';
|
---|
214 | num = num < 0 ? (-num << 1) | 1 : num << 1;
|
---|
215 | do {
|
---|
216 | var clamped = num & 31;
|
---|
217 | num >>= 5;
|
---|
218 | if (num > 0) {
|
---|
219 | clamped |= 32;
|
---|
220 | }
|
---|
221 | result += chars[clamped];
|
---|
222 | } while (num > 0);
|
---|
223 | return result;
|
---|
224 | }
|
---|
225 |
|
---|
226 | var btoa = function () {
|
---|
227 | throw new Error('Unsupported environment: `window.btoa` or `Buffer` should be supported.');
|
---|
228 | };
|
---|
229 | if (typeof window !== 'undefined' && typeof window.btoa === 'function') {
|
---|
230 | btoa = function (str) { return window.btoa(unescape(encodeURIComponent(str))); };
|
---|
231 | } else if (typeof Buffer === 'function') {
|
---|
232 | btoa = function (str) { return Buffer.from(str, 'utf-8').toString('base64'); };
|
---|
233 | }
|
---|
234 |
|
---|
235 | var SourceMap = function SourceMap(properties) {
|
---|
236 | this.version = 3;
|
---|
237 | this.file = properties.file;
|
---|
238 | this.sources = properties.sources;
|
---|
239 | this.sourcesContent = properties.sourcesContent;
|
---|
240 | this.names = properties.names;
|
---|
241 | this.mappings = encode(properties.mappings);
|
---|
242 | };
|
---|
243 |
|
---|
244 | SourceMap.prototype.toString = function toString () {
|
---|
245 | return JSON.stringify(this);
|
---|
246 | };
|
---|
247 |
|
---|
248 | SourceMap.prototype.toUrl = function toUrl () {
|
---|
249 | return 'data:application/json;charset=utf-8;base64,' + btoa(this.toString());
|
---|
250 | };
|
---|
251 |
|
---|
252 | function guessIndent(code) {
|
---|
253 | var lines = code.split('\n');
|
---|
254 |
|
---|
255 | var tabbed = lines.filter(function (line) { return /^\t+/.test(line); });
|
---|
256 | var spaced = lines.filter(function (line) { return /^ {2,}/.test(line); });
|
---|
257 |
|
---|
258 | if (tabbed.length === 0 && spaced.length === 0) {
|
---|
259 | return null;
|
---|
260 | }
|
---|
261 |
|
---|
262 | // More lines tabbed than spaced? Assume tabs, and
|
---|
263 | // default to tabs in the case of a tie (or nothing
|
---|
264 | // to go on)
|
---|
265 | if (tabbed.length >= spaced.length) {
|
---|
266 | return '\t';
|
---|
267 | }
|
---|
268 |
|
---|
269 | // Otherwise, we need to guess the multiple
|
---|
270 | var min = spaced.reduce(function (previous, current) {
|
---|
271 | var numSpaces = /^ +/.exec(current)[0].length;
|
---|
272 | return Math.min(numSpaces, previous);
|
---|
273 | }, Infinity);
|
---|
274 |
|
---|
275 | return new Array(min + 1).join(' ');
|
---|
276 | }
|
---|
277 |
|
---|
278 | function getRelativePath(from, to) {
|
---|
279 | var fromParts = from.split(/[/\\]/);
|
---|
280 | var toParts = to.split(/[/\\]/);
|
---|
281 |
|
---|
282 | fromParts.pop(); // get dirname
|
---|
283 |
|
---|
284 | while (fromParts[0] === toParts[0]) {
|
---|
285 | fromParts.shift();
|
---|
286 | toParts.shift();
|
---|
287 | }
|
---|
288 |
|
---|
289 | if (fromParts.length) {
|
---|
290 | var i = fromParts.length;
|
---|
291 | while (i--) { fromParts[i] = '..'; }
|
---|
292 | }
|
---|
293 |
|
---|
294 | return fromParts.concat(toParts).join('/');
|
---|
295 | }
|
---|
296 |
|
---|
297 | var toString = Object.prototype.toString;
|
---|
298 |
|
---|
299 | function isObject(thing) {
|
---|
300 | return toString.call(thing) === '[object Object]';
|
---|
301 | }
|
---|
302 |
|
---|
303 | function getLocator(source) {
|
---|
304 | var originalLines = source.split('\n');
|
---|
305 | var lineOffsets = [];
|
---|
306 |
|
---|
307 | for (var i = 0, pos = 0; i < originalLines.length; i++) {
|
---|
308 | lineOffsets.push(pos);
|
---|
309 | pos += originalLines[i].length + 1;
|
---|
310 | }
|
---|
311 |
|
---|
312 | return function locate(index) {
|
---|
313 | var i = 0;
|
---|
314 | var j = lineOffsets.length;
|
---|
315 | while (i < j) {
|
---|
316 | var m = (i + j) >> 1;
|
---|
317 | if (index < lineOffsets[m]) {
|
---|
318 | j = m;
|
---|
319 | } else {
|
---|
320 | i = m + 1;
|
---|
321 | }
|
---|
322 | }
|
---|
323 | var line = i - 1;
|
---|
324 | var column = index - lineOffsets[line];
|
---|
325 | return { line: line, column: column };
|
---|
326 | };
|
---|
327 | }
|
---|
328 |
|
---|
329 | var Mappings = function Mappings(hires) {
|
---|
330 | this.hires = hires;
|
---|
331 | this.generatedCodeLine = 0;
|
---|
332 | this.generatedCodeColumn = 0;
|
---|
333 | this.raw = [];
|
---|
334 | this.rawSegments = this.raw[this.generatedCodeLine] = [];
|
---|
335 | this.pending = null;
|
---|
336 | };
|
---|
337 |
|
---|
338 | Mappings.prototype.addEdit = function addEdit (sourceIndex, content, loc, nameIndex) {
|
---|
339 | if (content.length) {
|
---|
340 | var segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
|
---|
341 | if (nameIndex >= 0) {
|
---|
342 | segment.push(nameIndex);
|
---|
343 | }
|
---|
344 | this.rawSegments.push(segment);
|
---|
345 | } else if (this.pending) {
|
---|
346 | this.rawSegments.push(this.pending);
|
---|
347 | }
|
---|
348 |
|
---|
349 | this.advance(content);
|
---|
350 | this.pending = null;
|
---|
351 | };
|
---|
352 |
|
---|
353 | Mappings.prototype.addUneditedChunk = function addUneditedChunk (sourceIndex, chunk, original, loc, sourcemapLocations) {
|
---|
354 | var originalCharIndex = chunk.start;
|
---|
355 | var first = true;
|
---|
356 |
|
---|
357 | while (originalCharIndex < chunk.end) {
|
---|
358 | if (this.hires || first || sourcemapLocations.has(originalCharIndex)) {
|
---|
359 | this.rawSegments.push([this.generatedCodeColumn, sourceIndex, loc.line, loc.column]);
|
---|
360 | }
|
---|
361 |
|
---|
362 | if (original[originalCharIndex] === '\n') {
|
---|
363 | loc.line += 1;
|
---|
364 | loc.column = 0;
|
---|
365 | this.generatedCodeLine += 1;
|
---|
366 | this.raw[this.generatedCodeLine] = this.rawSegments = [];
|
---|
367 | this.generatedCodeColumn = 0;
|
---|
368 | first = true;
|
---|
369 | } else {
|
---|
370 | loc.column += 1;
|
---|
371 | this.generatedCodeColumn += 1;
|
---|
372 | first = false;
|
---|
373 | }
|
---|
374 |
|
---|
375 | originalCharIndex += 1;
|
---|
376 | }
|
---|
377 |
|
---|
378 | this.pending = null;
|
---|
379 | };
|
---|
380 |
|
---|
381 | Mappings.prototype.advance = function advance (str) {
|
---|
382 | if (!str) { return; }
|
---|
383 |
|
---|
384 | var lines = str.split('\n');
|
---|
385 |
|
---|
386 | if (lines.length > 1) {
|
---|
387 | for (var i = 0; i < lines.length - 1; i++) {
|
---|
388 | this.generatedCodeLine++;
|
---|
389 | this.raw[this.generatedCodeLine] = this.rawSegments = [];
|
---|
390 | }
|
---|
391 | this.generatedCodeColumn = 0;
|
---|
392 | }
|
---|
393 |
|
---|
394 | this.generatedCodeColumn += lines[lines.length - 1].length;
|
---|
395 | };
|
---|
396 |
|
---|
397 | var n = '\n';
|
---|
398 |
|
---|
399 | var warned = {
|
---|
400 | insertLeft: false,
|
---|
401 | insertRight: false,
|
---|
402 | storeName: false
|
---|
403 | };
|
---|
404 |
|
---|
405 | var MagicString = function MagicString(string, options) {
|
---|
406 | if ( options === void 0 ) options = {};
|
---|
407 |
|
---|
408 | var chunk = new Chunk(0, string.length, string);
|
---|
409 |
|
---|
410 | Object.defineProperties(this, {
|
---|
411 | original: { writable: true, value: string },
|
---|
412 | outro: { writable: true, value: '' },
|
---|
413 | intro: { writable: true, value: '' },
|
---|
414 | firstChunk: { writable: true, value: chunk },
|
---|
415 | lastChunk: { writable: true, value: chunk },
|
---|
416 | lastSearchedChunk: { writable: true, value: chunk },
|
---|
417 | byStart: { writable: true, value: {} },
|
---|
418 | byEnd: { writable: true, value: {} },
|
---|
419 | filename: { writable: true, value: options.filename },
|
---|
420 | indentExclusionRanges: { writable: true, value: options.indentExclusionRanges },
|
---|
421 | sourcemapLocations: { writable: true, value: new BitSet() },
|
---|
422 | storedNames: { writable: true, value: {} },
|
---|
423 | indentStr: { writable: true, value: guessIndent(string) }
|
---|
424 | });
|
---|
425 |
|
---|
426 | this.byStart[0] = chunk;
|
---|
427 | this.byEnd[string.length] = chunk;
|
---|
428 | };
|
---|
429 |
|
---|
430 | MagicString.prototype.addSourcemapLocation = function addSourcemapLocation (char) {
|
---|
431 | this.sourcemapLocations.add(char);
|
---|
432 | };
|
---|
433 |
|
---|
434 | MagicString.prototype.append = function append (content) {
|
---|
435 | if (typeof content !== 'string') { throw new TypeError('outro content must be a string'); }
|
---|
436 |
|
---|
437 | this.outro += content;
|
---|
438 | return this;
|
---|
439 | };
|
---|
440 |
|
---|
441 | MagicString.prototype.appendLeft = function appendLeft (index, content) {
|
---|
442 | if (typeof content !== 'string') { throw new TypeError('inserted content must be a string'); }
|
---|
443 |
|
---|
444 | this._split(index);
|
---|
445 |
|
---|
446 | var chunk = this.byEnd[index];
|
---|
447 |
|
---|
448 | if (chunk) {
|
---|
449 | chunk.appendLeft(content);
|
---|
450 | } else {
|
---|
451 | this.intro += content;
|
---|
452 | }
|
---|
453 | return this;
|
---|
454 | };
|
---|
455 |
|
---|
456 | MagicString.prototype.appendRight = function appendRight (index, content) {
|
---|
457 | if (typeof content !== 'string') { throw new TypeError('inserted content must be a string'); }
|
---|
458 |
|
---|
459 | this._split(index);
|
---|
460 |
|
---|
461 | var chunk = this.byStart[index];
|
---|
462 |
|
---|
463 | if (chunk) {
|
---|
464 | chunk.appendRight(content);
|
---|
465 | } else {
|
---|
466 | this.outro += content;
|
---|
467 | }
|
---|
468 | return this;
|
---|
469 | };
|
---|
470 |
|
---|
471 | MagicString.prototype.clone = function clone () {
|
---|
472 | var cloned = new MagicString(this.original, { filename: this.filename });
|
---|
473 |
|
---|
474 | var originalChunk = this.firstChunk;
|
---|
475 | var clonedChunk = (cloned.firstChunk = cloned.lastSearchedChunk = originalChunk.clone());
|
---|
476 |
|
---|
477 | while (originalChunk) {
|
---|
478 | cloned.byStart[clonedChunk.start] = clonedChunk;
|
---|
479 | cloned.byEnd[clonedChunk.end] = clonedChunk;
|
---|
480 |
|
---|
481 | var nextOriginalChunk = originalChunk.next;
|
---|
482 | var nextClonedChunk = nextOriginalChunk && nextOriginalChunk.clone();
|
---|
483 |
|
---|
484 | if (nextClonedChunk) {
|
---|
485 | clonedChunk.next = nextClonedChunk;
|
---|
486 | nextClonedChunk.previous = clonedChunk;
|
---|
487 |
|
---|
488 | clonedChunk = nextClonedChunk;
|
---|
489 | }
|
---|
490 |
|
---|
491 | originalChunk = nextOriginalChunk;
|
---|
492 | }
|
---|
493 |
|
---|
494 | cloned.lastChunk = clonedChunk;
|
---|
495 |
|
---|
496 | if (this.indentExclusionRanges) {
|
---|
497 | cloned.indentExclusionRanges = this.indentExclusionRanges.slice();
|
---|
498 | }
|
---|
499 |
|
---|
500 | cloned.sourcemapLocations = new BitSet(this.sourcemapLocations);
|
---|
501 |
|
---|
502 | cloned.intro = this.intro;
|
---|
503 | cloned.outro = this.outro;
|
---|
504 |
|
---|
505 | return cloned;
|
---|
506 | };
|
---|
507 |
|
---|
508 | MagicString.prototype.generateDecodedMap = function generateDecodedMap (options) {
|
---|
509 | var this$1 = this;
|
---|
510 |
|
---|
511 | options = options || {};
|
---|
512 |
|
---|
513 | var sourceIndex = 0;
|
---|
514 | var names = Object.keys(this.storedNames);
|
---|
515 | var mappings = new Mappings(options.hires);
|
---|
516 |
|
---|
517 | var locate = getLocator(this.original);
|
---|
518 |
|
---|
519 | if (this.intro) {
|
---|
520 | mappings.advance(this.intro);
|
---|
521 | }
|
---|
522 |
|
---|
523 | this.firstChunk.eachNext(function (chunk) {
|
---|
524 | var loc = locate(chunk.start);
|
---|
525 |
|
---|
526 | if (chunk.intro.length) { mappings.advance(chunk.intro); }
|
---|
527 |
|
---|
528 | if (chunk.edited) {
|
---|
529 | mappings.addEdit(
|
---|
530 | sourceIndex,
|
---|
531 | chunk.content,
|
---|
532 | loc,
|
---|
533 | chunk.storeName ? names.indexOf(chunk.original) : -1
|
---|
534 | );
|
---|
535 | } else {
|
---|
536 | mappings.addUneditedChunk(sourceIndex, chunk, this$1.original, loc, this$1.sourcemapLocations);
|
---|
537 | }
|
---|
538 |
|
---|
539 | if (chunk.outro.length) { mappings.advance(chunk.outro); }
|
---|
540 | });
|
---|
541 |
|
---|
542 | return {
|
---|
543 | file: options.file ? options.file.split(/[/\\]/).pop() : null,
|
---|
544 | sources: [options.source ? getRelativePath(options.file || '', options.source) : null],
|
---|
545 | sourcesContent: options.includeContent ? [this.original] : [null],
|
---|
546 | names: names,
|
---|
547 | mappings: mappings.raw
|
---|
548 | };
|
---|
549 | };
|
---|
550 |
|
---|
551 | MagicString.prototype.generateMap = function generateMap (options) {
|
---|
552 | return new SourceMap(this.generateDecodedMap(options));
|
---|
553 | };
|
---|
554 |
|
---|
555 | MagicString.prototype.getIndentString = function getIndentString () {
|
---|
556 | return this.indentStr === null ? '\t' : this.indentStr;
|
---|
557 | };
|
---|
558 |
|
---|
559 | MagicString.prototype.indent = function indent (indentStr, options) {
|
---|
560 | var pattern = /^[^\r\n]/gm;
|
---|
561 |
|
---|
562 | if (isObject(indentStr)) {
|
---|
563 | options = indentStr;
|
---|
564 | indentStr = undefined;
|
---|
565 | }
|
---|
566 |
|
---|
567 | indentStr = indentStr !== undefined ? indentStr : this.indentStr || '\t';
|
---|
568 |
|
---|
569 | if (indentStr === '') { return this; } // noop
|
---|
570 |
|
---|
571 | options = options || {};
|
---|
572 |
|
---|
573 | // Process exclusion ranges
|
---|
574 | var isExcluded = {};
|
---|
575 |
|
---|
576 | if (options.exclude) {
|
---|
577 | var exclusions =
|
---|
578 | typeof options.exclude[0] === 'number' ? [options.exclude] : options.exclude;
|
---|
579 | exclusions.forEach(function (exclusion) {
|
---|
580 | for (var i = exclusion[0]; i < exclusion[1]; i += 1) {
|
---|
581 | isExcluded[i] = true;
|
---|
582 | }
|
---|
583 | });
|
---|
584 | }
|
---|
585 |
|
---|
586 | var shouldIndentNextCharacter = options.indentStart !== false;
|
---|
587 | var replacer = function (match) {
|
---|
588 | if (shouldIndentNextCharacter) { return ("" + indentStr + match); }
|
---|
589 | shouldIndentNextCharacter = true;
|
---|
590 | return match;
|
---|
591 | };
|
---|
592 |
|
---|
593 | this.intro = this.intro.replace(pattern, replacer);
|
---|
594 |
|
---|
595 | var charIndex = 0;
|
---|
596 | var chunk = this.firstChunk;
|
---|
597 |
|
---|
598 | while (chunk) {
|
---|
599 | var end = chunk.end;
|
---|
600 |
|
---|
601 | if (chunk.edited) {
|
---|
602 | if (!isExcluded[charIndex]) {
|
---|
603 | chunk.content = chunk.content.replace(pattern, replacer);
|
---|
604 |
|
---|
605 | if (chunk.content.length) {
|
---|
606 | shouldIndentNextCharacter = chunk.content[chunk.content.length - 1] === '\n';
|
---|
607 | }
|
---|
608 | }
|
---|
609 | } else {
|
---|
610 | charIndex = chunk.start;
|
---|
611 |
|
---|
612 | while (charIndex < end) {
|
---|
613 | if (!isExcluded[charIndex]) {
|
---|
614 | var char = this.original[charIndex];
|
---|
615 |
|
---|
616 | if (char === '\n') {
|
---|
617 | shouldIndentNextCharacter = true;
|
---|
618 | } else if (char !== '\r' && shouldIndentNextCharacter) {
|
---|
619 | shouldIndentNextCharacter = false;
|
---|
620 |
|
---|
621 | if (charIndex === chunk.start) {
|
---|
622 | chunk.prependRight(indentStr);
|
---|
623 | } else {
|
---|
624 | this._splitChunk(chunk, charIndex);
|
---|
625 | chunk = chunk.next;
|
---|
626 | chunk.prependRight(indentStr);
|
---|
627 | }
|
---|
628 | }
|
---|
629 | }
|
---|
630 |
|
---|
631 | charIndex += 1;
|
---|
632 | }
|
---|
633 | }
|
---|
634 |
|
---|
635 | charIndex = chunk.end;
|
---|
636 | chunk = chunk.next;
|
---|
637 | }
|
---|
638 |
|
---|
639 | this.outro = this.outro.replace(pattern, replacer);
|
---|
640 |
|
---|
641 | return this;
|
---|
642 | };
|
---|
643 |
|
---|
644 | MagicString.prototype.insert = function insert () {
|
---|
645 | throw new Error('magicString.insert(...) is deprecated. Use prependRight(...) or appendLeft(...)');
|
---|
646 | };
|
---|
647 |
|
---|
648 | MagicString.prototype.insertLeft = function insertLeft (index, content) {
|
---|
649 | if (!warned.insertLeft) {
|
---|
650 | console.warn('magicString.insertLeft(...) is deprecated. Use magicString.appendLeft(...) instead'); // eslint-disable-line no-console
|
---|
651 | warned.insertLeft = true;
|
---|
652 | }
|
---|
653 |
|
---|
654 | return this.appendLeft(index, content);
|
---|
655 | };
|
---|
656 |
|
---|
657 | MagicString.prototype.insertRight = function insertRight (index, content) {
|
---|
658 | if (!warned.insertRight) {
|
---|
659 | console.warn('magicString.insertRight(...) is deprecated. Use magicString.prependRight(...) instead'); // eslint-disable-line no-console
|
---|
660 | warned.insertRight = true;
|
---|
661 | }
|
---|
662 |
|
---|
663 | return this.prependRight(index, content);
|
---|
664 | };
|
---|
665 |
|
---|
666 | MagicString.prototype.move = function move (start, end, index) {
|
---|
667 | if (index >= start && index <= end) { throw new Error('Cannot move a selection inside itself'); }
|
---|
668 |
|
---|
669 | this._split(start);
|
---|
670 | this._split(end);
|
---|
671 | this._split(index);
|
---|
672 |
|
---|
673 | var first = this.byStart[start];
|
---|
674 | var last = this.byEnd[end];
|
---|
675 |
|
---|
676 | var oldLeft = first.previous;
|
---|
677 | var oldRight = last.next;
|
---|
678 |
|
---|
679 | var newRight = this.byStart[index];
|
---|
680 | if (!newRight && last === this.lastChunk) { return this; }
|
---|
681 | var newLeft = newRight ? newRight.previous : this.lastChunk;
|
---|
682 |
|
---|
683 | if (oldLeft) { oldLeft.next = oldRight; }
|
---|
684 | if (oldRight) { oldRight.previous = oldLeft; }
|
---|
685 |
|
---|
686 | if (newLeft) { newLeft.next = first; }
|
---|
687 | if (newRight) { newRight.previous = last; }
|
---|
688 |
|
---|
689 | if (!first.previous) { this.firstChunk = last.next; }
|
---|
690 | if (!last.next) {
|
---|
691 | this.lastChunk = first.previous;
|
---|
692 | this.lastChunk.next = null;
|
---|
693 | }
|
---|
694 |
|
---|
695 | first.previous = newLeft;
|
---|
696 | last.next = newRight || null;
|
---|
697 |
|
---|
698 | if (!newLeft) { this.firstChunk = first; }
|
---|
699 | if (!newRight) { this.lastChunk = last; }
|
---|
700 | return this;
|
---|
701 | };
|
---|
702 |
|
---|
703 | MagicString.prototype.overwrite = function overwrite (start, end, content, options) {
|
---|
704 | if (typeof content !== 'string') { throw new TypeError('replacement content must be a string'); }
|
---|
705 |
|
---|
706 | while (start < 0) { start += this.original.length; }
|
---|
707 | while (end < 0) { end += this.original.length; }
|
---|
708 |
|
---|
709 | if (end > this.original.length) { throw new Error('end is out of bounds'); }
|
---|
710 | if (start === end)
|
---|
711 | { throw new Error('Cannot overwrite a zero-length range – use appendLeft or prependRight instead'); }
|
---|
712 |
|
---|
713 | this._split(start);
|
---|
714 | this._split(end);
|
---|
715 |
|
---|
716 | if (options === true) {
|
---|
717 | if (!warned.storeName) {
|
---|
718 | console.warn('The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string'); // eslint-disable-line no-console
|
---|
719 | warned.storeName = true;
|
---|
720 | }
|
---|
721 |
|
---|
722 | options = { storeName: true };
|
---|
723 | }
|
---|
724 | var storeName = options !== undefined ? options.storeName : false;
|
---|
725 | var contentOnly = options !== undefined ? options.contentOnly : false;
|
---|
726 |
|
---|
727 | if (storeName) {
|
---|
728 | var original = this.original.slice(start, end);
|
---|
729 | this.storedNames[original] = true;
|
---|
730 | }
|
---|
731 |
|
---|
732 | var first = this.byStart[start];
|
---|
733 | var last = this.byEnd[end];
|
---|
734 |
|
---|
735 | if (first) {
|
---|
736 | if (end > first.end && first.next !== this.byStart[first.end]) {
|
---|
737 | throw new Error('Cannot overwrite across a split point');
|
---|
738 | }
|
---|
739 |
|
---|
740 | first.edit(content, storeName, contentOnly);
|
---|
741 |
|
---|
742 | if (first !== last) {
|
---|
743 | var chunk = first.next;
|
---|
744 | while (chunk !== last) {
|
---|
745 | chunk.edit('', false);
|
---|
746 | chunk = chunk.next;
|
---|
747 | }
|
---|
748 |
|
---|
749 | chunk.edit('', false);
|
---|
750 | }
|
---|
751 | } else {
|
---|
752 | // must be inserting at the end
|
---|
753 | var newChunk = new Chunk(start, end, '').edit(content, storeName);
|
---|
754 |
|
---|
755 | // TODO last chunk in the array may not be the last chunk, if it's moved...
|
---|
756 | last.next = newChunk;
|
---|
757 | newChunk.previous = last;
|
---|
758 | }
|
---|
759 | return this;
|
---|
760 | };
|
---|
761 |
|
---|
762 | MagicString.prototype.prepend = function prepend (content) {
|
---|
763 | if (typeof content !== 'string') { throw new TypeError('outro content must be a string'); }
|
---|
764 |
|
---|
765 | this.intro = content + this.intro;
|
---|
766 | return this;
|
---|
767 | };
|
---|
768 |
|
---|
769 | MagicString.prototype.prependLeft = function prependLeft (index, content) {
|
---|
770 | if (typeof content !== 'string') { throw new TypeError('inserted content must be a string'); }
|
---|
771 |
|
---|
772 | this._split(index);
|
---|
773 |
|
---|
774 | var chunk = this.byEnd[index];
|
---|
775 |
|
---|
776 | if (chunk) {
|
---|
777 | chunk.prependLeft(content);
|
---|
778 | } else {
|
---|
779 | this.intro = content + this.intro;
|
---|
780 | }
|
---|
781 | return this;
|
---|
782 | };
|
---|
783 |
|
---|
784 | MagicString.prototype.prependRight = function prependRight (index, content) {
|
---|
785 | if (typeof content !== 'string') { throw new TypeError('inserted content must be a string'); }
|
---|
786 |
|
---|
787 | this._split(index);
|
---|
788 |
|
---|
789 | var chunk = this.byStart[index];
|
---|
790 |
|
---|
791 | if (chunk) {
|
---|
792 | chunk.prependRight(content);
|
---|
793 | } else {
|
---|
794 | this.outro = content + this.outro;
|
---|
795 | }
|
---|
796 | return this;
|
---|
797 | };
|
---|
798 |
|
---|
799 | MagicString.prototype.remove = function remove (start, end) {
|
---|
800 | while (start < 0) { start += this.original.length; }
|
---|
801 | while (end < 0) { end += this.original.length; }
|
---|
802 |
|
---|
803 | if (start === end) { return this; }
|
---|
804 |
|
---|
805 | if (start < 0 || end > this.original.length) { throw new Error('Character is out of bounds'); }
|
---|
806 | if (start > end) { throw new Error('end must be greater than start'); }
|
---|
807 |
|
---|
808 | this._split(start);
|
---|
809 | this._split(end);
|
---|
810 |
|
---|
811 | var chunk = this.byStart[start];
|
---|
812 |
|
---|
813 | while (chunk) {
|
---|
814 | chunk.intro = '';
|
---|
815 | chunk.outro = '';
|
---|
816 | chunk.edit('');
|
---|
817 |
|
---|
818 | chunk = end > chunk.end ? this.byStart[chunk.end] : null;
|
---|
819 | }
|
---|
820 | return this;
|
---|
821 | };
|
---|
822 |
|
---|
823 | MagicString.prototype.lastChar = function lastChar () {
|
---|
824 | if (this.outro.length)
|
---|
825 | { return this.outro[this.outro.length - 1]; }
|
---|
826 | var chunk = this.lastChunk;
|
---|
827 | do {
|
---|
828 | if (chunk.outro.length)
|
---|
829 | { return chunk.outro[chunk.outro.length - 1]; }
|
---|
830 | if (chunk.content.length)
|
---|
831 | { return chunk.content[chunk.content.length - 1]; }
|
---|
832 | if (chunk.intro.length)
|
---|
833 | { return chunk.intro[chunk.intro.length - 1]; }
|
---|
834 | } while (chunk = chunk.previous);
|
---|
835 | if (this.intro.length)
|
---|
836 | { return this.intro[this.intro.length - 1]; }
|
---|
837 | return '';
|
---|
838 | };
|
---|
839 |
|
---|
840 | MagicString.prototype.lastLine = function lastLine () {
|
---|
841 | var lineIndex = this.outro.lastIndexOf(n);
|
---|
842 | if (lineIndex !== -1)
|
---|
843 | { return this.outro.substr(lineIndex + 1); }
|
---|
844 | var lineStr = this.outro;
|
---|
845 | var chunk = this.lastChunk;
|
---|
846 | do {
|
---|
847 | if (chunk.outro.length > 0) {
|
---|
848 | lineIndex = chunk.outro.lastIndexOf(n);
|
---|
849 | if (lineIndex !== -1)
|
---|
850 | { return chunk.outro.substr(lineIndex + 1) + lineStr; }
|
---|
851 | lineStr = chunk.outro + lineStr;
|
---|
852 | }
|
---|
853 |
|
---|
854 | if (chunk.content.length > 0) {
|
---|
855 | lineIndex = chunk.content.lastIndexOf(n);
|
---|
856 | if (lineIndex !== -1)
|
---|
857 | { return chunk.content.substr(lineIndex + 1) + lineStr; }
|
---|
858 | lineStr = chunk.content + lineStr;
|
---|
859 | }
|
---|
860 |
|
---|
861 | if (chunk.intro.length > 0) {
|
---|
862 | lineIndex = chunk.intro.lastIndexOf(n);
|
---|
863 | if (lineIndex !== -1)
|
---|
864 | { return chunk.intro.substr(lineIndex + 1) + lineStr; }
|
---|
865 | lineStr = chunk.intro + lineStr;
|
---|
866 | }
|
---|
867 | } while (chunk = chunk.previous);
|
---|
868 | lineIndex = this.intro.lastIndexOf(n);
|
---|
869 | if (lineIndex !== -1)
|
---|
870 | { return this.intro.substr(lineIndex + 1) + lineStr; }
|
---|
871 | return this.intro + lineStr;
|
---|
872 | };
|
---|
873 |
|
---|
874 | MagicString.prototype.slice = function slice (start, end) {
|
---|
875 | if ( start === void 0 ) start = 0;
|
---|
876 | if ( end === void 0 ) end = this.original.length;
|
---|
877 |
|
---|
878 | while (start < 0) { start += this.original.length; }
|
---|
879 | while (end < 0) { end += this.original.length; }
|
---|
880 |
|
---|
881 | var result = '';
|
---|
882 |
|
---|
883 | // find start chunk
|
---|
884 | var chunk = this.firstChunk;
|
---|
885 | while (chunk && (chunk.start > start || chunk.end <= start)) {
|
---|
886 | // found end chunk before start
|
---|
887 | if (chunk.start < end && chunk.end >= end) {
|
---|
888 | return result;
|
---|
889 | }
|
---|
890 |
|
---|
891 | chunk = chunk.next;
|
---|
892 | }
|
---|
893 |
|
---|
894 | if (chunk && chunk.edited && chunk.start !== start)
|
---|
895 | { throw new Error(("Cannot use replaced character " + start + " as slice start anchor.")); }
|
---|
896 |
|
---|
897 | var startChunk = chunk;
|
---|
898 | while (chunk) {
|
---|
899 | if (chunk.intro && (startChunk !== chunk || chunk.start === start)) {
|
---|
900 | result += chunk.intro;
|
---|
901 | }
|
---|
902 |
|
---|
903 | var containsEnd = chunk.start < end && chunk.end >= end;
|
---|
904 | if (containsEnd && chunk.edited && chunk.end !== end)
|
---|
905 | { throw new Error(("Cannot use replaced character " + end + " as slice end anchor.")); }
|
---|
906 |
|
---|
907 | var sliceStart = startChunk === chunk ? start - chunk.start : 0;
|
---|
908 | var sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length;
|
---|
909 |
|
---|
910 | result += chunk.content.slice(sliceStart, sliceEnd);
|
---|
911 |
|
---|
912 | if (chunk.outro && (!containsEnd || chunk.end === end)) {
|
---|
913 | result += chunk.outro;
|
---|
914 | }
|
---|
915 |
|
---|
916 | if (containsEnd) {
|
---|
917 | break;
|
---|
918 | }
|
---|
919 |
|
---|
920 | chunk = chunk.next;
|
---|
921 | }
|
---|
922 |
|
---|
923 | return result;
|
---|
924 | };
|
---|
925 |
|
---|
926 | // TODO deprecate this? not really very useful
|
---|
927 | MagicString.prototype.snip = function snip (start, end) {
|
---|
928 | var clone = this.clone();
|
---|
929 | clone.remove(0, start);
|
---|
930 | clone.remove(end, clone.original.length);
|
---|
931 |
|
---|
932 | return clone;
|
---|
933 | };
|
---|
934 |
|
---|
935 | MagicString.prototype._split = function _split (index) {
|
---|
936 | if (this.byStart[index] || this.byEnd[index]) { return; }
|
---|
937 |
|
---|
938 | var chunk = this.lastSearchedChunk;
|
---|
939 | var searchForward = index > chunk.end;
|
---|
940 |
|
---|
941 | while (chunk) {
|
---|
942 | if (chunk.contains(index)) { return this._splitChunk(chunk, index); }
|
---|
943 |
|
---|
944 | chunk = searchForward ? this.byStart[chunk.end] : this.byEnd[chunk.start];
|
---|
945 | }
|
---|
946 | };
|
---|
947 |
|
---|
948 | MagicString.prototype._splitChunk = function _splitChunk (chunk, index) {
|
---|
949 | if (chunk.edited && chunk.content.length) {
|
---|
950 | // zero-length edited chunks are a special case (overlapping replacements)
|
---|
951 | var loc = getLocator(this.original)(index);
|
---|
952 | throw new Error(
|
---|
953 | ("Cannot split a chunk that has already been edited (" + (loc.line) + ":" + (loc.column) + " – \"" + (chunk.original) + "\")")
|
---|
954 | );
|
---|
955 | }
|
---|
956 |
|
---|
957 | var newChunk = chunk.split(index);
|
---|
958 |
|
---|
959 | this.byEnd[index] = chunk;
|
---|
960 | this.byStart[index] = newChunk;
|
---|
961 | this.byEnd[newChunk.end] = newChunk;
|
---|
962 |
|
---|
963 | if (chunk === this.lastChunk) { this.lastChunk = newChunk; }
|
---|
964 |
|
---|
965 | this.lastSearchedChunk = chunk;
|
---|
966 | return true;
|
---|
967 | };
|
---|
968 |
|
---|
969 | MagicString.prototype.toString = function toString () {
|
---|
970 | var str = this.intro;
|
---|
971 |
|
---|
972 | var chunk = this.firstChunk;
|
---|
973 | while (chunk) {
|
---|
974 | str += chunk.toString();
|
---|
975 | chunk = chunk.next;
|
---|
976 | }
|
---|
977 |
|
---|
978 | return str + this.outro;
|
---|
979 | };
|
---|
980 |
|
---|
981 | MagicString.prototype.isEmpty = function isEmpty () {
|
---|
982 | var chunk = this.firstChunk;
|
---|
983 | do {
|
---|
984 | if (chunk.intro.length && chunk.intro.trim() ||
|
---|
985 | chunk.content.length && chunk.content.trim() ||
|
---|
986 | chunk.outro.length && chunk.outro.trim())
|
---|
987 | { return false; }
|
---|
988 | } while (chunk = chunk.next);
|
---|
989 | return true;
|
---|
990 | };
|
---|
991 |
|
---|
992 | MagicString.prototype.length = function length () {
|
---|
993 | var chunk = this.firstChunk;
|
---|
994 | var length = 0;
|
---|
995 | do {
|
---|
996 | length += chunk.intro.length + chunk.content.length + chunk.outro.length;
|
---|
997 | } while (chunk = chunk.next);
|
---|
998 | return length;
|
---|
999 | };
|
---|
1000 |
|
---|
1001 | MagicString.prototype.trimLines = function trimLines () {
|
---|
1002 | return this.trim('[\\r\\n]');
|
---|
1003 | };
|
---|
1004 |
|
---|
1005 | MagicString.prototype.trim = function trim (charType) {
|
---|
1006 | return this.trimStart(charType).trimEnd(charType);
|
---|
1007 | };
|
---|
1008 |
|
---|
1009 | MagicString.prototype.trimEndAborted = function trimEndAborted (charType) {
|
---|
1010 | var rx = new RegExp((charType || '\\s') + '+$');
|
---|
1011 |
|
---|
1012 | this.outro = this.outro.replace(rx, '');
|
---|
1013 | if (this.outro.length) { return true; }
|
---|
1014 |
|
---|
1015 | var chunk = this.lastChunk;
|
---|
1016 |
|
---|
1017 | do {
|
---|
1018 | var end = chunk.end;
|
---|
1019 | var aborted = chunk.trimEnd(rx);
|
---|
1020 |
|
---|
1021 | // if chunk was trimmed, we have a new lastChunk
|
---|
1022 | if (chunk.end !== end) {
|
---|
1023 | if (this.lastChunk === chunk) {
|
---|
1024 | this.lastChunk = chunk.next;
|
---|
1025 | }
|
---|
1026 |
|
---|
1027 | this.byEnd[chunk.end] = chunk;
|
---|
1028 | this.byStart[chunk.next.start] = chunk.next;
|
---|
1029 | this.byEnd[chunk.next.end] = chunk.next;
|
---|
1030 | }
|
---|
1031 |
|
---|
1032 | if (aborted) { return true; }
|
---|
1033 | chunk = chunk.previous;
|
---|
1034 | } while (chunk);
|
---|
1035 |
|
---|
1036 | return false;
|
---|
1037 | };
|
---|
1038 |
|
---|
1039 | MagicString.prototype.trimEnd = function trimEnd (charType) {
|
---|
1040 | this.trimEndAborted(charType);
|
---|
1041 | return this;
|
---|
1042 | };
|
---|
1043 | MagicString.prototype.trimStartAborted = function trimStartAborted (charType) {
|
---|
1044 | var rx = new RegExp('^' + (charType || '\\s') + '+');
|
---|
1045 |
|
---|
1046 | this.intro = this.intro.replace(rx, '');
|
---|
1047 | if (this.intro.length) { return true; }
|
---|
1048 |
|
---|
1049 | var chunk = this.firstChunk;
|
---|
1050 |
|
---|
1051 | do {
|
---|
1052 | var end = chunk.end;
|
---|
1053 | var aborted = chunk.trimStart(rx);
|
---|
1054 |
|
---|
1055 | if (chunk.end !== end) {
|
---|
1056 | // special case...
|
---|
1057 | if (chunk === this.lastChunk) { this.lastChunk = chunk.next; }
|
---|
1058 |
|
---|
1059 | this.byEnd[chunk.end] = chunk;
|
---|
1060 | this.byStart[chunk.next.start] = chunk.next;
|
---|
1061 | this.byEnd[chunk.next.end] = chunk.next;
|
---|
1062 | }
|
---|
1063 |
|
---|
1064 | if (aborted) { return true; }
|
---|
1065 | chunk = chunk.next;
|
---|
1066 | } while (chunk);
|
---|
1067 |
|
---|
1068 | return false;
|
---|
1069 | };
|
---|
1070 |
|
---|
1071 | MagicString.prototype.trimStart = function trimStart (charType) {
|
---|
1072 | this.trimStartAborted(charType);
|
---|
1073 | return this;
|
---|
1074 | };
|
---|
1075 |
|
---|
1076 | var hasOwnProp = Object.prototype.hasOwnProperty;
|
---|
1077 |
|
---|
1078 | var Bundle = function Bundle(options) {
|
---|
1079 | if ( options === void 0 ) options = {};
|
---|
1080 |
|
---|
1081 | this.intro = options.intro || '';
|
---|
1082 | this.separator = options.separator !== undefined ? options.separator : '\n';
|
---|
1083 | this.sources = [];
|
---|
1084 | this.uniqueSources = [];
|
---|
1085 | this.uniqueSourceIndexByFilename = {};
|
---|
1086 | };
|
---|
1087 |
|
---|
1088 | Bundle.prototype.addSource = function addSource (source) {
|
---|
1089 | if (source instanceof MagicString) {
|
---|
1090 | return this.addSource({
|
---|
1091 | content: source,
|
---|
1092 | filename: source.filename,
|
---|
1093 | separator: this.separator
|
---|
1094 | });
|
---|
1095 | }
|
---|
1096 |
|
---|
1097 | if (!isObject(source) || !source.content) {
|
---|
1098 | throw new Error('bundle.addSource() takes an object with a `content` property, which should be an instance of MagicString, and an optional `filename`');
|
---|
1099 | }
|
---|
1100 |
|
---|
1101 | ['filename', 'indentExclusionRanges', 'separator'].forEach(function (option) {
|
---|
1102 | if (!hasOwnProp.call(source, option)) { source[option] = source.content[option]; }
|
---|
1103 | });
|
---|
1104 |
|
---|
1105 | if (source.separator === undefined) {
|
---|
1106 | // TODO there's a bunch of this sort of thing, needs cleaning up
|
---|
1107 | source.separator = this.separator;
|
---|
1108 | }
|
---|
1109 |
|
---|
1110 | if (source.filename) {
|
---|
1111 | if (!hasOwnProp.call(this.uniqueSourceIndexByFilename, source.filename)) {
|
---|
1112 | this.uniqueSourceIndexByFilename[source.filename] = this.uniqueSources.length;
|
---|
1113 | this.uniqueSources.push({ filename: source.filename, content: source.content.original });
|
---|
1114 | } else {
|
---|
1115 | var uniqueSource = this.uniqueSources[this.uniqueSourceIndexByFilename[source.filename]];
|
---|
1116 | if (source.content.original !== uniqueSource.content) {
|
---|
1117 | throw new Error(("Illegal source: same filename (" + (source.filename) + "), different contents"));
|
---|
1118 | }
|
---|
1119 | }
|
---|
1120 | }
|
---|
1121 |
|
---|
1122 | this.sources.push(source);
|
---|
1123 | return this;
|
---|
1124 | };
|
---|
1125 |
|
---|
1126 | Bundle.prototype.append = function append (str, options) {
|
---|
1127 | this.addSource({
|
---|
1128 | content: new MagicString(str),
|
---|
1129 | separator: (options && options.separator) || ''
|
---|
1130 | });
|
---|
1131 |
|
---|
1132 | return this;
|
---|
1133 | };
|
---|
1134 |
|
---|
1135 | Bundle.prototype.clone = function clone () {
|
---|
1136 | var bundle = new Bundle({
|
---|
1137 | intro: this.intro,
|
---|
1138 | separator: this.separator
|
---|
1139 | });
|
---|
1140 |
|
---|
1141 | this.sources.forEach(function (source) {
|
---|
1142 | bundle.addSource({
|
---|
1143 | filename: source.filename,
|
---|
1144 | content: source.content.clone(),
|
---|
1145 | separator: source.separator
|
---|
1146 | });
|
---|
1147 | });
|
---|
1148 |
|
---|
1149 | return bundle;
|
---|
1150 | };
|
---|
1151 |
|
---|
1152 | Bundle.prototype.generateDecodedMap = function generateDecodedMap (options) {
|
---|
1153 | var this$1 = this;
|
---|
1154 | if ( options === void 0 ) options = {};
|
---|
1155 |
|
---|
1156 | var names = [];
|
---|
1157 | this.sources.forEach(function (source) {
|
---|
1158 | Object.keys(source.content.storedNames).forEach(function (name) {
|
---|
1159 | if (!~names.indexOf(name)) { names.push(name); }
|
---|
1160 | });
|
---|
1161 | });
|
---|
1162 |
|
---|
1163 | var mappings = new Mappings(options.hires);
|
---|
1164 |
|
---|
1165 | if (this.intro) {
|
---|
1166 | mappings.advance(this.intro);
|
---|
1167 | }
|
---|
1168 |
|
---|
1169 | this.sources.forEach(function (source, i) {
|
---|
1170 | if (i > 0) {
|
---|
1171 | mappings.advance(this$1.separator);
|
---|
1172 | }
|
---|
1173 |
|
---|
1174 | var sourceIndex = source.filename ? this$1.uniqueSourceIndexByFilename[source.filename] : -1;
|
---|
1175 | var magicString = source.content;
|
---|
1176 | var locate = getLocator(magicString.original);
|
---|
1177 |
|
---|
1178 | if (magicString.intro) {
|
---|
1179 | mappings.advance(magicString.intro);
|
---|
1180 | }
|
---|
1181 |
|
---|
1182 | magicString.firstChunk.eachNext(function (chunk) {
|
---|
1183 | var loc = locate(chunk.start);
|
---|
1184 |
|
---|
1185 | if (chunk.intro.length) { mappings.advance(chunk.intro); }
|
---|
1186 |
|
---|
1187 | if (source.filename) {
|
---|
1188 | if (chunk.edited) {
|
---|
1189 | mappings.addEdit(
|
---|
1190 | sourceIndex,
|
---|
1191 | chunk.content,
|
---|
1192 | loc,
|
---|
1193 | chunk.storeName ? names.indexOf(chunk.original) : -1
|
---|
1194 | );
|
---|
1195 | } else {
|
---|
1196 | mappings.addUneditedChunk(
|
---|
1197 | sourceIndex,
|
---|
1198 | chunk,
|
---|
1199 | magicString.original,
|
---|
1200 | loc,
|
---|
1201 | magicString.sourcemapLocations
|
---|
1202 | );
|
---|
1203 | }
|
---|
1204 | } else {
|
---|
1205 | mappings.advance(chunk.content);
|
---|
1206 | }
|
---|
1207 |
|
---|
1208 | if (chunk.outro.length) { mappings.advance(chunk.outro); }
|
---|
1209 | });
|
---|
1210 |
|
---|
1211 | if (magicString.outro) {
|
---|
1212 | mappings.advance(magicString.outro);
|
---|
1213 | }
|
---|
1214 | });
|
---|
1215 |
|
---|
1216 | return {
|
---|
1217 | file: options.file ? options.file.split(/[/\\]/).pop() : null,
|
---|
1218 | sources: this.uniqueSources.map(function (source) {
|
---|
1219 | return options.file ? getRelativePath(options.file, source.filename) : source.filename;
|
---|
1220 | }),
|
---|
1221 | sourcesContent: this.uniqueSources.map(function (source) {
|
---|
1222 | return options.includeContent ? source.content : null;
|
---|
1223 | }),
|
---|
1224 | names: names,
|
---|
1225 | mappings: mappings.raw
|
---|
1226 | };
|
---|
1227 | };
|
---|
1228 |
|
---|
1229 | Bundle.prototype.generateMap = function generateMap (options) {
|
---|
1230 | return new SourceMap(this.generateDecodedMap(options));
|
---|
1231 | };
|
---|
1232 |
|
---|
1233 | Bundle.prototype.getIndentString = function getIndentString () {
|
---|
1234 | var indentStringCounts = {};
|
---|
1235 |
|
---|
1236 | this.sources.forEach(function (source) {
|
---|
1237 | var indentStr = source.content.indentStr;
|
---|
1238 |
|
---|
1239 | if (indentStr === null) { return; }
|
---|
1240 |
|
---|
1241 | if (!indentStringCounts[indentStr]) { indentStringCounts[indentStr] = 0; }
|
---|
1242 | indentStringCounts[indentStr] += 1;
|
---|
1243 | });
|
---|
1244 |
|
---|
1245 | return (
|
---|
1246 | Object.keys(indentStringCounts).sort(function (a, b) {
|
---|
1247 | return indentStringCounts[a] - indentStringCounts[b];
|
---|
1248 | })[0] || '\t'
|
---|
1249 | );
|
---|
1250 | };
|
---|
1251 |
|
---|
1252 | Bundle.prototype.indent = function indent (indentStr) {
|
---|
1253 | var this$1 = this;
|
---|
1254 |
|
---|
1255 | if (!arguments.length) {
|
---|
1256 | indentStr = this.getIndentString();
|
---|
1257 | }
|
---|
1258 |
|
---|
1259 | if (indentStr === '') { return this; } // noop
|
---|
1260 |
|
---|
1261 | var trailingNewline = !this.intro || this.intro.slice(-1) === '\n';
|
---|
1262 |
|
---|
1263 | this.sources.forEach(function (source, i) {
|
---|
1264 | var separator = source.separator !== undefined ? source.separator : this$1.separator;
|
---|
1265 | var indentStart = trailingNewline || (i > 0 && /\r?\n$/.test(separator));
|
---|
1266 |
|
---|
1267 | source.content.indent(indentStr, {
|
---|
1268 | exclude: source.indentExclusionRanges,
|
---|
1269 | indentStart: indentStart //: trailingNewline || /\r?\n$/.test( separator ) //true///\r?\n/.test( separator )
|
---|
1270 | });
|
---|
1271 |
|
---|
1272 | trailingNewline = source.content.lastChar() === '\n';
|
---|
1273 | });
|
---|
1274 |
|
---|
1275 | if (this.intro) {
|
---|
1276 | this.intro =
|
---|
1277 | indentStr +
|
---|
1278 | this.intro.replace(/^[^\n]/gm, function (match, index) {
|
---|
1279 | return index > 0 ? indentStr + match : match;
|
---|
1280 | });
|
---|
1281 | }
|
---|
1282 |
|
---|
1283 | return this;
|
---|
1284 | };
|
---|
1285 |
|
---|
1286 | Bundle.prototype.prepend = function prepend (str) {
|
---|
1287 | this.intro = str + this.intro;
|
---|
1288 | return this;
|
---|
1289 | };
|
---|
1290 |
|
---|
1291 | Bundle.prototype.toString = function toString () {
|
---|
1292 | var this$1 = this;
|
---|
1293 |
|
---|
1294 | var body = this.sources
|
---|
1295 | .map(function (source, i) {
|
---|
1296 | var separator = source.separator !== undefined ? source.separator : this$1.separator;
|
---|
1297 | var str = (i > 0 ? separator : '') + source.content.toString();
|
---|
1298 |
|
---|
1299 | return str;
|
---|
1300 | })
|
---|
1301 | .join('');
|
---|
1302 |
|
---|
1303 | return this.intro + body;
|
---|
1304 | };
|
---|
1305 |
|
---|
1306 | Bundle.prototype.isEmpty = function isEmpty () {
|
---|
1307 | if (this.intro.length && this.intro.trim())
|
---|
1308 | { return false; }
|
---|
1309 | if (this.sources.some(function (source) { return !source.content.isEmpty(); }))
|
---|
1310 | { return false; }
|
---|
1311 | return true;
|
---|
1312 | };
|
---|
1313 |
|
---|
1314 | Bundle.prototype.length = function length () {
|
---|
1315 | return this.sources.reduce(function (length, source) { return length + source.content.length(); }, this.intro.length);
|
---|
1316 | };
|
---|
1317 |
|
---|
1318 | Bundle.prototype.trimLines = function trimLines () {
|
---|
1319 | return this.trim('[\\r\\n]');
|
---|
1320 | };
|
---|
1321 |
|
---|
1322 | Bundle.prototype.trim = function trim (charType) {
|
---|
1323 | return this.trimStart(charType).trimEnd(charType);
|
---|
1324 | };
|
---|
1325 |
|
---|
1326 | Bundle.prototype.trimStart = function trimStart (charType) {
|
---|
1327 | var rx = new RegExp('^' + (charType || '\\s') + '+');
|
---|
1328 | this.intro = this.intro.replace(rx, '');
|
---|
1329 |
|
---|
1330 | if (!this.intro) {
|
---|
1331 | var source;
|
---|
1332 | var i = 0;
|
---|
1333 |
|
---|
1334 | do {
|
---|
1335 | source = this.sources[i++];
|
---|
1336 | if (!source) {
|
---|
1337 | break;
|
---|
1338 | }
|
---|
1339 | } while (!source.content.trimStartAborted(charType));
|
---|
1340 | }
|
---|
1341 |
|
---|
1342 | return this;
|
---|
1343 | };
|
---|
1344 |
|
---|
1345 | Bundle.prototype.trimEnd = function trimEnd (charType) {
|
---|
1346 | var rx = new RegExp((charType || '\\s') + '+$');
|
---|
1347 |
|
---|
1348 | var source;
|
---|
1349 | var i = this.sources.length - 1;
|
---|
1350 |
|
---|
1351 | do {
|
---|
1352 | source = this.sources[i--];
|
---|
1353 | if (!source) {
|
---|
1354 | this.intro = this.intro.replace(rx, '');
|
---|
1355 | break;
|
---|
1356 | }
|
---|
1357 | } while (!source.content.trimEndAborted(charType));
|
---|
1358 |
|
---|
1359 | return this;
|
---|
1360 | };
|
---|
1361 |
|
---|
1362 | MagicString.Bundle = Bundle;
|
---|
1363 | MagicString.SourceMap = SourceMap;
|
---|
1364 | MagicString.default = MagicString; // work around TypeScript bug https://github.com/Rich-Harris/magic-string/pull/121
|
---|
1365 |
|
---|
1366 | return MagicString;
|
---|
1367 |
|
---|
1368 | }));
|
---|
1369 | //# sourceMappingURL=magic-string.umd.js.map
|
---|