/// FIXME: This is pretty much the same file used in the gobble-concat package. It should be refactored out somehow. // Given a file name, reads it, checks it if has a sourcemap attached, then either: // - Creates an identity sourcemap if it doesn't exist, // - Reads the file with the sourcemap, or // - Decodes an embedded base64 sourcemap var SourceNode = require('source-map').SourceNode; var SourceMapConsumer = require('source-map').SourceMapConsumer; var sander = require('sander'), readFile = sander.readFile, readFileSync = sander.readFileSync, realpathSync = sander.realpathSync, path = require('path'); var sourceMapRegExp = new RegExp(/(?:\/\/#|\/\/@|\/\*#)\s*sourceMappingURL=(\S*)\s*(?:\*\/\s*)?$/); var dataUriRegexp = new RegExp(/^(data:)([\w\/\+]+)(;charset[=:][\w-]+)?(;base64)?,(.*)/); // From https://github.com/ragingwind/data-uri-regex, modified function getSourceNode(filepath, filename, forceIdentity) { return readFile(filepath, filename).then( srcBuffer => { var srcStr = srcBuffer.toString(); var match; if (!forceIdentity) { /// Run a regexp against the code to check for source mappings. var match = sourceMapRegExp.exec(srcStr); } else { srcStr = srcStr.replace(sourceMapRegExp, ''); } if (!match) { // Create identity sourcemap var realPath = realpathSync(filepath); var realFilename = realpathSync(realPath, filename); var lines = srcStr.split('\n'); var lineCount = lines.length; var identNode = new SourceNode(null, null, null, ''); identNode.setSourceContent(realFilename, srcStr); for (var i=0; i { // console.log('Loaded external sourcemap for ', filename + ": (" + sourcemapFilename + ")"); var parsedMap = new SourceMapConsumer( mapContents.toString() ); return SourceNode.fromStringWithSourceMap( srcStr, parsedMap ); }, err => { console.error(filepath, sourcemapFilename); throw new Error('File ' + filename + ' refers to a non-existing sourcemap at ' + filepath + ' / ' + sourcemapFilename + ' ' + err); }); } } }); } module.exports = getSourceNode;