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