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