forked from assetgraph/assetgraph-builder
/
angularPreMinification.js
61 lines (55 loc) · 1.92 KB
/
angularPreMinification.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
var ngmin = require('ngmin'),
seq = require('seq');
module.exports = function () {
return function angularPreMinification(assetGraph, cb) {
var query = assetGraph.query,
htmlAssets = [],
isAngular = false,
ngminRenderer = function (javaScript, cb) {
process.nextTick(function () {
cb(null, new assetGraph.JavaScript({
text: ngmin.annotate(javaScript.text)
}));
});
};
assetGraph.findAssets({
isHtml: true,
isInitial: true,
isInline: false
}).forEach(function (htmlAsset) {
var relationsToAsset = assetGraph.findRelations({
to: htmlAsset,
type: query.not('HtmlAnchor')
});
if (relationsToAsset.length === 0) {
htmlAssets.push(htmlAsset);
}
});
htmlAssets.forEach(function (htmlAsset) {
var document = htmlAsset.parseTree;
if (document.documentElement && document.querySelector('[ng-app]')
|| document.querySelector('[class~="ng-app:"]')) {
isAngular = true;
}
});
if (isAngular) {
console.warn('Angular');
seq(assetGraph.findAssets({type: 'JavaScript'}))
.parEach(function (javaScript) {
ngminRenderer(javaScript, this.into(javaScript.id));
})
.parEach(function (javaScript) {
var newAsset = this.vars[javaScript.id];
newAsset.minify(); // FIXME: This smells
javaScript.replaceWith(newAsset);
this()
})
.seq(function () {
cb();
})
['catch'](cb);
} else {
cb();
}
};
};