forked from andrey-skl/ng-annotate-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.js
99 lines (81 loc) · 3.2 KB
/
loader.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
var utils = require('loader-utils');
var clone = require('clone');
var SourceMapConsumer = require('source-map').SourceMapConsumer;
var SourceMapGenerator = require('source-map').SourceMapGenerator;
var normalizePath = require('normalize-path');
function loadPlugins(pluginNames) {
pluginNames = pluginNames || [];
return pluginNames.map(function(name) {
return require(name);
});
}
function getOptions(sourceMapEnabled, filename) {
// options object may be re-used across multiple invocations.
var options = clone(utils.getOptions(this) || {});
//"add" should be a default option if not overrided in query
if (options.add === undefined) {
options.add = true;
}
if (options.ngAnnotate === undefined) {
options.ngAnnotate = 'ng-annotate-patched';
}
if (sourceMapEnabled && options.map === undefined) {
options.map = {
inline: false,
inFile: filename,
};
}
if (options.plugin) {
options.plugin = loadPlugins(options.plugin);
}
return options;
}
function mergeSourceMaps(inputSourceMap, annotateMap) {
var outputSourceMap;
var sourceMapEnabled = this.sourceMap;
var filename = normalizePath(this.resourcePath);
this.cacheable && this.cacheable();
if (sourceMapEnabled && !inputSourceMap && annotateMap) {
outputSourceMap = annotateMap;
}
// Using BabelJS as an example,
// https://github.com/babel/babel/blob/d3a73b87e9007104cb4fec343f0cfb9e1c67a4ec/packages/babel/src/transformation/file/index.js#L465
// See also vinyl-sourcemaps-apply (used by gulp-ng-annotate) - https://github.com/floridoo/vinyl-sourcemaps-apply/blob/master/index.js
if (sourceMapEnabled && inputSourceMap) {
inputSourceMap.sourceRoot = '';
inputSourceMap.sources[0] = filename;
if (annotateMap) {
var generator = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(annotateMap));
generator.applySourceMap(new SourceMapConsumer(inputSourceMap), filename);
outputSourceMap = generator.toJSON();
//Should be set to avoid '../../file is not in SourceMap error https://github.com/huston007/ng-annotate-loader/pull/11'
outputSourceMap.sourceRoot = '';
//Copy file name from incoming file because it is empty by some unknown reaon
outputSourceMap.file = normalizePath(this.resourcePath);
} else {
outputSourceMap = inputSourceMap;
}
}
if (typeof outputSourceMap == 'string') {
return JSON.parse(outputSourceMap);
} else {
return outputSourceMap;
}
}
module.exports = function(source, inputSourceMap) {
var sourceMapEnabled = this.sourceMap;
var filename = normalizePath(this.resourcePath);
this.cacheable && this.cacheable();
var options = getOptions.call(this, sourceMapEnabled, filename);
var ngAnnotate = require(options.ngAnnotate);
var annotateResult = ngAnnotate(source, options);
if (annotateResult.errors) {
this.callback(annotateResult.errors);
} else if (annotateResult.src !== source) {
var outputSourceMap = mergeSourceMaps.call(this, inputSourceMap, annotateResult.map);
this.callback(null, annotateResult.src || source, outputSourceMap);
} else {
// if ngAnnotate did nothing, return map and result untouched
this.callback(null, source, inputSourceMap);
}
};