Skip to content

Commit

Permalink
Update: Rename variables and functions to be more consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Jun 17, 2017
1 parent 3634eaa commit 98586e0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 49 deletions.
17 changes: 11 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var PLUGIN_NAME = 'vinyl-sourcemap';
* @param options
* @param callback
*/
module.exports.add = function add (file, options, callback) {
function add(file, options, callback) {

// check if options are passed or a callback as second argument
// if there are 3 arguments, the options param should be an object
Expand All @@ -39,19 +39,19 @@ module.exports.add = function add (file, options, callback) {
return callback(null, file);
}

var source = {
var state = {
path: '', //root path for the sources in the map
map: null,
content: file.contents.toString(),
preExistingComment: null
};

if (options.loadMaps) {
helpers.loadInlineMaps(file, source);
helpers.loadInlineMaps(file, state);
}

helpers.addSourceMaps(file, source, options, callback);
};
helpers.addSourceMaps(file, state, options, callback);
}

/**
* Write the sourcemap (async, with callback function)
Expand All @@ -60,7 +60,7 @@ module.exports.add = function add (file, options, callback) {
* @param options
* @param callback
*/
module.exports.write = function write (file, destPath, options, cb) {
function write(file, destPath, options, cb) {

// Check arguments for optional destPath, options, or callback function
if (cb === undefined && typeof destPath === 'function') {
Expand Down Expand Up @@ -298,4 +298,9 @@ module.exports.write = function write (file, destPath, options, cb) {
}
async.waterfall(asyncTasks, cb);

}

module.exports = {
add: add,
write: write,
};
8 changes: 4 additions & 4 deletions lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ function generateJs(sourcePath, fileContent) {
return generator.toJSON();
}

function generateCss(source, fileContent) {
var generator = new SourceMapGenerator({ file: source });
function generateCss(sourcePath, fileContent) {
var generator = new SourceMapGenerator({ file: sourcePath });
var ast = css.parse(fileContent, { silent: true });

function registerTokens(ast) {
if (ast.position) {
generator.addMapping({
original: ast.position.start,
generated: ast.position.start,
source: source
source: sourcePath
});
}

Expand All @@ -54,7 +54,7 @@ function generateCss(source, fileContent) {
}
}
registerTokens(ast);
generator.setSourceContent(source, fileContent);
generator.setSourceContent(sourcePath, fileContent);

return generator.toJSON();
}
Expand Down
80 changes: 41 additions & 39 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,71 +22,73 @@ function unixStylePath(filePath) {
function parse(data) {
try {
return JSON.parse(stripBom(data));
} catch (err) {}
} catch (err) {
// TODO: should this log a debug?
}
}

function loadSourceMap(file, source, options, callback) {
if (source.map) {
function loadSourceMap(file, state, options, callback) {
if (state.map) {
return callback();
}

// look for source map comment referencing a source map file
var mapComment = convert.mapFileCommentRegex.exec(source.content);
var mapComment = convert.mapFileCommentRegex.exec(state.content);

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

// sources in external map are relative to map file
source.path = path.dirname(mapFile);
state.path = path.dirname(mapFile);

fs.readFile(mapFile, 'utf8', done);
fs.readFile(mapFile, 'utf8', onRead);

function done(err, data) {
function onRead(err, data) {
if (err) {
// console.log(err);
// if (options.debug) {
// console.log(PLUGIN_NAME + '-add: Can\'t read map file :' + mapFile);
// }
return callback();
}
source.map = parse(data);
state.map = parse(data);
callback();
}
}

// fix source paths and sourceContent for imported source map
function fixImportedSourceMap(file, source, options, callback) {
if (!source.map) {
function fixImportedSourceMap(file, state, options, callback) {
if (!state.map) {
return callback();
}

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

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

async.eachOf(source.map.sources, normalizeSourcesAndContent, callback);
async.eachOf(state.map.sources, normalizeSourcesAndContent, callback);

function assignSourcesContent(sourceContent, idx) {
source.map.sourcesContent[idx] = sourceContent;
state.map.sourcesContent[idx] = sourceContent;
}

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

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

if (source.map.sourcesContent[idx]) {
if (state.map.sourcesContent[idx]) {
return cb();
}

Expand All @@ -96,11 +98,11 @@ function fixImportedSourceMap(file, source, options, callback) {
}

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

source.map.sources[idx] = unixRelPath;
state.map.sources[idx] = unixRelPath;

if (absPath !== file.path) {
// load content from file async
Expand All @@ -111,7 +113,7 @@ function fixImportedSourceMap(file, source, options, callback) {
}

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

function onRead(err, data) {
Expand All @@ -128,28 +130,28 @@ function fixImportedSourceMap(file, source, options, callback) {
}
}

function mapsLoaded(file, source, options, callback) {
function mapsLoaded(file, state, options, callback) {

if (!source.map && options.identityMap) {
if (!state.map && options.identityMap) {
var sourcePath = unixStylePath(file.relative);

switch (file.extname) {
case '.js':
source.map = generate.js(sourcePath, source.content);
state.map = generate.js(sourcePath, state.content);
break;
case '.css':
source.map = generate.css(sourcePath, source.content);
state.map = generate.css(sourcePath, state.content);
break;
}
}

if (!source.map) {
source.map = {
if (!state.map) {
state.map = {
version: 3,
names: [],
mappings: '',
sources: [unixStylePath(file.relative)],
sourcesContent: [source.content]
sourcesContent: [state.content]
};
}

Expand All @@ -158,33 +160,33 @@ function mapsLoaded(file, source, options, callback) {
// sourceMap.preExistingComment = preExistingComment;
// }

source.map.file = unixStylePath(file.relative);
file.sourceMap = source.map;
state.map.file = unixStylePath(file.relative);
file.sourceMap = state.map;

callback();
}

function loadInlineMaps(file, source) {
function loadInlineMaps(file, state) {
// Try to read inline source map
source.map = convert.fromSource(source.content);
state.map = convert.fromSource(state.content);

if (source.map) {
source.map = source.map.toObject();
if (state.map) {
state.map = state.map.toObject();
// sources in map are relative to the source file
source.path = path.dirname(file.path);
source.content = convert.removeComments(source.content);
state.path = path.dirname(file.path);
state.content = convert.removeComments(state.content);
}
}

function addSourceMaps(file, source, options, callback) {
function addSourceMaps(file, state, options, callback) {

var tasks = [
loadSourceMap,
fixImportedSourceMap,
mapsLoaded,
];

async.applyEachSeries(tasks, file, source, options, done);
async.applyEachSeries(tasks, file, state, options, done);

function done(err) {
if (err) {
Expand Down

0 comments on commit 98586e0

Please sign in to comment.