Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

magic-string

Package Overview
Dependencies
Maintainers
1
Versions
116
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

magic-string - npm Package Compare versions

Comparing version 0.5.3 to 0.6.0

src/index.js

4

CHANGELOG.md
# changelog
## 0.6.0
* Use rollup for bundling, instead of esperanto
## 0.5.3

@@ -4,0 +8,0 @@

772

dist/magic-string.deps.js
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
global.MagicString = factory()
global.MagicString = factory();
}(this, function () { 'use strict';
var _btoa;
function getRelativePath(from, to) {
var fromParts = from.split(/[\/\\]/);
var toParts = to.split(/[\/\\]/);
fromParts.pop(); // get dirname
while (fromParts[0] === toParts[0]) {
fromParts.shift();
toParts.shift();
}
if (fromParts.length) {
var i = fromParts.length;
while (i--) fromParts[i] = '..';
}
return fromParts.concat(toParts).join('/');
}var _btoa;
if (typeof window !== 'undefined' && typeof window.btoa === 'function') {

@@ -19,9 +36,7 @@ _btoa = window.btoa;

var btoa = _btoa;
var btoa = _btoa;function __classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
function SourceMap___classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
var SourceMap = (function () {
function SourceMap(properties) {
SourceMap___classCallCheck(this, SourceMap);
__classCallCheck(this, SourceMap);

@@ -48,305 +63,51 @@ this.version = 3;

function getRelativePath(from, to) {
var fromParts = from.split(/[\/\\]/);
var toParts = to.split(/[\/\\]/);
function getSemis(str) {
return new Array(str.split('\n').length).join(';');
}
fromParts.pop(); // get dirname
function adjust(mappings, start, end, d) {
var i = end;
while (fromParts[0] === toParts[0]) {
fromParts.shift();
toParts.shift();
}
if (!d) return; // replacement is same length as replaced string
if (fromParts.length) {
var i = fromParts.length;
while (i--) fromParts[i] = '..';
while (i-- > start) {
if (~mappings[i]) {
mappings[i] += d;
}
}
return fromParts.concat(toParts).join('/');
}
var hasOwnProp = Object.prototype.hasOwnProperty;
var warned = false;
function Bundle___classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
function blank(mappings, start, i) {
while (i-- > start) {
mappings[i] = -1;
}
}
var Bundle = (function () {
function Bundle() {
var options = arguments[0] === undefined ? {} : arguments[0];
function reverse(mappings, i) {
var result, location;
Bundle___classCallCheck(this, Bundle);
result = new Uint32Array(i);
this.intro = options.intro || '';
this.outro = options.outro || '';
this.separator = options.separator !== undefined ? options.separator : '\n';
this.sources = [];
this.uniqueSources = [];
this.uniqueSourceIndexByFilename = {};
while (i--) {
result[i] = -1;
}
Bundle.prototype.addSource = function addSource(source) {
if (source instanceof MagicString) {
return this.addSource({
content: source,
filename: source.filename,
separator: this.separator
});
}
i = mappings.length;
while (i--) {
location = mappings[i];
if (typeof source !== 'object' || !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`');
if (~location) {
result[location] = i;
}
['filename', 'indentExclusionRanges', 'separator'].forEach(function (option) {
if (!hasOwnProp.call(source, option)) source[option] = source.content[option];
});
if (source.separator === undefined) {
// TODO there's a bunch of this sort of thing, needs cleaning up
source.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 {
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');
}
}
}
this.sources.push(source);
return this;
};
Bundle.prototype.append = function append(str, options) {
this.addSource({
content: new MagicString(str),
separator: options && options.separator || ''
});
return this;
};
Bundle.prototype.clone = function clone() {
var bundle = new Bundle({
intro: this.intro,
outro: this.outro,
separator: this.separator
});
this.sources.forEach(function (source) {
bundle.addSource({
filename: source.filename,
content: source.content.clone(),
separator: source.separator
});
});
return bundle;
};
Bundle.prototype.generateMap = function generateMap(options) {
var _this = this;
var offsets = {};
var encoded = getSemis(this.intro) + this.sources.map(function (source, i) {
var prefix = i > 0 ? getSemis(source.separator) || ',' : '';
var mappings = undefined;
// we don't bother encoding sources without a filename
if (!source.filename) {
mappings = getSemis(source.content.toString());
} else {
var sourceIndex = _this.uniqueSourceIndexByFilename[source.filename];
mappings = source.content.getMappings(options.hires, sourceIndex, offsets);
}
return prefix + mappings;
}).join('') + getSemis(this.outro);
return new SourceMap({
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: [],
mappings: encoded
});
};
Bundle.prototype.getIndentString = function getIndentString() {
var indentStringCounts = {};
this.sources.forEach(function (source) {
var indentStr = source.content.indentStr;
if (indentStr === null) return;
if (!indentStringCounts[indentStr]) indentStringCounts[indentStr] = 0;
indentStringCounts[indentStr] += 1;
});
return Object.keys(indentStringCounts).sort(function (a, b) {
return indentStringCounts[a] - indentStringCounts[b];
})[0] || '\t';
};
Bundle.prototype.indent = function indent(indentStr) {
var _this2 = this;
if (!indentStr) {
indentStr = this.getIndentString();
}
var trailingNewline = !this.intro || this.intro.slice(0, -1) === '\n';
this.sources.forEach(function (source, i) {
var separator = source.separator !== undefined ? source.separator : _this2.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.str.slice(0, -1) === '\n';
});
this.intro = this.intro.replace(/^[^\n]/gm, function (match, index) {
return index > 0 ? indentStr + match : match;
});
this.outro = this.outro.replace(/^[^\n]/gm, indentStr + '$&');
return this;
};
Bundle.prototype.prepend = function prepend(str) {
this.intro = str + this.intro;
return this;
};
Bundle.prototype.toString = function toString() {
var _this3 = this;
var body = this.sources.map(function (source, i) {
var separator = source.separator !== undefined ? source.separator : _this3.separator;
var str = (i > 0 ? separator : '') + source.content.toString();
return str;
}).join('');
return this.intro + body + this.outro;
};
Bundle.prototype.trimLines = function trimLines() {
return this.trim('[\\r\\n]');
};
Bundle.prototype.trim = function trim(charType) {
return this.trimStart(charType).trimEnd(charType);
};
Bundle.prototype.trimStart = function trimStart(charType) {
var rx = new RegExp('^' + (charType || '\\s') + '+');
this.intro = this.intro.replace(rx, '');
if (!this.intro) {
var source = undefined; // TODO put inside loop if safe
var i = 0;
do {
source = this.sources[i];
if (!source) {
this.outro = this.outro.replace(rx, '');
break;
}
source.content.trimStart();
i += 1;
} while (source.content.str === '');
}
return this;
};
Bundle.prototype.trimEnd = function trimEnd(charType) {
var rx = new RegExp((charType || '\\s') + '+$');
this.outro = this.outro.replace(rx, '');
if (!this.outro) {
var source = undefined;
var i = this.sources.length - 1;
do {
source = this.sources[i];
if (!source) {
this.intro = this.intro.replace(rx, '');
break;
}
source.content.trimEnd(charType);
i -= 1;
} while (source.content.str === '');
}
return this;
};
return Bundle;
})();
function stringify(source) {
return source.content.toString();
}
function getSemis(str) {
return new Array(str.split('\n').length).join(';');
}
function guessIndent(code) {
var lines = code.split('\n');
var tabbed = lines.filter(function (line) {
return /^\t+/.test(line);
});
var spaced = lines.filter(function (line) {
return /^ {2,}/.test(line);
});
if (tabbed.length === 0 && spaced.length === 0) {
return null;
}
// More lines tabbed than spaced? Assume tabs, and
// default to tabs in the case of a tie (or nothing
// to go on)
if (tabbed.length >= spaced.length) {
return '\t';
}
// Otherwise, we need to guess the multiple
var min = spaced.reduce(function (previous, current) {
var numSpaces = /^ +/.exec(current)[0].length;
return Math.min(numSpaces, previous);
}, Infinity);
return new Array(min + 1).join(' ');
return result;
}
var charToInteger = {};
var integerToChar = {};
var charToInteger = {};
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='.split( '' ).forEach( function ( char, i ) {

@@ -357,37 +118,22 @@ charToInteger[ char ] = i;

function decode ( string ) {
var result = [],
len = string.length,
i,
hasContinuationBit,
shift = 0,
value = 0,
integer,
shouldNegate;
function encodeInteger ( num ) {
var result = '', clamped;
for ( i = 0; i < len; i += 1 ) {
integer = charToInteger[ string[i] ];
if ( num < 0 ) {
num = ( -num << 1 ) | 1;
} else {
num <<= 1;
}
if ( integer === undefined ) {
throw new Error( 'Invalid character (' + string[i] + ')' );
do {
clamped = num & 31;
num >>= 5;
if ( num > 0 ) {
clamped |= 32;
}
hasContinuationBit = integer & 32;
result += integerToChar[ clamped ];
} while ( num > 0 );
integer &= 31;
value += integer << shift;
if ( hasContinuationBit ) {
shift += 5;
} else {
shouldNegate = value & 1;
value >>= 1;
result.push( shouldNegate ? -value : value );
// reset
value = shift = 0;
}
}
return result;

@@ -397,11 +143,13 @@ }

function encode ( value ) {
var result, i;
var result;
if ( typeof value === 'number' ) {
result = encodeInteger( value );
} else if ( Array.isArray( value ) ) {
result = '';
value.forEach( function ( num ) {
result += encodeInteger( num );
});
} else {
result = '';
for ( i = 0; i < value.length; i += 1 ) {
result += encodeInteger( value[i] );
}
throw new Error( 'vlq.encode accepts an integer or an array of integers' );
}

@@ -412,27 +160,39 @@

function encodeInteger ( num ) {
var result = '', clamped;
function getLocation(locations, char) {
var i;
if ( num < 0 ) {
num = ( -num << 1 ) | 1;
} else {
num <<= 1;
i = locations.length;
while (i--) {
if (locations[i] <= char) {
return {
line: i,
column: char - locations[i]
};
}
}
do {
clamped = num & 31;
num >>= 5;
throw new Error('Character out of bounds');
}
if ( num > 0 ) {
clamped |= 32;
function invert(str, mappings) {
var inverted = new Uint32Array(str.length),
i;
// initialise everything to -1
i = str.length;
while (i--) {
inverted[i] = -1;
}
// then apply the actual mappings
i = mappings.length;
while (i--) {
if (~mappings[i]) {
inverted[mappings[i]] = i;
}
}
result += integerToChar[ clamped ];
} while ( num > 0 );
return result;
return inverted;
}
var utils_encode = encode;
function encodeMappings(original, str, mappings, hires, sourcemapLocations, sourceIndex, offsets) {

@@ -512,3 +272,3 @@ // store locations, for fast lookup

return utils_encode(arr);
return encode(arr);
}).join(',');

@@ -520,47 +280,44 @@ }).join(';');

function invert(str, mappings) {
var inverted = new Uint32Array(str.length),
i;
function guessIndent(code) {
var lines = code.split('\n');
// initialise everything to -1
i = str.length;
while (i--) {
inverted[i] = -1;
var tabbed = lines.filter(function (line) {
return /^\t+/.test(line);
});
var spaced = lines.filter(function (line) {
return /^ {2,}/.test(line);
});
if (tabbed.length === 0 && spaced.length === 0) {
return null;
}
// then apply the actual mappings
i = mappings.length;
while (i--) {
if (~mappings[i]) {
inverted[mappings[i]] = i;
}
// More lines tabbed than spaced? Assume tabs, and
// default to tabs in the case of a tie (or nothing
// to go on)
if (tabbed.length >= spaced.length) {
return '\t';
}
return inverted;
// Otherwise, we need to guess the multiple
var min = spaced.reduce(function (previous, current) {
var numSpaces = /^ +/.exec(current)[0].length;
return Math.min(numSpaces, previous);
}, Infinity);
return new Array(min + 1).join(' ');
}
function getLocation(locations, char) {
var i;
function initMappings(i) {
var mappings = new Uint32Array(i);
i = locations.length;
while (i--) {
if (locations[i] <= char) {
return {
line: i,
column: char - locations[i]
};
}
mappings[i] = i;
}
throw new Error('Character out of bounds');
return mappings;
}
// do nothing
function ___classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
// do nothing
function MagicString___classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
var warned = false;
var MagicString = (function () {

@@ -570,3 +327,3 @@ function MagicString(string) {

MagicString___classCallCheck(this, MagicString);
___classCallCheck(this, MagicString);

@@ -979,56 +736,247 @@ this.original = this.str = string;

MagicString.Bundle = Bundle;
var hasOwnProp = Object.prototype.hasOwnProperty;function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
function adjust(mappings, start, end, d) {
var i = end;
var Bundle = (function () {
function Bundle() {
var options = arguments[0] === undefined ? {} : arguments[0];
if (!d) return; // replacement is same length as replaced string
_classCallCheck(this, Bundle);
while (i-- > start) {
if (~mappings[i]) {
mappings[i] += d;
}
}
}
this.intro = options.intro || '';
this.outro = options.outro || '';
this.separator = options.separator !== undefined ? options.separator : '\n';
function initMappings(i) {
var mappings = new Uint32Array(i);
this.sources = [];
while (i--) {
mappings[i] = i;
this.uniqueSources = [];
this.uniqueSourceIndexByFilename = {};
}
return mappings;
}
Bundle.prototype.addSource = function addSource(source) {
if (source instanceof MagicString) {
return this.addSource({
content: source,
filename: source.filename,
separator: this.separator
});
}
function blank(mappings, start, i) {
while (i-- > start) {
mappings[i] = -1;
}
}
if (typeof source !== 'object' || !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`');
}
function reverse(mappings, i) {
var result, location;
['filename', 'indentExclusionRanges', 'separator'].forEach(function (option) {
if (!hasOwnProp.call(source, option)) source[option] = source.content[option];
});
result = new Uint32Array(i);
if (source.separator === undefined) {
// TODO there's a bunch of this sort of thing, needs cleaning up
source.separator = this.separator;
}
while (i--) {
result[i] = -1;
}
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');
}
}
}
i = mappings.length;
while (i--) {
location = mappings[i];
this.sources.push(source);
return this;
};
if (~location) {
result[location] = i;
Bundle.prototype.append = function append(str, options) {
this.addSource({
content: new MagicString(str),
separator: options && options.separator || ''
});
return this;
};
Bundle.prototype.clone = function clone() {
var bundle = new Bundle({
intro: this.intro,
outro: this.outro,
separator: this.separator
});
this.sources.forEach(function (source) {
bundle.addSource({
filename: source.filename,
content: source.content.clone(),
separator: source.separator
});
});
return bundle;
};
Bundle.prototype.generateMap = function generateMap(options) {
var _this = this;
var offsets = {};
var encoded = getSemis(this.intro) + this.sources.map(function (source, i) {
var prefix = i > 0 ? getSemis(source.separator) || ',' : '';
var mappings = undefined;
// we don't bother encoding sources without a filename
if (!source.filename) {
mappings = getSemis(source.content.toString());
} else {
var sourceIndex = _this.uniqueSourceIndexByFilename[source.filename];
mappings = source.content.getMappings(options.hires, sourceIndex, offsets);
}
return prefix + mappings;
}).join('') + getSemis(this.outro);
return new SourceMap({
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: [],
mappings: encoded
});
};
Bundle.prototype.getIndentString = function getIndentString() {
var indentStringCounts = {};
this.sources.forEach(function (source) {
var indentStr = source.content.indentStr;
if (indentStr === null) return;
if (!indentStringCounts[indentStr]) indentStringCounts[indentStr] = 0;
indentStringCounts[indentStr] += 1;
});
return Object.keys(indentStringCounts).sort(function (a, b) {
return indentStringCounts[a] - indentStringCounts[b];
})[0] || '\t';
};
Bundle.prototype.indent = function indent(indentStr) {
var _this2 = this;
if (!indentStr) {
indentStr = this.getIndentString();
}
}
return result;
}
var trailingNewline = !this.intro || this.intro.slice(0, -1) === '\n';
return MagicString;
this.sources.forEach(function (source, i) {
var separator = source.separator !== undefined ? source.separator : _this2.separator;
var indentStart = trailingNewline || i > 0 && /\r?\n$/.test(separator);
}));
//# sourceMappingURL=magic-string.deps.js.map
source.content.indent(indentStr, {
exclude: source.indentExclusionRanges,
indentStart: indentStart //: trailingNewline || /\r?\n$/.test( separator ) //true///\r?\n/.test( separator )
});
trailingNewline = source.content.str.slice(0, -1) === '\n';
});
this.intro = this.intro.replace(/^[^\n]/gm, function (match, index) {
return index > 0 ? indentStr + match : match;
});
this.outro = this.outro.replace(/^[^\n]/gm, indentStr + '$&');
return this;
};
Bundle.prototype.prepend = function prepend(str) {
this.intro = str + this.intro;
return this;
};
Bundle.prototype.toString = function toString() {
var _this3 = this;
var body = this.sources.map(function (source, i) {
var separator = source.separator !== undefined ? source.separator : _this3.separator;
var str = (i > 0 ? separator : '') + source.content.toString();
return str;
}).join('');
return this.intro + body + this.outro;
};
Bundle.prototype.trimLines = function trimLines() {
return this.trim('[\\r\\n]');
};
Bundle.prototype.trim = function trim(charType) {
return this.trimStart(charType).trimEnd(charType);
};
Bundle.prototype.trimStart = function trimStart(charType) {
var rx = new RegExp('^' + (charType || '\\s') + '+');
this.intro = this.intro.replace(rx, '');
if (!this.intro) {
var source = undefined; // TODO put inside loop if safe
var i = 0;
do {
source = this.sources[i];
if (!source) {
this.outro = this.outro.replace(rx, '');
break;
}
source.content.trimStart();
i += 1;
} while (source.content.str === '');
}
return this;
};
Bundle.prototype.trimEnd = function trimEnd(charType) {
var rx = new RegExp((charType || '\\s') + '+$');
this.outro = this.outro.replace(rx, '');
if (!this.outro) {
var source = undefined;
var i = this.sources.length - 1;
do {
source = this.sources[i];
if (!source) {
this.intro = this.intro.replace(rx, '');
break;
}
source.content.trimEnd(charType);
i -= 1;
} while (source.content.str === '');
}
return this;
};
return Bundle;
})();
MagicString.Bundle = Bundle;
var index = MagicString;
return index;
}));
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('vlq')) :
typeof define === 'function' && define.amd ? define(['vlq'], factory) :
global.MagicString = factory(global.vlq)
global.MagicString = factory(vlq);
}(this, function (vlq) { 'use strict';
var _btoa;
function getRelativePath(from, to) {
var fromParts = from.split(/[\/\\]/);
var toParts = to.split(/[\/\\]/);
fromParts.pop(); // get dirname
while (fromParts[0] === toParts[0]) {
fromParts.shift();
toParts.shift();
}
if (fromParts.length) {
var i = fromParts.length;
while (i--) fromParts[i] = '..';
}
return fromParts.concat(toParts).join('/');
}var _btoa;
if (typeof window !== 'undefined' && typeof window.btoa === 'function') {

@@ -19,9 +36,7 @@ _btoa = window.btoa;

var btoa = _btoa;
var btoa = _btoa;function __classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
function SourceMap___classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
var SourceMap = (function () {
function SourceMap(properties) {
SourceMap___classCallCheck(this, SourceMap);
__classCallCheck(this, SourceMap);

@@ -48,304 +63,84 @@ this.version = 3;

function getRelativePath(from, to) {
var fromParts = from.split(/[\/\\]/);
var toParts = to.split(/[\/\\]/);
function getSemis(str) {
return new Array(str.split('\n').length).join(';');
}
fromParts.pop(); // get dirname
function adjust(mappings, start, end, d) {
var i = end;
while (fromParts[0] === toParts[0]) {
fromParts.shift();
toParts.shift();
}
if (!d) return; // replacement is same length as replaced string
if (fromParts.length) {
var i = fromParts.length;
while (i--) fromParts[i] = '..';
while (i-- > start) {
if (~mappings[i]) {
mappings[i] += d;
}
}
return fromParts.concat(toParts).join('/');
}
var hasOwnProp = Object.prototype.hasOwnProperty;
var warned = false;
function Bundle___classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
function blank(mappings, start, i) {
while (i-- > start) {
mappings[i] = -1;
}
}
var Bundle = (function () {
function Bundle() {
var options = arguments[0] === undefined ? {} : arguments[0];
function reverse(mappings, i) {
var result, location;
Bundle___classCallCheck(this, Bundle);
result = new Uint32Array(i);
this.intro = options.intro || '';
this.outro = options.outro || '';
this.separator = options.separator !== undefined ? options.separator : '\n';
this.sources = [];
this.uniqueSources = [];
this.uniqueSourceIndexByFilename = {};
while (i--) {
result[i] = -1;
}
Bundle.prototype.addSource = function addSource(source) {
if (source instanceof MagicString) {
return this.addSource({
content: source,
filename: source.filename,
separator: this.separator
});
}
i = mappings.length;
while (i--) {
location = mappings[i];
if (typeof source !== 'object' || !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`');
if (~location) {
result[location] = i;
}
}
['filename', 'indentExclusionRanges', 'separator'].forEach(function (option) {
if (!hasOwnProp.call(source, option)) source[option] = source.content[option];
});
return result;
}
if (source.separator === undefined) {
// TODO there's a bunch of this sort of thing, needs cleaning up
source.separator = this.separator;
}
function getLocation(locations, char) {
var i;
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');
}
}
i = locations.length;
while (i--) {
if (locations[i] <= char) {
return {
line: i,
column: char - locations[i]
};
}
}
this.sources.push(source);
return this;
};
Bundle.prototype.append = function append(str, options) {
this.addSource({
content: new MagicString(str),
separator: options && options.separator || ''
});
return this;
};
Bundle.prototype.clone = function clone() {
var bundle = new Bundle({
intro: this.intro,
outro: this.outro,
separator: this.separator
});
this.sources.forEach(function (source) {
bundle.addSource({
filename: source.filename,
content: source.content.clone(),
separator: source.separator
});
});
return bundle;
};
Bundle.prototype.generateMap = function generateMap(options) {
var _this = this;
var offsets = {};
var encoded = getSemis(this.intro) + this.sources.map(function (source, i) {
var prefix = i > 0 ? getSemis(source.separator) || ',' : '';
var mappings = undefined;
// we don't bother encoding sources without a filename
if (!source.filename) {
mappings = getSemis(source.content.toString());
} else {
var sourceIndex = _this.uniqueSourceIndexByFilename[source.filename];
mappings = source.content.getMappings(options.hires, sourceIndex, offsets);
}
return prefix + mappings;
}).join('') + getSemis(this.outro);
return new SourceMap({
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: [],
mappings: encoded
});
};
Bundle.prototype.getIndentString = function getIndentString() {
var indentStringCounts = {};
this.sources.forEach(function (source) {
var indentStr = source.content.indentStr;
if (indentStr === null) return;
if (!indentStringCounts[indentStr]) indentStringCounts[indentStr] = 0;
indentStringCounts[indentStr] += 1;
});
return Object.keys(indentStringCounts).sort(function (a, b) {
return indentStringCounts[a] - indentStringCounts[b];
})[0] || '\t';
};
Bundle.prototype.indent = function indent(indentStr) {
var _this2 = this;
if (!indentStr) {
indentStr = this.getIndentString();
}
var trailingNewline = !this.intro || this.intro.slice(0, -1) === '\n';
this.sources.forEach(function (source, i) {
var separator = source.separator !== undefined ? source.separator : _this2.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.str.slice(0, -1) === '\n';
});
this.intro = this.intro.replace(/^[^\n]/gm, function (match, index) {
return index > 0 ? indentStr + match : match;
});
this.outro = this.outro.replace(/^[^\n]/gm, indentStr + '$&');
return this;
};
Bundle.prototype.prepend = function prepend(str) {
this.intro = str + this.intro;
return this;
};
Bundle.prototype.toString = function toString() {
var _this3 = this;
var body = this.sources.map(function (source, i) {
var separator = source.separator !== undefined ? source.separator : _this3.separator;
var str = (i > 0 ? separator : '') + source.content.toString();
return str;
}).join('');
return this.intro + body + this.outro;
};
Bundle.prototype.trimLines = function trimLines() {
return this.trim('[\\r\\n]');
};
Bundle.prototype.trim = function trim(charType) {
return this.trimStart(charType).trimEnd(charType);
};
Bundle.prototype.trimStart = function trimStart(charType) {
var rx = new RegExp('^' + (charType || '\\s') + '+');
this.intro = this.intro.replace(rx, '');
if (!this.intro) {
var source = undefined; // TODO put inside loop if safe
var i = 0;
do {
source = this.sources[i];
if (!source) {
this.outro = this.outro.replace(rx, '');
break;
}
source.content.trimStart();
i += 1;
} while (source.content.str === '');
}
return this;
};
Bundle.prototype.trimEnd = function trimEnd(charType) {
var rx = new RegExp((charType || '\\s') + '+$');
this.outro = this.outro.replace(rx, '');
if (!this.outro) {
var source = undefined;
var i = this.sources.length - 1;
do {
source = this.sources[i];
if (!source) {
this.intro = this.intro.replace(rx, '');
break;
}
source.content.trimEnd(charType);
i -= 1;
} while (source.content.str === '');
}
return this;
};
return Bundle;
})();
function stringify(source) {
return source.content.toString();
throw new Error('Character out of bounds');
}
function getSemis(str) {
return new Array(str.split('\n').length).join(';');
}
function invert(str, mappings) {
var inverted = new Uint32Array(str.length),
i;
function guessIndent(code) {
var lines = code.split('\n');
var tabbed = lines.filter(function (line) {
return /^\t+/.test(line);
});
var spaced = lines.filter(function (line) {
return /^ {2,}/.test(line);
});
if (tabbed.length === 0 && spaced.length === 0) {
return null;
// initialise everything to -1
i = str.length;
while (i--) {
inverted[i] = -1;
}
// More lines tabbed than spaced? Assume tabs, and
// default to tabs in the case of a tie (or nothing
// to go on)
if (tabbed.length >= spaced.length) {
return '\t';
// then apply the actual mappings
i = mappings.length;
while (i--) {
if (~mappings[i]) {
inverted[mappings[i]] = i;
}
}
// Otherwise, we need to guess the multiple
var min = spaced.reduce(function (previous, current) {
var numSpaces = /^ +/.exec(current)[0].length;
return Math.min(numSpaces, previous);
}, Infinity);
return new Array(min + 1).join(' ');
return inverted;
}
var encode = vlq.encode;
function encodeMappings(original, str, mappings, hires, sourcemapLocations, sourceIndex, offsets) {

@@ -425,3 +220,3 @@ // store locations, for fast lookup

return encode(arr);
return vlq.encode(arr);
}).join(',');

@@ -433,47 +228,44 @@ }).join(';');

function invert(str, mappings) {
var inverted = new Uint32Array(str.length),
i;
function guessIndent(code) {
var lines = code.split('\n');
// initialise everything to -1
i = str.length;
while (i--) {
inverted[i] = -1;
var tabbed = lines.filter(function (line) {
return /^\t+/.test(line);
});
var spaced = lines.filter(function (line) {
return /^ {2,}/.test(line);
});
if (tabbed.length === 0 && spaced.length === 0) {
return null;
}
// then apply the actual mappings
i = mappings.length;
while (i--) {
if (~mappings[i]) {
inverted[mappings[i]] = i;
}
// More lines tabbed than spaced? Assume tabs, and
// default to tabs in the case of a tie (or nothing
// to go on)
if (tabbed.length >= spaced.length) {
return '\t';
}
return inverted;
// Otherwise, we need to guess the multiple
var min = spaced.reduce(function (previous, current) {
var numSpaces = /^ +/.exec(current)[0].length;
return Math.min(numSpaces, previous);
}, Infinity);
return new Array(min + 1).join(' ');
}
function getLocation(locations, char) {
var i;
function initMappings(i) {
var mappings = new Uint32Array(i);
i = locations.length;
while (i--) {
if (locations[i] <= char) {
return {
line: i,
column: char - locations[i]
};
}
mappings[i] = i;
}
throw new Error('Character out of bounds');
return mappings;
}
// do nothing
function ___classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
// do nothing
function MagicString___classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
var warned = false;
var MagicString = (function () {

@@ -483,3 +275,3 @@ function MagicString(string) {

MagicString___classCallCheck(this, MagicString);
___classCallCheck(this, MagicString);

@@ -892,56 +684,247 @@ this.original = this.str = string;

MagicString.Bundle = Bundle;
var hasOwnProp = Object.prototype.hasOwnProperty;function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
function adjust(mappings, start, end, d) {
var i = end;
var Bundle = (function () {
function Bundle() {
var options = arguments[0] === undefined ? {} : arguments[0];
if (!d) return; // replacement is same length as replaced string
_classCallCheck(this, Bundle);
while (i-- > start) {
if (~mappings[i]) {
mappings[i] += d;
}
}
}
this.intro = options.intro || '';
this.outro = options.outro || '';
this.separator = options.separator !== undefined ? options.separator : '\n';
function initMappings(i) {
var mappings = new Uint32Array(i);
this.sources = [];
while (i--) {
mappings[i] = i;
this.uniqueSources = [];
this.uniqueSourceIndexByFilename = {};
}
return mappings;
}
Bundle.prototype.addSource = function addSource(source) {
if (source instanceof MagicString) {
return this.addSource({
content: source,
filename: source.filename,
separator: this.separator
});
}
function blank(mappings, start, i) {
while (i-- > start) {
mappings[i] = -1;
}
}
if (typeof source !== 'object' || !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`');
}
function reverse(mappings, i) {
var result, location;
['filename', 'indentExclusionRanges', 'separator'].forEach(function (option) {
if (!hasOwnProp.call(source, option)) source[option] = source.content[option];
});
result = new Uint32Array(i);
if (source.separator === undefined) {
// TODO there's a bunch of this sort of thing, needs cleaning up
source.separator = this.separator;
}
while (i--) {
result[i] = -1;
}
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');
}
}
}
i = mappings.length;
while (i--) {
location = mappings[i];
this.sources.push(source);
return this;
};
if (~location) {
result[location] = i;
Bundle.prototype.append = function append(str, options) {
this.addSource({
content: new MagicString(str),
separator: options && options.separator || ''
});
return this;
};
Bundle.prototype.clone = function clone() {
var bundle = new Bundle({
intro: this.intro,
outro: this.outro,
separator: this.separator
});
this.sources.forEach(function (source) {
bundle.addSource({
filename: source.filename,
content: source.content.clone(),
separator: source.separator
});
});
return bundle;
};
Bundle.prototype.generateMap = function generateMap(options) {
var _this = this;
var offsets = {};
var encoded = getSemis(this.intro) + this.sources.map(function (source, i) {
var prefix = i > 0 ? getSemis(source.separator) || ',' : '';
var mappings = undefined;
// we don't bother encoding sources without a filename
if (!source.filename) {
mappings = getSemis(source.content.toString());
} else {
var sourceIndex = _this.uniqueSourceIndexByFilename[source.filename];
mappings = source.content.getMappings(options.hires, sourceIndex, offsets);
}
return prefix + mappings;
}).join('') + getSemis(this.outro);
return new SourceMap({
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: [],
mappings: encoded
});
};
Bundle.prototype.getIndentString = function getIndentString() {
var indentStringCounts = {};
this.sources.forEach(function (source) {
var indentStr = source.content.indentStr;
if (indentStr === null) return;
if (!indentStringCounts[indentStr]) indentStringCounts[indentStr] = 0;
indentStringCounts[indentStr] += 1;
});
return Object.keys(indentStringCounts).sort(function (a, b) {
return indentStringCounts[a] - indentStringCounts[b];
})[0] || '\t';
};
Bundle.prototype.indent = function indent(indentStr) {
var _this2 = this;
if (!indentStr) {
indentStr = this.getIndentString();
}
}
return result;
}
var trailingNewline = !this.intro || this.intro.slice(0, -1) === '\n';
return MagicString;
this.sources.forEach(function (source, i) {
var separator = source.separator !== undefined ? source.separator : _this2.separator;
var indentStart = trailingNewline || i > 0 && /\r?\n$/.test(separator);
}));
//# sourceMappingURL=magic-string.js.map
source.content.indent(indentStr, {
exclude: source.indentExclusionRanges,
indentStart: indentStart //: trailingNewline || /\r?\n$/.test( separator ) //true///\r?\n/.test( separator )
});
trailingNewline = source.content.str.slice(0, -1) === '\n';
});
this.intro = this.intro.replace(/^[^\n]/gm, function (match, index) {
return index > 0 ? indentStr + match : match;
});
this.outro = this.outro.replace(/^[^\n]/gm, indentStr + '$&');
return this;
};
Bundle.prototype.prepend = function prepend(str) {
this.intro = str + this.intro;
return this;
};
Bundle.prototype.toString = function toString() {
var _this3 = this;
var body = this.sources.map(function (source, i) {
var separator = source.separator !== undefined ? source.separator : _this3.separator;
var str = (i > 0 ? separator : '') + source.content.toString();
return str;
}).join('');
return this.intro + body + this.outro;
};
Bundle.prototype.trimLines = function trimLines() {
return this.trim('[\\r\\n]');
};
Bundle.prototype.trim = function trim(charType) {
return this.trimStart(charType).trimEnd(charType);
};
Bundle.prototype.trimStart = function trimStart(charType) {
var rx = new RegExp('^' + (charType || '\\s') + '+');
this.intro = this.intro.replace(rx, '');
if (!this.intro) {
var source = undefined; // TODO put inside loop if safe
var i = 0;
do {
source = this.sources[i];
if (!source) {
this.outro = this.outro.replace(rx, '');
break;
}
source.content.trimStart();
i += 1;
} while (source.content.str === '');
}
return this;
};
Bundle.prototype.trimEnd = function trimEnd(charType) {
var rx = new RegExp((charType || '\\s') + '+$');
this.outro = this.outro.replace(rx, '');
if (!this.outro) {
var source = undefined;
var i = this.sources.length - 1;
do {
source = this.sources[i];
if (!source) {
this.intro = this.intro.replace(rx, '');
break;
}
source.content.trimEnd(charType);
i -= 1;
} while (source.content.str === '');
}
return this;
};
return Bundle;
})();
MagicString.Bundle = Bundle;
var index = MagicString;
return index;
}));

@@ -5,3 +5,3 @@ {

"author": "Rich Harris",
"version": "0.5.3",
"version": "0.6.0",
"repository": "https://github.com/rich-harris/magic-string",

@@ -18,3 +18,3 @@ "main": "dist/magic-string.js",

"gobble-babel": "^5.1.0",
"gobble-esperanto-bundle": "^0.2.0",
"gobble-rollup": "^0.2.0",
"mocha": "^2.1.0",

@@ -21,0 +21,0 @@ "resolve": "^1.1.0",

import MagicString from '../MagicString/index';
import SourceMap from '../SourceMap';
import SourceMap from '../utils/SourceMap';
import getRelativePath from '../utils/getRelativePath';

@@ -4,0 +4,0 @@ import hasOwnProp from '../utils/hasOwnProp';

@@ -1,2 +0,2 @@

import encode from '../utils/encode';
import { encode } from 'vlq';

@@ -3,0 +3,0 @@ export default function encodeMappings ( original, str, mappings, hires, sourcemapLocations, sourceIndex, offsets ) {

@@ -1,3 +0,2 @@

import Bundle from '../Bundle';
import SourceMap from '../SourceMap';
import SourceMap from '../utils/SourceMap';
import guessIndent from './guessIndent';

@@ -409,4 +408,2 @@ import encodeMappings from './encodeMappings';

MagicString.Bundle = Bundle;
function adjust ( mappings, start, end, d ) {

@@ -413,0 +410,0 @@ var i = end;

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc