Skip to content

Commit

Permalink
File: support prev map (issue #16)
Browse files Browse the repository at this point in the history
  • Loading branch information
miripiruni committed May 17, 2017
1 parent af4478f commit 8634dfd
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions lib/file.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var os = require('os'),
path = require('path'),
url = require('url'),
fs = require('fs'),
mozilla = require('source-map'),
SourceMapGenerator = mozilla.SourceMapGenerator,
utils = require('./utils');
Expand Down Expand Up @@ -37,6 +38,60 @@ File.prototype = {
return this;
},

writeFileFromPrevMap: function (file, prev) {
if (prev === false) {
return this.writeFileContent(file);
}

if (prev) {
if (typeof prev === 'string') {
return prev;
}

if (typeof prev === 'function') {
var prevPath = prev(file);

if (prevPath && fs.existsSync && fs.existsSync(prevPath)) {
return fs.readFileSync(prevPath, 'utf-8').toString().trim();
} else {
throw new Error('Unable to load previous source map: ' + prevPath.toString());
}
}

if (prev instanceof mozilla.SourceMapConsumer) {
return mozilla.SourceMapGenerator.fromSourceMap(prev).toString();
}

if (prev instanceof mozilla.SourceMapGenerator) {
return prev.toString();
}

if (isMap(prev)) {
return JSON.stringify(prev);
}

throw new Error('Unsupported previous source map format: ' + prev.toString());

}

if (this.inline) {
return this.decodeInline(this.annotation);
}

if (this.annotation) {
var map = this.annotation;
if (file) {
map = path.join(path.dirname(file), map);
}

this.root = path.dirname(map);

return (fs.existsSync && fs.existsSync(map)) ?
fs.readFileSync(map, 'utf-8').toString().trim() :
false;
}
},

writeLine: function (line) {
this.write(line + '\n');

Expand Down Expand Up @@ -131,6 +186,12 @@ File.prototype = {
}
};


function isMap(map) {
if (typeof map !== 'object') return false;
return typeof map.mappings === 'string' || typeof map._mappings === 'string';
}

function getLines(text) {
return text.split(/\r\n|\r|\n/);
}
Expand Down

0 comments on commit 8634dfd

Please sign in to comment.