Skip to content

Commit

Permalink
Update: Refactor loading of existing sourcemap (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro Henkel authored and phated committed Jun 17, 2017
1 parent 3d4d127 commit 6835e80
Showing 1 changed file with 54 additions and 47 deletions.
101 changes: 54 additions & 47 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ var generate = require('./generate');

var urlRegex = /^(https?|webpack(-[^:]+)?):\/\//;

function unixStylePath (filePath) {
function isRemoteSource(source) {
return source.match(urlRegex);
}

function unixStylePath(filePath) {
return filePath.split(path.sep).join('/');
}

Expand Down Expand Up @@ -64,61 +68,64 @@ function fixImportedSourceMap(file, source, options, callback) {

source.map.sourcesContent = source.map.sourcesContent || [];

var loadSourceAsync = function (tuple, onLoaded) {
var i = tuple[0];
var absPath = tuple[1];
// remove source map comment from source
file.contents = new Buffer(source.content, 'utf8');

fs.readFile(absPath, 'utf8', onRead);
async.eachOf(source.map.sources, normalizeSourcesAndContent, callback);

function onRead(err, data) {
if (err) {
// if (options.debug) {
// console.warn(PLUGIN_NAME + '-add: source file not found: ' + absPath);
// }
source.map.sourcesContent[i] = null;
return onLoaded();
}
source.map.sourcesContent[i] = stripBom(data);
onLoaded();
function assignSourcesContent(sourceContent, idx) {
source.map.sourcesContent[idx] = sourceContent;
}

function normalizeSourcesAndContent(sourcePath, idx, cb) {
var sourceRoot = source.map.sourceRoot || '';
var sourceContent = source.map.sourcesContent[idx] || null;

if (isRemoteSource(sourcePath)) {
assignSourcesContent(sourceContent, idx);
return cb();
}
};

var sourcesToLoadAsync = source.map.sources.reduce(function(result, sourcePath, i) {
if (sourcePath.match(urlRegex)) {
source.map.sourcesContent[i] = source.map.sourcesContent[i] || null;
return result;
if (source.map.sourcesContent[idx]) {
return cb();
}
var absPath = path.resolve(source.path, sourcePath);
source.map.sources[i] = unixStylePath(path.relative(file.base, absPath));
if (!source.map.sourcesContent[i]) {
var sourceContent = null;
if (source.map.sourceRoot) {
if (source.map.sourceRoot.match(urlRegex)) {
source.map.sourcesContent[i] = null;
return result;
}
absPath = path.resolve(source.path, source.map.sourceRoot, sourcePath);
}
if (absPath === file.path) {
// if current file: use content
sourceContent = source.content;
} else {
// else load content from file async

if (sourceRoot && isRemoteSource(sourceRoot)) {
assignSourcesContent(sourceContent, idx);
return cb();
}

var basePath = path.resolve(file.base, sourceRoot);
var absPath = path.resolve(source.path, sourceRoot, sourcePath);
var relPath = path.relative(basePath, absPath);
var unixRelPath = unixStylePath(relPath);

source.map.sources[idx] = unixRelPath;

if (absPath !== file.path) {
// load content from file async
// if (options.debug) {
// console.log(PLUGIN_NAME + '-add: No source content for "' + source + '". Loading from file.');
// }
return fs.readFile(absPath, 'utf8', onRead);
}

// if current file: use content
assignSourcesContent(source.content, idx);
cb();

function onRead(err, data) {
if (err) {
// if (options.debug) {
// console.log(PLUGIN_NAME + '-add: No source content for "' + source + '". Loading from file.');
// console.warn(PLUGIN_NAME + '-add: source file not found: ' + absPath);
// }
result.push([i, absPath]);
return result;
assignSourcesContent(null, idx);
return cb();
}
source.map.sourcesContent[i] = sourceContent;
assignSourcesContent(stripBom(data), idx);
cb();
}
return result;
}, []);

// remove source map comment from source
file.contents = new Buffer(source.content, 'utf8');

async.each(sourcesToLoadAsync, loadSourceAsync, callback);
}
}

function mapsLoaded(file, source, options, callback) {
Expand Down

0 comments on commit 6835e80

Please sign in to comment.