[57e58a3] | 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 | class BitSet {
|
---|
| 8 | constructor(arg) {
|
---|
| 9 | this.bits = arg instanceof BitSet ? arg.bits.slice() : [];
|
---|
| 10 | }
|
---|
| 11 |
|
---|
| 12 | add(n) {
|
---|
| 13 | this.bits[n >> 5] |= 1 << (n & 31);
|
---|
| 14 | }
|
---|
| 15 |
|
---|
| 16 | has(n) {
|
---|
| 17 | return !!(this.bits[n >> 5] & (1 << (n & 31)));
|
---|
| 18 | }
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | class Chunk {
|
---|
| 22 | constructor(start, end, content) {
|
---|
| 23 | this.start = start;
|
---|
| 24 | this.end = end;
|
---|
| 25 | this.original = content;
|
---|
| 26 |
|
---|
| 27 | this.intro = '';
|
---|
| 28 | this.outro = '';
|
---|
| 29 |
|
---|
| 30 | this.content = content;
|
---|
| 31 | this.storeName = false;
|
---|
| 32 | this.edited = false;
|
---|
| 33 |
|
---|
| 34 | {
|
---|
| 35 | this.previous = null;
|
---|
| 36 | this.next = null;
|
---|
| 37 | }
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | appendLeft(content) {
|
---|
| 41 | this.outro += content;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | appendRight(content) {
|
---|
| 45 | this.intro = this.intro + content;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | clone() {
|
---|
| 49 | const chunk = new Chunk(this.start, this.end, this.original);
|
---|
| 50 |
|
---|
| 51 | chunk.intro = this.intro;
|
---|
| 52 | chunk.outro = this.outro;
|
---|
| 53 | chunk.content = this.content;
|
---|
| 54 | chunk.storeName = this.storeName;
|
---|
| 55 | chunk.edited = this.edited;
|
---|
| 56 |
|
---|
| 57 | return chunk;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | contains(index) {
|
---|
| 61 | return this.start < index && index < this.end;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | eachNext(fn) {
|
---|
| 65 | let chunk = this;
|
---|
| 66 | while (chunk) {
|
---|
| 67 | fn(chunk);
|
---|
| 68 | chunk = chunk.next;
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | eachPrevious(fn) {
|
---|
| 73 | let chunk = this;
|
---|
| 74 | while (chunk) {
|
---|
| 75 | fn(chunk);
|
---|
| 76 | chunk = chunk.previous;
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | edit(content, storeName, contentOnly) {
|
---|
| 81 | this.content = content;
|
---|
| 82 | if (!contentOnly) {
|
---|
| 83 | this.intro = '';
|
---|
| 84 | this.outro = '';
|
---|
| 85 | }
|
---|
| 86 | this.storeName = storeName;
|
---|
| 87 |
|
---|
| 88 | this.edited = true;
|
---|
| 89 |
|
---|
| 90 | return this;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | prependLeft(content) {
|
---|
| 94 | this.outro = content + this.outro;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | prependRight(content) {
|
---|
| 98 | this.intro = content + this.intro;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | reset() {
|
---|
| 102 | this.intro = '';
|
---|
| 103 | this.outro = '';
|
---|
| 104 | if (this.edited) {
|
---|
| 105 | this.content = this.original;
|
---|
| 106 | this.storeName = false;
|
---|
| 107 | this.edited = false;
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | split(index) {
|
---|
| 112 | const sliceIndex = index - this.start;
|
---|
| 113 |
|
---|
| 114 | const originalBefore = this.original.slice(0, sliceIndex);
|
---|
| 115 | const originalAfter = this.original.slice(sliceIndex);
|
---|
| 116 |
|
---|
| 117 | this.original = originalBefore;
|
---|
| 118 |
|
---|
| 119 | const newChunk = new Chunk(index, this.end, originalAfter);
|
---|
| 120 | newChunk.outro = this.outro;
|
---|
| 121 | this.outro = '';
|
---|
| 122 |
|
---|
| 123 | this.end = index;
|
---|
| 124 |
|
---|
| 125 | if (this.edited) {
|
---|
| 126 | // after split we should save the edit content record into the correct chunk
|
---|
| 127 | // to make sure sourcemap correct
|
---|
| 128 | // For example:
|
---|
| 129 | // ' test'.trim()
|
---|
| 130 | // split -> ' ' + 'test'
|
---|
| 131 | // ✔️ edit -> '' + 'test'
|
---|
| 132 | // ✖️ edit -> 'test' + ''
|
---|
| 133 | // TODO is this block necessary?...
|
---|
| 134 | newChunk.edit('', false);
|
---|
| 135 | this.content = '';
|
---|
| 136 | } else {
|
---|
| 137 | this.content = originalBefore;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | newChunk.next = this.next;
|
---|
| 141 | if (newChunk.next) newChunk.next.previous = newChunk;
|
---|
| 142 | newChunk.previous = this;
|
---|
| 143 | this.next = newChunk;
|
---|
| 144 |
|
---|
| 145 | return newChunk;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | toString() {
|
---|
| 149 | return this.intro + this.content + this.outro;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | trimEnd(rx) {
|
---|
| 153 | this.outro = this.outro.replace(rx, '');
|
---|
| 154 | if (this.outro.length) return true;
|
---|
| 155 |
|
---|
| 156 | const trimmed = this.content.replace(rx, '');
|
---|
| 157 |
|
---|
| 158 | if (trimmed.length) {
|
---|
| 159 | if (trimmed !== this.content) {
|
---|
| 160 | this.split(this.start + trimmed.length).edit('', undefined, true);
|
---|
| 161 | if (this.edited) {
|
---|
| 162 | // save the change, if it has been edited
|
---|
| 163 | this.edit(trimmed, this.storeName, true);
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 | return true;
|
---|
| 167 | } else {
|
---|
| 168 | this.edit('', undefined, true);
|
---|
| 169 |
|
---|
| 170 | this.intro = this.intro.replace(rx, '');
|
---|
| 171 | if (this.intro.length) return true;
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | trimStart(rx) {
|
---|
| 176 | this.intro = this.intro.replace(rx, '');
|
---|
| 177 | if (this.intro.length) return true;
|
---|
| 178 |
|
---|
| 179 | const trimmed = this.content.replace(rx, '');
|
---|
| 180 |
|
---|
| 181 | if (trimmed.length) {
|
---|
| 182 | if (trimmed !== this.content) {
|
---|
| 183 | const newChunk = this.split(this.end - trimmed.length);
|
---|
| 184 | if (this.edited) {
|
---|
| 185 | // save the change, if it has been edited
|
---|
| 186 | newChunk.edit(trimmed, this.storeName, true);
|
---|
| 187 | }
|
---|
| 188 | this.edit('', undefined, true);
|
---|
| 189 | }
|
---|
| 190 | return true;
|
---|
| 191 | } else {
|
---|
| 192 | this.edit('', undefined, true);
|
---|
| 193 |
|
---|
| 194 | this.outro = this.outro.replace(rx, '');
|
---|
| 195 | if (this.outro.length) return true;
|
---|
| 196 | }
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | const comma = ','.charCodeAt(0);
|
---|
| 201 | const semicolon = ';'.charCodeAt(0);
|
---|
| 202 | const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
---|
| 203 | const intToChar = new Uint8Array(64); // 64 possible chars.
|
---|
| 204 | const charToInt = new Uint8Array(128); // z is 122 in ASCII
|
---|
| 205 | for (let i = 0; i < chars.length; i++) {
|
---|
| 206 | const c = chars.charCodeAt(i);
|
---|
| 207 | intToChar[i] = c;
|
---|
| 208 | charToInt[c] = i;
|
---|
| 209 | }
|
---|
| 210 | function encodeInteger(builder, num, relative) {
|
---|
| 211 | let delta = num - relative;
|
---|
| 212 | delta = delta < 0 ? (-delta << 1) | 1 : delta << 1;
|
---|
| 213 | do {
|
---|
| 214 | let clamped = delta & 0b011111;
|
---|
| 215 | delta >>>= 5;
|
---|
| 216 | if (delta > 0)
|
---|
| 217 | clamped |= 0b100000;
|
---|
| 218 | builder.write(intToChar[clamped]);
|
---|
| 219 | } while (delta > 0);
|
---|
| 220 | return num;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | const bufLength = 1024 * 16;
|
---|
| 224 | // Provide a fallback for older environments.
|
---|
| 225 | const td = typeof TextDecoder !== 'undefined'
|
---|
| 226 | ? /* #__PURE__ */ new TextDecoder()
|
---|
| 227 | : typeof Buffer !== 'undefined'
|
---|
| 228 | ? {
|
---|
| 229 | decode(buf) {
|
---|
| 230 | const out = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
|
---|
| 231 | return out.toString();
|
---|
| 232 | },
|
---|
| 233 | }
|
---|
| 234 | : {
|
---|
| 235 | decode(buf) {
|
---|
| 236 | let out = '';
|
---|
| 237 | for (let i = 0; i < buf.length; i++) {
|
---|
| 238 | out += String.fromCharCode(buf[i]);
|
---|
| 239 | }
|
---|
| 240 | return out;
|
---|
| 241 | },
|
---|
| 242 | };
|
---|
| 243 | class StringWriter {
|
---|
| 244 | constructor() {
|
---|
| 245 | this.pos = 0;
|
---|
| 246 | this.out = '';
|
---|
| 247 | this.buffer = new Uint8Array(bufLength);
|
---|
| 248 | }
|
---|
| 249 | write(v) {
|
---|
| 250 | const { buffer } = this;
|
---|
| 251 | buffer[this.pos++] = v;
|
---|
| 252 | if (this.pos === bufLength) {
|
---|
| 253 | this.out += td.decode(buffer);
|
---|
| 254 | this.pos = 0;
|
---|
| 255 | }
|
---|
| 256 | }
|
---|
| 257 | flush() {
|
---|
| 258 | const { buffer, out, pos } = this;
|
---|
| 259 | return pos > 0 ? out + td.decode(buffer.subarray(0, pos)) : out;
|
---|
| 260 | }
|
---|
| 261 | }
|
---|
| 262 | function encode(decoded) {
|
---|
| 263 | const writer = new StringWriter();
|
---|
| 264 | let sourcesIndex = 0;
|
---|
| 265 | let sourceLine = 0;
|
---|
| 266 | let sourceColumn = 0;
|
---|
| 267 | let namesIndex = 0;
|
---|
| 268 | for (let i = 0; i < decoded.length; i++) {
|
---|
| 269 | const line = decoded[i];
|
---|
| 270 | if (i > 0)
|
---|
| 271 | writer.write(semicolon);
|
---|
| 272 | if (line.length === 0)
|
---|
| 273 | continue;
|
---|
| 274 | let genColumn = 0;
|
---|
| 275 | for (let j = 0; j < line.length; j++) {
|
---|
| 276 | const segment = line[j];
|
---|
| 277 | if (j > 0)
|
---|
| 278 | writer.write(comma);
|
---|
| 279 | genColumn = encodeInteger(writer, segment[0], genColumn);
|
---|
| 280 | if (segment.length === 1)
|
---|
| 281 | continue;
|
---|
| 282 | sourcesIndex = encodeInteger(writer, segment[1], sourcesIndex);
|
---|
| 283 | sourceLine = encodeInteger(writer, segment[2], sourceLine);
|
---|
| 284 | sourceColumn = encodeInteger(writer, segment[3], sourceColumn);
|
---|
| 285 | if (segment.length === 4)
|
---|
| 286 | continue;
|
---|
| 287 | namesIndex = encodeInteger(writer, segment[4], namesIndex);
|
---|
| 288 | }
|
---|
| 289 | }
|
---|
| 290 | return writer.flush();
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | function getBtoa() {
|
---|
| 294 | if (typeof globalThis !== 'undefined' && typeof globalThis.btoa === 'function') {
|
---|
| 295 | return (str) => globalThis.btoa(unescape(encodeURIComponent(str)));
|
---|
| 296 | } else if (typeof Buffer === 'function') {
|
---|
| 297 | return (str) => Buffer.from(str, 'utf-8').toString('base64');
|
---|
| 298 | } else {
|
---|
| 299 | return () => {
|
---|
| 300 | throw new Error('Unsupported environment: `window.btoa` or `Buffer` should be supported.');
|
---|
| 301 | };
|
---|
| 302 | }
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | const btoa = /*#__PURE__*/ getBtoa();
|
---|
| 306 |
|
---|
| 307 | class SourceMap {
|
---|
| 308 | constructor(properties) {
|
---|
| 309 | this.version = 3;
|
---|
| 310 | this.file = properties.file;
|
---|
| 311 | this.sources = properties.sources;
|
---|
| 312 | this.sourcesContent = properties.sourcesContent;
|
---|
| 313 | this.names = properties.names;
|
---|
| 314 | this.mappings = encode(properties.mappings);
|
---|
| 315 | if (typeof properties.x_google_ignoreList !== 'undefined') {
|
---|
| 316 | this.x_google_ignoreList = properties.x_google_ignoreList;
|
---|
| 317 | }
|
---|
| 318 | if (typeof properties.debugId !== 'undefined') {
|
---|
| 319 | this.debugId = properties.debugId;
|
---|
| 320 | }
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 | toString() {
|
---|
| 324 | return JSON.stringify(this);
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | toUrl() {
|
---|
| 328 | return 'data:application/json;charset=utf-8;base64,' + btoa(this.toString());
|
---|
| 329 | }
|
---|
| 330 | }
|
---|
| 331 |
|
---|
| 332 | function guessIndent(code) {
|
---|
| 333 | const lines = code.split('\n');
|
---|
| 334 |
|
---|
| 335 | const tabbed = lines.filter((line) => /^\t+/.test(line));
|
---|
| 336 | const spaced = lines.filter((line) => /^ {2,}/.test(line));
|
---|
| 337 |
|
---|
| 338 | if (tabbed.length === 0 && spaced.length === 0) {
|
---|
| 339 | return null;
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | // More lines tabbed than spaced? Assume tabs, and
|
---|
| 343 | // default to tabs in the case of a tie (or nothing
|
---|
| 344 | // to go on)
|
---|
| 345 | if (tabbed.length >= spaced.length) {
|
---|
| 346 | return '\t';
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | // Otherwise, we need to guess the multiple
|
---|
| 350 | const min = spaced.reduce((previous, current) => {
|
---|
| 351 | const numSpaces = /^ +/.exec(current)[0].length;
|
---|
| 352 | return Math.min(numSpaces, previous);
|
---|
| 353 | }, Infinity);
|
---|
| 354 |
|
---|
| 355 | return new Array(min + 1).join(' ');
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 | function getRelativePath(from, to) {
|
---|
| 359 | const fromParts = from.split(/[/\\]/);
|
---|
| 360 | const toParts = to.split(/[/\\]/);
|
---|
| 361 |
|
---|
| 362 | fromParts.pop(); // get dirname
|
---|
| 363 |
|
---|
| 364 | while (fromParts[0] === toParts[0]) {
|
---|
| 365 | fromParts.shift();
|
---|
| 366 | toParts.shift();
|
---|
| 367 | }
|
---|
| 368 |
|
---|
| 369 | if (fromParts.length) {
|
---|
| 370 | let i = fromParts.length;
|
---|
| 371 | while (i--) fromParts[i] = '..';
|
---|
| 372 | }
|
---|
| 373 |
|
---|
| 374 | return fromParts.concat(toParts).join('/');
|
---|
| 375 | }
|
---|
| 376 |
|
---|
| 377 | const toString = Object.prototype.toString;
|
---|
| 378 |
|
---|
| 379 | function isObject(thing) {
|
---|
| 380 | return toString.call(thing) === '[object Object]';
|
---|
| 381 | }
|
---|
| 382 |
|
---|
| 383 | function getLocator(source) {
|
---|
| 384 | const originalLines = source.split('\n');
|
---|
| 385 | const lineOffsets = [];
|
---|
| 386 |
|
---|
| 387 | for (let i = 0, pos = 0; i < originalLines.length; i++) {
|
---|
| 388 | lineOffsets.push(pos);
|
---|
| 389 | pos += originalLines[i].length + 1;
|
---|
| 390 | }
|
---|
| 391 |
|
---|
| 392 | return function locate(index) {
|
---|
| 393 | let i = 0;
|
---|
| 394 | let j = lineOffsets.length;
|
---|
| 395 | while (i < j) {
|
---|
| 396 | const m = (i + j) >> 1;
|
---|
| 397 | if (index < lineOffsets[m]) {
|
---|
| 398 | j = m;
|
---|
| 399 | } else {
|
---|
| 400 | i = m + 1;
|
---|
| 401 | }
|
---|
| 402 | }
|
---|
| 403 | const line = i - 1;
|
---|
| 404 | const column = index - lineOffsets[line];
|
---|
| 405 | return { line, column };
|
---|
| 406 | };
|
---|
| 407 | }
|
---|
| 408 |
|
---|
| 409 | const wordRegex = /\w/;
|
---|
| 410 |
|
---|
| 411 | class Mappings {
|
---|
| 412 | constructor(hires) {
|
---|
| 413 | this.hires = hires;
|
---|
| 414 | this.generatedCodeLine = 0;
|
---|
| 415 | this.generatedCodeColumn = 0;
|
---|
| 416 | this.raw = [];
|
---|
| 417 | this.rawSegments = this.raw[this.generatedCodeLine] = [];
|
---|
| 418 | this.pending = null;
|
---|
| 419 | }
|
---|
| 420 |
|
---|
| 421 | addEdit(sourceIndex, content, loc, nameIndex) {
|
---|
| 422 | if (content.length) {
|
---|
| 423 | const contentLengthMinusOne = content.length - 1;
|
---|
| 424 | let contentLineEnd = content.indexOf('\n', 0);
|
---|
| 425 | let previousContentLineEnd = -1;
|
---|
| 426 | // Loop through each line in the content and add a segment, but stop if the last line is empty,
|
---|
| 427 | // else code afterwards would fill one line too many
|
---|
| 428 | while (contentLineEnd >= 0 && contentLengthMinusOne > contentLineEnd) {
|
---|
| 429 | const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
|
---|
| 430 | if (nameIndex >= 0) {
|
---|
| 431 | segment.push(nameIndex);
|
---|
| 432 | }
|
---|
| 433 | this.rawSegments.push(segment);
|
---|
| 434 |
|
---|
| 435 | this.generatedCodeLine += 1;
|
---|
| 436 | this.raw[this.generatedCodeLine] = this.rawSegments = [];
|
---|
| 437 | this.generatedCodeColumn = 0;
|
---|
| 438 |
|
---|
| 439 | previousContentLineEnd = contentLineEnd;
|
---|
| 440 | contentLineEnd = content.indexOf('\n', contentLineEnd + 1);
|
---|
| 441 | }
|
---|
| 442 |
|
---|
| 443 | const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
|
---|
| 444 | if (nameIndex >= 0) {
|
---|
| 445 | segment.push(nameIndex);
|
---|
| 446 | }
|
---|
| 447 | this.rawSegments.push(segment);
|
---|
| 448 |
|
---|
| 449 | this.advance(content.slice(previousContentLineEnd + 1));
|
---|
| 450 | } else if (this.pending) {
|
---|
| 451 | this.rawSegments.push(this.pending);
|
---|
| 452 | this.advance(content);
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 | this.pending = null;
|
---|
| 456 | }
|
---|
| 457 |
|
---|
| 458 | addUneditedChunk(sourceIndex, chunk, original, loc, sourcemapLocations) {
|
---|
| 459 | let originalCharIndex = chunk.start;
|
---|
| 460 | let first = true;
|
---|
| 461 | // when iterating each char, check if it's in a word boundary
|
---|
| 462 | let charInHiresBoundary = false;
|
---|
| 463 |
|
---|
| 464 | while (originalCharIndex < chunk.end) {
|
---|
| 465 | if (original[originalCharIndex] === '\n') {
|
---|
| 466 | loc.line += 1;
|
---|
| 467 | loc.column = 0;
|
---|
| 468 | this.generatedCodeLine += 1;
|
---|
| 469 | this.raw[this.generatedCodeLine] = this.rawSegments = [];
|
---|
| 470 | this.generatedCodeColumn = 0;
|
---|
| 471 | first = true;
|
---|
| 472 | charInHiresBoundary = false;
|
---|
| 473 | } else {
|
---|
| 474 | if (this.hires || first || sourcemapLocations.has(originalCharIndex)) {
|
---|
| 475 | const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
|
---|
| 476 |
|
---|
| 477 | if (this.hires === 'boundary') {
|
---|
| 478 | // in hires "boundary", group segments per word boundary than per char
|
---|
| 479 | if (wordRegex.test(original[originalCharIndex])) {
|
---|
| 480 | // for first char in the boundary found, start the boundary by pushing a segment
|
---|
| 481 | if (!charInHiresBoundary) {
|
---|
| 482 | this.rawSegments.push(segment);
|
---|
| 483 | charInHiresBoundary = true;
|
---|
| 484 | }
|
---|
| 485 | } else {
|
---|
| 486 | // for non-word char, end the boundary by pushing a segment
|
---|
| 487 | this.rawSegments.push(segment);
|
---|
| 488 | charInHiresBoundary = false;
|
---|
| 489 | }
|
---|
| 490 | } else {
|
---|
| 491 | this.rawSegments.push(segment);
|
---|
| 492 | }
|
---|
| 493 | }
|
---|
| 494 |
|
---|
| 495 | loc.column += 1;
|
---|
| 496 | this.generatedCodeColumn += 1;
|
---|
| 497 | first = false;
|
---|
| 498 | }
|
---|
| 499 |
|
---|
| 500 | originalCharIndex += 1;
|
---|
| 501 | }
|
---|
| 502 |
|
---|
| 503 | this.pending = null;
|
---|
| 504 | }
|
---|
| 505 |
|
---|
| 506 | advance(str) {
|
---|
| 507 | if (!str) return;
|
---|
| 508 |
|
---|
| 509 | const lines = str.split('\n');
|
---|
| 510 |
|
---|
| 511 | if (lines.length > 1) {
|
---|
| 512 | for (let i = 0; i < lines.length - 1; i++) {
|
---|
| 513 | this.generatedCodeLine++;
|
---|
| 514 | this.raw[this.generatedCodeLine] = this.rawSegments = [];
|
---|
| 515 | }
|
---|
| 516 | this.generatedCodeColumn = 0;
|
---|
| 517 | }
|
---|
| 518 |
|
---|
| 519 | this.generatedCodeColumn += lines[lines.length - 1].length;
|
---|
| 520 | }
|
---|
| 521 | }
|
---|
| 522 |
|
---|
| 523 | const n = '\n';
|
---|
| 524 |
|
---|
| 525 | const warned = {
|
---|
| 526 | insertLeft: false,
|
---|
| 527 | insertRight: false,
|
---|
| 528 | storeName: false,
|
---|
| 529 | };
|
---|
| 530 |
|
---|
| 531 | class MagicString {
|
---|
| 532 | constructor(string, options = {}) {
|
---|
| 533 | const chunk = new Chunk(0, string.length, string);
|
---|
| 534 |
|
---|
| 535 | Object.defineProperties(this, {
|
---|
| 536 | original: { writable: true, value: string },
|
---|
| 537 | outro: { writable: true, value: '' },
|
---|
| 538 | intro: { writable: true, value: '' },
|
---|
| 539 | firstChunk: { writable: true, value: chunk },
|
---|
| 540 | lastChunk: { writable: true, value: chunk },
|
---|
| 541 | lastSearchedChunk: { writable: true, value: chunk },
|
---|
| 542 | byStart: { writable: true, value: {} },
|
---|
| 543 | byEnd: { writable: true, value: {} },
|
---|
| 544 | filename: { writable: true, value: options.filename },
|
---|
| 545 | indentExclusionRanges: { writable: true, value: options.indentExclusionRanges },
|
---|
| 546 | sourcemapLocations: { writable: true, value: new BitSet() },
|
---|
| 547 | storedNames: { writable: true, value: {} },
|
---|
| 548 | indentStr: { writable: true, value: undefined },
|
---|
| 549 | ignoreList: { writable: true, value: options.ignoreList },
|
---|
| 550 | offset: { writable: true, value: options.offset || 0 },
|
---|
| 551 | });
|
---|
| 552 |
|
---|
| 553 | this.byStart[0] = chunk;
|
---|
| 554 | this.byEnd[string.length] = chunk;
|
---|
| 555 | }
|
---|
| 556 |
|
---|
| 557 | addSourcemapLocation(char) {
|
---|
| 558 | this.sourcemapLocations.add(char);
|
---|
| 559 | }
|
---|
| 560 |
|
---|
| 561 | append(content) {
|
---|
| 562 | if (typeof content !== 'string') throw new TypeError('outro content must be a string');
|
---|
| 563 |
|
---|
| 564 | this.outro += content;
|
---|
| 565 | return this;
|
---|
| 566 | }
|
---|
| 567 |
|
---|
| 568 | appendLeft(index, content) {
|
---|
| 569 | index = index + this.offset;
|
---|
| 570 |
|
---|
| 571 | if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
|
---|
| 572 |
|
---|
| 573 | this._split(index);
|
---|
| 574 |
|
---|
| 575 | const chunk = this.byEnd[index];
|
---|
| 576 |
|
---|
| 577 | if (chunk) {
|
---|
| 578 | chunk.appendLeft(content);
|
---|
| 579 | } else {
|
---|
| 580 | this.intro += content;
|
---|
| 581 | }
|
---|
| 582 | return this;
|
---|
| 583 | }
|
---|
| 584 |
|
---|
| 585 | appendRight(index, content) {
|
---|
| 586 | index = index + this.offset;
|
---|
| 587 |
|
---|
| 588 | if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
|
---|
| 589 |
|
---|
| 590 | this._split(index);
|
---|
| 591 |
|
---|
| 592 | const chunk = this.byStart[index];
|
---|
| 593 |
|
---|
| 594 | if (chunk) {
|
---|
| 595 | chunk.appendRight(content);
|
---|
| 596 | } else {
|
---|
| 597 | this.outro += content;
|
---|
| 598 | }
|
---|
| 599 | return this;
|
---|
| 600 | }
|
---|
| 601 |
|
---|
| 602 | clone() {
|
---|
| 603 | const cloned = new MagicString(this.original, { filename: this.filename, offset: this.offset });
|
---|
| 604 |
|
---|
| 605 | let originalChunk = this.firstChunk;
|
---|
| 606 | let clonedChunk = (cloned.firstChunk = cloned.lastSearchedChunk = originalChunk.clone());
|
---|
| 607 |
|
---|
| 608 | while (originalChunk) {
|
---|
| 609 | cloned.byStart[clonedChunk.start] = clonedChunk;
|
---|
| 610 | cloned.byEnd[clonedChunk.end] = clonedChunk;
|
---|
| 611 |
|
---|
| 612 | const nextOriginalChunk = originalChunk.next;
|
---|
| 613 | const nextClonedChunk = nextOriginalChunk && nextOriginalChunk.clone();
|
---|
| 614 |
|
---|
| 615 | if (nextClonedChunk) {
|
---|
| 616 | clonedChunk.next = nextClonedChunk;
|
---|
| 617 | nextClonedChunk.previous = clonedChunk;
|
---|
| 618 |
|
---|
| 619 | clonedChunk = nextClonedChunk;
|
---|
| 620 | }
|
---|
| 621 |
|
---|
| 622 | originalChunk = nextOriginalChunk;
|
---|
| 623 | }
|
---|
| 624 |
|
---|
| 625 | cloned.lastChunk = clonedChunk;
|
---|
| 626 |
|
---|
| 627 | if (this.indentExclusionRanges) {
|
---|
| 628 | cloned.indentExclusionRanges = this.indentExclusionRanges.slice();
|
---|
| 629 | }
|
---|
| 630 |
|
---|
| 631 | cloned.sourcemapLocations = new BitSet(this.sourcemapLocations);
|
---|
| 632 |
|
---|
| 633 | cloned.intro = this.intro;
|
---|
| 634 | cloned.outro = this.outro;
|
---|
| 635 |
|
---|
| 636 | return cloned;
|
---|
| 637 | }
|
---|
| 638 |
|
---|
| 639 | generateDecodedMap(options) {
|
---|
| 640 | options = options || {};
|
---|
| 641 |
|
---|
| 642 | const sourceIndex = 0;
|
---|
| 643 | const names = Object.keys(this.storedNames);
|
---|
| 644 | const mappings = new Mappings(options.hires);
|
---|
| 645 |
|
---|
| 646 | const locate = getLocator(this.original);
|
---|
| 647 |
|
---|
| 648 | if (this.intro) {
|
---|
| 649 | mappings.advance(this.intro);
|
---|
| 650 | }
|
---|
| 651 |
|
---|
| 652 | this.firstChunk.eachNext((chunk) => {
|
---|
| 653 | const loc = locate(chunk.start);
|
---|
| 654 |
|
---|
| 655 | if (chunk.intro.length) mappings.advance(chunk.intro);
|
---|
| 656 |
|
---|
| 657 | if (chunk.edited) {
|
---|
| 658 | mappings.addEdit(
|
---|
| 659 | sourceIndex,
|
---|
| 660 | chunk.content,
|
---|
| 661 | loc,
|
---|
| 662 | chunk.storeName ? names.indexOf(chunk.original) : -1,
|
---|
| 663 | );
|
---|
| 664 | } else {
|
---|
| 665 | mappings.addUneditedChunk(sourceIndex, chunk, this.original, loc, this.sourcemapLocations);
|
---|
| 666 | }
|
---|
| 667 |
|
---|
| 668 | if (chunk.outro.length) mappings.advance(chunk.outro);
|
---|
| 669 | });
|
---|
| 670 |
|
---|
| 671 | return {
|
---|
| 672 | file: options.file ? options.file.split(/[/\\]/).pop() : undefined,
|
---|
| 673 | sources: [
|
---|
| 674 | options.source ? getRelativePath(options.file || '', options.source) : options.file || '',
|
---|
| 675 | ],
|
---|
| 676 | sourcesContent: options.includeContent ? [this.original] : undefined,
|
---|
| 677 | names,
|
---|
| 678 | mappings: mappings.raw,
|
---|
| 679 | x_google_ignoreList: this.ignoreList ? [sourceIndex] : undefined,
|
---|
| 680 | };
|
---|
| 681 | }
|
---|
| 682 |
|
---|
| 683 | generateMap(options) {
|
---|
| 684 | return new SourceMap(this.generateDecodedMap(options));
|
---|
| 685 | }
|
---|
| 686 |
|
---|
| 687 | _ensureindentStr() {
|
---|
| 688 | if (this.indentStr === undefined) {
|
---|
| 689 | this.indentStr = guessIndent(this.original);
|
---|
| 690 | }
|
---|
| 691 | }
|
---|
| 692 |
|
---|
| 693 | _getRawIndentString() {
|
---|
| 694 | this._ensureindentStr();
|
---|
| 695 | return this.indentStr;
|
---|
| 696 | }
|
---|
| 697 |
|
---|
| 698 | getIndentString() {
|
---|
| 699 | this._ensureindentStr();
|
---|
| 700 | return this.indentStr === null ? '\t' : this.indentStr;
|
---|
| 701 | }
|
---|
| 702 |
|
---|
| 703 | indent(indentStr, options) {
|
---|
| 704 | const pattern = /^[^\r\n]/gm;
|
---|
| 705 |
|
---|
| 706 | if (isObject(indentStr)) {
|
---|
| 707 | options = indentStr;
|
---|
| 708 | indentStr = undefined;
|
---|
| 709 | }
|
---|
| 710 |
|
---|
| 711 | if (indentStr === undefined) {
|
---|
| 712 | this._ensureindentStr();
|
---|
| 713 | indentStr = this.indentStr || '\t';
|
---|
| 714 | }
|
---|
| 715 |
|
---|
| 716 | if (indentStr === '') return this; // noop
|
---|
| 717 |
|
---|
| 718 | options = options || {};
|
---|
| 719 |
|
---|
| 720 | // Process exclusion ranges
|
---|
| 721 | const isExcluded = {};
|
---|
| 722 |
|
---|
| 723 | if (options.exclude) {
|
---|
| 724 | const exclusions =
|
---|
| 725 | typeof options.exclude[0] === 'number' ? [options.exclude] : options.exclude;
|
---|
| 726 | exclusions.forEach((exclusion) => {
|
---|
| 727 | for (let i = exclusion[0]; i < exclusion[1]; i += 1) {
|
---|
| 728 | isExcluded[i] = true;
|
---|
| 729 | }
|
---|
| 730 | });
|
---|
| 731 | }
|
---|
| 732 |
|
---|
| 733 | let shouldIndentNextCharacter = options.indentStart !== false;
|
---|
| 734 | const replacer = (match) => {
|
---|
| 735 | if (shouldIndentNextCharacter) return `${indentStr}${match}`;
|
---|
| 736 | shouldIndentNextCharacter = true;
|
---|
| 737 | return match;
|
---|
| 738 | };
|
---|
| 739 |
|
---|
| 740 | this.intro = this.intro.replace(pattern, replacer);
|
---|
| 741 |
|
---|
| 742 | let charIndex = 0;
|
---|
| 743 | let chunk = this.firstChunk;
|
---|
| 744 |
|
---|
| 745 | while (chunk) {
|
---|
| 746 | const end = chunk.end;
|
---|
| 747 |
|
---|
| 748 | if (chunk.edited) {
|
---|
| 749 | if (!isExcluded[charIndex]) {
|
---|
| 750 | chunk.content = chunk.content.replace(pattern, replacer);
|
---|
| 751 |
|
---|
| 752 | if (chunk.content.length) {
|
---|
| 753 | shouldIndentNextCharacter = chunk.content[chunk.content.length - 1] === '\n';
|
---|
| 754 | }
|
---|
| 755 | }
|
---|
| 756 | } else {
|
---|
| 757 | charIndex = chunk.start;
|
---|
| 758 |
|
---|
| 759 | while (charIndex < end) {
|
---|
| 760 | if (!isExcluded[charIndex]) {
|
---|
| 761 | const char = this.original[charIndex];
|
---|
| 762 |
|
---|
| 763 | if (char === '\n') {
|
---|
| 764 | shouldIndentNextCharacter = true;
|
---|
| 765 | } else if (char !== '\r' && shouldIndentNextCharacter) {
|
---|
| 766 | shouldIndentNextCharacter = false;
|
---|
| 767 |
|
---|
| 768 | if (charIndex === chunk.start) {
|
---|
| 769 | chunk.prependRight(indentStr);
|
---|
| 770 | } else {
|
---|
| 771 | this._splitChunk(chunk, charIndex);
|
---|
| 772 | chunk = chunk.next;
|
---|
| 773 | chunk.prependRight(indentStr);
|
---|
| 774 | }
|
---|
| 775 | }
|
---|
| 776 | }
|
---|
| 777 |
|
---|
| 778 | charIndex += 1;
|
---|
| 779 | }
|
---|
| 780 | }
|
---|
| 781 |
|
---|
| 782 | charIndex = chunk.end;
|
---|
| 783 | chunk = chunk.next;
|
---|
| 784 | }
|
---|
| 785 |
|
---|
| 786 | this.outro = this.outro.replace(pattern, replacer);
|
---|
| 787 |
|
---|
| 788 | return this;
|
---|
| 789 | }
|
---|
| 790 |
|
---|
| 791 | insert() {
|
---|
| 792 | throw new Error(
|
---|
| 793 | 'magicString.insert(...) is deprecated. Use prependRight(...) or appendLeft(...)',
|
---|
| 794 | );
|
---|
| 795 | }
|
---|
| 796 |
|
---|
| 797 | insertLeft(index, content) {
|
---|
| 798 | if (!warned.insertLeft) {
|
---|
| 799 | console.warn(
|
---|
| 800 | 'magicString.insertLeft(...) is deprecated. Use magicString.appendLeft(...) instead',
|
---|
| 801 | );
|
---|
| 802 | warned.insertLeft = true;
|
---|
| 803 | }
|
---|
| 804 |
|
---|
| 805 | return this.appendLeft(index, content);
|
---|
| 806 | }
|
---|
| 807 |
|
---|
| 808 | insertRight(index, content) {
|
---|
| 809 | if (!warned.insertRight) {
|
---|
| 810 | console.warn(
|
---|
| 811 | 'magicString.insertRight(...) is deprecated. Use magicString.prependRight(...) instead',
|
---|
| 812 | );
|
---|
| 813 | warned.insertRight = true;
|
---|
| 814 | }
|
---|
| 815 |
|
---|
| 816 | return this.prependRight(index, content);
|
---|
| 817 | }
|
---|
| 818 |
|
---|
| 819 | move(start, end, index) {
|
---|
| 820 | start = start + this.offset;
|
---|
| 821 | end = end + this.offset;
|
---|
| 822 | index = index + this.offset;
|
---|
| 823 |
|
---|
| 824 | if (index >= start && index <= end) throw new Error('Cannot move a selection inside itself');
|
---|
| 825 |
|
---|
| 826 | this._split(start);
|
---|
| 827 | this._split(end);
|
---|
| 828 | this._split(index);
|
---|
| 829 |
|
---|
| 830 | const first = this.byStart[start];
|
---|
| 831 | const last = this.byEnd[end];
|
---|
| 832 |
|
---|
| 833 | const oldLeft = first.previous;
|
---|
| 834 | const oldRight = last.next;
|
---|
| 835 |
|
---|
| 836 | const newRight = this.byStart[index];
|
---|
| 837 | if (!newRight && last === this.lastChunk) return this;
|
---|
| 838 | const newLeft = newRight ? newRight.previous : this.lastChunk;
|
---|
| 839 |
|
---|
| 840 | if (oldLeft) oldLeft.next = oldRight;
|
---|
| 841 | if (oldRight) oldRight.previous = oldLeft;
|
---|
| 842 |
|
---|
| 843 | if (newLeft) newLeft.next = first;
|
---|
| 844 | if (newRight) newRight.previous = last;
|
---|
| 845 |
|
---|
| 846 | if (!first.previous) this.firstChunk = last.next;
|
---|
| 847 | if (!last.next) {
|
---|
| 848 | this.lastChunk = first.previous;
|
---|
| 849 | this.lastChunk.next = null;
|
---|
| 850 | }
|
---|
| 851 |
|
---|
| 852 | first.previous = newLeft;
|
---|
| 853 | last.next = newRight || null;
|
---|
| 854 |
|
---|
| 855 | if (!newLeft) this.firstChunk = first;
|
---|
| 856 | if (!newRight) this.lastChunk = last;
|
---|
| 857 | return this;
|
---|
| 858 | }
|
---|
| 859 |
|
---|
| 860 | overwrite(start, end, content, options) {
|
---|
| 861 | options = options || {};
|
---|
| 862 | return this.update(start, end, content, { ...options, overwrite: !options.contentOnly });
|
---|
| 863 | }
|
---|
| 864 |
|
---|
| 865 | update(start, end, content, options) {
|
---|
| 866 | start = start + this.offset;
|
---|
| 867 | end = end + this.offset;
|
---|
| 868 |
|
---|
| 869 | if (typeof content !== 'string') throw new TypeError('replacement content must be a string');
|
---|
| 870 |
|
---|
| 871 | if (this.original.length !== 0) {
|
---|
| 872 | while (start < 0) start += this.original.length;
|
---|
| 873 | while (end < 0) end += this.original.length;
|
---|
| 874 | }
|
---|
| 875 |
|
---|
| 876 | if (end > this.original.length) throw new Error('end is out of bounds');
|
---|
| 877 | if (start === end)
|
---|
| 878 | throw new Error(
|
---|
| 879 | 'Cannot overwrite a zero-length range – use appendLeft or prependRight instead',
|
---|
| 880 | );
|
---|
| 881 |
|
---|
| 882 | this._split(start);
|
---|
| 883 | this._split(end);
|
---|
| 884 |
|
---|
| 885 | if (options === true) {
|
---|
| 886 | if (!warned.storeName) {
|
---|
| 887 | console.warn(
|
---|
| 888 | 'The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string',
|
---|
| 889 | );
|
---|
| 890 | warned.storeName = true;
|
---|
| 891 | }
|
---|
| 892 |
|
---|
| 893 | options = { storeName: true };
|
---|
| 894 | }
|
---|
| 895 | const storeName = options !== undefined ? options.storeName : false;
|
---|
| 896 | const overwrite = options !== undefined ? options.overwrite : false;
|
---|
| 897 |
|
---|
| 898 | if (storeName) {
|
---|
| 899 | const original = this.original.slice(start, end);
|
---|
| 900 | Object.defineProperty(this.storedNames, original, {
|
---|
| 901 | writable: true,
|
---|
| 902 | value: true,
|
---|
| 903 | enumerable: true,
|
---|
| 904 | });
|
---|
| 905 | }
|
---|
| 906 |
|
---|
| 907 | const first = this.byStart[start];
|
---|
| 908 | const last = this.byEnd[end];
|
---|
| 909 |
|
---|
| 910 | if (first) {
|
---|
| 911 | let chunk = first;
|
---|
| 912 | while (chunk !== last) {
|
---|
| 913 | if (chunk.next !== this.byStart[chunk.end]) {
|
---|
| 914 | throw new Error('Cannot overwrite across a split point');
|
---|
| 915 | }
|
---|
| 916 | chunk = chunk.next;
|
---|
| 917 | chunk.edit('', false);
|
---|
| 918 | }
|
---|
| 919 |
|
---|
| 920 | first.edit(content, storeName, !overwrite);
|
---|
| 921 | } else {
|
---|
| 922 | // must be inserting at the end
|
---|
| 923 | const newChunk = new Chunk(start, end, '').edit(content, storeName);
|
---|
| 924 |
|
---|
| 925 | // TODO last chunk in the array may not be the last chunk, if it's moved...
|
---|
| 926 | last.next = newChunk;
|
---|
| 927 | newChunk.previous = last;
|
---|
| 928 | }
|
---|
| 929 | return this;
|
---|
| 930 | }
|
---|
| 931 |
|
---|
| 932 | prepend(content) {
|
---|
| 933 | if (typeof content !== 'string') throw new TypeError('outro content must be a string');
|
---|
| 934 |
|
---|
| 935 | this.intro = content + this.intro;
|
---|
| 936 | return this;
|
---|
| 937 | }
|
---|
| 938 |
|
---|
| 939 | prependLeft(index, content) {
|
---|
| 940 | index = index + this.offset;
|
---|
| 941 |
|
---|
| 942 | if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
|
---|
| 943 |
|
---|
| 944 | this._split(index);
|
---|
| 945 |
|
---|
| 946 | const chunk = this.byEnd[index];
|
---|
| 947 |
|
---|
| 948 | if (chunk) {
|
---|
| 949 | chunk.prependLeft(content);
|
---|
| 950 | } else {
|
---|
| 951 | this.intro = content + this.intro;
|
---|
| 952 | }
|
---|
| 953 | return this;
|
---|
| 954 | }
|
---|
| 955 |
|
---|
| 956 | prependRight(index, content) {
|
---|
| 957 | index = index + this.offset;
|
---|
| 958 |
|
---|
| 959 | if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
|
---|
| 960 |
|
---|
| 961 | this._split(index);
|
---|
| 962 |
|
---|
| 963 | const chunk = this.byStart[index];
|
---|
| 964 |
|
---|
| 965 | if (chunk) {
|
---|
| 966 | chunk.prependRight(content);
|
---|
| 967 | } else {
|
---|
| 968 | this.outro = content + this.outro;
|
---|
| 969 | }
|
---|
| 970 | return this;
|
---|
| 971 | }
|
---|
| 972 |
|
---|
| 973 | remove(start, end) {
|
---|
| 974 | start = start + this.offset;
|
---|
| 975 | end = end + this.offset;
|
---|
| 976 |
|
---|
| 977 | if (this.original.length !== 0) {
|
---|
| 978 | while (start < 0) start += this.original.length;
|
---|
| 979 | while (end < 0) end += this.original.length;
|
---|
| 980 | }
|
---|
| 981 |
|
---|
| 982 | if (start === end) return this;
|
---|
| 983 |
|
---|
| 984 | if (start < 0 || end > this.original.length) throw new Error('Character is out of bounds');
|
---|
| 985 | if (start > end) throw new Error('end must be greater than start');
|
---|
| 986 |
|
---|
| 987 | this._split(start);
|
---|
| 988 | this._split(end);
|
---|
| 989 |
|
---|
| 990 | let chunk = this.byStart[start];
|
---|
| 991 |
|
---|
| 992 | while (chunk) {
|
---|
| 993 | chunk.intro = '';
|
---|
| 994 | chunk.outro = '';
|
---|
| 995 | chunk.edit('');
|
---|
| 996 |
|
---|
| 997 | chunk = end > chunk.end ? this.byStart[chunk.end] : null;
|
---|
| 998 | }
|
---|
| 999 | return this;
|
---|
| 1000 | }
|
---|
| 1001 |
|
---|
| 1002 | reset(start, end) {
|
---|
| 1003 | start = start + this.offset;
|
---|
| 1004 | end = end + this.offset;
|
---|
| 1005 |
|
---|
| 1006 | if (this.original.length !== 0) {
|
---|
| 1007 | while (start < 0) start += this.original.length;
|
---|
| 1008 | while (end < 0) end += this.original.length;
|
---|
| 1009 | }
|
---|
| 1010 |
|
---|
| 1011 | if (start === end) return this;
|
---|
| 1012 |
|
---|
| 1013 | if (start < 0 || end > this.original.length) throw new Error('Character is out of bounds');
|
---|
| 1014 | if (start > end) throw new Error('end must be greater than start');
|
---|
| 1015 |
|
---|
| 1016 | this._split(start);
|
---|
| 1017 | this._split(end);
|
---|
| 1018 |
|
---|
| 1019 | let chunk = this.byStart[start];
|
---|
| 1020 |
|
---|
| 1021 | while (chunk) {
|
---|
| 1022 | chunk.reset();
|
---|
| 1023 |
|
---|
| 1024 | chunk = end > chunk.end ? this.byStart[chunk.end] : null;
|
---|
| 1025 | }
|
---|
| 1026 | return this;
|
---|
| 1027 | }
|
---|
| 1028 |
|
---|
| 1029 | lastChar() {
|
---|
| 1030 | if (this.outro.length) return this.outro[this.outro.length - 1];
|
---|
| 1031 | let chunk = this.lastChunk;
|
---|
| 1032 | do {
|
---|
| 1033 | if (chunk.outro.length) return chunk.outro[chunk.outro.length - 1];
|
---|
| 1034 | if (chunk.content.length) return chunk.content[chunk.content.length - 1];
|
---|
| 1035 | if (chunk.intro.length) return chunk.intro[chunk.intro.length - 1];
|
---|
| 1036 | } while ((chunk = chunk.previous));
|
---|
| 1037 | if (this.intro.length) return this.intro[this.intro.length - 1];
|
---|
| 1038 | return '';
|
---|
| 1039 | }
|
---|
| 1040 |
|
---|
| 1041 | lastLine() {
|
---|
| 1042 | let lineIndex = this.outro.lastIndexOf(n);
|
---|
| 1043 | if (lineIndex !== -1) return this.outro.substr(lineIndex + 1);
|
---|
| 1044 | let lineStr = this.outro;
|
---|
| 1045 | let chunk = this.lastChunk;
|
---|
| 1046 | do {
|
---|
| 1047 | if (chunk.outro.length > 0) {
|
---|
| 1048 | lineIndex = chunk.outro.lastIndexOf(n);
|
---|
| 1049 | if (lineIndex !== -1) return chunk.outro.substr(lineIndex + 1) + lineStr;
|
---|
| 1050 | lineStr = chunk.outro + lineStr;
|
---|
| 1051 | }
|
---|
| 1052 |
|
---|
| 1053 | if (chunk.content.length > 0) {
|
---|
| 1054 | lineIndex = chunk.content.lastIndexOf(n);
|
---|
| 1055 | if (lineIndex !== -1) return chunk.content.substr(lineIndex + 1) + lineStr;
|
---|
| 1056 | lineStr = chunk.content + lineStr;
|
---|
| 1057 | }
|
---|
| 1058 |
|
---|
| 1059 | if (chunk.intro.length > 0) {
|
---|
| 1060 | lineIndex = chunk.intro.lastIndexOf(n);
|
---|
| 1061 | if (lineIndex !== -1) return chunk.intro.substr(lineIndex + 1) + lineStr;
|
---|
| 1062 | lineStr = chunk.intro + lineStr;
|
---|
| 1063 | }
|
---|
| 1064 | } while ((chunk = chunk.previous));
|
---|
| 1065 | lineIndex = this.intro.lastIndexOf(n);
|
---|
| 1066 | if (lineIndex !== -1) return this.intro.substr(lineIndex + 1) + lineStr;
|
---|
| 1067 | return this.intro + lineStr;
|
---|
| 1068 | }
|
---|
| 1069 |
|
---|
| 1070 | slice(start = 0, end = this.original.length - this.offset) {
|
---|
| 1071 | start = start + this.offset;
|
---|
| 1072 | end = end + this.offset;
|
---|
| 1073 |
|
---|
| 1074 | if (this.original.length !== 0) {
|
---|
| 1075 | while (start < 0) start += this.original.length;
|
---|
| 1076 | while (end < 0) end += this.original.length;
|
---|
| 1077 | }
|
---|
| 1078 |
|
---|
| 1079 | let result = '';
|
---|
| 1080 |
|
---|
| 1081 | // find start chunk
|
---|
| 1082 | let chunk = this.firstChunk;
|
---|
| 1083 | while (chunk && (chunk.start > start || chunk.end <= start)) {
|
---|
| 1084 | // found end chunk before start
|
---|
| 1085 | if (chunk.start < end && chunk.end >= end) {
|
---|
| 1086 | return result;
|
---|
| 1087 | }
|
---|
| 1088 |
|
---|
| 1089 | chunk = chunk.next;
|
---|
| 1090 | }
|
---|
| 1091 |
|
---|
| 1092 | if (chunk && chunk.edited && chunk.start !== start)
|
---|
| 1093 | throw new Error(`Cannot use replaced character ${start} as slice start anchor.`);
|
---|
| 1094 |
|
---|
| 1095 | const startChunk = chunk;
|
---|
| 1096 | while (chunk) {
|
---|
| 1097 | if (chunk.intro && (startChunk !== chunk || chunk.start === start)) {
|
---|
| 1098 | result += chunk.intro;
|
---|
| 1099 | }
|
---|
| 1100 |
|
---|
| 1101 | const containsEnd = chunk.start < end && chunk.end >= end;
|
---|
| 1102 | if (containsEnd && chunk.edited && chunk.end !== end)
|
---|
| 1103 | throw new Error(`Cannot use replaced character ${end} as slice end anchor.`);
|
---|
| 1104 |
|
---|
| 1105 | const sliceStart = startChunk === chunk ? start - chunk.start : 0;
|
---|
| 1106 | const sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length;
|
---|
| 1107 |
|
---|
| 1108 | result += chunk.content.slice(sliceStart, sliceEnd);
|
---|
| 1109 |
|
---|
| 1110 | if (chunk.outro && (!containsEnd || chunk.end === end)) {
|
---|
| 1111 | result += chunk.outro;
|
---|
| 1112 | }
|
---|
| 1113 |
|
---|
| 1114 | if (containsEnd) {
|
---|
| 1115 | break;
|
---|
| 1116 | }
|
---|
| 1117 |
|
---|
| 1118 | chunk = chunk.next;
|
---|
| 1119 | }
|
---|
| 1120 |
|
---|
| 1121 | return result;
|
---|
| 1122 | }
|
---|
| 1123 |
|
---|
| 1124 | // TODO deprecate this? not really very useful
|
---|
| 1125 | snip(start, end) {
|
---|
| 1126 | const clone = this.clone();
|
---|
| 1127 | clone.remove(0, start);
|
---|
| 1128 | clone.remove(end, clone.original.length);
|
---|
| 1129 |
|
---|
| 1130 | return clone;
|
---|
| 1131 | }
|
---|
| 1132 |
|
---|
| 1133 | _split(index) {
|
---|
| 1134 | if (this.byStart[index] || this.byEnd[index]) return;
|
---|
| 1135 |
|
---|
| 1136 | let chunk = this.lastSearchedChunk;
|
---|
| 1137 | const searchForward = index > chunk.end;
|
---|
| 1138 |
|
---|
| 1139 | while (chunk) {
|
---|
| 1140 | if (chunk.contains(index)) return this._splitChunk(chunk, index);
|
---|
| 1141 |
|
---|
| 1142 | chunk = searchForward ? this.byStart[chunk.end] : this.byEnd[chunk.start];
|
---|
| 1143 | }
|
---|
| 1144 | }
|
---|
| 1145 |
|
---|
| 1146 | _splitChunk(chunk, index) {
|
---|
| 1147 | if (chunk.edited && chunk.content.length) {
|
---|
| 1148 | // zero-length edited chunks are a special case (overlapping replacements)
|
---|
| 1149 | const loc = getLocator(this.original)(index);
|
---|
| 1150 | throw new Error(
|
---|
| 1151 | `Cannot split a chunk that has already been edited (${loc.line}:${loc.column} – "${chunk.original}")`,
|
---|
| 1152 | );
|
---|
| 1153 | }
|
---|
| 1154 |
|
---|
| 1155 | const newChunk = chunk.split(index);
|
---|
| 1156 |
|
---|
| 1157 | this.byEnd[index] = chunk;
|
---|
| 1158 | this.byStart[index] = newChunk;
|
---|
| 1159 | this.byEnd[newChunk.end] = newChunk;
|
---|
| 1160 |
|
---|
| 1161 | if (chunk === this.lastChunk) this.lastChunk = newChunk;
|
---|
| 1162 |
|
---|
| 1163 | this.lastSearchedChunk = chunk;
|
---|
| 1164 | return true;
|
---|
| 1165 | }
|
---|
| 1166 |
|
---|
| 1167 | toString() {
|
---|
| 1168 | let str = this.intro;
|
---|
| 1169 |
|
---|
| 1170 | let chunk = this.firstChunk;
|
---|
| 1171 | while (chunk) {
|
---|
| 1172 | str += chunk.toString();
|
---|
| 1173 | chunk = chunk.next;
|
---|
| 1174 | }
|
---|
| 1175 |
|
---|
| 1176 | return str + this.outro;
|
---|
| 1177 | }
|
---|
| 1178 |
|
---|
| 1179 | isEmpty() {
|
---|
| 1180 | let chunk = this.firstChunk;
|
---|
| 1181 | do {
|
---|
| 1182 | if (
|
---|
| 1183 | (chunk.intro.length && chunk.intro.trim()) ||
|
---|
| 1184 | (chunk.content.length && chunk.content.trim()) ||
|
---|
| 1185 | (chunk.outro.length && chunk.outro.trim())
|
---|
| 1186 | )
|
---|
| 1187 | return false;
|
---|
| 1188 | } while ((chunk = chunk.next));
|
---|
| 1189 | return true;
|
---|
| 1190 | }
|
---|
| 1191 |
|
---|
| 1192 | length() {
|
---|
| 1193 | let chunk = this.firstChunk;
|
---|
| 1194 | let length = 0;
|
---|
| 1195 | do {
|
---|
| 1196 | length += chunk.intro.length + chunk.content.length + chunk.outro.length;
|
---|
| 1197 | } while ((chunk = chunk.next));
|
---|
| 1198 | return length;
|
---|
| 1199 | }
|
---|
| 1200 |
|
---|
| 1201 | trimLines() {
|
---|
| 1202 | return this.trim('[\\r\\n]');
|
---|
| 1203 | }
|
---|
| 1204 |
|
---|
| 1205 | trim(charType) {
|
---|
| 1206 | return this.trimStart(charType).trimEnd(charType);
|
---|
| 1207 | }
|
---|
| 1208 |
|
---|
| 1209 | trimEndAborted(charType) {
|
---|
| 1210 | const rx = new RegExp((charType || '\\s') + '+$');
|
---|
| 1211 |
|
---|
| 1212 | this.outro = this.outro.replace(rx, '');
|
---|
| 1213 | if (this.outro.length) return true;
|
---|
| 1214 |
|
---|
| 1215 | let chunk = this.lastChunk;
|
---|
| 1216 |
|
---|
| 1217 | do {
|
---|
| 1218 | const end = chunk.end;
|
---|
| 1219 | const aborted = chunk.trimEnd(rx);
|
---|
| 1220 |
|
---|
| 1221 | // if chunk was trimmed, we have a new lastChunk
|
---|
| 1222 | if (chunk.end !== end) {
|
---|
| 1223 | if (this.lastChunk === chunk) {
|
---|
| 1224 | this.lastChunk = chunk.next;
|
---|
| 1225 | }
|
---|
| 1226 |
|
---|
| 1227 | this.byEnd[chunk.end] = chunk;
|
---|
| 1228 | this.byStart[chunk.next.start] = chunk.next;
|
---|
| 1229 | this.byEnd[chunk.next.end] = chunk.next;
|
---|
| 1230 | }
|
---|
| 1231 |
|
---|
| 1232 | if (aborted) return true;
|
---|
| 1233 | chunk = chunk.previous;
|
---|
| 1234 | } while (chunk);
|
---|
| 1235 |
|
---|
| 1236 | return false;
|
---|
| 1237 | }
|
---|
| 1238 |
|
---|
| 1239 | trimEnd(charType) {
|
---|
| 1240 | this.trimEndAborted(charType);
|
---|
| 1241 | return this;
|
---|
| 1242 | }
|
---|
| 1243 | trimStartAborted(charType) {
|
---|
| 1244 | const rx = new RegExp('^' + (charType || '\\s') + '+');
|
---|
| 1245 |
|
---|
| 1246 | this.intro = this.intro.replace(rx, '');
|
---|
| 1247 | if (this.intro.length) return true;
|
---|
| 1248 |
|
---|
| 1249 | let chunk = this.firstChunk;
|
---|
| 1250 |
|
---|
| 1251 | do {
|
---|
| 1252 | const end = chunk.end;
|
---|
| 1253 | const aborted = chunk.trimStart(rx);
|
---|
| 1254 |
|
---|
| 1255 | if (chunk.end !== end) {
|
---|
| 1256 | // special case...
|
---|
| 1257 | if (chunk === this.lastChunk) this.lastChunk = chunk.next;
|
---|
| 1258 |
|
---|
| 1259 | this.byEnd[chunk.end] = chunk;
|
---|
| 1260 | this.byStart[chunk.next.start] = chunk.next;
|
---|
| 1261 | this.byEnd[chunk.next.end] = chunk.next;
|
---|
| 1262 | }
|
---|
| 1263 |
|
---|
| 1264 | if (aborted) return true;
|
---|
| 1265 | chunk = chunk.next;
|
---|
| 1266 | } while (chunk);
|
---|
| 1267 |
|
---|
| 1268 | return false;
|
---|
| 1269 | }
|
---|
| 1270 |
|
---|
| 1271 | trimStart(charType) {
|
---|
| 1272 | this.trimStartAborted(charType);
|
---|
| 1273 | return this;
|
---|
| 1274 | }
|
---|
| 1275 |
|
---|
| 1276 | hasChanged() {
|
---|
| 1277 | return this.original !== this.toString();
|
---|
| 1278 | }
|
---|
| 1279 |
|
---|
| 1280 | _replaceRegexp(searchValue, replacement) {
|
---|
| 1281 | function getReplacement(match, str) {
|
---|
| 1282 | if (typeof replacement === 'string') {
|
---|
| 1283 | return replacement.replace(/\$(\$|&|\d+)/g, (_, i) => {
|
---|
| 1284 | // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_a_parameter
|
---|
| 1285 | if (i === '$') return '$';
|
---|
| 1286 | if (i === '&') return match[0];
|
---|
| 1287 | const num = +i;
|
---|
| 1288 | if (num < match.length) return match[+i];
|
---|
| 1289 | return `$${i}`;
|
---|
| 1290 | });
|
---|
| 1291 | } else {
|
---|
| 1292 | return replacement(...match, match.index, str, match.groups);
|
---|
| 1293 | }
|
---|
| 1294 | }
|
---|
| 1295 | function matchAll(re, str) {
|
---|
| 1296 | let match;
|
---|
| 1297 | const matches = [];
|
---|
| 1298 | while ((match = re.exec(str))) {
|
---|
| 1299 | matches.push(match);
|
---|
| 1300 | }
|
---|
| 1301 | return matches;
|
---|
| 1302 | }
|
---|
| 1303 | if (searchValue.global) {
|
---|
| 1304 | const matches = matchAll(searchValue, this.original);
|
---|
| 1305 | matches.forEach((match) => {
|
---|
| 1306 | if (match.index != null) {
|
---|
| 1307 | const replacement = getReplacement(match, this.original);
|
---|
| 1308 | if (replacement !== match[0]) {
|
---|
| 1309 | this.overwrite(match.index, match.index + match[0].length, replacement);
|
---|
| 1310 | }
|
---|
| 1311 | }
|
---|
| 1312 | });
|
---|
| 1313 | } else {
|
---|
| 1314 | const match = this.original.match(searchValue);
|
---|
| 1315 | if (match && match.index != null) {
|
---|
| 1316 | const replacement = getReplacement(match, this.original);
|
---|
| 1317 | if (replacement !== match[0]) {
|
---|
| 1318 | this.overwrite(match.index, match.index + match[0].length, replacement);
|
---|
| 1319 | }
|
---|
| 1320 | }
|
---|
| 1321 | }
|
---|
| 1322 | return this;
|
---|
| 1323 | }
|
---|
| 1324 |
|
---|
| 1325 | _replaceString(string, replacement) {
|
---|
| 1326 | const { original } = this;
|
---|
| 1327 | const index = original.indexOf(string);
|
---|
| 1328 |
|
---|
| 1329 | if (index !== -1) {
|
---|
| 1330 | this.overwrite(index, index + string.length, replacement);
|
---|
| 1331 | }
|
---|
| 1332 |
|
---|
| 1333 | return this;
|
---|
| 1334 | }
|
---|
| 1335 |
|
---|
| 1336 | replace(searchValue, replacement) {
|
---|
| 1337 | if (typeof searchValue === 'string') {
|
---|
| 1338 | return this._replaceString(searchValue, replacement);
|
---|
| 1339 | }
|
---|
| 1340 |
|
---|
| 1341 | return this._replaceRegexp(searchValue, replacement);
|
---|
| 1342 | }
|
---|
| 1343 |
|
---|
| 1344 | _replaceAllString(string, replacement) {
|
---|
| 1345 | const { original } = this;
|
---|
| 1346 | const stringLength = string.length;
|
---|
| 1347 | for (
|
---|
| 1348 | let index = original.indexOf(string);
|
---|
| 1349 | index !== -1;
|
---|
| 1350 | index = original.indexOf(string, index + stringLength)
|
---|
| 1351 | ) {
|
---|
| 1352 | const previous = original.slice(index, index + stringLength);
|
---|
| 1353 | if (previous !== replacement) this.overwrite(index, index + stringLength, replacement);
|
---|
| 1354 | }
|
---|
| 1355 |
|
---|
| 1356 | return this;
|
---|
| 1357 | }
|
---|
| 1358 |
|
---|
| 1359 | replaceAll(searchValue, replacement) {
|
---|
| 1360 | if (typeof searchValue === 'string') {
|
---|
| 1361 | return this._replaceAllString(searchValue, replacement);
|
---|
| 1362 | }
|
---|
| 1363 |
|
---|
| 1364 | if (!searchValue.global) {
|
---|
| 1365 | throw new TypeError(
|
---|
| 1366 | 'MagicString.prototype.replaceAll called with a non-global RegExp argument',
|
---|
| 1367 | );
|
---|
| 1368 | }
|
---|
| 1369 |
|
---|
| 1370 | return this._replaceRegexp(searchValue, replacement);
|
---|
| 1371 | }
|
---|
| 1372 | }
|
---|
| 1373 |
|
---|
| 1374 | const hasOwnProp = Object.prototype.hasOwnProperty;
|
---|
| 1375 |
|
---|
| 1376 | class Bundle {
|
---|
| 1377 | constructor(options = {}) {
|
---|
| 1378 | this.intro = options.intro || '';
|
---|
| 1379 | this.separator = options.separator !== undefined ? options.separator : '\n';
|
---|
| 1380 | this.sources = [];
|
---|
| 1381 | this.uniqueSources = [];
|
---|
| 1382 | this.uniqueSourceIndexByFilename = {};
|
---|
| 1383 | }
|
---|
| 1384 |
|
---|
| 1385 | addSource(source) {
|
---|
| 1386 | if (source instanceof MagicString) {
|
---|
| 1387 | return this.addSource({
|
---|
| 1388 | content: source,
|
---|
| 1389 | filename: source.filename,
|
---|
| 1390 | separator: this.separator,
|
---|
| 1391 | });
|
---|
| 1392 | }
|
---|
| 1393 |
|
---|
| 1394 | if (!isObject(source) || !source.content) {
|
---|
| 1395 | throw new Error(
|
---|
| 1396 | 'bundle.addSource() takes an object with a `content` property, which should be an instance of MagicString, and an optional `filename`',
|
---|
| 1397 | );
|
---|
| 1398 | }
|
---|
| 1399 |
|
---|
| 1400 | ['filename', 'ignoreList', 'indentExclusionRanges', 'separator'].forEach((option) => {
|
---|
| 1401 | if (!hasOwnProp.call(source, option)) source[option] = source.content[option];
|
---|
| 1402 | });
|
---|
| 1403 |
|
---|
| 1404 | if (source.separator === undefined) {
|
---|
| 1405 | // TODO there's a bunch of this sort of thing, needs cleaning up
|
---|
| 1406 | source.separator = this.separator;
|
---|
| 1407 | }
|
---|
| 1408 |
|
---|
| 1409 | if (source.filename) {
|
---|
| 1410 | if (!hasOwnProp.call(this.uniqueSourceIndexByFilename, source.filename)) {
|
---|
| 1411 | this.uniqueSourceIndexByFilename[source.filename] = this.uniqueSources.length;
|
---|
| 1412 | this.uniqueSources.push({ filename: source.filename, content: source.content.original });
|
---|
| 1413 | } else {
|
---|
| 1414 | const uniqueSource = this.uniqueSources[this.uniqueSourceIndexByFilename[source.filename]];
|
---|
| 1415 | if (source.content.original !== uniqueSource.content) {
|
---|
| 1416 | throw new Error(`Illegal source: same filename (${source.filename}), different contents`);
|
---|
| 1417 | }
|
---|
| 1418 | }
|
---|
| 1419 | }
|
---|
| 1420 |
|
---|
| 1421 | this.sources.push(source);
|
---|
| 1422 | return this;
|
---|
| 1423 | }
|
---|
| 1424 |
|
---|
| 1425 | append(str, options) {
|
---|
| 1426 | this.addSource({
|
---|
| 1427 | content: new MagicString(str),
|
---|
| 1428 | separator: (options && options.separator) || '',
|
---|
| 1429 | });
|
---|
| 1430 |
|
---|
| 1431 | return this;
|
---|
| 1432 | }
|
---|
| 1433 |
|
---|
| 1434 | clone() {
|
---|
| 1435 | const bundle = new Bundle({
|
---|
| 1436 | intro: this.intro,
|
---|
| 1437 | separator: this.separator,
|
---|
| 1438 | });
|
---|
| 1439 |
|
---|
| 1440 | this.sources.forEach((source) => {
|
---|
| 1441 | bundle.addSource({
|
---|
| 1442 | filename: source.filename,
|
---|
| 1443 | content: source.content.clone(),
|
---|
| 1444 | separator: source.separator,
|
---|
| 1445 | });
|
---|
| 1446 | });
|
---|
| 1447 |
|
---|
| 1448 | return bundle;
|
---|
| 1449 | }
|
---|
| 1450 |
|
---|
| 1451 | generateDecodedMap(options = {}) {
|
---|
| 1452 | const names = [];
|
---|
| 1453 | let x_google_ignoreList = undefined;
|
---|
| 1454 | this.sources.forEach((source) => {
|
---|
| 1455 | Object.keys(source.content.storedNames).forEach((name) => {
|
---|
| 1456 | if (!~names.indexOf(name)) names.push(name);
|
---|
| 1457 | });
|
---|
| 1458 | });
|
---|
| 1459 |
|
---|
| 1460 | const mappings = new Mappings(options.hires);
|
---|
| 1461 |
|
---|
| 1462 | if (this.intro) {
|
---|
| 1463 | mappings.advance(this.intro);
|
---|
| 1464 | }
|
---|
| 1465 |
|
---|
| 1466 | this.sources.forEach((source, i) => {
|
---|
| 1467 | if (i > 0) {
|
---|
| 1468 | mappings.advance(this.separator);
|
---|
| 1469 | }
|
---|
| 1470 |
|
---|
| 1471 | const sourceIndex = source.filename ? this.uniqueSourceIndexByFilename[source.filename] : -1;
|
---|
| 1472 | const magicString = source.content;
|
---|
| 1473 | const locate = getLocator(magicString.original);
|
---|
| 1474 |
|
---|
| 1475 | if (magicString.intro) {
|
---|
| 1476 | mappings.advance(magicString.intro);
|
---|
| 1477 | }
|
---|
| 1478 |
|
---|
| 1479 | magicString.firstChunk.eachNext((chunk) => {
|
---|
| 1480 | const loc = locate(chunk.start);
|
---|
| 1481 |
|
---|
| 1482 | if (chunk.intro.length) mappings.advance(chunk.intro);
|
---|
| 1483 |
|
---|
| 1484 | if (source.filename) {
|
---|
| 1485 | if (chunk.edited) {
|
---|
| 1486 | mappings.addEdit(
|
---|
| 1487 | sourceIndex,
|
---|
| 1488 | chunk.content,
|
---|
| 1489 | loc,
|
---|
| 1490 | chunk.storeName ? names.indexOf(chunk.original) : -1,
|
---|
| 1491 | );
|
---|
| 1492 | } else {
|
---|
| 1493 | mappings.addUneditedChunk(
|
---|
| 1494 | sourceIndex,
|
---|
| 1495 | chunk,
|
---|
| 1496 | magicString.original,
|
---|
| 1497 | loc,
|
---|
| 1498 | magicString.sourcemapLocations,
|
---|
| 1499 | );
|
---|
| 1500 | }
|
---|
| 1501 | } else {
|
---|
| 1502 | mappings.advance(chunk.content);
|
---|
| 1503 | }
|
---|
| 1504 |
|
---|
| 1505 | if (chunk.outro.length) mappings.advance(chunk.outro);
|
---|
| 1506 | });
|
---|
| 1507 |
|
---|
| 1508 | if (magicString.outro) {
|
---|
| 1509 | mappings.advance(magicString.outro);
|
---|
| 1510 | }
|
---|
| 1511 |
|
---|
| 1512 | if (source.ignoreList && sourceIndex !== -1) {
|
---|
| 1513 | if (x_google_ignoreList === undefined) {
|
---|
| 1514 | x_google_ignoreList = [];
|
---|
| 1515 | }
|
---|
| 1516 | x_google_ignoreList.push(sourceIndex);
|
---|
| 1517 | }
|
---|
| 1518 | });
|
---|
| 1519 |
|
---|
| 1520 | return {
|
---|
| 1521 | file: options.file ? options.file.split(/[/\\]/).pop() : undefined,
|
---|
| 1522 | sources: this.uniqueSources.map((source) => {
|
---|
| 1523 | return options.file ? getRelativePath(options.file, source.filename) : source.filename;
|
---|
| 1524 | }),
|
---|
| 1525 | sourcesContent: this.uniqueSources.map((source) => {
|
---|
| 1526 | return options.includeContent ? source.content : null;
|
---|
| 1527 | }),
|
---|
| 1528 | names,
|
---|
| 1529 | mappings: mappings.raw,
|
---|
| 1530 | x_google_ignoreList,
|
---|
| 1531 | };
|
---|
| 1532 | }
|
---|
| 1533 |
|
---|
| 1534 | generateMap(options) {
|
---|
| 1535 | return new SourceMap(this.generateDecodedMap(options));
|
---|
| 1536 | }
|
---|
| 1537 |
|
---|
| 1538 | getIndentString() {
|
---|
| 1539 | const indentStringCounts = {};
|
---|
| 1540 |
|
---|
| 1541 | this.sources.forEach((source) => {
|
---|
| 1542 | const indentStr = source.content._getRawIndentString();
|
---|
| 1543 |
|
---|
| 1544 | if (indentStr === null) return;
|
---|
| 1545 |
|
---|
| 1546 | if (!indentStringCounts[indentStr]) indentStringCounts[indentStr] = 0;
|
---|
| 1547 | indentStringCounts[indentStr] += 1;
|
---|
| 1548 | });
|
---|
| 1549 |
|
---|
| 1550 | return (
|
---|
| 1551 | Object.keys(indentStringCounts).sort((a, b) => {
|
---|
| 1552 | return indentStringCounts[a] - indentStringCounts[b];
|
---|
| 1553 | })[0] || '\t'
|
---|
| 1554 | );
|
---|
| 1555 | }
|
---|
| 1556 |
|
---|
| 1557 | indent(indentStr) {
|
---|
| 1558 | if (!arguments.length) {
|
---|
| 1559 | indentStr = this.getIndentString();
|
---|
| 1560 | }
|
---|
| 1561 |
|
---|
| 1562 | if (indentStr === '') return this; // noop
|
---|
| 1563 |
|
---|
| 1564 | let trailingNewline = !this.intro || this.intro.slice(-1) === '\n';
|
---|
| 1565 |
|
---|
| 1566 | this.sources.forEach((source, i) => {
|
---|
| 1567 | const separator = source.separator !== undefined ? source.separator : this.separator;
|
---|
| 1568 | const indentStart = trailingNewline || (i > 0 && /\r?\n$/.test(separator));
|
---|
| 1569 |
|
---|
| 1570 | source.content.indent(indentStr, {
|
---|
| 1571 | exclude: source.indentExclusionRanges,
|
---|
| 1572 | indentStart, //: trailingNewline || /\r?\n$/.test( separator ) //true///\r?\n/.test( separator )
|
---|
| 1573 | });
|
---|
| 1574 |
|
---|
| 1575 | trailingNewline = source.content.lastChar() === '\n';
|
---|
| 1576 | });
|
---|
| 1577 |
|
---|
| 1578 | if (this.intro) {
|
---|
| 1579 | this.intro =
|
---|
| 1580 | indentStr +
|
---|
| 1581 | this.intro.replace(/^[^\n]/gm, (match, index) => {
|
---|
| 1582 | return index > 0 ? indentStr + match : match;
|
---|
| 1583 | });
|
---|
| 1584 | }
|
---|
| 1585 |
|
---|
| 1586 | return this;
|
---|
| 1587 | }
|
---|
| 1588 |
|
---|
| 1589 | prepend(str) {
|
---|
| 1590 | this.intro = str + this.intro;
|
---|
| 1591 | return this;
|
---|
| 1592 | }
|
---|
| 1593 |
|
---|
| 1594 | toString() {
|
---|
| 1595 | const body = this.sources
|
---|
| 1596 | .map((source, i) => {
|
---|
| 1597 | const separator = source.separator !== undefined ? source.separator : this.separator;
|
---|
| 1598 | const str = (i > 0 ? separator : '') + source.content.toString();
|
---|
| 1599 |
|
---|
| 1600 | return str;
|
---|
| 1601 | })
|
---|
| 1602 | .join('');
|
---|
| 1603 |
|
---|
| 1604 | return this.intro + body;
|
---|
| 1605 | }
|
---|
| 1606 |
|
---|
| 1607 | isEmpty() {
|
---|
| 1608 | if (this.intro.length && this.intro.trim()) return false;
|
---|
| 1609 | if (this.sources.some((source) => !source.content.isEmpty())) return false;
|
---|
| 1610 | return true;
|
---|
| 1611 | }
|
---|
| 1612 |
|
---|
| 1613 | length() {
|
---|
| 1614 | return this.sources.reduce(
|
---|
| 1615 | (length, source) => length + source.content.length(),
|
---|
| 1616 | this.intro.length,
|
---|
| 1617 | );
|
---|
| 1618 | }
|
---|
| 1619 |
|
---|
| 1620 | trimLines() {
|
---|
| 1621 | return this.trim('[\\r\\n]');
|
---|
| 1622 | }
|
---|
| 1623 |
|
---|
| 1624 | trim(charType) {
|
---|
| 1625 | return this.trimStart(charType).trimEnd(charType);
|
---|
| 1626 | }
|
---|
| 1627 |
|
---|
| 1628 | trimStart(charType) {
|
---|
| 1629 | const rx = new RegExp('^' + (charType || '\\s') + '+');
|
---|
| 1630 | this.intro = this.intro.replace(rx, '');
|
---|
| 1631 |
|
---|
| 1632 | if (!this.intro) {
|
---|
| 1633 | let source;
|
---|
| 1634 | let i = 0;
|
---|
| 1635 |
|
---|
| 1636 | do {
|
---|
| 1637 | source = this.sources[i++];
|
---|
| 1638 | if (!source) {
|
---|
| 1639 | break;
|
---|
| 1640 | }
|
---|
| 1641 | } while (!source.content.trimStartAborted(charType));
|
---|
| 1642 | }
|
---|
| 1643 |
|
---|
| 1644 | return this;
|
---|
| 1645 | }
|
---|
| 1646 |
|
---|
| 1647 | trimEnd(charType) {
|
---|
| 1648 | const rx = new RegExp((charType || '\\s') + '+$');
|
---|
| 1649 |
|
---|
| 1650 | let source;
|
---|
| 1651 | let i = this.sources.length - 1;
|
---|
| 1652 |
|
---|
| 1653 | do {
|
---|
| 1654 | source = this.sources[i--];
|
---|
| 1655 | if (!source) {
|
---|
| 1656 | this.intro = this.intro.replace(rx, '');
|
---|
| 1657 | break;
|
---|
| 1658 | }
|
---|
| 1659 | } while (!source.content.trimEndAborted(charType));
|
---|
| 1660 |
|
---|
| 1661 | return this;
|
---|
| 1662 | }
|
---|
| 1663 | }
|
---|
| 1664 |
|
---|
| 1665 | MagicString.Bundle = Bundle;
|
---|
| 1666 | MagicString.SourceMap = SourceMap;
|
---|
| 1667 | MagicString.default = MagicString; // work around TypeScript bug https://github.com/Rich-Harris/magic-string/pull/121
|
---|
| 1668 |
|
---|
| 1669 | return MagicString;
|
---|
| 1670 |
|
---|
| 1671 | }));
|
---|
| 1672 | //# sourceMappingURL=magic-string.umd.js.map
|
---|