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