Skip to content

Commit

Permalink
Update: Reduce nesting of functions in add
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Jun 17, 2017
1 parent 0416af0 commit 1d7879a
Showing 1 changed file with 93 additions and 90 deletions.
183 changes: 93 additions & 90 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,112 +75,113 @@ module.exports.add = function add (file, options, cb) {

var fileContent = file.contents.toString();
var sourceMap;
var sourcePath = ''; //root path for the sources in the map

var loadMaps = function (callback) {

var sourcePath = ''; //root path for the sources in the map

if (options.loadMaps) {
// Try to read inline source map
sourceMap = convert.fromSource(fileContent);

// fix source paths and sourceContent for imported source map
var fixImportedSourceMap = function () {
sourceMap.sourcesContent = sourceMap.sourcesContent || [];

var loadSourceAsync = function (source, onLoaded) {
var i = source[0],
absPath = source[1];
fs.readFile(absPath, 'utf8', function (err, data) {
if (err) {
if (options.debug) {
console.warn(PLUGIN_NAME + '-add: source file not found: ' + absPath);
}
sourceMap.sourcesContent[i] = null;
return onLoaded();
}
sourceMap.sourcesContent[i] = stripBom(data);
onLoaded();
});
};
if (sourceMap) {
sourceMap = sourceMap.toObject();
// sources in map are relative to the source file
sourcePath = path.dirname(file.path);
fileContent = convert.removeComments(fileContent);
}
}

var sourcesToLoadAsync = sourceMap.sources.reduce(function(result, source, i) {
if (source.match(urlRegex)) {
sourceMap.sourcesContent[i] = sourceMap.sourcesContent[i] || null;
return result;
}
var absPath = path.resolve(sourcePath, source);
sourceMap.sources[i] = unixStylePath(path.relative(file.base, absPath));
if (!sourceMap.sourcesContent[i]) {
var sourceContent = null;
if (sourceMap.sourceRoot) {
if (sourceMap.sourceRoot.match(urlRegex)) {
sourceMap.sourcesContent[i] = null;
return result;
}
absPath = path.resolve(sourcePath, sourceMap.sourceRoot, source);
}
if (absPath === file.path) {
// if current file: use content
sourceContent = fileContent;
} else {
// else load content from file async
if (options.debug) {
console.log(PLUGIN_NAME + '-add: No source content for "' + source + '". Loading from file.');
}
result.push([i, absPath]);
return result;
}
sourceMap.sourcesContent[i] = sourceContent;
}
return result;
}, []);
var loadSourceMap = function (callback) {
if (sourceMap) {
return callback();
}

// remove source map comment from source
file.contents = new Buffer(fileContent, 'utf8');
// look for source map comment referencing a source map file
var mapComment = convert.mapFileCommentRegex.exec(fileContent);

async.each(sourcesToLoadAsync, loadSourceAsync, callback);
};
var mapFile;
if (mapComment) {
mapFile = path.resolve(path.dirname(file.path), mapComment[1] || mapComment[2]);
fileContent = convert.removeMapFileComments(fileContent);
// if no comment try map file with same name as source file
} else {
mapFile = file.path + '.map';
}

var loadSourceMap = function (callback) {
// look for source map comment referencing a source map file
var mapComment = convert.mapFileCommentRegex.exec(fileContent);
// sources in external map are relative to map file
sourcePath = path.dirname(mapFile);

var mapFile;
if (mapComment) {
mapFile = path.resolve(path.dirname(file.path), mapComment[1] || mapComment[2]);
fileContent = convert.removeMapFileComments(fileContent);
// if no comment try map file with same name as source file
} else {
mapFile = file.path + '.map';
fs.readFile(mapFile, 'utf8', function (err, data) {
if (err) {
console.log(err);
if (options.debug) {
console.log(PLUGIN_NAME + '-add: Can\'t read map file :' + mapFile);
}
return callback();
}
sourceMap = parse(data);
callback();
});
};

// fix source paths and sourceContent for imported source map
var fixImportedSourceMap = function (callback) {
if (!sourceMap) {
return callback();
}

// sources in external map are relative to map file
sourcePath = path.dirname(mapFile);
sourceMap.sourcesContent = sourceMap.sourcesContent || [];

fs.readFile(mapFile, 'utf8', function (err, data) {
var loadSourceAsync = function (source, onLoaded) {
var i = source[0],
absPath = source[1];
fs.readFile(absPath, 'utf8', function (err, data) {
if (err) {
if (options.debug) {
console.log(PLUGIN_NAME + '-add: Can\'t read map file :' + mapFile);
console.warn(PLUGIN_NAME + '-add: source file not found: ' + absPath);
}
return callback();
sourceMap.sourcesContent[i] = null;
return onLoaded();
}
sourceMap = parse(data);
callback();
sourceMap.sourcesContent[i] = stripBom(data);
onLoaded();
});

};

var asyncTasks = [fixImportedSourceMap];
if (sourceMap) {
sourceMap = sourceMap.toObject();
// sources in map are relative to the source file
sourcePath = path.dirname(file.path);
fileContent = convert.removeComments(fileContent);
} else {
asyncTasks.unshift(loadSourceMap);
}
async.waterfall(asyncTasks, callback);
var sourcesToLoadAsync = sourceMap.sources.reduce(function(result, source, i) {
if (source.match(urlRegex)) {
sourceMap.sourcesContent[i] = sourceMap.sourcesContent[i] || null;
return result;
}
var absPath = path.resolve(sourcePath, source);
sourceMap.sources[i] = unixStylePath(path.relative(file.base, absPath));
if (!sourceMap.sourcesContent[i]) {
var sourceContent = null;
if (sourceMap.sourceRoot) {
if (sourceMap.sourceRoot.match(urlRegex)) {
sourceMap.sourcesContent[i] = null;
return result;
}
absPath = path.resolve(sourcePath, sourceMap.sourceRoot, source);
}
if (absPath === file.path) {
// if current file: use content
sourceContent = fileContent;
} else {
// else load content from file async
if (options.debug) {
console.log(PLUGIN_NAME + '-add: No source content for "' + source + '". Loading from file.');
}
result.push([i, absPath]);
return result;
}
sourceMap.sourcesContent[i] = sourceContent;
}
return result;
}, []);

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

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

var mapsLoaded = function (callback) {
Expand Down Expand Up @@ -234,10 +235,12 @@ module.exports.add = function add (file, options, cb) {

};

var asyncTasks = [mapsLoaded];
if (options.loadMaps) {
asyncTasks.unshift(loadMaps);
}

var asyncTasks = [
loadSourceMap,
fixImportedSourceMap,
mapsLoaded
];
async.waterfall(asyncTasks, cb);

};
Expand Down

0 comments on commit 1d7879a

Please sign in to comment.