-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes source maps when using e.g. ts-loader in front of this loader
- Loading branch information
Showing
2 changed files
with
28 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters