-
Notifications
You must be signed in to change notification settings - Fork 30
/
index.js
67 lines (52 loc) · 2.23 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
var ngAnnotate = require('ng-annotate'),
SourceMapSource = require('webpack-core/lib/SourceMapSource');
function ngAnnotatePlugin(options) {
this.options = options || { add: true, sourceMap: false };
}
ngAnnotatePlugin.prototype.apply = function apply(compiler) {
var options = this.options;
// Skip vendor chunks by default unless options.annotateChunk is provided
var annotateChunk = options.annotateChunk || function(chunk) {
return !chunk.name || !chunk.name.startsWith("vendors~");
};
compiler.hooks.compilation.tap('NgAnnotateWebpackPlugin', function(compilation) {
compilation.hooks.optimizeChunkAssets.tapAsync('NgAnnotateWebpackPlugin', function(chunks, callback) {
var files = [];
function getFilesFromChunk(chunk) {
if (annotateChunk(chunk)) {
files = files.concat(chunk.files);
}
}
function annotateFile(file) {
if (options.sourceMap) {
options.map = {
inFile: file,
sourceRoot: ""
};
}
var value = ngAnnotate(compilation.assets[file].source(), options);
var asset = compilation.assets[file];
if (options.sourceMap && asset.sourceAndMap) {
var sourceAndMap = asset.sourceAndMap();
var map = sourceAndMap.map;
var input = sourceAndMap.source;
} else {
map = asset.map();
}
if (!value.errors) {
if (options.sourceMap && asset.sourceAndMap) {
compilation.assets[file] = new SourceMapSource(value.src, file, JSON.parse(value.map), input, map);
}
else {
compilation.assets[file] = new SourceMapSource(value.src, file, map);
}
}
}
chunks.forEach(getFilesFromChunk);
files = files.concat(compilation.additionalChunkAssets);
files.forEach(annotateFile);
callback();
});
});
};
module.exports = ngAnnotatePlugin;