-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (35 loc) · 1.37 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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 options = getOptions.bind(this)();
var result = ngAnnotate(content, options);
if (result.errors) {
result.errors.forEach(function(error) {
that.emitError(error);
});
}
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());
}