This commit is contained in:
chrosey
2017-09-13 07:52:34 +02:00
parent a1f16c37f4
commit 2340b0226b
24621 changed files with 2912161 additions and 149 deletions
+21
View File
@@ -0,0 +1,21 @@
import btoa from './btoa.js';
export default function SourceMap ( properties ) {
this.version = 3;
this.file = properties.file;
this.sources = properties.sources;
this.sourcesContent = properties.sourcesContent;
this.names = properties.names;
this.mappings = properties.mappings;
}
SourceMap.prototype = {
toString () {
return JSON.stringify( this );
},
toUrl () {
return 'data:application/json;charset=utf-8;base64,' + btoa( this.toString() );
}
};
+18
View File
@@ -0,0 +1,18 @@
export default class Stats {
constructor () {
Object.defineProperties( this, {
startTimes: { value: {} }
});
}
time ( label ) {
this.startTimes[ label ] = process.hrtime();
}
timeEnd ( label ) {
const elapsed = process.hrtime( this.startTimes[ label ] );
if ( !this[ label ] ) this[ label ] = 0;
this[ label ] += elapsed[0] * 1e3 + elapsed[1] * 1e-6;
}
}
+13
View File
@@ -0,0 +1,13 @@
let _btoa;
if ( typeof window !== 'undefined' && typeof window.btoa === 'function' ) {
_btoa = window.btoa;
} else if ( typeof Buffer === 'function' ) {
_btoa = str => new Buffer( str ).toString( 'base64' );
} else {
_btoa = () => {
throw new Error( 'Unsupported environment: `window.btoa` or `Buffer` should be supported.' );
};
}
export default _btoa;
+132
View File
@@ -0,0 +1,132 @@
import { encode } from 'vlq';
import getLocator from './getLocator.js';
export default function encodeMappings ( original, intro, chunk, hires, sourcemapLocations, sourceIndex, offsets, names ) {
let rawLines = [];
let generatedCodeLine = intro.split( '\n' ).length - 1;
let rawSegments = rawLines[ generatedCodeLine ] = [];
let generatedCodeColumn = 0;
const locate = getLocator( original );
function addEdit ( content, original, loc, nameIndex, i ) {
if ( i || content.length ) {
rawSegments.push({
generatedCodeLine,
generatedCodeColumn,
sourceCodeLine: loc.line,
sourceCodeColumn: loc.column,
sourceCodeName: nameIndex,
sourceIndex
});
}
let lines = content.split( '\n' );
let lastLine = lines.pop();
if ( lines.length ) {
generatedCodeLine += lines.length;
rawLines[ generatedCodeLine ] = rawSegments = [];
generatedCodeColumn = lastLine.length;
} else {
generatedCodeColumn += lastLine.length;
}
lines = original.split( '\n' );
lastLine = lines.pop();
if ( lines.length ) {
loc.line += lines.length;
loc.column = lastLine.length;
} else {
loc.column += lastLine.length;
}
}
function addUneditedChunk ( chunk, loc ) {
let originalCharIndex = chunk.start;
let first = true;
while ( originalCharIndex < chunk.end ) {
if ( hires || first || sourcemapLocations[ originalCharIndex ] ) {
rawSegments.push({
generatedCodeLine,
generatedCodeColumn,
sourceCodeLine: loc.line,
sourceCodeColumn: loc.column,
sourceCodeName: -1,
sourceIndex
});
}
if ( original[ originalCharIndex ] === '\n' ) {
loc.line += 1;
loc.column = 0;
generatedCodeLine += 1;
rawLines[ generatedCodeLine ] = rawSegments = [];
generatedCodeColumn = 0;
} else {
loc.column += 1;
generatedCodeColumn += 1;
}
originalCharIndex += 1;
first = false;
}
}
while ( chunk ) {
let loc = locate( chunk.start );
if ( chunk.intro.length ) {
addEdit( chunk.intro, '', loc, -1, !!chunk.previous );
}
if ( chunk.edited ) {
addEdit( chunk.content, chunk.original, loc, chunk.storeName ? names.indexOf( chunk.original ) : -1, !!chunk.previous );
} else {
addUneditedChunk( chunk, loc );
}
if ( chunk.outro.length ) {
addEdit( chunk.outro, '', loc, -1, !!chunk.previous );
}
const nextChunk = chunk.next;
chunk = nextChunk;
}
offsets.sourceIndex = offsets.sourceIndex || 0;
offsets.sourceCodeLine = offsets.sourceCodeLine || 0;
offsets.sourceCodeColumn = offsets.sourceCodeColumn || 0;
offsets.sourceCodeName = offsets.sourceCodeName || 0;
const encoded = rawLines.map( segments => {
let generatedCodeColumn = 0;
return segments.map( segment => {
let arr = [
segment.generatedCodeColumn - generatedCodeColumn,
segment.sourceIndex - offsets.sourceIndex,
segment.sourceCodeLine - offsets.sourceCodeLine,
segment.sourceCodeColumn - offsets.sourceCodeColumn
];
generatedCodeColumn = segment.generatedCodeColumn;
offsets.sourceIndex = segment.sourceIndex;
offsets.sourceCodeLine = segment.sourceCodeLine;
offsets.sourceCodeColumn = segment.sourceCodeColumn;
if ( ~segment.sourceCodeName ) {
arr.push( segment.sourceCodeName - offsets.sourceCodeName );
offsets.sourceCodeName = segment.sourceCodeName;
}
return encode( arr );
}).join( ',' );
}).join( ';' );
return encoded;
}
+35
View File
@@ -0,0 +1,35 @@
export default function getLocator ( source ) {
let originalLines = source.split( '\n' );
let start = 0;
let lineRanges = originalLines.map( ( line, i ) => {
const end = start + line.length + 1;
const range = { start, end, line: i };
start = end;
return range;
});
let i = 0;
function rangeContains ( range, index ) {
return range.start <= index && index < range.end;
}
function getLocation ( range, index ) {
return { line: range.line, column: index - range.start };
}
return function locate ( index ) {
let range = lineRanges[i];
const d = index >= range.end ? 1 : -1;
while ( range ) {
if ( rangeContains( range, index ) ) return getLocation( range, index );
i += d;
range = lineRanges[i];
}
};
}
+18
View File
@@ -0,0 +1,18 @@
export default function getRelativePath ( from, to ) {
let fromParts = from.split( /[\/\\]/ );
let toParts = to.split( /[\/\\]/ );
fromParts.pop(); // get dirname
while ( fromParts[0] === toParts[0] ) {
fromParts.shift();
toParts.shift();
}
if ( fromParts.length ) {
let i = fromParts.length;
while ( i-- ) fromParts[i] = '..';
}
return fromParts.concat( toParts ).join( '/' );
}
+25
View File
@@ -0,0 +1,25 @@
export default function guessIndent ( code ) {
const lines = code.split( '\n' );
const tabbed = lines.filter( line => /^\t+/.test( line ) );
const spaced = lines.filter( line => /^ {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
const min = spaced.reduce( ( previous, current ) => {
const numSpaces = /^ +/.exec( current )[0].length;
return Math.min( numSpaces, previous );
}, Infinity );
return new Array( min + 1 ).join( ' ' );
}
+1
View File
@@ -0,0 +1 @@
export default Object.prototype.hasOwnProperty;
+5
View File
@@ -0,0 +1,5 @@
const toString = Object.prototype.toString;
export default function isObject ( thing ) {
return toString.call( thing ) === '[object Object]';
}