Skip to content

Commit

Permalink
Fixes source maps when using e.g. ts-loader in front of this loader
Browse files Browse the repository at this point in the history
  • Loading branch information
larrifax committed Mar 15, 2015
1 parent e0c4038 commit 57ded0a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
31 changes: 26 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -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());
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {}
}

0 comments on commit 57ded0a

Please sign in to comment.