diff --git a/index.js b/index.js index f26c943..a6432f3 100644 --- a/index.js +++ b/index.js @@ -1,23 +1,44 @@ var ngAnnotate = require("ng-annotate"); var loaderUtils = require("loader-utils"); +var SourceMapConsumer = require("source-map").SourceMapConsumer; +var SourceMapGenerator = require("source-map").SourceMapGenerator; function isEmpty(obj) { return Object.keys(obj).length === 0; } +function getOptions() { + var defaults = { add: true }; + var query = loaderUtils.parseQuery(this.query); + var options = isEmpty(query) ? defaults : query; + + if (this.sourceMap && options.sourcemap === undefined) { + options.sourcemap = { inline: false, inFile: this.resourcePath }; + } + + return options; +} + module.exports = function (content, map) { + var that = this; var callback = this.async(); this.cacheable && this.cacheable(); - var query = loaderUtils.parseQuery(this.query); - var options = isEmpty(query) ? { add: true } : query; + var options = getOptions.bind(this)(); var result = ngAnnotate(content, options); if (result.errors) { result.errors.forEach(function(error) { - this.emitError(error); + that.emitError(error); }); } - callback(null, result.src, map); -} + if (this.sourceMap) { + var originalSourcemap = new SourceMapConsumer(map); + var annotatedSourcemap = new SourceMapConsumer(result.map); + var resultingSourcemap = SourceMapGenerator.fromSourceMap(annotatedSourcemap); + resultingSourcemap.applySourceMap(originalSourcemap, this.resourcePath); + } + + callback(null, result.src, resultingSourcemap === undefined ? map : resultingSourcemap.toJSON()); +} \ No newline at end of file diff --git a/package.json b/package.json index f90855c..1f62c78 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,8 @@ "homepage": "https://github.com/larrifax/ng-annotate-loader", "dependencies": { "loader-utils": "^0.2.6", - "ng-annotate": "^0.15.4" + "ng-annotate": "^0.15.4", + "source-map": "^0.4.2" }, "devDependencies": {} }