magic-string
Advanced tools
Comparing version 0.25.9 to 0.26.0
@@ -5,198 +5,204 @@ 'use strict'; | ||
var BitSet = function BitSet(arg) { | ||
this.bits = arg instanceof BitSet ? arg.bits.slice() : []; | ||
}; | ||
class BitSet { | ||
constructor(arg) { | ||
this.bits = arg instanceof BitSet ? arg.bits.slice() : []; | ||
} | ||
BitSet.prototype.add = function add (n) { | ||
this.bits[n >> 5] |= 1 << (n & 31); | ||
}; | ||
add(n) { | ||
this.bits[n >> 5] |= 1 << (n & 31); | ||
} | ||
BitSet.prototype.has = function has (n) { | ||
return !!(this.bits[n >> 5] & (1 << (n & 31))); | ||
}; | ||
has(n) { | ||
return !!(this.bits[n >> 5] & (1 << (n & 31))); | ||
} | ||
} | ||
var Chunk = function Chunk(start, end, content) { | ||
this.start = start; | ||
this.end = end; | ||
this.original = content; | ||
class Chunk { | ||
constructor(start, end, content) { | ||
this.start = start; | ||
this.end = end; | ||
this.original = content; | ||
this.intro = ''; | ||
this.outro = ''; | ||
this.intro = ''; | ||
this.outro = ''; | ||
this.content = content; | ||
this.storeName = false; | ||
this.edited = false; | ||
this.content = content; | ||
this.storeName = false; | ||
this.edited = false; | ||
// we make these non-enumerable, for sanity while debugging | ||
Object.defineProperties(this, { | ||
previous: { writable: true, value: null }, | ||
next: { writable: true, value: null }, | ||
}); | ||
}; | ||
// we make these non-enumerable, for sanity while debugging | ||
Object.defineProperties(this, { | ||
previous: { writable: true, value: null }, | ||
next: { writable: true, value: null }, | ||
}); | ||
} | ||
Chunk.prototype.appendLeft = function appendLeft (content) { | ||
this.outro += content; | ||
}; | ||
appendLeft(content) { | ||
this.outro += content; | ||
} | ||
Chunk.prototype.appendRight = function appendRight (content) { | ||
this.intro = this.intro + content; | ||
}; | ||
appendRight(content) { | ||
this.intro = this.intro + content; | ||
} | ||
Chunk.prototype.clone = function clone () { | ||
var chunk = new Chunk(this.start, this.end, this.original); | ||
clone() { | ||
const chunk = new Chunk(this.start, this.end, this.original); | ||
chunk.intro = this.intro; | ||
chunk.outro = this.outro; | ||
chunk.content = this.content; | ||
chunk.storeName = this.storeName; | ||
chunk.edited = this.edited; | ||
chunk.intro = this.intro; | ||
chunk.outro = this.outro; | ||
chunk.content = this.content; | ||
chunk.storeName = this.storeName; | ||
chunk.edited = this.edited; | ||
return chunk; | ||
}; | ||
return chunk; | ||
} | ||
Chunk.prototype.contains = function contains (index) { | ||
return this.start < index && index < this.end; | ||
}; | ||
contains(index) { | ||
return this.start < index && index < this.end; | ||
} | ||
Chunk.prototype.eachNext = function eachNext (fn) { | ||
var chunk = this; | ||
while (chunk) { | ||
fn(chunk); | ||
chunk = chunk.next; | ||
eachNext(fn) { | ||
let chunk = this; | ||
while (chunk) { | ||
fn(chunk); | ||
chunk = chunk.next; | ||
} | ||
} | ||
}; | ||
Chunk.prototype.eachPrevious = function eachPrevious (fn) { | ||
var chunk = this; | ||
while (chunk) { | ||
fn(chunk); | ||
chunk = chunk.previous; | ||
eachPrevious(fn) { | ||
let chunk = this; | ||
while (chunk) { | ||
fn(chunk); | ||
chunk = chunk.previous; | ||
} | ||
} | ||
}; | ||
Chunk.prototype.edit = function edit (content, storeName, contentOnly) { | ||
this.content = content; | ||
if (!contentOnly) { | ||
this.intro = ''; | ||
this.outro = ''; | ||
edit(content, storeName, contentOnly) { | ||
this.content = content; | ||
if (!contentOnly) { | ||
this.intro = ''; | ||
this.outro = ''; | ||
} | ||
this.storeName = storeName; | ||
this.edited = true; | ||
return this; | ||
} | ||
this.storeName = storeName; | ||
this.edited = true; | ||
prependLeft(content) { | ||
this.outro = content + this.outro; | ||
} | ||
return this; | ||
}; | ||
prependRight(content) { | ||
this.intro = content + this.intro; | ||
} | ||
Chunk.prototype.prependLeft = function prependLeft (content) { | ||
this.outro = content + this.outro; | ||
}; | ||
split(index) { | ||
const sliceIndex = index - this.start; | ||
Chunk.prototype.prependRight = function prependRight (content) { | ||
this.intro = content + this.intro; | ||
}; | ||
const originalBefore = this.original.slice(0, sliceIndex); | ||
const originalAfter = this.original.slice(sliceIndex); | ||
Chunk.prototype.split = function split (index) { | ||
var sliceIndex = index - this.start; | ||
this.original = originalBefore; | ||
var originalBefore = this.original.slice(0, sliceIndex); | ||
var originalAfter = this.original.slice(sliceIndex); | ||
const newChunk = new Chunk(index, this.end, originalAfter); | ||
newChunk.outro = this.outro; | ||
this.outro = ''; | ||
this.original = originalBefore; | ||
this.end = index; | ||
var newChunk = new Chunk(index, this.end, originalAfter); | ||
newChunk.outro = this.outro; | ||
this.outro = ''; | ||
if (this.edited) { | ||
// TODO is this block necessary?... | ||
newChunk.edit('', false); | ||
this.content = ''; | ||
} else { | ||
this.content = originalBefore; | ||
} | ||
this.end = index; | ||
newChunk.next = this.next; | ||
if (newChunk.next) newChunk.next.previous = newChunk; | ||
newChunk.previous = this; | ||
this.next = newChunk; | ||
if (this.edited) { | ||
// TODO is this block necessary?... | ||
newChunk.edit('', false); | ||
this.content = ''; | ||
} else { | ||
this.content = originalBefore; | ||
return newChunk; | ||
} | ||
newChunk.next = this.next; | ||
if (newChunk.next) { newChunk.next.previous = newChunk; } | ||
newChunk.previous = this; | ||
this.next = newChunk; | ||
toString() { | ||
return this.intro + this.content + this.outro; | ||
} | ||
return newChunk; | ||
}; | ||
trimEnd(rx) { | ||
this.outro = this.outro.replace(rx, ''); | ||
if (this.outro.length) return true; | ||
Chunk.prototype.toString = function toString () { | ||
return this.intro + this.content + this.outro; | ||
}; | ||
const trimmed = this.content.replace(rx, ''); | ||
Chunk.prototype.trimEnd = function trimEnd (rx) { | ||
this.outro = this.outro.replace(rx, ''); | ||
if (this.outro.length) { return true; } | ||
if (trimmed.length) { | ||
if (trimmed !== this.content) { | ||
this.split(this.start + trimmed.length).edit('', undefined, true); | ||
} | ||
return true; | ||
} else { | ||
this.edit('', undefined, true); | ||
var trimmed = this.content.replace(rx, ''); | ||
if (trimmed.length) { | ||
if (trimmed !== this.content) { | ||
this.split(this.start + trimmed.length).edit('', undefined, true); | ||
this.intro = this.intro.replace(rx, ''); | ||
if (this.intro.length) return true; | ||
} | ||
return true; | ||
} else { | ||
this.edit('', undefined, true); | ||
} | ||
trimStart(rx) { | ||
this.intro = this.intro.replace(rx, ''); | ||
if (this.intro.length) { return true; } | ||
} | ||
}; | ||
if (this.intro.length) return true; | ||
Chunk.prototype.trimStart = function trimStart (rx) { | ||
this.intro = this.intro.replace(rx, ''); | ||
if (this.intro.length) { return true; } | ||
const trimmed = this.content.replace(rx, ''); | ||
var trimmed = this.content.replace(rx, ''); | ||
if (trimmed.length) { | ||
if (trimmed !== this.content) { | ||
this.split(this.end - trimmed.length); | ||
this.edit('', undefined, true); | ||
} | ||
return true; | ||
} else { | ||
this.edit('', undefined, true); | ||
if (trimmed.length) { | ||
if (trimmed !== this.content) { | ||
this.split(this.end - trimmed.length); | ||
this.edit('', undefined, true); | ||
this.outro = this.outro.replace(rx, ''); | ||
if (this.outro.length) return true; | ||
} | ||
return true; | ||
} else { | ||
this.edit('', undefined, true); | ||
this.outro = this.outro.replace(rx, ''); | ||
if (this.outro.length) { return true; } | ||
} | ||
}; | ||
} | ||
var btoa = function () { | ||
let btoa = () => { | ||
throw new Error('Unsupported environment: `window.btoa` or `Buffer` should be supported.'); | ||
}; | ||
if (typeof window !== 'undefined' && typeof window.btoa === 'function') { | ||
btoa = function (str) { return window.btoa(unescape(encodeURIComponent(str))); }; | ||
btoa = (str) => window.btoa(unescape(encodeURIComponent(str))); | ||
} else if (typeof Buffer === 'function') { | ||
btoa = function (str) { return Buffer.from(str, 'utf-8').toString('base64'); }; | ||
btoa = (str) => Buffer.from(str, 'utf-8').toString('base64'); | ||
} | ||
var SourceMap = function SourceMap(properties) { | ||
this.version = 3; | ||
this.file = properties.file; | ||
this.sources = properties.sources; | ||
this.sourcesContent = properties.sourcesContent; | ||
this.names = properties.names; | ||
this.mappings = sourcemapCodec.encode(properties.mappings); | ||
}; | ||
class SourceMap { | ||
constructor(properties) { | ||
this.version = 3; | ||
this.file = properties.file; | ||
this.sources = properties.sources; | ||
this.sourcesContent = properties.sourcesContent; | ||
this.names = properties.names; | ||
this.mappings = sourcemapCodec.encode(properties.mappings); | ||
} | ||
SourceMap.prototype.toString = function toString () { | ||
return JSON.stringify(this); | ||
}; | ||
toString() { | ||
return JSON.stringify(this); | ||
} | ||
SourceMap.prototype.toUrl = function toUrl () { | ||
return 'data:application/json;charset=utf-8;base64,' + btoa(this.toString()); | ||
}; | ||
toUrl() { | ||
return 'data:application/json;charset=utf-8;base64,' + btoa(this.toString()); | ||
} | ||
} | ||
function guessIndent(code) { | ||
var lines = code.split('\n'); | ||
const lines = code.split('\n'); | ||
var tabbed = lines.filter(function (line) { return /^\t+/.test(line); }); | ||
var spaced = lines.filter(function (line) { return /^ {2,}/.test(line); }); | ||
const tabbed = lines.filter((line) => /^\t+/.test(line)); | ||
const spaced = lines.filter((line) => /^ {2,}/.test(line)); | ||
@@ -215,4 +221,4 @@ if (tabbed.length === 0 && spaced.length === 0) { | ||
// Otherwise, we need to guess the multiple | ||
var min = spaced.reduce(function (previous, current) { | ||
var numSpaces = /^ +/.exec(current)[0].length; | ||
const min = spaced.reduce((previous, current) => { | ||
const numSpaces = /^ +/.exec(current)[0].length; | ||
return Math.min(numSpaces, previous); | ||
@@ -225,4 +231,4 @@ }, Infinity); | ||
function getRelativePath(from, to) { | ||
var fromParts = from.split(/[/\\]/); | ||
var toParts = to.split(/[/\\]/); | ||
const fromParts = from.split(/[/\\]/); | ||
const toParts = to.split(/[/\\]/); | ||
@@ -237,4 +243,4 @@ fromParts.pop(); // get dirname | ||
if (fromParts.length) { | ||
var i = fromParts.length; | ||
while (i--) { fromParts[i] = '..'; } | ||
let i = fromParts.length; | ||
while (i--) fromParts[i] = '..'; | ||
} | ||
@@ -245,3 +251,3 @@ | ||
var toString = Object.prototype.toString; | ||
const toString = Object.prototype.toString; | ||
@@ -253,6 +259,6 @@ function isObject(thing) { | ||
function getLocator(source) { | ||
var originalLines = source.split('\n'); | ||
var lineOffsets = []; | ||
const originalLines = source.split('\n'); | ||
const lineOffsets = []; | ||
for (var i = 0, pos = 0; i < originalLines.length; i++) { | ||
for (let i = 0, pos = 0; i < originalLines.length; i++) { | ||
lineOffsets.push(pos); | ||
@@ -263,6 +269,6 @@ pos += originalLines[i].length + 1; | ||
return function locate(index) { | ||
var i = 0; | ||
var j = lineOffsets.length; | ||
let i = 0; | ||
let j = lineOffsets.length; | ||
while (i < j) { | ||
var m = (i + j) >> 1; | ||
const m = (i + j) >> 1; | ||
if (index < lineOffsets[m]) { | ||
@@ -274,79 +280,81 @@ j = m; | ||
} | ||
var line = i - 1; | ||
var column = index - lineOffsets[line]; | ||
return { line: line, column: column }; | ||
const line = i - 1; | ||
const column = index - lineOffsets[line]; | ||
return { line, column }; | ||
}; | ||
} | ||
var Mappings = function Mappings(hires) { | ||
this.hires = hires; | ||
this.generatedCodeLine = 0; | ||
this.generatedCodeColumn = 0; | ||
this.raw = []; | ||
this.rawSegments = this.raw[this.generatedCodeLine] = []; | ||
this.pending = null; | ||
}; | ||
class Mappings { | ||
constructor(hires) { | ||
this.hires = hires; | ||
this.generatedCodeLine = 0; | ||
this.generatedCodeColumn = 0; | ||
this.raw = []; | ||
this.rawSegments = this.raw[this.generatedCodeLine] = []; | ||
this.pending = null; | ||
} | ||
Mappings.prototype.addEdit = function addEdit (sourceIndex, content, loc, nameIndex) { | ||
if (content.length) { | ||
var segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column]; | ||
if (nameIndex >= 0) { | ||
segment.push(nameIndex); | ||
addEdit(sourceIndex, content, loc, nameIndex) { | ||
if (content.length) { | ||
const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column]; | ||
if (nameIndex >= 0) { | ||
segment.push(nameIndex); | ||
} | ||
this.rawSegments.push(segment); | ||
} else if (this.pending) { | ||
this.rawSegments.push(this.pending); | ||
} | ||
this.rawSegments.push(segment); | ||
} else if (this.pending) { | ||
this.rawSegments.push(this.pending); | ||
this.advance(content); | ||
this.pending = null; | ||
} | ||
this.advance(content); | ||
this.pending = null; | ||
}; | ||
addUneditedChunk(sourceIndex, chunk, original, loc, sourcemapLocations) { | ||
let originalCharIndex = chunk.start; | ||
let first = true; | ||
Mappings.prototype.addUneditedChunk = function addUneditedChunk (sourceIndex, chunk, original, loc, sourcemapLocations) { | ||
var originalCharIndex = chunk.start; | ||
var first = true; | ||
while (originalCharIndex < chunk.end) { | ||
if (this.hires || first || sourcemapLocations.has(originalCharIndex)) { | ||
this.rawSegments.push([this.generatedCodeColumn, sourceIndex, loc.line, loc.column]); | ||
} | ||
while (originalCharIndex < chunk.end) { | ||
if (this.hires || first || sourcemapLocations.has(originalCharIndex)) { | ||
this.rawSegments.push([this.generatedCodeColumn, sourceIndex, loc.line, loc.column]); | ||
} | ||
if (original[originalCharIndex] === '\n') { | ||
loc.line += 1; | ||
loc.column = 0; | ||
this.generatedCodeLine += 1; | ||
this.raw[this.generatedCodeLine] = this.rawSegments = []; | ||
this.generatedCodeColumn = 0; | ||
first = true; | ||
} else { | ||
loc.column += 1; | ||
this.generatedCodeColumn += 1; | ||
first = false; | ||
} | ||
if (original[originalCharIndex] === '\n') { | ||
loc.line += 1; | ||
loc.column = 0; | ||
this.generatedCodeLine += 1; | ||
this.raw[this.generatedCodeLine] = this.rawSegments = []; | ||
this.generatedCodeColumn = 0; | ||
first = true; | ||
} else { | ||
loc.column += 1; | ||
this.generatedCodeColumn += 1; | ||
first = false; | ||
originalCharIndex += 1; | ||
} | ||
originalCharIndex += 1; | ||
this.pending = null; | ||
} | ||
this.pending = null; | ||
}; | ||
advance(str) { | ||
if (!str) return; | ||
Mappings.prototype.advance = function advance (str) { | ||
if (!str) { return; } | ||
const lines = str.split('\n'); | ||
var lines = str.split('\n'); | ||
if (lines.length > 1) { | ||
for (let i = 0; i < lines.length - 1; i++) { | ||
this.generatedCodeLine++; | ||
this.raw[this.generatedCodeLine] = this.rawSegments = []; | ||
} | ||
this.generatedCodeColumn = 0; | ||
} | ||
if (lines.length > 1) { | ||
for (var i = 0; i < lines.length - 1; i++) { | ||
this.generatedCodeLine++; | ||
this.raw[this.generatedCodeLine] = this.rawSegments = []; | ||
} | ||
this.generatedCodeColumn = 0; | ||
this.generatedCodeColumn += lines[lines.length - 1].length; | ||
} | ||
} | ||
this.generatedCodeColumn += lines[lines.length - 1].length; | ||
}; | ||
const n = '\n'; | ||
var n = '\n'; | ||
var warned = { | ||
const warned = { | ||
insertLeft: false, | ||
@@ -357,959 +365,993 @@ insertRight: false, | ||
var MagicString = function MagicString(string, options) { | ||
if ( options === void 0 ) options = {}; | ||
class MagicString { | ||
constructor(string, options = {}) { | ||
const chunk = new Chunk(0, string.length, string); | ||
var chunk = new Chunk(0, string.length, string); | ||
Object.defineProperties(this, { | ||
original: { writable: true, value: string }, | ||
outro: { writable: true, value: '' }, | ||
intro: { writable: true, value: '' }, | ||
firstChunk: { writable: true, value: chunk }, | ||
lastChunk: { writable: true, value: chunk }, | ||
lastSearchedChunk: { writable: true, value: chunk }, | ||
byStart: { writable: true, value: {} }, | ||
byEnd: { writable: true, value: {} }, | ||
filename: { writable: true, value: options.filename }, | ||
indentExclusionRanges: { writable: true, value: options.indentExclusionRanges }, | ||
sourcemapLocations: { writable: true, value: new BitSet() }, | ||
storedNames: { writable: true, value: {} }, | ||
indentStr: { writable: true, value: guessIndent(string) }, | ||
}); | ||
Object.defineProperties(this, { | ||
original: { writable: true, value: string }, | ||
outro: { writable: true, value: '' }, | ||
intro: { writable: true, value: '' }, | ||
firstChunk: { writable: true, value: chunk }, | ||
lastChunk: { writable: true, value: chunk }, | ||
lastSearchedChunk: { writable: true, value: chunk }, | ||
byStart: { writable: true, value: {} }, | ||
byEnd: { writable: true, value: {} }, | ||
filename: { writable: true, value: options.filename }, | ||
indentExclusionRanges: { writable: true, value: options.indentExclusionRanges }, | ||
sourcemapLocations: { writable: true, value: new BitSet() }, | ||
storedNames: { writable: true, value: {} }, | ||
indentStr: { writable: true, value: guessIndent(string) }, | ||
}); | ||
this.byStart[0] = chunk; | ||
this.byEnd[string.length] = chunk; | ||
} | ||
this.byStart[0] = chunk; | ||
this.byEnd[string.length] = chunk; | ||
}; | ||
addSourcemapLocation(char) { | ||
this.sourcemapLocations.add(char); | ||
} | ||
MagicString.prototype.addSourcemapLocation = function addSourcemapLocation (char) { | ||
this.sourcemapLocations.add(char); | ||
}; | ||
append(content) { | ||
if (typeof content !== 'string') throw new TypeError('outro content must be a string'); | ||
MagicString.prototype.append = function append (content) { | ||
if (typeof content !== 'string') { throw new TypeError('outro content must be a string'); } | ||
this.outro += content; | ||
return this; | ||
} | ||
this.outro += content; | ||
return this; | ||
}; | ||
appendLeft(index, content) { | ||
if (typeof content !== 'string') throw new TypeError('inserted content must be a string'); | ||
MagicString.prototype.appendLeft = function appendLeft (index, content) { | ||
if (typeof content !== 'string') { throw new TypeError('inserted content must be a string'); } | ||
this._split(index); | ||
this._split(index); | ||
const chunk = this.byEnd[index]; | ||
var chunk = this.byEnd[index]; | ||
if (chunk) { | ||
chunk.appendLeft(content); | ||
} else { | ||
this.intro += content; | ||
if (chunk) { | ||
chunk.appendLeft(content); | ||
} else { | ||
this.intro += content; | ||
} | ||
return this; | ||
} | ||
return this; | ||
}; | ||
MagicString.prototype.appendRight = function appendRight (index, content) { | ||
if (typeof content !== 'string') { throw new TypeError('inserted content must be a string'); } | ||
appendRight(index, content) { | ||
if (typeof content !== 'string') throw new TypeError('inserted content must be a string'); | ||
this._split(index); | ||
this._split(index); | ||
var chunk = this.byStart[index]; | ||
const chunk = this.byStart[index]; | ||
if (chunk) { | ||
chunk.appendRight(content); | ||
} else { | ||
this.outro += content; | ||
if (chunk) { | ||
chunk.appendRight(content); | ||
} else { | ||
this.outro += content; | ||
} | ||
return this; | ||
} | ||
return this; | ||
}; | ||
MagicString.prototype.clone = function clone () { | ||
var cloned = new MagicString(this.original, { filename: this.filename }); | ||
clone() { | ||
const cloned = new MagicString(this.original, { filename: this.filename }); | ||
var originalChunk = this.firstChunk; | ||
var clonedChunk = (cloned.firstChunk = cloned.lastSearchedChunk = originalChunk.clone()); | ||
let originalChunk = this.firstChunk; | ||
let clonedChunk = (cloned.firstChunk = cloned.lastSearchedChunk = originalChunk.clone()); | ||
while (originalChunk) { | ||
cloned.byStart[clonedChunk.start] = clonedChunk; | ||
cloned.byEnd[clonedChunk.end] = clonedChunk; | ||
while (originalChunk) { | ||
cloned.byStart[clonedChunk.start] = clonedChunk; | ||
cloned.byEnd[clonedChunk.end] = clonedChunk; | ||
var nextOriginalChunk = originalChunk.next; | ||
var nextClonedChunk = nextOriginalChunk && nextOriginalChunk.clone(); | ||
const nextOriginalChunk = originalChunk.next; | ||
const nextClonedChunk = nextOriginalChunk && nextOriginalChunk.clone(); | ||
if (nextClonedChunk) { | ||
clonedChunk.next = nextClonedChunk; | ||
nextClonedChunk.previous = clonedChunk; | ||
if (nextClonedChunk) { | ||
clonedChunk.next = nextClonedChunk; | ||
nextClonedChunk.previous = clonedChunk; | ||
clonedChunk = nextClonedChunk; | ||
clonedChunk = nextClonedChunk; | ||
} | ||
originalChunk = nextOriginalChunk; | ||
} | ||
originalChunk = nextOriginalChunk; | ||
} | ||
cloned.lastChunk = clonedChunk; | ||
cloned.lastChunk = clonedChunk; | ||
if (this.indentExclusionRanges) { | ||
cloned.indentExclusionRanges = this.indentExclusionRanges.slice(); | ||
} | ||
if (this.indentExclusionRanges) { | ||
cloned.indentExclusionRanges = this.indentExclusionRanges.slice(); | ||
} | ||
cloned.sourcemapLocations = new BitSet(this.sourcemapLocations); | ||
cloned.sourcemapLocations = new BitSet(this.sourcemapLocations); | ||
cloned.intro = this.intro; | ||
cloned.outro = this.outro; | ||
cloned.intro = this.intro; | ||
cloned.outro = this.outro; | ||
return cloned; | ||
} | ||
return cloned; | ||
}; | ||
generateDecodedMap(options) { | ||
options = options || {}; | ||
MagicString.prototype.generateDecodedMap = function generateDecodedMap (options) { | ||
var this$1$1 = this; | ||
const sourceIndex = 0; | ||
const names = Object.keys(this.storedNames); | ||
const mappings = new Mappings(options.hires); | ||
options = options || {}; | ||
const locate = getLocator(this.original); | ||
var sourceIndex = 0; | ||
var names = Object.keys(this.storedNames); | ||
var mappings = new Mappings(options.hires); | ||
if (this.intro) { | ||
mappings.advance(this.intro); | ||
} | ||
var locate = getLocator(this.original); | ||
this.firstChunk.eachNext((chunk) => { | ||
const loc = locate(chunk.start); | ||
if (this.intro) { | ||
mappings.advance(this.intro); | ||
} | ||
if (chunk.intro.length) mappings.advance(chunk.intro); | ||
this.firstChunk.eachNext(function (chunk) { | ||
var loc = locate(chunk.start); | ||
if (chunk.edited) { | ||
mappings.addEdit( | ||
sourceIndex, | ||
chunk.content, | ||
loc, | ||
chunk.storeName ? names.indexOf(chunk.original) : -1 | ||
); | ||
} else { | ||
mappings.addUneditedChunk(sourceIndex, chunk, this.original, loc, this.sourcemapLocations); | ||
} | ||
if (chunk.intro.length) { mappings.advance(chunk.intro); } | ||
if (chunk.outro.length) mappings.advance(chunk.outro); | ||
}); | ||
if (chunk.edited) { | ||
mappings.addEdit( | ||
sourceIndex, | ||
chunk.content, | ||
loc, | ||
chunk.storeName ? names.indexOf(chunk.original) : -1 | ||
); | ||
} else { | ||
mappings.addUneditedChunk(sourceIndex, chunk, this$1$1.original, loc, this$1$1.sourcemapLocations); | ||
} | ||
return { | ||
file: options.file ? options.file.split(/[/\\]/).pop() : null, | ||
sources: [options.source ? getRelativePath(options.file || '', options.source) : null], | ||
sourcesContent: options.includeContent ? [this.original] : [null], | ||
names, | ||
mappings: mappings.raw, | ||
}; | ||
} | ||
if (chunk.outro.length) { mappings.advance(chunk.outro); } | ||
}); | ||
generateMap(options) { | ||
return new SourceMap(this.generateDecodedMap(options)); | ||
} | ||
return { | ||
file: options.file ? options.file.split(/[/\\]/).pop() : null, | ||
sources: [options.source ? getRelativePath(options.file || '', options.source) : null], | ||
sourcesContent: options.includeContent ? [this.original] : [null], | ||
names: names, | ||
mappings: mappings.raw, | ||
}; | ||
}; | ||
getIndentString() { | ||
return this.indentStr === null ? '\t' : this.indentStr; | ||
} | ||
MagicString.prototype.generateMap = function generateMap (options) { | ||
return new SourceMap(this.generateDecodedMap(options)); | ||
}; | ||
indent(indentStr, options) { | ||
const pattern = /^[^\r\n]/gm; | ||
MagicString.prototype.getIndentString = function getIndentString () { | ||
return this.indentStr === null ? '\t' : this.indentStr; | ||
}; | ||
if (isObject(indentStr)) { | ||
options = indentStr; | ||
indentStr = undefined; | ||
} | ||
MagicString.prototype.indent = function indent (indentStr, options) { | ||
var pattern = /^[^\r\n]/gm; | ||
indentStr = indentStr !== undefined ? indentStr : this.indentStr || '\t'; | ||
if (isObject(indentStr)) { | ||
options = indentStr; | ||
indentStr = undefined; | ||
} | ||
if (indentStr === '') return this; // noop | ||
indentStr = indentStr !== undefined ? indentStr : this.indentStr || '\t'; | ||
options = options || {}; | ||
if (indentStr === '') { return this; } // noop | ||
// Process exclusion ranges | ||
const isExcluded = {}; | ||
options = options || {}; | ||
if (options.exclude) { | ||
const exclusions = | ||
typeof options.exclude[0] === 'number' ? [options.exclude] : options.exclude; | ||
exclusions.forEach((exclusion) => { | ||
for (let i = exclusion[0]; i < exclusion[1]; i += 1) { | ||
isExcluded[i] = true; | ||
} | ||
}); | ||
} | ||
// Process exclusion ranges | ||
var isExcluded = {}; | ||
let shouldIndentNextCharacter = options.indentStart !== false; | ||
const replacer = (match) => { | ||
if (shouldIndentNextCharacter) return `${indentStr}${match}`; | ||
shouldIndentNextCharacter = true; | ||
return match; | ||
}; | ||
if (options.exclude) { | ||
var exclusions = | ||
typeof options.exclude[0] === 'number' ? [options.exclude] : options.exclude; | ||
exclusions.forEach(function (exclusion) { | ||
for (var i = exclusion[0]; i < exclusion[1]; i += 1) { | ||
isExcluded[i] = true; | ||
} | ||
}); | ||
} | ||
this.intro = this.intro.replace(pattern, replacer); | ||
var shouldIndentNextCharacter = options.indentStart !== false; | ||
var replacer = function (match) { | ||
if (shouldIndentNextCharacter) { return ("" + indentStr + match); } | ||
shouldIndentNextCharacter = true; | ||
return match; | ||
}; | ||
let charIndex = 0; | ||
let chunk = this.firstChunk; | ||
this.intro = this.intro.replace(pattern, replacer); | ||
while (chunk) { | ||
const end = chunk.end; | ||
var charIndex = 0; | ||
var chunk = this.firstChunk; | ||
if (chunk.edited) { | ||
if (!isExcluded[charIndex]) { | ||
chunk.content = chunk.content.replace(pattern, replacer); | ||
while (chunk) { | ||
var end = chunk.end; | ||
if (chunk.edited) { | ||
if (!isExcluded[charIndex]) { | ||
chunk.content = chunk.content.replace(pattern, replacer); | ||
if (chunk.content.length) { | ||
shouldIndentNextCharacter = chunk.content[chunk.content.length - 1] === '\n'; | ||
if (chunk.content.length) { | ||
shouldIndentNextCharacter = chunk.content[chunk.content.length - 1] === '\n'; | ||
} | ||
} | ||
} | ||
} else { | ||
charIndex = chunk.start; | ||
} else { | ||
charIndex = chunk.start; | ||
while (charIndex < end) { | ||
if (!isExcluded[charIndex]) { | ||
var char = this.original[charIndex]; | ||
while (charIndex < end) { | ||
if (!isExcluded[charIndex]) { | ||
const char = this.original[charIndex]; | ||
if (char === '\n') { | ||
shouldIndentNextCharacter = true; | ||
} else if (char !== '\r' && shouldIndentNextCharacter) { | ||
shouldIndentNextCharacter = false; | ||
if (char === '\n') { | ||
shouldIndentNextCharacter = true; | ||
} else if (char !== '\r' && shouldIndentNextCharacter) { | ||
shouldIndentNextCharacter = false; | ||
if (charIndex === chunk.start) { | ||
chunk.prependRight(indentStr); | ||
} else { | ||
this._splitChunk(chunk, charIndex); | ||
chunk = chunk.next; | ||
chunk.prependRight(indentStr); | ||
if (charIndex === chunk.start) { | ||
chunk.prependRight(indentStr); | ||
} else { | ||
this._splitChunk(chunk, charIndex); | ||
chunk = chunk.next; | ||
chunk.prependRight(indentStr); | ||
} | ||
} | ||
} | ||
charIndex += 1; | ||
} | ||
} | ||
charIndex += 1; | ||
} | ||
charIndex = chunk.end; | ||
chunk = chunk.next; | ||
} | ||
charIndex = chunk.end; | ||
chunk = chunk.next; | ||
this.outro = this.outro.replace(pattern, replacer); | ||
return this; | ||
} | ||
this.outro = this.outro.replace(pattern, replacer); | ||
insert() { | ||
throw new Error( | ||
'magicString.insert(...) is deprecated. Use prependRight(...) or appendLeft(...)' | ||
); | ||
} | ||
return this; | ||
}; | ||
insertLeft(index, content) { | ||
if (!warned.insertLeft) { | ||
console.warn( | ||
'magicString.insertLeft(...) is deprecated. Use magicString.appendLeft(...) instead' | ||
); // eslint-disable-line no-console | ||
warned.insertLeft = true; | ||
} | ||
MagicString.prototype.insert = function insert () { | ||
throw new Error( | ||
'magicString.insert(...) is deprecated. Use prependRight(...) or appendLeft(...)' | ||
); | ||
}; | ||
MagicString.prototype.insertLeft = function insertLeft (index, content) { | ||
if (!warned.insertLeft) { | ||
console.warn( | ||
'magicString.insertLeft(...) is deprecated. Use magicString.appendLeft(...) instead' | ||
); // eslint-disable-line no-console | ||
warned.insertLeft = true; | ||
return this.appendLeft(index, content); | ||
} | ||
return this.appendLeft(index, content); | ||
}; | ||
insertRight(index, content) { | ||
if (!warned.insertRight) { | ||
console.warn( | ||
'magicString.insertRight(...) is deprecated. Use magicString.prependRight(...) instead' | ||
); // eslint-disable-line no-console | ||
warned.insertRight = true; | ||
} | ||
MagicString.prototype.insertRight = function insertRight (index, content) { | ||
if (!warned.insertRight) { | ||
console.warn( | ||
'magicString.insertRight(...) is deprecated. Use magicString.prependRight(...) instead' | ||
); // eslint-disable-line no-console | ||
warned.insertRight = true; | ||
return this.prependRight(index, content); | ||
} | ||
return this.prependRight(index, content); | ||
}; | ||
move(start, end, index) { | ||
if (index >= start && index <= end) throw new Error('Cannot move a selection inside itself'); | ||
MagicString.prototype.move = function move (start, end, index) { | ||
if (index >= start && index <= end) { throw new Error('Cannot move a selection inside itself'); } | ||
this._split(start); | ||
this._split(end); | ||
this._split(index); | ||
this._split(start); | ||
this._split(end); | ||
this._split(index); | ||
const first = this.byStart[start]; | ||
const last = this.byEnd[end]; | ||
var first = this.byStart[start]; | ||
var last = this.byEnd[end]; | ||
const oldLeft = first.previous; | ||
const oldRight = last.next; | ||
var oldLeft = first.previous; | ||
var oldRight = last.next; | ||
const newRight = this.byStart[index]; | ||
if (!newRight && last === this.lastChunk) return this; | ||
const newLeft = newRight ? newRight.previous : this.lastChunk; | ||
var newRight = this.byStart[index]; | ||
if (!newRight && last === this.lastChunk) { return this; } | ||
var newLeft = newRight ? newRight.previous : this.lastChunk; | ||
if (oldLeft) oldLeft.next = oldRight; | ||
if (oldRight) oldRight.previous = oldLeft; | ||
if (oldLeft) { oldLeft.next = oldRight; } | ||
if (oldRight) { oldRight.previous = oldLeft; } | ||
if (newLeft) newLeft.next = first; | ||
if (newRight) newRight.previous = last; | ||
if (newLeft) { newLeft.next = first; } | ||
if (newRight) { newRight.previous = last; } | ||
if (!first.previous) this.firstChunk = last.next; | ||
if (!last.next) { | ||
this.lastChunk = first.previous; | ||
this.lastChunk.next = null; | ||
} | ||
if (!first.previous) { this.firstChunk = last.next; } | ||
if (!last.next) { | ||
this.lastChunk = first.previous; | ||
this.lastChunk.next = null; | ||
first.previous = newLeft; | ||
last.next = newRight || null; | ||
if (!newLeft) this.firstChunk = first; | ||
if (!newRight) this.lastChunk = last; | ||
return this; | ||
} | ||
first.previous = newLeft; | ||
last.next = newRight || null; | ||
overwrite(start, end, content, options) { | ||
if (typeof content !== 'string') throw new TypeError('replacement content must be a string'); | ||
if (!newLeft) { this.firstChunk = first; } | ||
if (!newRight) { this.lastChunk = last; } | ||
return this; | ||
}; | ||
while (start < 0) start += this.original.length; | ||
while (end < 0) end += this.original.length; | ||
MagicString.prototype.overwrite = function overwrite (start, end, content, options) { | ||
if (typeof content !== 'string') { throw new TypeError('replacement content must be a string'); } | ||
if (end > this.original.length) throw new Error('end is out of bounds'); | ||
if (start === end) | ||
throw new Error( | ||
'Cannot overwrite a zero-length range – use appendLeft or prependRight instead' | ||
); | ||
while (start < 0) { start += this.original.length; } | ||
while (end < 0) { end += this.original.length; } | ||
this._split(start); | ||
this._split(end); | ||
if (end > this.original.length) { throw new Error('end is out of bounds'); } | ||
if (start === end) | ||
{ throw new Error( | ||
'Cannot overwrite a zero-length range – use appendLeft or prependRight instead' | ||
); } | ||
if (options === true) { | ||
if (!warned.storeName) { | ||
console.warn( | ||
'The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string' | ||
); // eslint-disable-line no-console | ||
warned.storeName = true; | ||
} | ||
this._split(start); | ||
this._split(end); | ||
options = { storeName: true }; | ||
} | ||
const storeName = options !== undefined ? options.storeName : false; | ||
const contentOnly = options !== undefined ? options.contentOnly : false; | ||
if (options === true) { | ||
if (!warned.storeName) { | ||
console.warn( | ||
'The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string' | ||
); // eslint-disable-line no-console | ||
warned.storeName = true; | ||
if (storeName) { | ||
const original = this.original.slice(start, end); | ||
Object.defineProperty(this.storedNames, original, { writable: true, value: true, enumerable: true }); | ||
} | ||
options = { storeName: true }; | ||
} | ||
var storeName = options !== undefined ? options.storeName : false; | ||
var contentOnly = options !== undefined ? options.contentOnly : false; | ||
const first = this.byStart[start]; | ||
const last = this.byEnd[end]; | ||
if (storeName) { | ||
var original = this.original.slice(start, end); | ||
Object.defineProperty(this.storedNames, original, { writable: true, value: true, enumerable: true }); | ||
} | ||
if (first) { | ||
let chunk = first; | ||
while (chunk !== last) { | ||
if (chunk.next !== this.byStart[chunk.end]) { | ||
throw new Error('Cannot overwrite across a split point'); | ||
} | ||
chunk = chunk.next; | ||
chunk.edit('', false); | ||
} | ||
var first = this.byStart[start]; | ||
var last = this.byEnd[end]; | ||
first.edit(content, storeName, contentOnly); | ||
} else { | ||
// must be inserting at the end | ||
const newChunk = new Chunk(start, end, '').edit(content, storeName); | ||
if (first) { | ||
var chunk = first; | ||
while (chunk !== last) { | ||
if (chunk.next !== this.byStart[chunk.end]) { | ||
throw new Error('Cannot overwrite across a split point'); | ||
} | ||
chunk = chunk.next; | ||
chunk.edit('', false); | ||
// TODO last chunk in the array may not be the last chunk, if it's moved... | ||
last.next = newChunk; | ||
newChunk.previous = last; | ||
} | ||
return this; | ||
} | ||
first.edit(content, storeName, contentOnly); | ||
} else { | ||
// must be inserting at the end | ||
var newChunk = new Chunk(start, end, '').edit(content, storeName); | ||
prepend(content) { | ||
if (typeof content !== 'string') throw new TypeError('outro content must be a string'); | ||
// TODO last chunk in the array may not be the last chunk, if it's moved... | ||
last.next = newChunk; | ||
newChunk.previous = last; | ||
this.intro = content + this.intro; | ||
return this; | ||
} | ||
return this; | ||
}; | ||
MagicString.prototype.prepend = function prepend (content) { | ||
if (typeof content !== 'string') { throw new TypeError('outro content must be a string'); } | ||
prependLeft(index, content) { | ||
if (typeof content !== 'string') throw new TypeError('inserted content must be a string'); | ||
this.intro = content + this.intro; | ||
return this; | ||
}; | ||
this._split(index); | ||
MagicString.prototype.prependLeft = function prependLeft (index, content) { | ||
if (typeof content !== 'string') { throw new TypeError('inserted content must be a string'); } | ||
const chunk = this.byEnd[index]; | ||
this._split(index); | ||
var chunk = this.byEnd[index]; | ||
if (chunk) { | ||
chunk.prependLeft(content); | ||
} else { | ||
this.intro = content + this.intro; | ||
if (chunk) { | ||
chunk.prependLeft(content); | ||
} else { | ||
this.intro = content + this.intro; | ||
} | ||
return this; | ||
} | ||
return this; | ||
}; | ||
MagicString.prototype.prependRight = function prependRight (index, content) { | ||
if (typeof content !== 'string') { throw new TypeError('inserted content must be a string'); } | ||
prependRight(index, content) { | ||
if (typeof content !== 'string') throw new TypeError('inserted content must be a string'); | ||
this._split(index); | ||
this._split(index); | ||
var chunk = this.byStart[index]; | ||
const chunk = this.byStart[index]; | ||
if (chunk) { | ||
chunk.prependRight(content); | ||
} else { | ||
this.outro = content + this.outro; | ||
if (chunk) { | ||
chunk.prependRight(content); | ||
} else { | ||
this.outro = content + this.outro; | ||
} | ||
return this; | ||
} | ||
return this; | ||
}; | ||
MagicString.prototype.remove = function remove (start, end) { | ||
while (start < 0) { start += this.original.length; } | ||
while (end < 0) { end += this.original.length; } | ||
remove(start, end) { | ||
while (start < 0) start += this.original.length; | ||
while (end < 0) end += this.original.length; | ||
if (start === end) { return this; } | ||
if (start === end) return this; | ||
if (start < 0 || end > this.original.length) { throw new Error('Character is out of bounds'); } | ||
if (start > end) { throw new Error('end must be greater than start'); } | ||
if (start < 0 || end > this.original.length) throw new Error('Character is out of bounds'); | ||
if (start > end) throw new Error('end must be greater than start'); | ||
this._split(start); | ||
this._split(end); | ||
this._split(start); | ||
this._split(end); | ||
var chunk = this.byStart[start]; | ||
let chunk = this.byStart[start]; | ||
while (chunk) { | ||
chunk.intro = ''; | ||
chunk.outro = ''; | ||
chunk.edit(''); | ||
while (chunk) { | ||
chunk.intro = ''; | ||
chunk.outro = ''; | ||
chunk.edit(''); | ||
chunk = end > chunk.end ? this.byStart[chunk.end] : null; | ||
chunk = end > chunk.end ? this.byStart[chunk.end] : null; | ||
} | ||
return this; | ||
} | ||
return this; | ||
}; | ||
MagicString.prototype.lastChar = function lastChar () { | ||
if (this.outro.length) { return this.outro[this.outro.length - 1]; } | ||
var chunk = this.lastChunk; | ||
do { | ||
if (chunk.outro.length) { return chunk.outro[chunk.outro.length - 1]; } | ||
if (chunk.content.length) { return chunk.content[chunk.content.length - 1]; } | ||
if (chunk.intro.length) { return chunk.intro[chunk.intro.length - 1]; } | ||
} while ((chunk = chunk.previous)); | ||
if (this.intro.length) { return this.intro[this.intro.length - 1]; } | ||
return ''; | ||
}; | ||
lastChar() { | ||
if (this.outro.length) return this.outro[this.outro.length - 1]; | ||
let chunk = this.lastChunk; | ||
do { | ||
if (chunk.outro.length) return chunk.outro[chunk.outro.length - 1]; | ||
if (chunk.content.length) return chunk.content[chunk.content.length - 1]; | ||
if (chunk.intro.length) return chunk.intro[chunk.intro.length - 1]; | ||
} while ((chunk = chunk.previous)); | ||
if (this.intro.length) return this.intro[this.intro.length - 1]; | ||
return ''; | ||
} | ||
MagicString.prototype.lastLine = function lastLine () { | ||
var lineIndex = this.outro.lastIndexOf(n); | ||
if (lineIndex !== -1) { return this.outro.substr(lineIndex + 1); } | ||
var lineStr = this.outro; | ||
var chunk = this.lastChunk; | ||
do { | ||
if (chunk.outro.length > 0) { | ||
lineIndex = chunk.outro.lastIndexOf(n); | ||
if (lineIndex !== -1) { return chunk.outro.substr(lineIndex + 1) + lineStr; } | ||
lineStr = chunk.outro + lineStr; | ||
} | ||
lastLine() { | ||
let lineIndex = this.outro.lastIndexOf(n); | ||
if (lineIndex !== -1) return this.outro.substr(lineIndex + 1); | ||
let lineStr = this.outro; | ||
let chunk = this.lastChunk; | ||
do { | ||
if (chunk.outro.length > 0) { | ||
lineIndex = chunk.outro.lastIndexOf(n); | ||
if (lineIndex !== -1) return chunk.outro.substr(lineIndex + 1) + lineStr; | ||
lineStr = chunk.outro + lineStr; | ||
} | ||
if (chunk.content.length > 0) { | ||
lineIndex = chunk.content.lastIndexOf(n); | ||
if (lineIndex !== -1) { return chunk.content.substr(lineIndex + 1) + lineStr; } | ||
lineStr = chunk.content + lineStr; | ||
} | ||
if (chunk.content.length > 0) { | ||
lineIndex = chunk.content.lastIndexOf(n); | ||
if (lineIndex !== -1) return chunk.content.substr(lineIndex + 1) + lineStr; | ||
lineStr = chunk.content + lineStr; | ||
} | ||
if (chunk.intro.length > 0) { | ||
lineIndex = chunk.intro.lastIndexOf(n); | ||
if (lineIndex !== -1) { return chunk.intro.substr(lineIndex + 1) + lineStr; } | ||
lineStr = chunk.intro + lineStr; | ||
} | ||
} while ((chunk = chunk.previous)); | ||
lineIndex = this.intro.lastIndexOf(n); | ||
if (lineIndex !== -1) { return this.intro.substr(lineIndex + 1) + lineStr; } | ||
return this.intro + lineStr; | ||
}; | ||
if (chunk.intro.length > 0) { | ||
lineIndex = chunk.intro.lastIndexOf(n); | ||
if (lineIndex !== -1) return chunk.intro.substr(lineIndex + 1) + lineStr; | ||
lineStr = chunk.intro + lineStr; | ||
} | ||
} while ((chunk = chunk.previous)); | ||
lineIndex = this.intro.lastIndexOf(n); | ||
if (lineIndex !== -1) return this.intro.substr(lineIndex + 1) + lineStr; | ||
return this.intro + lineStr; | ||
} | ||
MagicString.prototype.slice = function slice (start, end) { | ||
if ( start === void 0 ) start = 0; | ||
if ( end === void 0 ) end = this.original.length; | ||
slice(start = 0, end = this.original.length) { | ||
while (start < 0) start += this.original.length; | ||
while (end < 0) end += this.original.length; | ||
while (start < 0) { start += this.original.length; } | ||
while (end < 0) { end += this.original.length; } | ||
let result = ''; | ||
var result = ''; | ||
// find start chunk | ||
let chunk = this.firstChunk; | ||
while (chunk && (chunk.start > start || chunk.end <= start)) { | ||
// found end chunk before start | ||
if (chunk.start < end && chunk.end >= end) { | ||
return result; | ||
} | ||
// find start chunk | ||
var chunk = this.firstChunk; | ||
while (chunk && (chunk.start > start || chunk.end <= start)) { | ||
// found end chunk before start | ||
if (chunk.start < end && chunk.end >= end) { | ||
return result; | ||
chunk = chunk.next; | ||
} | ||
chunk = chunk.next; | ||
} | ||
if (chunk && chunk.edited && chunk.start !== start) | ||
throw new Error(`Cannot use replaced character ${start} as slice start anchor.`); | ||
if (chunk && chunk.edited && chunk.start !== start) | ||
{ throw new Error(("Cannot use replaced character " + start + " as slice start anchor.")); } | ||
const startChunk = chunk; | ||
while (chunk) { | ||
if (chunk.intro && (startChunk !== chunk || chunk.start === start)) { | ||
result += chunk.intro; | ||
} | ||
var startChunk = chunk; | ||
while (chunk) { | ||
if (chunk.intro && (startChunk !== chunk || chunk.start === start)) { | ||
result += chunk.intro; | ||
} | ||
const containsEnd = chunk.start < end && chunk.end >= end; | ||
if (containsEnd && chunk.edited && chunk.end !== end) | ||
throw new Error(`Cannot use replaced character ${end} as slice end anchor.`); | ||
var containsEnd = chunk.start < end && chunk.end >= end; | ||
if (containsEnd && chunk.edited && chunk.end !== end) | ||
{ throw new Error(("Cannot use replaced character " + end + " as slice end anchor.")); } | ||
const sliceStart = startChunk === chunk ? start - chunk.start : 0; | ||
const sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length; | ||
var sliceStart = startChunk === chunk ? start - chunk.start : 0; | ||
var sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length; | ||
result += chunk.content.slice(sliceStart, sliceEnd); | ||
result += chunk.content.slice(sliceStart, sliceEnd); | ||
if (chunk.outro && (!containsEnd || chunk.end === end)) { | ||
result += chunk.outro; | ||
} | ||
if (chunk.outro && (!containsEnd || chunk.end === end)) { | ||
result += chunk.outro; | ||
} | ||
if (containsEnd) { | ||
break; | ||
} | ||
if (containsEnd) { | ||
break; | ||
chunk = chunk.next; | ||
} | ||
chunk = chunk.next; | ||
return result; | ||
} | ||
return result; | ||
}; | ||
// TODO deprecate this? not really very useful | ||
snip(start, end) { | ||
const clone = this.clone(); | ||
clone.remove(0, start); | ||
clone.remove(end, clone.original.length); | ||
// TODO deprecate this? not really very useful | ||
MagicString.prototype.snip = function snip (start, end) { | ||
var clone = this.clone(); | ||
clone.remove(0, start); | ||
clone.remove(end, clone.original.length); | ||
return clone; | ||
} | ||
return clone; | ||
}; | ||
_split(index) { | ||
if (this.byStart[index] || this.byEnd[index]) return; | ||
MagicString.prototype._split = function _split (index) { | ||
if (this.byStart[index] || this.byEnd[index]) { return; } | ||
let chunk = this.lastSearchedChunk; | ||
const searchForward = index > chunk.end; | ||
var chunk = this.lastSearchedChunk; | ||
var searchForward = index > chunk.end; | ||
while (chunk) { | ||
if (chunk.contains(index)) return this._splitChunk(chunk, index); | ||
while (chunk) { | ||
if (chunk.contains(index)) { return this._splitChunk(chunk, index); } | ||
chunk = searchForward ? this.byStart[chunk.end] : this.byEnd[chunk.start]; | ||
chunk = searchForward ? this.byStart[chunk.end] : this.byEnd[chunk.start]; | ||
} | ||
} | ||
}; | ||
MagicString.prototype._splitChunk = function _splitChunk (chunk, index) { | ||
if (chunk.edited && chunk.content.length) { | ||
// zero-length edited chunks are a special case (overlapping replacements) | ||
var loc = getLocator(this.original)(index); | ||
throw new Error( | ||
("Cannot split a chunk that has already been edited (" + (loc.line) + ":" + (loc.column) + " – \"" + (chunk.original) + "\")") | ||
); | ||
} | ||
_splitChunk(chunk, index) { | ||
if (chunk.edited && chunk.content.length) { | ||
// zero-length edited chunks are a special case (overlapping replacements) | ||
const loc = getLocator(this.original)(index); | ||
throw new Error( | ||
`Cannot split a chunk that has already been edited (${loc.line}:${loc.column} – "${chunk.original}")` | ||
); | ||
} | ||
var newChunk = chunk.split(index); | ||
const newChunk = chunk.split(index); | ||
this.byEnd[index] = chunk; | ||
this.byStart[index] = newChunk; | ||
this.byEnd[newChunk.end] = newChunk; | ||
this.byEnd[index] = chunk; | ||
this.byStart[index] = newChunk; | ||
this.byEnd[newChunk.end] = newChunk; | ||
if (chunk === this.lastChunk) { this.lastChunk = newChunk; } | ||
if (chunk === this.lastChunk) this.lastChunk = newChunk; | ||
this.lastSearchedChunk = chunk; | ||
return true; | ||
}; | ||
this.lastSearchedChunk = chunk; | ||
return true; | ||
} | ||
MagicString.prototype.toString = function toString () { | ||
var str = this.intro; | ||
toString() { | ||
let str = this.intro; | ||
var chunk = this.firstChunk; | ||
while (chunk) { | ||
str += chunk.toString(); | ||
chunk = chunk.next; | ||
let chunk = this.firstChunk; | ||
while (chunk) { | ||
str += chunk.toString(); | ||
chunk = chunk.next; | ||
} | ||
return str + this.outro; | ||
} | ||
return str + this.outro; | ||
}; | ||
isEmpty() { | ||
let chunk = this.firstChunk; | ||
do { | ||
if ( | ||
(chunk.intro.length && chunk.intro.trim()) || | ||
(chunk.content.length && chunk.content.trim()) || | ||
(chunk.outro.length && chunk.outro.trim()) | ||
) | ||
return false; | ||
} while ((chunk = chunk.next)); | ||
return true; | ||
} | ||
MagicString.prototype.isEmpty = function isEmpty () { | ||
var chunk = this.firstChunk; | ||
do { | ||
if ( | ||
(chunk.intro.length && chunk.intro.trim()) || | ||
(chunk.content.length && chunk.content.trim()) || | ||
(chunk.outro.length && chunk.outro.trim()) | ||
) | ||
{ return false; } | ||
} while ((chunk = chunk.next)); | ||
return true; | ||
}; | ||
length() { | ||
let chunk = this.firstChunk; | ||
let length = 0; | ||
do { | ||
length += chunk.intro.length + chunk.content.length + chunk.outro.length; | ||
} while ((chunk = chunk.next)); | ||
return length; | ||
} | ||
MagicString.prototype.length = function length () { | ||
var chunk = this.firstChunk; | ||
var length = 0; | ||
do { | ||
length += chunk.intro.length + chunk.content.length + chunk.outro.length; | ||
} while ((chunk = chunk.next)); | ||
return length; | ||
}; | ||
trimLines() { | ||
return this.trim('[\\r\\n]'); | ||
} | ||
MagicString.prototype.trimLines = function trimLines () { | ||
return this.trim('[\\r\\n]'); | ||
}; | ||
trim(charType) { | ||
return this.trimStart(charType).trimEnd(charType); | ||
} | ||
MagicString.prototype.trim = function trim (charType) { | ||
return this.trimStart(charType).trimEnd(charType); | ||
}; | ||
trimEndAborted(charType) { | ||
const rx = new RegExp((charType || '\\s') + '+$'); | ||
MagicString.prototype.trimEndAborted = function trimEndAborted (charType) { | ||
var rx = new RegExp((charType || '\\s') + '+$'); | ||
this.outro = this.outro.replace(rx, ''); | ||
if (this.outro.length) return true; | ||
this.outro = this.outro.replace(rx, ''); | ||
if (this.outro.length) { return true; } | ||
let chunk = this.lastChunk; | ||
var chunk = this.lastChunk; | ||
do { | ||
const end = chunk.end; | ||
const aborted = chunk.trimEnd(rx); | ||
do { | ||
var end = chunk.end; | ||
var aborted = chunk.trimEnd(rx); | ||
// if chunk was trimmed, we have a new lastChunk | ||
if (chunk.end !== end) { | ||
if (this.lastChunk === chunk) { | ||
this.lastChunk = chunk.next; | ||
} | ||
// if chunk was trimmed, we have a new lastChunk | ||
if (chunk.end !== end) { | ||
if (this.lastChunk === chunk) { | ||
this.lastChunk = chunk.next; | ||
this.byEnd[chunk.end] = chunk; | ||
this.byStart[chunk.next.start] = chunk.next; | ||
this.byEnd[chunk.next.end] = chunk.next; | ||
} | ||
this.byEnd[chunk.end] = chunk; | ||
this.byStart[chunk.next.start] = chunk.next; | ||
this.byEnd[chunk.next.end] = chunk.next; | ||
} | ||
if (aborted) return true; | ||
chunk = chunk.previous; | ||
} while (chunk); | ||
if (aborted) { return true; } | ||
chunk = chunk.previous; | ||
} while (chunk); | ||
return false; | ||
} | ||
return false; | ||
}; | ||
trimEnd(charType) { | ||
this.trimEndAborted(charType); | ||
return this; | ||
} | ||
trimStartAborted(charType) { | ||
const rx = new RegExp('^' + (charType || '\\s') + '+'); | ||
MagicString.prototype.trimEnd = function trimEnd (charType) { | ||
this.trimEndAborted(charType); | ||
return this; | ||
}; | ||
MagicString.prototype.trimStartAborted = function trimStartAborted (charType) { | ||
var rx = new RegExp('^' + (charType || '\\s') + '+'); | ||
this.intro = this.intro.replace(rx, ''); | ||
if (this.intro.length) return true; | ||
this.intro = this.intro.replace(rx, ''); | ||
if (this.intro.length) { return true; } | ||
let chunk = this.firstChunk; | ||
var chunk = this.firstChunk; | ||
do { | ||
const end = chunk.end; | ||
const aborted = chunk.trimStart(rx); | ||
do { | ||
var end = chunk.end; | ||
var aborted = chunk.trimStart(rx); | ||
if (chunk.end !== end) { | ||
// special case... | ||
if (chunk === this.lastChunk) this.lastChunk = chunk.next; | ||
if (chunk.end !== end) { | ||
// special case... | ||
if (chunk === this.lastChunk) { this.lastChunk = chunk.next; } | ||
this.byEnd[chunk.end] = chunk; | ||
this.byStart[chunk.next.start] = chunk.next; | ||
this.byEnd[chunk.next.end] = chunk.next; | ||
} | ||
this.byEnd[chunk.end] = chunk; | ||
this.byStart[chunk.next.start] = chunk.next; | ||
this.byEnd[chunk.next.end] = chunk.next; | ||
} | ||
if (aborted) return true; | ||
chunk = chunk.next; | ||
} while (chunk); | ||
if (aborted) { return true; } | ||
chunk = chunk.next; | ||
} while (chunk); | ||
return false; | ||
} | ||
return false; | ||
}; | ||
trimStart(charType) { | ||
this.trimStartAborted(charType); | ||
return this; | ||
} | ||
MagicString.prototype.trimStart = function trimStart (charType) { | ||
this.trimStartAborted(charType); | ||
return this; | ||
}; | ||
var hasOwnProp = Object.prototype.hasOwnProperty; | ||
var Bundle = function Bundle(options) { | ||
if ( options === void 0 ) options = {}; | ||
this.intro = options.intro || ''; | ||
this.separator = options.separator !== undefined ? options.separator : '\n'; | ||
this.sources = []; | ||
this.uniqueSources = []; | ||
this.uniqueSourceIndexByFilename = {}; | ||
}; | ||
Bundle.prototype.addSource = function addSource (source) { | ||
if (source instanceof MagicString) { | ||
return this.addSource({ | ||
content: source, | ||
filename: source.filename, | ||
separator: this.separator, | ||
}); | ||
hasChanged() { | ||
return this.original !== this.toString(); | ||
} | ||
if (!isObject(source) || !source.content) { | ||
throw new Error( | ||
'bundle.addSource() takes an object with a `content` property, which should be an instance of MagicString, and an optional `filename`' | ||
); | ||
replace(searchValue, replacement) { | ||
function getReplacement(match) { | ||
if (typeof replacement === 'string') { | ||
return replacement.replace(/\$(\$|&|\d+)/g, (_, i) => { | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_a_parameter | ||
if (i === '$') | ||
return '$'; | ||
if (i === '&') | ||
return match[0]; | ||
const num = +i; | ||
if (num < match.length) | ||
return match[+i]; | ||
return `$${i}`; | ||
}); | ||
} | ||
else { | ||
return replacement(...match); | ||
} | ||
} | ||
function matchAll(re, str) { | ||
let match; | ||
const matches = []; | ||
while (match = re.exec(str)) { | ||
matches.push(match); | ||
} | ||
return matches; | ||
} | ||
if (typeof searchValue !== 'string' && searchValue.global) { | ||
const matches = matchAll(searchValue, this.original); | ||
matches.forEach((match) => { | ||
if (match.index != null) | ||
this.overwrite(match.index, match.index + match[0].length, getReplacement(match)); | ||
}); | ||
} | ||
else { | ||
const match = this.original.match(searchValue); | ||
if (match && match.index != null) | ||
this.overwrite(match.index, match.index + match[0].length, getReplacement(match)); | ||
} | ||
return this; | ||
} | ||
} | ||
['filename', 'indentExclusionRanges', 'separator'].forEach(function (option) { | ||
if (!hasOwnProp.call(source, option)) { source[option] = source.content[option]; } | ||
}); | ||
const hasOwnProp = Object.prototype.hasOwnProperty; | ||
if (source.separator === undefined) { | ||
// TODO there's a bunch of this sort of thing, needs cleaning up | ||
source.separator = this.separator; | ||
class Bundle { | ||
constructor(options = {}) { | ||
this.intro = options.intro || ''; | ||
this.separator = options.separator !== undefined ? options.separator : '\n'; | ||
this.sources = []; | ||
this.uniqueSources = []; | ||
this.uniqueSourceIndexByFilename = {}; | ||
} | ||
if (source.filename) { | ||
if (!hasOwnProp.call(this.uniqueSourceIndexByFilename, source.filename)) { | ||
this.uniqueSourceIndexByFilename[source.filename] = this.uniqueSources.length; | ||
this.uniqueSources.push({ filename: source.filename, content: source.content.original }); | ||
} else { | ||
var uniqueSource = this.uniqueSources[this.uniqueSourceIndexByFilename[source.filename]]; | ||
if (source.content.original !== uniqueSource.content) { | ||
throw new Error(("Illegal source: same filename (" + (source.filename) + "), different contents")); | ||
} | ||
addSource(source) { | ||
if (source instanceof MagicString) { | ||
return this.addSource({ | ||
content: source, | ||
filename: source.filename, | ||
separator: this.separator, | ||
}); | ||
} | ||
} | ||
this.sources.push(source); | ||
return this; | ||
}; | ||
if (!isObject(source) || !source.content) { | ||
throw new Error( | ||
'bundle.addSource() takes an object with a `content` property, which should be an instance of MagicString, and an optional `filename`' | ||
); | ||
} | ||
Bundle.prototype.append = function append (str, options) { | ||
this.addSource({ | ||
content: new MagicString(str), | ||
separator: (options && options.separator) || '', | ||
}); | ||
['filename', 'indentExclusionRanges', 'separator'].forEach((option) => { | ||
if (!hasOwnProp.call(source, option)) source[option] = source.content[option]; | ||
}); | ||
return this; | ||
}; | ||
if (source.separator === undefined) { | ||
// TODO there's a bunch of this sort of thing, needs cleaning up | ||
source.separator = this.separator; | ||
} | ||
Bundle.prototype.clone = function clone () { | ||
var bundle = new Bundle({ | ||
intro: this.intro, | ||
separator: this.separator, | ||
}); | ||
if (source.filename) { | ||
if (!hasOwnProp.call(this.uniqueSourceIndexByFilename, source.filename)) { | ||
this.uniqueSourceIndexByFilename[source.filename] = this.uniqueSources.length; | ||
this.uniqueSources.push({ filename: source.filename, content: source.content.original }); | ||
} else { | ||
const uniqueSource = this.uniqueSources[this.uniqueSourceIndexByFilename[source.filename]]; | ||
if (source.content.original !== uniqueSource.content) { | ||
throw new Error(`Illegal source: same filename (${source.filename}), different contents`); | ||
} | ||
} | ||
} | ||
this.sources.forEach(function (source) { | ||
bundle.addSource({ | ||
filename: source.filename, | ||
content: source.content.clone(), | ||
separator: source.separator, | ||
this.sources.push(source); | ||
return this; | ||
} | ||
append(str, options) { | ||
this.addSource({ | ||
content: new MagicString(str), | ||
separator: (options && options.separator) || '', | ||
}); | ||
}); | ||
return bundle; | ||
}; | ||
return this; | ||
} | ||
Bundle.prototype.generateDecodedMap = function generateDecodedMap (options) { | ||
var this$1$1 = this; | ||
if ( options === void 0 ) options = {}; | ||
clone() { | ||
const bundle = new Bundle({ | ||
intro: this.intro, | ||
separator: this.separator, | ||
}); | ||
var names = []; | ||
this.sources.forEach(function (source) { | ||
Object.keys(source.content.storedNames).forEach(function (name) { | ||
if (!~names.indexOf(name)) { names.push(name); } | ||
this.sources.forEach((source) => { | ||
bundle.addSource({ | ||
filename: source.filename, | ||
content: source.content.clone(), | ||
separator: source.separator, | ||
}); | ||
}); | ||
}); | ||
var mappings = new Mappings(options.hires); | ||
if (this.intro) { | ||
mappings.advance(this.intro); | ||
return bundle; | ||
} | ||
this.sources.forEach(function (source, i) { | ||
if (i > 0) { | ||
mappings.advance(this$1$1.separator); | ||
} | ||
generateDecodedMap(options = {}) { | ||
const names = []; | ||
this.sources.forEach((source) => { | ||
Object.keys(source.content.storedNames).forEach((name) => { | ||
if (!~names.indexOf(name)) names.push(name); | ||
}); | ||
}); | ||
var sourceIndex = source.filename ? this$1$1.uniqueSourceIndexByFilename[source.filename] : -1; | ||
var magicString = source.content; | ||
var locate = getLocator(magicString.original); | ||
const mappings = new Mappings(options.hires); | ||
if (magicString.intro) { | ||
mappings.advance(magicString.intro); | ||
if (this.intro) { | ||
mappings.advance(this.intro); | ||
} | ||
magicString.firstChunk.eachNext(function (chunk) { | ||
var loc = locate(chunk.start); | ||
this.sources.forEach((source, i) => { | ||
if (i > 0) { | ||
mappings.advance(this.separator); | ||
} | ||
if (chunk.intro.length) { mappings.advance(chunk.intro); } | ||
const sourceIndex = source.filename ? this.uniqueSourceIndexByFilename[source.filename] : -1; | ||
const magicString = source.content; | ||
const locate = getLocator(magicString.original); | ||
if (source.filename) { | ||
if (chunk.edited) { | ||
mappings.addEdit( | ||
sourceIndex, | ||
chunk.content, | ||
loc, | ||
chunk.storeName ? names.indexOf(chunk.original) : -1 | ||
); | ||
if (magicString.intro) { | ||
mappings.advance(magicString.intro); | ||
} | ||
magicString.firstChunk.eachNext((chunk) => { | ||
const loc = locate(chunk.start); | ||
if (chunk.intro.length) mappings.advance(chunk.intro); | ||
if (source.filename) { | ||
if (chunk.edited) { | ||
mappings.addEdit( | ||
sourceIndex, | ||
chunk.content, | ||
loc, | ||
chunk.storeName ? names.indexOf(chunk.original) : -1 | ||
); | ||
} else { | ||
mappings.addUneditedChunk( | ||
sourceIndex, | ||
chunk, | ||
magicString.original, | ||
loc, | ||
magicString.sourcemapLocations | ||
); | ||
} | ||
} else { | ||
mappings.addUneditedChunk( | ||
sourceIndex, | ||
chunk, | ||
magicString.original, | ||
loc, | ||
magicString.sourcemapLocations | ||
); | ||
mappings.advance(chunk.content); | ||
} | ||
} else { | ||
mappings.advance(chunk.content); | ||
if (chunk.outro.length) mappings.advance(chunk.outro); | ||
}); | ||
if (magicString.outro) { | ||
mappings.advance(magicString.outro); | ||
} | ||
if (chunk.outro.length) { mappings.advance(chunk.outro); } | ||
}); | ||
if (magicString.outro) { | ||
mappings.advance(magicString.outro); | ||
} | ||
}); | ||
return { | ||
file: options.file ? options.file.split(/[/\\]/).pop() : null, | ||
sources: this.uniqueSources.map((source) => { | ||
return options.file ? getRelativePath(options.file, source.filename) : source.filename; | ||
}), | ||
sourcesContent: this.uniqueSources.map((source) => { | ||
return options.includeContent ? source.content : null; | ||
}), | ||
names, | ||
mappings: mappings.raw, | ||
}; | ||
} | ||
return { | ||
file: options.file ? options.file.split(/[/\\]/).pop() : null, | ||
sources: this.uniqueSources.map(function (source) { | ||
return options.file ? getRelativePath(options.file, source.filename) : source.filename; | ||
}), | ||
sourcesContent: this.uniqueSources.map(function (source) { | ||
return options.includeContent ? source.content : null; | ||
}), | ||
names: names, | ||
mappings: mappings.raw, | ||
}; | ||
}; | ||
generateMap(options) { | ||
return new SourceMap(this.generateDecodedMap(options)); | ||
} | ||
Bundle.prototype.generateMap = function generateMap (options) { | ||
return new SourceMap(this.generateDecodedMap(options)); | ||
}; | ||
getIndentString() { | ||
const indentStringCounts = {}; | ||
Bundle.prototype.getIndentString = function getIndentString () { | ||
var indentStringCounts = {}; | ||
this.sources.forEach((source) => { | ||
const indentStr = source.content.indentStr; | ||
this.sources.forEach(function (source) { | ||
var indentStr = source.content.indentStr; | ||
if (indentStr === null) return; | ||
if (indentStr === null) { return; } | ||
if (!indentStringCounts[indentStr]) indentStringCounts[indentStr] = 0; | ||
indentStringCounts[indentStr] += 1; | ||
}); | ||
if (!indentStringCounts[indentStr]) { indentStringCounts[indentStr] = 0; } | ||
indentStringCounts[indentStr] += 1; | ||
}); | ||
return ( | ||
Object.keys(indentStringCounts).sort((a, b) => { | ||
return indentStringCounts[a] - indentStringCounts[b]; | ||
})[0] || '\t' | ||
); | ||
} | ||
return ( | ||
Object.keys(indentStringCounts).sort(function (a, b) { | ||
return indentStringCounts[a] - indentStringCounts[b]; | ||
})[0] || '\t' | ||
); | ||
}; | ||
indent(indentStr) { | ||
if (!arguments.length) { | ||
indentStr = this.getIndentString(); | ||
} | ||
Bundle.prototype.indent = function indent (indentStr) { | ||
var this$1$1 = this; | ||
if (indentStr === '') return this; // noop | ||
if (!arguments.length) { | ||
indentStr = this.getIndentString(); | ||
} | ||
let trailingNewline = !this.intro || this.intro.slice(-1) === '\n'; | ||
if (indentStr === '') { return this; } // noop | ||
this.sources.forEach((source, i) => { | ||
const separator = source.separator !== undefined ? source.separator : this.separator; | ||
const indentStart = trailingNewline || (i > 0 && /\r?\n$/.test(separator)); | ||
var trailingNewline = !this.intro || this.intro.slice(-1) === '\n'; | ||
source.content.indent(indentStr, { | ||
exclude: source.indentExclusionRanges, | ||
indentStart, //: trailingNewline || /\r?\n$/.test( separator ) //true///\r?\n/.test( separator ) | ||
}); | ||
this.sources.forEach(function (source, i) { | ||
var separator = source.separator !== undefined ? source.separator : this$1$1.separator; | ||
var indentStart = trailingNewline || (i > 0 && /\r?\n$/.test(separator)); | ||
source.content.indent(indentStr, { | ||
exclude: source.indentExclusionRanges, | ||
indentStart: indentStart, //: trailingNewline || /\r?\n$/.test( separator ) //true///\r?\n/.test( separator ) | ||
trailingNewline = source.content.lastChar() === '\n'; | ||
}); | ||
trailingNewline = source.content.lastChar() === '\n'; | ||
}); | ||
if (this.intro) { | ||
this.intro = | ||
indentStr + | ||
this.intro.replace(/^[^\n]/gm, (match, index) => { | ||
return index > 0 ? indentStr + match : match; | ||
}); | ||
} | ||
if (this.intro) { | ||
this.intro = | ||
indentStr + | ||
this.intro.replace(/^[^\n]/gm, function (match, index) { | ||
return index > 0 ? indentStr + match : match; | ||
}); | ||
return this; | ||
} | ||
return this; | ||
}; | ||
prepend(str) { | ||
this.intro = str + this.intro; | ||
return this; | ||
} | ||
Bundle.prototype.prepend = function prepend (str) { | ||
this.intro = str + this.intro; | ||
return this; | ||
}; | ||
toString() { | ||
const body = this.sources | ||
.map((source, i) => { | ||
const separator = source.separator !== undefined ? source.separator : this.separator; | ||
const str = (i > 0 ? separator : '') + source.content.toString(); | ||
Bundle.prototype.toString = function toString () { | ||
var this$1$1 = this; | ||
return str; | ||
}) | ||
.join(''); | ||
var body = this.sources | ||
.map(function (source, i) { | ||
var separator = source.separator !== undefined ? source.separator : this$1$1.separator; | ||
var str = (i > 0 ? separator : '') + source.content.toString(); | ||
return this.intro + body; | ||
} | ||
return str; | ||
}) | ||
.join(''); | ||
isEmpty() { | ||
if (this.intro.length && this.intro.trim()) return false; | ||
if (this.sources.some((source) => !source.content.isEmpty())) return false; | ||
return true; | ||
} | ||
return this.intro + body; | ||
}; | ||
length() { | ||
return this.sources.reduce( | ||
(length, source) => length + source.content.length(), | ||
this.intro.length | ||
); | ||
} | ||
Bundle.prototype.isEmpty = function isEmpty () { | ||
if (this.intro.length && this.intro.trim()) { return false; } | ||
if (this.sources.some(function (source) { return !source.content.isEmpty(); })) { return false; } | ||
return true; | ||
}; | ||
trimLines() { | ||
return this.trim('[\\r\\n]'); | ||
} | ||
Bundle.prototype.length = function length () { | ||
return this.sources.reduce( | ||
function (length, source) { return length + source.content.length(); }, | ||
this.intro.length | ||
); | ||
}; | ||
trim(charType) { | ||
return this.trimStart(charType).trimEnd(charType); | ||
} | ||
Bundle.prototype.trimLines = function trimLines () { | ||
return this.trim('[\\r\\n]'); | ||
}; | ||
trimStart(charType) { | ||
const rx = new RegExp('^' + (charType || '\\s') + '+'); | ||
this.intro = this.intro.replace(rx, ''); | ||
Bundle.prototype.trim = function trim (charType) { | ||
return this.trimStart(charType).trimEnd(charType); | ||
}; | ||
if (!this.intro) { | ||
let source; | ||
let i = 0; | ||
Bundle.prototype.trimStart = function trimStart (charType) { | ||
var rx = new RegExp('^' + (charType || '\\s') + '+'); | ||
this.intro = this.intro.replace(rx, ''); | ||
do { | ||
source = this.sources[i++]; | ||
if (!source) { | ||
break; | ||
} | ||
} while (!source.content.trimStartAborted(charType)); | ||
} | ||
if (!this.intro) { | ||
var source; | ||
var i = 0; | ||
return this; | ||
} | ||
trimEnd(charType) { | ||
const rx = new RegExp((charType || '\\s') + '+$'); | ||
let source; | ||
let i = this.sources.length - 1; | ||
do { | ||
source = this.sources[i++]; | ||
source = this.sources[i--]; | ||
if (!source) { | ||
this.intro = this.intro.replace(rx, ''); | ||
break; | ||
} | ||
} while (!source.content.trimStartAborted(charType)); | ||
} while (!source.content.trimEndAborted(charType)); | ||
return this; | ||
} | ||
} | ||
return this; | ||
}; | ||
Bundle.prototype.trimEnd = function trimEnd (charType) { | ||
var rx = new RegExp((charType || '\\s') + '+$'); | ||
var source; | ||
var i = this.sources.length - 1; | ||
do { | ||
source = this.sources[i--]; | ||
if (!source) { | ||
this.intro = this.intro.replace(rx, ''); | ||
break; | ||
} | ||
} while (!source.content.trimEndAborted(charType)); | ||
return this; | ||
}; | ||
MagicString.Bundle = Bundle; | ||
@@ -1316,0 +1358,0 @@ MagicString.SourceMap = SourceMap; |
@@ -7,166 +7,170 @@ (function (global, factory) { | ||
var BitSet = function BitSet(arg) { | ||
this.bits = arg instanceof BitSet ? arg.bits.slice() : []; | ||
}; | ||
class BitSet { | ||
constructor(arg) { | ||
this.bits = arg instanceof BitSet ? arg.bits.slice() : []; | ||
} | ||
BitSet.prototype.add = function add (n) { | ||
this.bits[n >> 5] |= 1 << (n & 31); | ||
}; | ||
add(n) { | ||
this.bits[n >> 5] |= 1 << (n & 31); | ||
} | ||
BitSet.prototype.has = function has (n) { | ||
return !!(this.bits[n >> 5] & (1 << (n & 31))); | ||
}; | ||
has(n) { | ||
return !!(this.bits[n >> 5] & (1 << (n & 31))); | ||
} | ||
} | ||
var Chunk = function Chunk(start, end, content) { | ||
this.start = start; | ||
this.end = end; | ||
this.original = content; | ||
class Chunk { | ||
constructor(start, end, content) { | ||
this.start = start; | ||
this.end = end; | ||
this.original = content; | ||
this.intro = ''; | ||
this.outro = ''; | ||
this.intro = ''; | ||
this.outro = ''; | ||
this.content = content; | ||
this.storeName = false; | ||
this.edited = false; | ||
this.content = content; | ||
this.storeName = false; | ||
this.edited = false; | ||
// we make these non-enumerable, for sanity while debugging | ||
Object.defineProperties(this, { | ||
previous: { writable: true, value: null }, | ||
next: { writable: true, value: null }, | ||
}); | ||
}; | ||
// we make these non-enumerable, for sanity while debugging | ||
Object.defineProperties(this, { | ||
previous: { writable: true, value: null }, | ||
next: { writable: true, value: null }, | ||
}); | ||
} | ||
Chunk.prototype.appendLeft = function appendLeft (content) { | ||
this.outro += content; | ||
}; | ||
appendLeft(content) { | ||
this.outro += content; | ||
} | ||
Chunk.prototype.appendRight = function appendRight (content) { | ||
this.intro = this.intro + content; | ||
}; | ||
appendRight(content) { | ||
this.intro = this.intro + content; | ||
} | ||
Chunk.prototype.clone = function clone () { | ||
var chunk = new Chunk(this.start, this.end, this.original); | ||
clone() { | ||
const chunk = new Chunk(this.start, this.end, this.original); | ||
chunk.intro = this.intro; | ||
chunk.outro = this.outro; | ||
chunk.content = this.content; | ||
chunk.storeName = this.storeName; | ||
chunk.edited = this.edited; | ||
chunk.intro = this.intro; | ||
chunk.outro = this.outro; | ||
chunk.content = this.content; | ||
chunk.storeName = this.storeName; | ||
chunk.edited = this.edited; | ||
return chunk; | ||
}; | ||
return chunk; | ||
} | ||
Chunk.prototype.contains = function contains (index) { | ||
return this.start < index && index < this.end; | ||
}; | ||
contains(index) { | ||
return this.start < index && index < this.end; | ||
} | ||
Chunk.prototype.eachNext = function eachNext (fn) { | ||
var chunk = this; | ||
while (chunk) { | ||
fn(chunk); | ||
chunk = chunk.next; | ||
eachNext(fn) { | ||
let chunk = this; | ||
while (chunk) { | ||
fn(chunk); | ||
chunk = chunk.next; | ||
} | ||
} | ||
}; | ||
Chunk.prototype.eachPrevious = function eachPrevious (fn) { | ||
var chunk = this; | ||
while (chunk) { | ||
fn(chunk); | ||
chunk = chunk.previous; | ||
eachPrevious(fn) { | ||
let chunk = this; | ||
while (chunk) { | ||
fn(chunk); | ||
chunk = chunk.previous; | ||
} | ||
} | ||
}; | ||
Chunk.prototype.edit = function edit (content, storeName, contentOnly) { | ||
this.content = content; | ||
if (!contentOnly) { | ||
this.intro = ''; | ||
this.outro = ''; | ||
edit(content, storeName, contentOnly) { | ||
this.content = content; | ||
if (!contentOnly) { | ||
this.intro = ''; | ||
this.outro = ''; | ||
} | ||
this.storeName = storeName; | ||
this.edited = true; | ||
return this; | ||
} | ||
this.storeName = storeName; | ||
this.edited = true; | ||
prependLeft(content) { | ||
this.outro = content + this.outro; | ||
} | ||
return this; | ||
}; | ||
prependRight(content) { | ||
this.intro = content + this.intro; | ||
} | ||
Chunk.prototype.prependLeft = function prependLeft (content) { | ||
this.outro = content + this.outro; | ||
}; | ||
split(index) { | ||
const sliceIndex = index - this.start; | ||
Chunk.prototype.prependRight = function prependRight (content) { | ||
this.intro = content + this.intro; | ||
}; | ||
const originalBefore = this.original.slice(0, sliceIndex); | ||
const originalAfter = this.original.slice(sliceIndex); | ||
Chunk.prototype.split = function split (index) { | ||
var sliceIndex = index - this.start; | ||
this.original = originalBefore; | ||
var originalBefore = this.original.slice(0, sliceIndex); | ||
var originalAfter = this.original.slice(sliceIndex); | ||
const newChunk = new Chunk(index, this.end, originalAfter); | ||
newChunk.outro = this.outro; | ||
this.outro = ''; | ||
this.original = originalBefore; | ||
this.end = index; | ||
var newChunk = new Chunk(index, this.end, originalAfter); | ||
newChunk.outro = this.outro; | ||
this.outro = ''; | ||
if (this.edited) { | ||
// TODO is this block necessary?... | ||
newChunk.edit('', false); | ||
this.content = ''; | ||
} else { | ||
this.content = originalBefore; | ||
} | ||
this.end = index; | ||
newChunk.next = this.next; | ||
if (newChunk.next) newChunk.next.previous = newChunk; | ||
newChunk.previous = this; | ||
this.next = newChunk; | ||
if (this.edited) { | ||
// TODO is this block necessary?... | ||
newChunk.edit('', false); | ||
this.content = ''; | ||
} else { | ||
this.content = originalBefore; | ||
return newChunk; | ||
} | ||
newChunk.next = this.next; | ||
if (newChunk.next) { newChunk.next.previous = newChunk; } | ||
newChunk.previous = this; | ||
this.next = newChunk; | ||
toString() { | ||
return this.intro + this.content + this.outro; | ||
} | ||
return newChunk; | ||
}; | ||
trimEnd(rx) { | ||
this.outro = this.outro.replace(rx, ''); | ||
if (this.outro.length) return true; | ||
Chunk.prototype.toString = function toString () { | ||
return this.intro + this.content + this.outro; | ||
}; | ||
const trimmed = this.content.replace(rx, ''); | ||
Chunk.prototype.trimEnd = function trimEnd (rx) { | ||
this.outro = this.outro.replace(rx, ''); | ||
if (this.outro.length) { return true; } | ||
if (trimmed.length) { | ||
if (trimmed !== this.content) { | ||
this.split(this.start + trimmed.length).edit('', undefined, true); | ||
} | ||
return true; | ||
} else { | ||
this.edit('', undefined, true); | ||
var trimmed = this.content.replace(rx, ''); | ||
if (trimmed.length) { | ||
if (trimmed !== this.content) { | ||
this.split(this.start + trimmed.length).edit('', undefined, true); | ||
this.intro = this.intro.replace(rx, ''); | ||
if (this.intro.length) return true; | ||
} | ||
return true; | ||
} else { | ||
this.edit('', undefined, true); | ||
} | ||
trimStart(rx) { | ||
this.intro = this.intro.replace(rx, ''); | ||
if (this.intro.length) { return true; } | ||
} | ||
}; | ||
if (this.intro.length) return true; | ||
Chunk.prototype.trimStart = function trimStart (rx) { | ||
this.intro = this.intro.replace(rx, ''); | ||
if (this.intro.length) { return true; } | ||
const trimmed = this.content.replace(rx, ''); | ||
var trimmed = this.content.replace(rx, ''); | ||
if (trimmed.length) { | ||
if (trimmed !== this.content) { | ||
this.split(this.end - trimmed.length); | ||
this.edit('', undefined, true); | ||
} | ||
return true; | ||
} else { | ||
this.edit('', undefined, true); | ||
if (trimmed.length) { | ||
if (trimmed !== this.content) { | ||
this.split(this.end - trimmed.length); | ||
this.edit('', undefined, true); | ||
this.outro = this.outro.replace(rx, ''); | ||
if (this.outro.length) return true; | ||
} | ||
return true; | ||
} else { | ||
this.edit('', undefined, true); | ||
this.outro = this.outro.replace(rx, ''); | ||
if (this.outro.length) { return true; } | ||
} | ||
}; | ||
} | ||
@@ -229,33 +233,35 @@ var charToInteger = {}; | ||
var btoa = function () { | ||
let btoa = () => { | ||
throw new Error('Unsupported environment: `window.btoa` or `Buffer` should be supported.'); | ||
}; | ||
if (typeof window !== 'undefined' && typeof window.btoa === 'function') { | ||
btoa = function (str) { return window.btoa(unescape(encodeURIComponent(str))); }; | ||
btoa = (str) => window.btoa(unescape(encodeURIComponent(str))); | ||
} else if (typeof Buffer === 'function') { | ||
btoa = function (str) { return Buffer.from(str, 'utf-8').toString('base64'); }; | ||
btoa = (str) => Buffer.from(str, 'utf-8').toString('base64'); | ||
} | ||
var SourceMap = function SourceMap(properties) { | ||
this.version = 3; | ||
this.file = properties.file; | ||
this.sources = properties.sources; | ||
this.sourcesContent = properties.sourcesContent; | ||
this.names = properties.names; | ||
this.mappings = encode(properties.mappings); | ||
}; | ||
class SourceMap { | ||
constructor(properties) { | ||
this.version = 3; | ||
this.file = properties.file; | ||
this.sources = properties.sources; | ||
this.sourcesContent = properties.sourcesContent; | ||
this.names = properties.names; | ||
this.mappings = encode(properties.mappings); | ||
} | ||
SourceMap.prototype.toString = function toString () { | ||
return JSON.stringify(this); | ||
}; | ||
toString() { | ||
return JSON.stringify(this); | ||
} | ||
SourceMap.prototype.toUrl = function toUrl () { | ||
return 'data:application/json;charset=utf-8;base64,' + btoa(this.toString()); | ||
}; | ||
toUrl() { | ||
return 'data:application/json;charset=utf-8;base64,' + btoa(this.toString()); | ||
} | ||
} | ||
function guessIndent(code) { | ||
var lines = code.split('\n'); | ||
const lines = code.split('\n'); | ||
var tabbed = lines.filter(function (line) { return /^\t+/.test(line); }); | ||
var spaced = lines.filter(function (line) { return /^ {2,}/.test(line); }); | ||
const tabbed = lines.filter((line) => /^\t+/.test(line)); | ||
const spaced = lines.filter((line) => /^ {2,}/.test(line)); | ||
@@ -274,4 +280,4 @@ if (tabbed.length === 0 && spaced.length === 0) { | ||
// Otherwise, we need to guess the multiple | ||
var min = spaced.reduce(function (previous, current) { | ||
var numSpaces = /^ +/.exec(current)[0].length; | ||
const min = spaced.reduce((previous, current) => { | ||
const numSpaces = /^ +/.exec(current)[0].length; | ||
return Math.min(numSpaces, previous); | ||
@@ -284,4 +290,4 @@ }, Infinity); | ||
function getRelativePath(from, to) { | ||
var fromParts = from.split(/[/\\]/); | ||
var toParts = to.split(/[/\\]/); | ||
const fromParts = from.split(/[/\\]/); | ||
const toParts = to.split(/[/\\]/); | ||
@@ -296,4 +302,4 @@ fromParts.pop(); // get dirname | ||
if (fromParts.length) { | ||
var i = fromParts.length; | ||
while (i--) { fromParts[i] = '..'; } | ||
let i = fromParts.length; | ||
while (i--) fromParts[i] = '..'; | ||
} | ||
@@ -304,3 +310,3 @@ | ||
var toString = Object.prototype.toString; | ||
const toString = Object.prototype.toString; | ||
@@ -312,6 +318,6 @@ function isObject(thing) { | ||
function getLocator(source) { | ||
var originalLines = source.split('\n'); | ||
var lineOffsets = []; | ||
const originalLines = source.split('\n'); | ||
const lineOffsets = []; | ||
for (var i = 0, pos = 0; i < originalLines.length; i++) { | ||
for (let i = 0, pos = 0; i < originalLines.length; i++) { | ||
lineOffsets.push(pos); | ||
@@ -322,6 +328,6 @@ pos += originalLines[i].length + 1; | ||
return function locate(index) { | ||
var i = 0; | ||
var j = lineOffsets.length; | ||
let i = 0; | ||
let j = lineOffsets.length; | ||
while (i < j) { | ||
var m = (i + j) >> 1; | ||
const m = (i + j) >> 1; | ||
if (index < lineOffsets[m]) { | ||
@@ -333,79 +339,81 @@ j = m; | ||
} | ||
var line = i - 1; | ||
var column = index - lineOffsets[line]; | ||
return { line: line, column: column }; | ||
const line = i - 1; | ||
const column = index - lineOffsets[line]; | ||
return { line, column }; | ||
}; | ||
} | ||
var Mappings = function Mappings(hires) { | ||
this.hires = hires; | ||
this.generatedCodeLine = 0; | ||
this.generatedCodeColumn = 0; | ||
this.raw = []; | ||
this.rawSegments = this.raw[this.generatedCodeLine] = []; | ||
this.pending = null; | ||
}; | ||
class Mappings { | ||
constructor(hires) { | ||
this.hires = hires; | ||
this.generatedCodeLine = 0; | ||
this.generatedCodeColumn = 0; | ||
this.raw = []; | ||
this.rawSegments = this.raw[this.generatedCodeLine] = []; | ||
this.pending = null; | ||
} | ||
Mappings.prototype.addEdit = function addEdit (sourceIndex, content, loc, nameIndex) { | ||
if (content.length) { | ||
var segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column]; | ||
if (nameIndex >= 0) { | ||
segment.push(nameIndex); | ||
addEdit(sourceIndex, content, loc, nameIndex) { | ||
if (content.length) { | ||
const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column]; | ||
if (nameIndex >= 0) { | ||
segment.push(nameIndex); | ||
} | ||
this.rawSegments.push(segment); | ||
} else if (this.pending) { | ||
this.rawSegments.push(this.pending); | ||
} | ||
this.rawSegments.push(segment); | ||
} else if (this.pending) { | ||
this.rawSegments.push(this.pending); | ||
this.advance(content); | ||
this.pending = null; | ||
} | ||
this.advance(content); | ||
this.pending = null; | ||
}; | ||
addUneditedChunk(sourceIndex, chunk, original, loc, sourcemapLocations) { | ||
let originalCharIndex = chunk.start; | ||
let first = true; | ||
Mappings.prototype.addUneditedChunk = function addUneditedChunk (sourceIndex, chunk, original, loc, sourcemapLocations) { | ||
var originalCharIndex = chunk.start; | ||
var first = true; | ||
while (originalCharIndex < chunk.end) { | ||
if (this.hires || first || sourcemapLocations.has(originalCharIndex)) { | ||
this.rawSegments.push([this.generatedCodeColumn, sourceIndex, loc.line, loc.column]); | ||
} | ||
while (originalCharIndex < chunk.end) { | ||
if (this.hires || first || sourcemapLocations.has(originalCharIndex)) { | ||
this.rawSegments.push([this.generatedCodeColumn, sourceIndex, loc.line, loc.column]); | ||
} | ||
if (original[originalCharIndex] === '\n') { | ||
loc.line += 1; | ||
loc.column = 0; | ||
this.generatedCodeLine += 1; | ||
this.raw[this.generatedCodeLine] = this.rawSegments = []; | ||
this.generatedCodeColumn = 0; | ||
first = true; | ||
} else { | ||
loc.column += 1; | ||
this.generatedCodeColumn += 1; | ||
first = false; | ||
} | ||
if (original[originalCharIndex] === '\n') { | ||
loc.line += 1; | ||
loc.column = 0; | ||
this.generatedCodeLine += 1; | ||
this.raw[this.generatedCodeLine] = this.rawSegments = []; | ||
this.generatedCodeColumn = 0; | ||
first = true; | ||
} else { | ||
loc.column += 1; | ||
this.generatedCodeColumn += 1; | ||
first = false; | ||
originalCharIndex += 1; | ||
} | ||
originalCharIndex += 1; | ||
this.pending = null; | ||
} | ||
this.pending = null; | ||
}; | ||
advance(str) { | ||
if (!str) return; | ||
Mappings.prototype.advance = function advance (str) { | ||
if (!str) { return; } | ||
const lines = str.split('\n'); | ||
var lines = str.split('\n'); | ||
if (lines.length > 1) { | ||
for (let i = 0; i < lines.length - 1; i++) { | ||
this.generatedCodeLine++; | ||
this.raw[this.generatedCodeLine] = this.rawSegments = []; | ||
} | ||
this.generatedCodeColumn = 0; | ||
} | ||
if (lines.length > 1) { | ||
for (var i = 0; i < lines.length - 1; i++) { | ||
this.generatedCodeLine++; | ||
this.raw[this.generatedCodeLine] = this.rawSegments = []; | ||
} | ||
this.generatedCodeColumn = 0; | ||
this.generatedCodeColumn += lines[lines.length - 1].length; | ||
} | ||
} | ||
this.generatedCodeColumn += lines[lines.length - 1].length; | ||
}; | ||
const n = '\n'; | ||
var n = '\n'; | ||
var warned = { | ||
const warned = { | ||
insertLeft: false, | ||
@@ -416,959 +424,993 @@ insertRight: false, | ||
var MagicString = function MagicString(string, options) { | ||
if ( options === void 0 ) options = {}; | ||
class MagicString { | ||
constructor(string, options = {}) { | ||
const chunk = new Chunk(0, string.length, string); | ||
var chunk = new Chunk(0, string.length, string); | ||
Object.defineProperties(this, { | ||
original: { writable: true, value: string }, | ||
outro: { writable: true, value: '' }, | ||
intro: { writable: true, value: '' }, | ||
firstChunk: { writable: true, value: chunk }, | ||
lastChunk: { writable: true, value: chunk }, | ||
lastSearchedChunk: { writable: true, value: chunk }, | ||
byStart: { writable: true, value: {} }, | ||
byEnd: { writable: true, value: {} }, | ||
filename: { writable: true, value: options.filename }, | ||
indentExclusionRanges: { writable: true, value: options.indentExclusionRanges }, | ||
sourcemapLocations: { writable: true, value: new BitSet() }, | ||
storedNames: { writable: true, value: {} }, | ||
indentStr: { writable: true, value: guessIndent(string) }, | ||
}); | ||
Object.defineProperties(this, { | ||
original: { writable: true, value: string }, | ||
outro: { writable: true, value: '' }, | ||
intro: { writable: true, value: '' }, | ||
firstChunk: { writable: true, value: chunk }, | ||
lastChunk: { writable: true, value: chunk }, | ||
lastSearchedChunk: { writable: true, value: chunk }, | ||
byStart: { writable: true, value: {} }, | ||
byEnd: { writable: true, value: {} }, | ||
filename: { writable: true, value: options.filename }, | ||
indentExclusionRanges: { writable: true, value: options.indentExclusionRanges }, | ||
sourcemapLocations: { writable: true, value: new BitSet() }, | ||
storedNames: { writable: true, value: {} }, | ||
indentStr: { writable: true, value: guessIndent(string) }, | ||
}); | ||
this.byStart[0] = chunk; | ||
this.byEnd[string.length] = chunk; | ||
} | ||
this.byStart[0] = chunk; | ||
this.byEnd[string.length] = chunk; | ||
}; | ||
addSourcemapLocation(char) { | ||
this.sourcemapLocations.add(char); | ||
} | ||
MagicString.prototype.addSourcemapLocation = function addSourcemapLocation (char) { | ||
this.sourcemapLocations.add(char); | ||
}; | ||
append(content) { | ||
if (typeof content !== 'string') throw new TypeError('outro content must be a string'); | ||
MagicString.prototype.append = function append (content) { | ||
if (typeof content !== 'string') { throw new TypeError('outro content must be a string'); } | ||
this.outro += content; | ||
return this; | ||
} | ||
this.outro += content; | ||
return this; | ||
}; | ||
appendLeft(index, content) { | ||
if (typeof content !== 'string') throw new TypeError('inserted content must be a string'); | ||
MagicString.prototype.appendLeft = function appendLeft (index, content) { | ||
if (typeof content !== 'string') { throw new TypeError('inserted content must be a string'); } | ||
this._split(index); | ||
this._split(index); | ||
const chunk = this.byEnd[index]; | ||
var chunk = this.byEnd[index]; | ||
if (chunk) { | ||
chunk.appendLeft(content); | ||
} else { | ||
this.intro += content; | ||
if (chunk) { | ||
chunk.appendLeft(content); | ||
} else { | ||
this.intro += content; | ||
} | ||
return this; | ||
} | ||
return this; | ||
}; | ||
MagicString.prototype.appendRight = function appendRight (index, content) { | ||
if (typeof content !== 'string') { throw new TypeError('inserted content must be a string'); } | ||
appendRight(index, content) { | ||
if (typeof content !== 'string') throw new TypeError('inserted content must be a string'); | ||
this._split(index); | ||
this._split(index); | ||
var chunk = this.byStart[index]; | ||
const chunk = this.byStart[index]; | ||
if (chunk) { | ||
chunk.appendRight(content); | ||
} else { | ||
this.outro += content; | ||
if (chunk) { | ||
chunk.appendRight(content); | ||
} else { | ||
this.outro += content; | ||
} | ||
return this; | ||
} | ||
return this; | ||
}; | ||
MagicString.prototype.clone = function clone () { | ||
var cloned = new MagicString(this.original, { filename: this.filename }); | ||
clone() { | ||
const cloned = new MagicString(this.original, { filename: this.filename }); | ||
var originalChunk = this.firstChunk; | ||
var clonedChunk = (cloned.firstChunk = cloned.lastSearchedChunk = originalChunk.clone()); | ||
let originalChunk = this.firstChunk; | ||
let clonedChunk = (cloned.firstChunk = cloned.lastSearchedChunk = originalChunk.clone()); | ||
while (originalChunk) { | ||
cloned.byStart[clonedChunk.start] = clonedChunk; | ||
cloned.byEnd[clonedChunk.end] = clonedChunk; | ||
while (originalChunk) { | ||
cloned.byStart[clonedChunk.start] = clonedChunk; | ||
cloned.byEnd[clonedChunk.end] = clonedChunk; | ||
var nextOriginalChunk = originalChunk.next; | ||
var nextClonedChunk = nextOriginalChunk && nextOriginalChunk.clone(); | ||
const nextOriginalChunk = originalChunk.next; | ||
const nextClonedChunk = nextOriginalChunk && nextOriginalChunk.clone(); | ||
if (nextClonedChunk) { | ||
clonedChunk.next = nextClonedChunk; | ||
nextClonedChunk.previous = clonedChunk; | ||
if (nextClonedChunk) { | ||
clonedChunk.next = nextClonedChunk; | ||
nextClonedChunk.previous = clonedChunk; | ||
clonedChunk = nextClonedChunk; | ||
clonedChunk = nextClonedChunk; | ||
} | ||
originalChunk = nextOriginalChunk; | ||
} | ||
originalChunk = nextOriginalChunk; | ||
} | ||
cloned.lastChunk = clonedChunk; | ||
cloned.lastChunk = clonedChunk; | ||
if (this.indentExclusionRanges) { | ||
cloned.indentExclusionRanges = this.indentExclusionRanges.slice(); | ||
} | ||
if (this.indentExclusionRanges) { | ||
cloned.indentExclusionRanges = this.indentExclusionRanges.slice(); | ||
} | ||
cloned.sourcemapLocations = new BitSet(this.sourcemapLocations); | ||
cloned.sourcemapLocations = new BitSet(this.sourcemapLocations); | ||
cloned.intro = this.intro; | ||
cloned.outro = this.outro; | ||
cloned.intro = this.intro; | ||
cloned.outro = this.outro; | ||
return cloned; | ||
} | ||
return cloned; | ||
}; | ||
generateDecodedMap(options) { | ||
options = options || {}; | ||
MagicString.prototype.generateDecodedMap = function generateDecodedMap (options) { | ||
var this$1$1 = this; | ||
const sourceIndex = 0; | ||
const names = Object.keys(this.storedNames); | ||
const mappings = new Mappings(options.hires); | ||
options = options || {}; | ||
const locate = getLocator(this.original); | ||
var sourceIndex = 0; | ||
var names = Object.keys(this.storedNames); | ||
var mappings = new Mappings(options.hires); | ||
if (this.intro) { | ||
mappings.advance(this.intro); | ||
} | ||
var locate = getLocator(this.original); | ||
this.firstChunk.eachNext((chunk) => { | ||
const loc = locate(chunk.start); | ||
if (this.intro) { | ||
mappings.advance(this.intro); | ||
} | ||
if (chunk.intro.length) mappings.advance(chunk.intro); | ||
this.firstChunk.eachNext(function (chunk) { | ||
var loc = locate(chunk.start); | ||
if (chunk.edited) { | ||
mappings.addEdit( | ||
sourceIndex, | ||
chunk.content, | ||
loc, | ||
chunk.storeName ? names.indexOf(chunk.original) : -1 | ||
); | ||
} else { | ||
mappings.addUneditedChunk(sourceIndex, chunk, this.original, loc, this.sourcemapLocations); | ||
} | ||
if (chunk.intro.length) { mappings.advance(chunk.intro); } | ||
if (chunk.outro.length) mappings.advance(chunk.outro); | ||
}); | ||
if (chunk.edited) { | ||
mappings.addEdit( | ||
sourceIndex, | ||
chunk.content, | ||
loc, | ||
chunk.storeName ? names.indexOf(chunk.original) : -1 | ||
); | ||
} else { | ||
mappings.addUneditedChunk(sourceIndex, chunk, this$1$1.original, loc, this$1$1.sourcemapLocations); | ||
} | ||
return { | ||
file: options.file ? options.file.split(/[/\\]/).pop() : null, | ||
sources: [options.source ? getRelativePath(options.file || '', options.source) : null], | ||
sourcesContent: options.includeContent ? [this.original] : [null], | ||
names, | ||
mappings: mappings.raw, | ||
}; | ||
} | ||
if (chunk.outro.length) { mappings.advance(chunk.outro); } | ||
}); | ||
generateMap(options) { | ||
return new SourceMap(this.generateDecodedMap(options)); | ||
} | ||
return { | ||
file: options.file ? options.file.split(/[/\\]/).pop() : null, | ||
sources: [options.source ? getRelativePath(options.file || '', options.source) : null], | ||
sourcesContent: options.includeContent ? [this.original] : [null], | ||
names: names, | ||
mappings: mappings.raw, | ||
}; | ||
}; | ||
getIndentString() { | ||
return this.indentStr === null ? '\t' : this.indentStr; | ||
} | ||
MagicString.prototype.generateMap = function generateMap (options) { | ||
return new SourceMap(this.generateDecodedMap(options)); | ||
}; | ||
indent(indentStr, options) { | ||
const pattern = /^[^\r\n]/gm; | ||
MagicString.prototype.getIndentString = function getIndentString () { | ||
return this.indentStr === null ? '\t' : this.indentStr; | ||
}; | ||
if (isObject(indentStr)) { | ||
options = indentStr; | ||
indentStr = undefined; | ||
} | ||
MagicString.prototype.indent = function indent (indentStr, options) { | ||
var pattern = /^[^\r\n]/gm; | ||
indentStr = indentStr !== undefined ? indentStr : this.indentStr || '\t'; | ||
if (isObject(indentStr)) { | ||
options = indentStr; | ||
indentStr = undefined; | ||
} | ||
if (indentStr === '') return this; // noop | ||
indentStr = indentStr !== undefined ? indentStr : this.indentStr || '\t'; | ||
options = options || {}; | ||
if (indentStr === '') { return this; } // noop | ||
// Process exclusion ranges | ||
const isExcluded = {}; | ||
options = options || {}; | ||
if (options.exclude) { | ||
const exclusions = | ||
typeof options.exclude[0] === 'number' ? [options.exclude] : options.exclude; | ||
exclusions.forEach((exclusion) => { | ||
for (let i = exclusion[0]; i < exclusion[1]; i += 1) { | ||
isExcluded[i] = true; | ||
} | ||
}); | ||
} | ||
// Process exclusion ranges | ||
var isExcluded = {}; | ||
let shouldIndentNextCharacter = options.indentStart !== false; | ||
const replacer = (match) => { | ||
if (shouldIndentNextCharacter) return `${indentStr}${match}`; | ||
shouldIndentNextCharacter = true; | ||
return match; | ||
}; | ||
if (options.exclude) { | ||
var exclusions = | ||
typeof options.exclude[0] === 'number' ? [options.exclude] : options.exclude; | ||
exclusions.forEach(function (exclusion) { | ||
for (var i = exclusion[0]; i < exclusion[1]; i += 1) { | ||
isExcluded[i] = true; | ||
} | ||
}); | ||
} | ||
this.intro = this.intro.replace(pattern, replacer); | ||
var shouldIndentNextCharacter = options.indentStart !== false; | ||
var replacer = function (match) { | ||
if (shouldIndentNextCharacter) { return ("" + indentStr + match); } | ||
shouldIndentNextCharacter = true; | ||
return match; | ||
}; | ||
let charIndex = 0; | ||
let chunk = this.firstChunk; | ||
this.intro = this.intro.replace(pattern, replacer); | ||
while (chunk) { | ||
const end = chunk.end; | ||
var charIndex = 0; | ||
var chunk = this.firstChunk; | ||
if (chunk.edited) { | ||
if (!isExcluded[charIndex]) { | ||
chunk.content = chunk.content.replace(pattern, replacer); | ||
while (chunk) { | ||
var end = chunk.end; | ||
if (chunk.edited) { | ||
if (!isExcluded[charIndex]) { | ||
chunk.content = chunk.content.replace(pattern, replacer); | ||
if (chunk.content.length) { | ||
shouldIndentNextCharacter = chunk.content[chunk.content.length - 1] === '\n'; | ||
if (chunk.content.length) { | ||
shouldIndentNextCharacter = chunk.content[chunk.content.length - 1] === '\n'; | ||
} | ||
} | ||
} | ||
} else { | ||
charIndex = chunk.start; | ||
} else { | ||
charIndex = chunk.start; | ||
while (charIndex < end) { | ||
if (!isExcluded[charIndex]) { | ||
var char = this.original[charIndex]; | ||
while (charIndex < end) { | ||
if (!isExcluded[charIndex]) { | ||
const char = this.original[charIndex]; | ||
if (char === '\n') { | ||
shouldIndentNextCharacter = true; | ||
} else if (char !== '\r' && shouldIndentNextCharacter) { | ||
shouldIndentNextCharacter = false; | ||
if (char === '\n') { | ||
shouldIndentNextCharacter = true; | ||
} else if (char !== '\r' && shouldIndentNextCharacter) { | ||
shouldIndentNextCharacter = false; | ||
if (charIndex === chunk.start) { | ||
chunk.prependRight(indentStr); | ||
} else { | ||
this._splitChunk(chunk, charIndex); | ||
chunk = chunk.next; | ||
chunk.prependRight(indentStr); | ||
if (charIndex === chunk.start) { | ||
chunk.prependRight(indentStr); | ||
} else { | ||
this._splitChunk(chunk, charIndex); | ||
chunk = chunk.next; | ||
chunk.prependRight(indentStr); | ||
} | ||
} | ||
} | ||
charIndex += 1; | ||
} | ||
} | ||
charIndex += 1; | ||
} | ||
charIndex = chunk.end; | ||
chunk = chunk.next; | ||
} | ||
charIndex = chunk.end; | ||
chunk = chunk.next; | ||
this.outro = this.outro.replace(pattern, replacer); | ||
return this; | ||
} | ||
this.outro = this.outro.replace(pattern, replacer); | ||
insert() { | ||
throw new Error( | ||
'magicString.insert(...) is deprecated. Use prependRight(...) or appendLeft(...)' | ||
); | ||
} | ||
return this; | ||
}; | ||
insertLeft(index, content) { | ||
if (!warned.insertLeft) { | ||
console.warn( | ||
'magicString.insertLeft(...) is deprecated. Use magicString.appendLeft(...) instead' | ||
); // eslint-disable-line no-console | ||
warned.insertLeft = true; | ||
} | ||
MagicString.prototype.insert = function insert () { | ||
throw new Error( | ||
'magicString.insert(...) is deprecated. Use prependRight(...) or appendLeft(...)' | ||
); | ||
}; | ||
MagicString.prototype.insertLeft = function insertLeft (index, content) { | ||
if (!warned.insertLeft) { | ||
console.warn( | ||
'magicString.insertLeft(...) is deprecated. Use magicString.appendLeft(...) instead' | ||
); // eslint-disable-line no-console | ||
warned.insertLeft = true; | ||
return this.appendLeft(index, content); | ||
} | ||
return this.appendLeft(index, content); | ||
}; | ||
insertRight(index, content) { | ||
if (!warned.insertRight) { | ||
console.warn( | ||
'magicString.insertRight(...) is deprecated. Use magicString.prependRight(...) instead' | ||
); // eslint-disable-line no-console | ||
warned.insertRight = true; | ||
} | ||
MagicString.prototype.insertRight = function insertRight (index, content) { | ||
if (!warned.insertRight) { | ||
console.warn( | ||
'magicString.insertRight(...) is deprecated. Use magicString.prependRight(...) instead' | ||
); // eslint-disable-line no-console | ||
warned.insertRight = true; | ||
return this.prependRight(index, content); | ||
} | ||
return this.prependRight(index, content); | ||
}; | ||
move(start, end, index) { | ||
if (index >= start && index <= end) throw new Error('Cannot move a selection inside itself'); | ||
MagicString.prototype.move = function move (start, end, index) { | ||
if (index >= start && index <= end) { throw new Error('Cannot move a selection inside itself'); } | ||
this._split(start); | ||
this._split(end); | ||
this._split(index); | ||
this._split(start); | ||
this._split(end); | ||
this._split(index); | ||
const first = this.byStart[start]; | ||
const last = this.byEnd[end]; | ||
var first = this.byStart[start]; | ||
var last = this.byEnd[end]; | ||
const oldLeft = first.previous; | ||
const oldRight = last.next; | ||
var oldLeft = first.previous; | ||
var oldRight = last.next; | ||
const newRight = this.byStart[index]; | ||
if (!newRight && last === this.lastChunk) return this; | ||
const newLeft = newRight ? newRight.previous : this.lastChunk; | ||
var newRight = this.byStart[index]; | ||
if (!newRight && last === this.lastChunk) { return this; } | ||
var newLeft = newRight ? newRight.previous : this.lastChunk; | ||
if (oldLeft) oldLeft.next = oldRight; | ||
if (oldRight) oldRight.previous = oldLeft; | ||
if (oldLeft) { oldLeft.next = oldRight; } | ||
if (oldRight) { oldRight.previous = oldLeft; } | ||
if (newLeft) newLeft.next = first; | ||
if (newRight) newRight.previous = last; | ||
if (newLeft) { newLeft.next = first; } | ||
if (newRight) { newRight.previous = last; } | ||
if (!first.previous) this.firstChunk = last.next; | ||
if (!last.next) { | ||
this.lastChunk = first.previous; | ||
this.lastChunk.next = null; | ||
} | ||
if (!first.previous) { this.firstChunk = last.next; } | ||
if (!last.next) { | ||
this.lastChunk = first.previous; | ||
this.lastChunk.next = null; | ||
first.previous = newLeft; | ||
last.next = newRight || null; | ||
if (!newLeft) this.firstChunk = first; | ||
if (!newRight) this.lastChunk = last; | ||
return this; | ||
} | ||
first.previous = newLeft; | ||
last.next = newRight || null; | ||
overwrite(start, end, content, options) { | ||
if (typeof content !== 'string') throw new TypeError('replacement content must be a string'); | ||
if (!newLeft) { this.firstChunk = first; } | ||
if (!newRight) { this.lastChunk = last; } | ||
return this; | ||
}; | ||
while (start < 0) start += this.original.length; | ||
while (end < 0) end += this.original.length; | ||
MagicString.prototype.overwrite = function overwrite (start, end, content, options) { | ||
if (typeof content !== 'string') { throw new TypeError('replacement content must be a string'); } | ||
if (end > this.original.length) throw new Error('end is out of bounds'); | ||
if (start === end) | ||
throw new Error( | ||
'Cannot overwrite a zero-length range – use appendLeft or prependRight instead' | ||
); | ||
while (start < 0) { start += this.original.length; } | ||
while (end < 0) { end += this.original.length; } | ||
this._split(start); | ||
this._split(end); | ||
if (end > this.original.length) { throw new Error('end is out of bounds'); } | ||
if (start === end) | ||
{ throw new Error( | ||
'Cannot overwrite a zero-length range – use appendLeft or prependRight instead' | ||
); } | ||
if (options === true) { | ||
if (!warned.storeName) { | ||
console.warn( | ||
'The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string' | ||
); // eslint-disable-line no-console | ||
warned.storeName = true; | ||
} | ||
this._split(start); | ||
this._split(end); | ||
options = { storeName: true }; | ||
} | ||
const storeName = options !== undefined ? options.storeName : false; | ||
const contentOnly = options !== undefined ? options.contentOnly : false; | ||
if (options === true) { | ||
if (!warned.storeName) { | ||
console.warn( | ||
'The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string' | ||
); // eslint-disable-line no-console | ||
warned.storeName = true; | ||
if (storeName) { | ||
const original = this.original.slice(start, end); | ||
Object.defineProperty(this.storedNames, original, { writable: true, value: true, enumerable: true }); | ||
} | ||
options = { storeName: true }; | ||
} | ||
var storeName = options !== undefined ? options.storeName : false; | ||
var contentOnly = options !== undefined ? options.contentOnly : false; | ||
const first = this.byStart[start]; | ||
const last = this.byEnd[end]; | ||
if (storeName) { | ||
var original = this.original.slice(start, end); | ||
Object.defineProperty(this.storedNames, original, { writable: true, value: true, enumerable: true }); | ||
} | ||
if (first) { | ||
let chunk = first; | ||
while (chunk !== last) { | ||
if (chunk.next !== this.byStart[chunk.end]) { | ||
throw new Error('Cannot overwrite across a split point'); | ||
} | ||
chunk = chunk.next; | ||
chunk.edit('', false); | ||
} | ||
var first = this.byStart[start]; | ||
var last = this.byEnd[end]; | ||
first.edit(content, storeName, contentOnly); | ||
} else { | ||
// must be inserting at the end | ||
const newChunk = new Chunk(start, end, '').edit(content, storeName); | ||
if (first) { | ||
var chunk = first; | ||
while (chunk !== last) { | ||
if (chunk.next !== this.byStart[chunk.end]) { | ||
throw new Error('Cannot overwrite across a split point'); | ||
} | ||
chunk = chunk.next; | ||
chunk.edit('', false); | ||
// TODO last chunk in the array may not be the last chunk, if it's moved... | ||
last.next = newChunk; | ||
newChunk.previous = last; | ||
} | ||
return this; | ||
} | ||
first.edit(content, storeName, contentOnly); | ||
} else { | ||
// must be inserting at the end | ||
var newChunk = new Chunk(start, end, '').edit(content, storeName); | ||
prepend(content) { | ||
if (typeof content !== 'string') throw new TypeError('outro content must be a string'); | ||
// TODO last chunk in the array may not be the last chunk, if it's moved... | ||
last.next = newChunk; | ||
newChunk.previous = last; | ||
this.intro = content + this.intro; | ||
return this; | ||
} | ||
return this; | ||
}; | ||
MagicString.prototype.prepend = function prepend (content) { | ||
if (typeof content !== 'string') { throw new TypeError('outro content must be a string'); } | ||
prependLeft(index, content) { | ||
if (typeof content !== 'string') throw new TypeError('inserted content must be a string'); | ||
this.intro = content + this.intro; | ||
return this; | ||
}; | ||
this._split(index); | ||
MagicString.prototype.prependLeft = function prependLeft (index, content) { | ||
if (typeof content !== 'string') { throw new TypeError('inserted content must be a string'); } | ||
const chunk = this.byEnd[index]; | ||
this._split(index); | ||
var chunk = this.byEnd[index]; | ||
if (chunk) { | ||
chunk.prependLeft(content); | ||
} else { | ||
this.intro = content + this.intro; | ||
if (chunk) { | ||
chunk.prependLeft(content); | ||
} else { | ||
this.intro = content + this.intro; | ||
} | ||
return this; | ||
} | ||
return this; | ||
}; | ||
MagicString.prototype.prependRight = function prependRight (index, content) { | ||
if (typeof content !== 'string') { throw new TypeError('inserted content must be a string'); } | ||
prependRight(index, content) { | ||
if (typeof content !== 'string') throw new TypeError('inserted content must be a string'); | ||
this._split(index); | ||
this._split(index); | ||
var chunk = this.byStart[index]; | ||
const chunk = this.byStart[index]; | ||
if (chunk) { | ||
chunk.prependRight(content); | ||
} else { | ||
this.outro = content + this.outro; | ||
if (chunk) { | ||
chunk.prependRight(content); | ||
} else { | ||
this.outro = content + this.outro; | ||
} | ||
return this; | ||
} | ||
return this; | ||
}; | ||
MagicString.prototype.remove = function remove (start, end) { | ||
while (start < 0) { start += this.original.length; } | ||
while (end < 0) { end += this.original.length; } | ||
remove(start, end) { | ||
while (start < 0) start += this.original.length; | ||
while (end < 0) end += this.original.length; | ||
if (start === end) { return this; } | ||
if (start === end) return this; | ||
if (start < 0 || end > this.original.length) { throw new Error('Character is out of bounds'); } | ||
if (start > end) { throw new Error('end must be greater than start'); } | ||
if (start < 0 || end > this.original.length) throw new Error('Character is out of bounds'); | ||
if (start > end) throw new Error('end must be greater than start'); | ||
this._split(start); | ||
this._split(end); | ||
this._split(start); | ||
this._split(end); | ||
var chunk = this.byStart[start]; | ||
let chunk = this.byStart[start]; | ||
while (chunk) { | ||
chunk.intro = ''; | ||
chunk.outro = ''; | ||
chunk.edit(''); | ||
while (chunk) { | ||
chunk.intro = ''; | ||
chunk.outro = ''; | ||
chunk.edit(''); | ||
chunk = end > chunk.end ? this.byStart[chunk.end] : null; | ||
chunk = end > chunk.end ? this.byStart[chunk.end] : null; | ||
} | ||
return this; | ||
} | ||
return this; | ||
}; | ||
MagicString.prototype.lastChar = function lastChar () { | ||
if (this.outro.length) { return this.outro[this.outro.length - 1]; } | ||
var chunk = this.lastChunk; | ||
do { | ||
if (chunk.outro.length) { return chunk.outro[chunk.outro.length - 1]; } | ||
if (chunk.content.length) { return chunk.content[chunk.content.length - 1]; } | ||
if (chunk.intro.length) { return chunk.intro[chunk.intro.length - 1]; } | ||
} while ((chunk = chunk.previous)); | ||
if (this.intro.length) { return this.intro[this.intro.length - 1]; } | ||
return ''; | ||
}; | ||
lastChar() { | ||
if (this.outro.length) return this.outro[this.outro.length - 1]; | ||
let chunk = this.lastChunk; | ||
do { | ||
if (chunk.outro.length) return chunk.outro[chunk.outro.length - 1]; | ||
if (chunk.content.length) return chunk.content[chunk.content.length - 1]; | ||
if (chunk.intro.length) return chunk.intro[chunk.intro.length - 1]; | ||
} while ((chunk = chunk.previous)); | ||
if (this.intro.length) return this.intro[this.intro.length - 1]; | ||
return ''; | ||
} | ||
MagicString.prototype.lastLine = function lastLine () { | ||
var lineIndex = this.outro.lastIndexOf(n); | ||
if (lineIndex !== -1) { return this.outro.substr(lineIndex + 1); } | ||
var lineStr = this.outro; | ||
var chunk = this.lastChunk; | ||
do { | ||
if (chunk.outro.length > 0) { | ||
lineIndex = chunk.outro.lastIndexOf(n); | ||
if (lineIndex !== -1) { return chunk.outro.substr(lineIndex + 1) + lineStr; } | ||
lineStr = chunk.outro + lineStr; | ||
} | ||
lastLine() { | ||
let lineIndex = this.outro.lastIndexOf(n); | ||
if (lineIndex !== -1) return this.outro.substr(lineIndex + 1); | ||
let lineStr = this.outro; | ||
let chunk = this.lastChunk; | ||
do { | ||
if (chunk.outro.length > 0) { | ||
lineIndex = chunk.outro.lastIndexOf(n); | ||
if (lineIndex !== -1) return chunk.outro.substr(lineIndex + 1) + lineStr; | ||
lineStr = chunk.outro + lineStr; | ||
} | ||
if (chunk.content.length > 0) { | ||
lineIndex = chunk.content.lastIndexOf(n); | ||
if (lineIndex !== -1) { return chunk.content.substr(lineIndex + 1) + lineStr; } | ||
lineStr = chunk.content + lineStr; | ||
} | ||
if (chunk.content.length > 0) { | ||
lineIndex = chunk.content.lastIndexOf(n); | ||
if (lineIndex !== -1) return chunk.content.substr(lineIndex + 1) + lineStr; | ||
lineStr = chunk.content + lineStr; | ||
} | ||
if (chunk.intro.length > 0) { | ||
lineIndex = chunk.intro.lastIndexOf(n); | ||
if (lineIndex !== -1) { return chunk.intro.substr(lineIndex + 1) + lineStr; } | ||
lineStr = chunk.intro + lineStr; | ||
} | ||
} while ((chunk = chunk.previous)); | ||
lineIndex = this.intro.lastIndexOf(n); | ||
if (lineIndex !== -1) { return this.intro.substr(lineIndex + 1) + lineStr; } | ||
return this.intro + lineStr; | ||
}; | ||
if (chunk.intro.length > 0) { | ||
lineIndex = chunk.intro.lastIndexOf(n); | ||
if (lineIndex !== -1) return chunk.intro.substr(lineIndex + 1) + lineStr; | ||
lineStr = chunk.intro + lineStr; | ||
} | ||
} while ((chunk = chunk.previous)); | ||
lineIndex = this.intro.lastIndexOf(n); | ||
if (lineIndex !== -1) return this.intro.substr(lineIndex + 1) + lineStr; | ||
return this.intro + lineStr; | ||
} | ||
MagicString.prototype.slice = function slice (start, end) { | ||
if ( start === void 0 ) start = 0; | ||
if ( end === void 0 ) end = this.original.length; | ||
slice(start = 0, end = this.original.length) { | ||
while (start < 0) start += this.original.length; | ||
while (end < 0) end += this.original.length; | ||
while (start < 0) { start += this.original.length; } | ||
while (end < 0) { end += this.original.length; } | ||
let result = ''; | ||
var result = ''; | ||
// find start chunk | ||
let chunk = this.firstChunk; | ||
while (chunk && (chunk.start > start || chunk.end <= start)) { | ||
// found end chunk before start | ||
if (chunk.start < end && chunk.end >= end) { | ||
return result; | ||
} | ||
// find start chunk | ||
var chunk = this.firstChunk; | ||
while (chunk && (chunk.start > start || chunk.end <= start)) { | ||
// found end chunk before start | ||
if (chunk.start < end && chunk.end >= end) { | ||
return result; | ||
chunk = chunk.next; | ||
} | ||
chunk = chunk.next; | ||
} | ||
if (chunk && chunk.edited && chunk.start !== start) | ||
throw new Error(`Cannot use replaced character ${start} as slice start anchor.`); | ||
if (chunk && chunk.edited && chunk.start !== start) | ||
{ throw new Error(("Cannot use replaced character " + start + " as slice start anchor.")); } | ||
const startChunk = chunk; | ||
while (chunk) { | ||
if (chunk.intro && (startChunk !== chunk || chunk.start === start)) { | ||
result += chunk.intro; | ||
} | ||
var startChunk = chunk; | ||
while (chunk) { | ||
if (chunk.intro && (startChunk !== chunk || chunk.start === start)) { | ||
result += chunk.intro; | ||
} | ||
const containsEnd = chunk.start < end && chunk.end >= end; | ||
if (containsEnd && chunk.edited && chunk.end !== end) | ||
throw new Error(`Cannot use replaced character ${end} as slice end anchor.`); | ||
var containsEnd = chunk.start < end && chunk.end >= end; | ||
if (containsEnd && chunk.edited && chunk.end !== end) | ||
{ throw new Error(("Cannot use replaced character " + end + " as slice end anchor.")); } | ||
const sliceStart = startChunk === chunk ? start - chunk.start : 0; | ||
const sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length; | ||
var sliceStart = startChunk === chunk ? start - chunk.start : 0; | ||
var sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length; | ||
result += chunk.content.slice(sliceStart, sliceEnd); | ||
result += chunk.content.slice(sliceStart, sliceEnd); | ||
if (chunk.outro && (!containsEnd || chunk.end === end)) { | ||
result += chunk.outro; | ||
} | ||
if (chunk.outro && (!containsEnd || chunk.end === end)) { | ||
result += chunk.outro; | ||
} | ||
if (containsEnd) { | ||
break; | ||
} | ||
if (containsEnd) { | ||
break; | ||
chunk = chunk.next; | ||
} | ||
chunk = chunk.next; | ||
return result; | ||
} | ||
return result; | ||
}; | ||
// TODO deprecate this? not really very useful | ||
snip(start, end) { | ||
const clone = this.clone(); | ||
clone.remove(0, start); | ||
clone.remove(end, clone.original.length); | ||
// TODO deprecate this? not really very useful | ||
MagicString.prototype.snip = function snip (start, end) { | ||
var clone = this.clone(); | ||
clone.remove(0, start); | ||
clone.remove(end, clone.original.length); | ||
return clone; | ||
} | ||
return clone; | ||
}; | ||
_split(index) { | ||
if (this.byStart[index] || this.byEnd[index]) return; | ||
MagicString.prototype._split = function _split (index) { | ||
if (this.byStart[index] || this.byEnd[index]) { return; } | ||
let chunk = this.lastSearchedChunk; | ||
const searchForward = index > chunk.end; | ||
var chunk = this.lastSearchedChunk; | ||
var searchForward = index > chunk.end; | ||
while (chunk) { | ||
if (chunk.contains(index)) return this._splitChunk(chunk, index); | ||
while (chunk) { | ||
if (chunk.contains(index)) { return this._splitChunk(chunk, index); } | ||
chunk = searchForward ? this.byStart[chunk.end] : this.byEnd[chunk.start]; | ||
chunk = searchForward ? this.byStart[chunk.end] : this.byEnd[chunk.start]; | ||
} | ||
} | ||
}; | ||
MagicString.prototype._splitChunk = function _splitChunk (chunk, index) { | ||
if (chunk.edited && chunk.content.length) { | ||
// zero-length edited chunks are a special case (overlapping replacements) | ||
var loc = getLocator(this.original)(index); | ||
throw new Error( | ||
("Cannot split a chunk that has already been edited (" + (loc.line) + ":" + (loc.column) + " – \"" + (chunk.original) + "\")") | ||
); | ||
} | ||
_splitChunk(chunk, index) { | ||
if (chunk.edited && chunk.content.length) { | ||
// zero-length edited chunks are a special case (overlapping replacements) | ||
const loc = getLocator(this.original)(index); | ||
throw new Error( | ||
`Cannot split a chunk that has already been edited (${loc.line}:${loc.column} – "${chunk.original}")` | ||
); | ||
} | ||
var newChunk = chunk.split(index); | ||
const newChunk = chunk.split(index); | ||
this.byEnd[index] = chunk; | ||
this.byStart[index] = newChunk; | ||
this.byEnd[newChunk.end] = newChunk; | ||
this.byEnd[index] = chunk; | ||
this.byStart[index] = newChunk; | ||
this.byEnd[newChunk.end] = newChunk; | ||
if (chunk === this.lastChunk) { this.lastChunk = newChunk; } | ||
if (chunk === this.lastChunk) this.lastChunk = newChunk; | ||
this.lastSearchedChunk = chunk; | ||
return true; | ||
}; | ||
this.lastSearchedChunk = chunk; | ||
return true; | ||
} | ||
MagicString.prototype.toString = function toString () { | ||
var str = this.intro; | ||
toString() { | ||
let str = this.intro; | ||
var chunk = this.firstChunk; | ||
while (chunk) { | ||
str += chunk.toString(); | ||
chunk = chunk.next; | ||
let chunk = this.firstChunk; | ||
while (chunk) { | ||
str += chunk.toString(); | ||
chunk = chunk.next; | ||
} | ||
return str + this.outro; | ||
} | ||
return str + this.outro; | ||
}; | ||
isEmpty() { | ||
let chunk = this.firstChunk; | ||
do { | ||
if ( | ||
(chunk.intro.length && chunk.intro.trim()) || | ||
(chunk.content.length && chunk.content.trim()) || | ||
(chunk.outro.length && chunk.outro.trim()) | ||
) | ||
return false; | ||
} while ((chunk = chunk.next)); | ||
return true; | ||
} | ||
MagicString.prototype.isEmpty = function isEmpty () { | ||
var chunk = this.firstChunk; | ||
do { | ||
if ( | ||
(chunk.intro.length && chunk.intro.trim()) || | ||
(chunk.content.length && chunk.content.trim()) || | ||
(chunk.outro.length && chunk.outro.trim()) | ||
) | ||
{ return false; } | ||
} while ((chunk = chunk.next)); | ||
return true; | ||
}; | ||
length() { | ||
let chunk = this.firstChunk; | ||
let length = 0; | ||
do { | ||
length += chunk.intro.length + chunk.content.length + chunk.outro.length; | ||
} while ((chunk = chunk.next)); | ||
return length; | ||
} | ||
MagicString.prototype.length = function length () { | ||
var chunk = this.firstChunk; | ||
var length = 0; | ||
do { | ||
length += chunk.intro.length + chunk.content.length + chunk.outro.length; | ||
} while ((chunk = chunk.next)); | ||
return length; | ||
}; | ||
trimLines() { | ||
return this.trim('[\\r\\n]'); | ||
} | ||
MagicString.prototype.trimLines = function trimLines () { | ||
return this.trim('[\\r\\n]'); | ||
}; | ||
trim(charType) { | ||
return this.trimStart(charType).trimEnd(charType); | ||
} | ||
MagicString.prototype.trim = function trim (charType) { | ||
return this.trimStart(charType).trimEnd(charType); | ||
}; | ||
trimEndAborted(charType) { | ||
const rx = new RegExp((charType || '\\s') + '+$'); | ||
MagicString.prototype.trimEndAborted = function trimEndAborted (charType) { | ||
var rx = new RegExp((charType || '\\s') + '+$'); | ||
this.outro = this.outro.replace(rx, ''); | ||
if (this.outro.length) return true; | ||
this.outro = this.outro.replace(rx, ''); | ||
if (this.outro.length) { return true; } | ||
let chunk = this.lastChunk; | ||
var chunk = this.lastChunk; | ||
do { | ||
const end = chunk.end; | ||
const aborted = chunk.trimEnd(rx); | ||
do { | ||
var end = chunk.end; | ||
var aborted = chunk.trimEnd(rx); | ||
// if chunk was trimmed, we have a new lastChunk | ||
if (chunk.end !== end) { | ||
if (this.lastChunk === chunk) { | ||
this.lastChunk = chunk.next; | ||
} | ||
// if chunk was trimmed, we have a new lastChunk | ||
if (chunk.end !== end) { | ||
if (this.lastChunk === chunk) { | ||
this.lastChunk = chunk.next; | ||
this.byEnd[chunk.end] = chunk; | ||
this.byStart[chunk.next.start] = chunk.next; | ||
this.byEnd[chunk.next.end] = chunk.next; | ||
} | ||
this.byEnd[chunk.end] = chunk; | ||
this.byStart[chunk.next.start] = chunk.next; | ||
this.byEnd[chunk.next.end] = chunk.next; | ||
} | ||
if (aborted) return true; | ||
chunk = chunk.previous; | ||
} while (chunk); | ||
if (aborted) { return true; } | ||
chunk = chunk.previous; | ||
} while (chunk); | ||
return false; | ||
} | ||
return false; | ||
}; | ||
trimEnd(charType) { | ||
this.trimEndAborted(charType); | ||
return this; | ||
} | ||
trimStartAborted(charType) { | ||
const rx = new RegExp('^' + (charType || '\\s') + '+'); | ||
MagicString.prototype.trimEnd = function trimEnd (charType) { | ||
this.trimEndAborted(charType); | ||
return this; | ||
}; | ||
MagicString.prototype.trimStartAborted = function trimStartAborted (charType) { | ||
var rx = new RegExp('^' + (charType || '\\s') + '+'); | ||
this.intro = this.intro.replace(rx, ''); | ||
if (this.intro.length) return true; | ||
this.intro = this.intro.replace(rx, ''); | ||
if (this.intro.length) { return true; } | ||
let chunk = this.firstChunk; | ||
var chunk = this.firstChunk; | ||
do { | ||
const end = chunk.end; | ||
const aborted = chunk.trimStart(rx); | ||
do { | ||
var end = chunk.end; | ||
var aborted = chunk.trimStart(rx); | ||
if (chunk.end !== end) { | ||
// special case... | ||
if (chunk === this.lastChunk) this.lastChunk = chunk.next; | ||
if (chunk.end !== end) { | ||
// special case... | ||
if (chunk === this.lastChunk) { this.lastChunk = chunk.next; } | ||
this.byEnd[chunk.end] = chunk; | ||
this.byStart[chunk.next.start] = chunk.next; | ||
this.byEnd[chunk.next.end] = chunk.next; | ||
} | ||
this.byEnd[chunk.end] = chunk; | ||
this.byStart[chunk.next.start] = chunk.next; | ||
this.byEnd[chunk.next.end] = chunk.next; | ||
} | ||
if (aborted) return true; | ||
chunk = chunk.next; | ||
} while (chunk); | ||
if (aborted) { return true; } | ||
chunk = chunk.next; | ||
} while (chunk); | ||
return false; | ||
} | ||
return false; | ||
}; | ||
trimStart(charType) { | ||
this.trimStartAborted(charType); | ||
return this; | ||
} | ||
MagicString.prototype.trimStart = function trimStart (charType) { | ||
this.trimStartAborted(charType); | ||
return this; | ||
}; | ||
var hasOwnProp = Object.prototype.hasOwnProperty; | ||
var Bundle = function Bundle(options) { | ||
if ( options === void 0 ) options = {}; | ||
this.intro = options.intro || ''; | ||
this.separator = options.separator !== undefined ? options.separator : '\n'; | ||
this.sources = []; | ||
this.uniqueSources = []; | ||
this.uniqueSourceIndexByFilename = {}; | ||
}; | ||
Bundle.prototype.addSource = function addSource (source) { | ||
if (source instanceof MagicString) { | ||
return this.addSource({ | ||
content: source, | ||
filename: source.filename, | ||
separator: this.separator, | ||
}); | ||
hasChanged() { | ||
return this.original !== this.toString(); | ||
} | ||
if (!isObject(source) || !source.content) { | ||
throw new Error( | ||
'bundle.addSource() takes an object with a `content` property, which should be an instance of MagicString, and an optional `filename`' | ||
); | ||
replace(searchValue, replacement) { | ||
function getReplacement(match) { | ||
if (typeof replacement === 'string') { | ||
return replacement.replace(/\$(\$|&|\d+)/g, (_, i) => { | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_a_parameter | ||
if (i === '$') | ||
return '$'; | ||
if (i === '&') | ||
return match[0]; | ||
const num = +i; | ||
if (num < match.length) | ||
return match[+i]; | ||
return `$${i}`; | ||
}); | ||
} | ||
else { | ||
return replacement(...match); | ||
} | ||
} | ||
function matchAll(re, str) { | ||
let match; | ||
const matches = []; | ||
while (match = re.exec(str)) { | ||
matches.push(match); | ||
} | ||
return matches; | ||
} | ||
if (typeof searchValue !== 'string' && searchValue.global) { | ||
const matches = matchAll(searchValue, this.original); | ||
matches.forEach((match) => { | ||
if (match.index != null) | ||
this.overwrite(match.index, match.index + match[0].length, getReplacement(match)); | ||
}); | ||
} | ||
else { | ||
const match = this.original.match(searchValue); | ||
if (match && match.index != null) | ||
this.overwrite(match.index, match.index + match[0].length, getReplacement(match)); | ||
} | ||
return this; | ||
} | ||
} | ||
['filename', 'indentExclusionRanges', 'separator'].forEach(function (option) { | ||
if (!hasOwnProp.call(source, option)) { source[option] = source.content[option]; } | ||
}); | ||
const hasOwnProp = Object.prototype.hasOwnProperty; | ||
if (source.separator === undefined) { | ||
// TODO there's a bunch of this sort of thing, needs cleaning up | ||
source.separator = this.separator; | ||
class Bundle { | ||
constructor(options = {}) { | ||
this.intro = options.intro || ''; | ||
this.separator = options.separator !== undefined ? options.separator : '\n'; | ||
this.sources = []; | ||
this.uniqueSources = []; | ||
this.uniqueSourceIndexByFilename = {}; | ||
} | ||
if (source.filename) { | ||
if (!hasOwnProp.call(this.uniqueSourceIndexByFilename, source.filename)) { | ||
this.uniqueSourceIndexByFilename[source.filename] = this.uniqueSources.length; | ||
this.uniqueSources.push({ filename: source.filename, content: source.content.original }); | ||
} else { | ||
var uniqueSource = this.uniqueSources[this.uniqueSourceIndexByFilename[source.filename]]; | ||
if (source.content.original !== uniqueSource.content) { | ||
throw new Error(("Illegal source: same filename (" + (source.filename) + "), different contents")); | ||
} | ||
addSource(source) { | ||
if (source instanceof MagicString) { | ||
return this.addSource({ | ||
content: source, | ||
filename: source.filename, | ||
separator: this.separator, | ||
}); | ||
} | ||
} | ||
this.sources.push(source); | ||
return this; | ||
}; | ||
if (!isObject(source) || !source.content) { | ||
throw new Error( | ||
'bundle.addSource() takes an object with a `content` property, which should be an instance of MagicString, and an optional `filename`' | ||
); | ||
} | ||
Bundle.prototype.append = function append (str, options) { | ||
this.addSource({ | ||
content: new MagicString(str), | ||
separator: (options && options.separator) || '', | ||
}); | ||
['filename', 'indentExclusionRanges', 'separator'].forEach((option) => { | ||
if (!hasOwnProp.call(source, option)) source[option] = source.content[option]; | ||
}); | ||
return this; | ||
}; | ||
if (source.separator === undefined) { | ||
// TODO there's a bunch of this sort of thing, needs cleaning up | ||
source.separator = this.separator; | ||
} | ||
Bundle.prototype.clone = function clone () { | ||
var bundle = new Bundle({ | ||
intro: this.intro, | ||
separator: this.separator, | ||
}); | ||
if (source.filename) { | ||
if (!hasOwnProp.call(this.uniqueSourceIndexByFilename, source.filename)) { | ||
this.uniqueSourceIndexByFilename[source.filename] = this.uniqueSources.length; | ||
this.uniqueSources.push({ filename: source.filename, content: source.content.original }); | ||
} else { | ||
const uniqueSource = this.uniqueSources[this.uniqueSourceIndexByFilename[source.filename]]; | ||
if (source.content.original !== uniqueSource.content) { | ||
throw new Error(`Illegal source: same filename (${source.filename}), different contents`); | ||
} | ||
} | ||
} | ||
this.sources.forEach(function (source) { | ||
bundle.addSource({ | ||
filename: source.filename, | ||
content: source.content.clone(), | ||
separator: source.separator, | ||
this.sources.push(source); | ||
return this; | ||
} | ||
append(str, options) { | ||
this.addSource({ | ||
content: new MagicString(str), | ||
separator: (options && options.separator) || '', | ||
}); | ||
}); | ||
return bundle; | ||
}; | ||
return this; | ||
} | ||
Bundle.prototype.generateDecodedMap = function generateDecodedMap (options) { | ||
var this$1$1 = this; | ||
if ( options === void 0 ) options = {}; | ||
clone() { | ||
const bundle = new Bundle({ | ||
intro: this.intro, | ||
separator: this.separator, | ||
}); | ||
var names = []; | ||
this.sources.forEach(function (source) { | ||
Object.keys(source.content.storedNames).forEach(function (name) { | ||
if (!~names.indexOf(name)) { names.push(name); } | ||
this.sources.forEach((source) => { | ||
bundle.addSource({ | ||
filename: source.filename, | ||
content: source.content.clone(), | ||
separator: source.separator, | ||
}); | ||
}); | ||
}); | ||
var mappings = new Mappings(options.hires); | ||
if (this.intro) { | ||
mappings.advance(this.intro); | ||
return bundle; | ||
} | ||
this.sources.forEach(function (source, i) { | ||
if (i > 0) { | ||
mappings.advance(this$1$1.separator); | ||
} | ||
generateDecodedMap(options = {}) { | ||
const names = []; | ||
this.sources.forEach((source) => { | ||
Object.keys(source.content.storedNames).forEach((name) => { | ||
if (!~names.indexOf(name)) names.push(name); | ||
}); | ||
}); | ||
var sourceIndex = source.filename ? this$1$1.uniqueSourceIndexByFilename[source.filename] : -1; | ||
var magicString = source.content; | ||
var locate = getLocator(magicString.original); | ||
const mappings = new Mappings(options.hires); | ||
if (magicString.intro) { | ||
mappings.advance(magicString.intro); | ||
if (this.intro) { | ||
mappings.advance(this.intro); | ||
} | ||
magicString.firstChunk.eachNext(function (chunk) { | ||
var loc = locate(chunk.start); | ||
this.sources.forEach((source, i) => { | ||
if (i > 0) { | ||
mappings.advance(this.separator); | ||
} | ||
if (chunk.intro.length) { mappings.advance(chunk.intro); } | ||
const sourceIndex = source.filename ? this.uniqueSourceIndexByFilename[source.filename] : -1; | ||
const magicString = source.content; | ||
const locate = getLocator(magicString.original); | ||
if (source.filename) { | ||
if (chunk.edited) { | ||
mappings.addEdit( | ||
sourceIndex, | ||
chunk.content, | ||
loc, | ||
chunk.storeName ? names.indexOf(chunk.original) : -1 | ||
); | ||
if (magicString.intro) { | ||
mappings.advance(magicString.intro); | ||
} | ||
magicString.firstChunk.eachNext((chunk) => { | ||
const loc = locate(chunk.start); | ||
if (chunk.intro.length) mappings.advance(chunk.intro); | ||
if (source.filename) { | ||
if (chunk.edited) { | ||
mappings.addEdit( | ||
sourceIndex, | ||
chunk.content, | ||
loc, | ||
chunk.storeName ? names.indexOf(chunk.original) : -1 | ||
); | ||
} else { | ||
mappings.addUneditedChunk( | ||
sourceIndex, | ||
chunk, | ||
magicString.original, | ||
loc, | ||
magicString.sourcemapLocations | ||
); | ||
} | ||
} else { | ||
mappings.addUneditedChunk( | ||
sourceIndex, | ||
chunk, | ||
magicString.original, | ||
loc, | ||
magicString.sourcemapLocations | ||
); | ||
mappings.advance(chunk.content); | ||
} | ||
} else { | ||
mappings.advance(chunk.content); | ||
if (chunk.outro.length) mappings.advance(chunk.outro); | ||
}); | ||
if (magicString.outro) { | ||
mappings.advance(magicString.outro); | ||
} | ||
if (chunk.outro.length) { mappings.advance(chunk.outro); } | ||
}); | ||
if (magicString.outro) { | ||
mappings.advance(magicString.outro); | ||
} | ||
}); | ||
return { | ||
file: options.file ? options.file.split(/[/\\]/).pop() : null, | ||
sources: this.uniqueSources.map((source) => { | ||
return options.file ? getRelativePath(options.file, source.filename) : source.filename; | ||
}), | ||
sourcesContent: this.uniqueSources.map((source) => { | ||
return options.includeContent ? source.content : null; | ||
}), | ||
names, | ||
mappings: mappings.raw, | ||
}; | ||
} | ||
return { | ||
file: options.file ? options.file.split(/[/\\]/).pop() : null, | ||
sources: this.uniqueSources.map(function (source) { | ||
return options.file ? getRelativePath(options.file, source.filename) : source.filename; | ||
}), | ||
sourcesContent: this.uniqueSources.map(function (source) { | ||
return options.includeContent ? source.content : null; | ||
}), | ||
names: names, | ||
mappings: mappings.raw, | ||
}; | ||
}; | ||
generateMap(options) { | ||
return new SourceMap(this.generateDecodedMap(options)); | ||
} | ||
Bundle.prototype.generateMap = function generateMap (options) { | ||
return new SourceMap(this.generateDecodedMap(options)); | ||
}; | ||
getIndentString() { | ||
const indentStringCounts = {}; | ||
Bundle.prototype.getIndentString = function getIndentString () { | ||
var indentStringCounts = {}; | ||
this.sources.forEach((source) => { | ||
const indentStr = source.content.indentStr; | ||
this.sources.forEach(function (source) { | ||
var indentStr = source.content.indentStr; | ||
if (indentStr === null) return; | ||
if (indentStr === null) { return; } | ||
if (!indentStringCounts[indentStr]) indentStringCounts[indentStr] = 0; | ||
indentStringCounts[indentStr] += 1; | ||
}); | ||
if (!indentStringCounts[indentStr]) { indentStringCounts[indentStr] = 0; } | ||
indentStringCounts[indentStr] += 1; | ||
}); | ||
return ( | ||
Object.keys(indentStringCounts).sort((a, b) => { | ||
return indentStringCounts[a] - indentStringCounts[b]; | ||
})[0] || '\t' | ||
); | ||
} | ||
return ( | ||
Object.keys(indentStringCounts).sort(function (a, b) { | ||
return indentStringCounts[a] - indentStringCounts[b]; | ||
})[0] || '\t' | ||
); | ||
}; | ||
indent(indentStr) { | ||
if (!arguments.length) { | ||
indentStr = this.getIndentString(); | ||
} | ||
Bundle.prototype.indent = function indent (indentStr) { | ||
var this$1$1 = this; | ||
if (indentStr === '') return this; // noop | ||
if (!arguments.length) { | ||
indentStr = this.getIndentString(); | ||
} | ||
let trailingNewline = !this.intro || this.intro.slice(-1) === '\n'; | ||
if (indentStr === '') { return this; } // noop | ||
this.sources.forEach((source, i) => { | ||
const separator = source.separator !== undefined ? source.separator : this.separator; | ||
const indentStart = trailingNewline || (i > 0 && /\r?\n$/.test(separator)); | ||
var trailingNewline = !this.intro || this.intro.slice(-1) === '\n'; | ||
source.content.indent(indentStr, { | ||
exclude: source.indentExclusionRanges, | ||
indentStart, //: trailingNewline || /\r?\n$/.test( separator ) //true///\r?\n/.test( separator ) | ||
}); | ||
this.sources.forEach(function (source, i) { | ||
var separator = source.separator !== undefined ? source.separator : this$1$1.separator; | ||
var indentStart = trailingNewline || (i > 0 && /\r?\n$/.test(separator)); | ||
source.content.indent(indentStr, { | ||
exclude: source.indentExclusionRanges, | ||
indentStart: indentStart, //: trailingNewline || /\r?\n$/.test( separator ) //true///\r?\n/.test( separator ) | ||
trailingNewline = source.content.lastChar() === '\n'; | ||
}); | ||
trailingNewline = source.content.lastChar() === '\n'; | ||
}); | ||
if (this.intro) { | ||
this.intro = | ||
indentStr + | ||
this.intro.replace(/^[^\n]/gm, (match, index) => { | ||
return index > 0 ? indentStr + match : match; | ||
}); | ||
} | ||
if (this.intro) { | ||
this.intro = | ||
indentStr + | ||
this.intro.replace(/^[^\n]/gm, function (match, index) { | ||
return index > 0 ? indentStr + match : match; | ||
}); | ||
return this; | ||
} | ||
return this; | ||
}; | ||
prepend(str) { | ||
this.intro = str + this.intro; | ||
return this; | ||
} | ||
Bundle.prototype.prepend = function prepend (str) { | ||
this.intro = str + this.intro; | ||
return this; | ||
}; | ||
toString() { | ||
const body = this.sources | ||
.map((source, i) => { | ||
const separator = source.separator !== undefined ? source.separator : this.separator; | ||
const str = (i > 0 ? separator : '') + source.content.toString(); | ||
Bundle.prototype.toString = function toString () { | ||
var this$1$1 = this; | ||
return str; | ||
}) | ||
.join(''); | ||
var body = this.sources | ||
.map(function (source, i) { | ||
var separator = source.separator !== undefined ? source.separator : this$1$1.separator; | ||
var str = (i > 0 ? separator : '') + source.content.toString(); | ||
return this.intro + body; | ||
} | ||
return str; | ||
}) | ||
.join(''); | ||
isEmpty() { | ||
if (this.intro.length && this.intro.trim()) return false; | ||
if (this.sources.some((source) => !source.content.isEmpty())) return false; | ||
return true; | ||
} | ||
return this.intro + body; | ||
}; | ||
length() { | ||
return this.sources.reduce( | ||
(length, source) => length + source.content.length(), | ||
this.intro.length | ||
); | ||
} | ||
Bundle.prototype.isEmpty = function isEmpty () { | ||
if (this.intro.length && this.intro.trim()) { return false; } | ||
if (this.sources.some(function (source) { return !source.content.isEmpty(); })) { return false; } | ||
return true; | ||
}; | ||
trimLines() { | ||
return this.trim('[\\r\\n]'); | ||
} | ||
Bundle.prototype.length = function length () { | ||
return this.sources.reduce( | ||
function (length, source) { return length + source.content.length(); }, | ||
this.intro.length | ||
); | ||
}; | ||
trim(charType) { | ||
return this.trimStart(charType).trimEnd(charType); | ||
} | ||
Bundle.prototype.trimLines = function trimLines () { | ||
return this.trim('[\\r\\n]'); | ||
}; | ||
trimStart(charType) { | ||
const rx = new RegExp('^' + (charType || '\\s') + '+'); | ||
this.intro = this.intro.replace(rx, ''); | ||
Bundle.prototype.trim = function trim (charType) { | ||
return this.trimStart(charType).trimEnd(charType); | ||
}; | ||
if (!this.intro) { | ||
let source; | ||
let i = 0; | ||
Bundle.prototype.trimStart = function trimStart (charType) { | ||
var rx = new RegExp('^' + (charType || '\\s') + '+'); | ||
this.intro = this.intro.replace(rx, ''); | ||
do { | ||
source = this.sources[i++]; | ||
if (!source) { | ||
break; | ||
} | ||
} while (!source.content.trimStartAborted(charType)); | ||
} | ||
if (!this.intro) { | ||
var source; | ||
var i = 0; | ||
return this; | ||
} | ||
trimEnd(charType) { | ||
const rx = new RegExp((charType || '\\s') + '+$'); | ||
let source; | ||
let i = this.sources.length - 1; | ||
do { | ||
source = this.sources[i++]; | ||
source = this.sources[i--]; | ||
if (!source) { | ||
this.intro = this.intro.replace(rx, ''); | ||
break; | ||
} | ||
} while (!source.content.trimStartAborted(charType)); | ||
} while (!source.content.trimEndAborted(charType)); | ||
return this; | ||
} | ||
} | ||
return this; | ||
}; | ||
Bundle.prototype.trimEnd = function trimEnd (charType) { | ||
var rx = new RegExp((charType || '\\s') + '+$'); | ||
var source; | ||
var i = this.sources.length - 1; | ||
do { | ||
source = this.sources[i--]; | ||
if (!source) { | ||
this.intro = this.intro.replace(rx, ''); | ||
break; | ||
} | ||
} while (!source.content.trimEndAborted(charType)); | ||
return this; | ||
}; | ||
MagicString.Bundle = Bundle; | ||
@@ -1375,0 +1417,0 @@ MagicString.SourceMap = SourceMap; |
@@ -207,2 +207,6 @@ export interface BundleOptions { | ||
trimLines(): MagicString; | ||
/** | ||
* String replacement with RegExp or string. | ||
*/ | ||
replace(regex: RegExp | string, replacement: string | ((substring: string, ...args: any[]) => string)): MagicString; | ||
@@ -217,2 +221,7 @@ lastChar(): string; | ||
/** | ||
* Indicates if the string has been changed. | ||
*/ | ||
hasChanged(): boolean; | ||
original: string; | ||
@@ -219,0 +228,0 @@ /** |
{ | ||
"name": "magic-string", | ||
"version": "0.25.9", | ||
"version": "0.26.0", | ||
"description": "Modify strings, generate sourcemaps", | ||
@@ -16,4 +16,11 @@ "keywords": [ | ||
"main": "dist/magic-string.cjs.js", | ||
"module": "dist/magic-string.es.js", | ||
"jsnext:main": "dist/magic-string.es.js", | ||
"module": "dist/magic-string.es.mjs", | ||
"jsnext:main": "dist/magic-string.es.mjs", | ||
"exports": { | ||
"./package.json": "./package.json", | ||
".": { | ||
"import": "./dist/magic-string.es.mjs", | ||
"require": "./dist/magic-string.cjs.js" | ||
} | ||
}, | ||
"typings": "index.d.ts", | ||
@@ -41,3 +48,2 @@ "files": [ | ||
"devDependencies": { | ||
"@rollup/plugin-buble": "^0.21.3", | ||
"@rollup/plugin-node-resolve": "^13.1.3", | ||
@@ -47,3 +53,3 @@ "@rollup/plugin-replace": "^4.0.0", | ||
"conventional-changelog-cli": "^2.2.2", | ||
"eslint": "^7.32.0", | ||
"eslint": "^8.10.0", | ||
"mocha": "^9.2.1", | ||
@@ -54,3 +60,6 @@ "prettier": "^2.5.1", | ||
"source-map-support": "^0.5.21" | ||
}, | ||
"engines": { | ||
"node": ">=12" | ||
} | ||
} |
@@ -120,2 +120,6 @@ # magic-string | ||
### s.hasChanged() | ||
Indicates if the string has been changed. | ||
### s.indent( prefix[, options] ) | ||
@@ -165,2 +169,20 @@ | ||
### s.replace( regexp, substitution ) | ||
String replacement with RegExp or string, a replacer function is also supported. Returns `this`. | ||
```ts | ||
import MagicString from 'magic-string' | ||
const s = new MagicString(source) | ||
s.replace(foo, 'bar') | ||
s.replace(/foo/g, 'bar') | ||
s.replace(/(\w)(\d+)/g, (_, $1, $2) => $1.toUpperCase() + $2) | ||
``` | ||
The differences from [`String.replace`]((https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace)): | ||
- It will always match against the **original string** | ||
- It mutates the magic string state (use `.clone()` to be immutable) | ||
### s.remove( start, end ) | ||
@@ -167,0 +189,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
381736
10
3478
275
1