/
index.js
134 lines (126 loc) · 4.75 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
var Path = require('path'),
fs = require('fs'),
relationLabelByType = {
HtmlScript: '<script>',
HtmlStyle: '<style>',
HtmlCacheManifest: '<html manifest>',
HtmlIFrame: '<iframe>',
HtmlFrame: '<frame>',
HtmlAlternateLink: '<link rel=alternate>',
HtmlConditionalComment: function (htmlConditionalComment) {return '<!--[if ' + htmlConditionalComment.condition + ']>';},
HtmlImage: '<img>',
HtmlAudio: '<audio>',
HtmlShortcutIcon: 'icon',
HtmlVideo: '<video>',
HtmlVideoPoster: '<video poster>',
HtmlEmbed: '<embed>',
HtmlApplet: '<applet>',
HtmlObject: '<object>',
HtmlEdgeSideInclude: '<esi:include>',
HtmlAnchor: '<a>',
HtmlRequireJsMain: 'data-main',
JavaScriptInclude: 'INCLUDE',
JavaScriptGetText: 'GETTEXT',
JavaScriptGetStaticUrl: 'GETSTATICURL',
JavaScriptAmdDefine: 'define',
JavaScriptAmdRequire: 'require',
CssImage: 'background-image',
CssImport: '@import',
CssBehavior: 'behavior',
CssFontFaceSrc: '@font-face src',
CssAlphaImageLoader: 'AlphaImageLoader'
};
module.exports = function (name) {
name = (name || 'assetviz');
var targetFileName = name + '.html',
data = {
assets: [],
relations: []
};
return function drawGraph(assetGraph, cb) {
var idx = 0,
vizGraph = new assetGraph.constructor({ root: Path.normalize(Path.dirname(module.filename) + '/tpl/') }),
query = assetGraph.constructor.query;
assetGraph.findAssets().forEach(function (asset) {
if (asset.url || asset.outgoingRelations.length) {
asset.idx = idx;
var size = 400;
if (asset.url && asset.isLoaded) {
size = asset.rawSrc.length;
}
data.assets.push({
path: asset.url ? Path.relative(assetGraph.root, asset.url) : '',
fileName: (asset.url ? Path.basename(asset.url) : 'i:' + asset).replace(/"/g, '\\"'),
type: asset.type.toLowerCase(),
size: size,
r: 3 + Math.sqrt(size / 100),
outgoing: 0,
initial: asset.isInitial
});
idx += 1;
}
});
assetGraph.findRelations().forEach(function (relation) {
if ('idx' in relation.from && 'idx' in relation.to) {
var typeString = relationLabelByType[relation.type] || '';
if (typeof typeString === 'function') {
typeString = typeString(relation);
}
data.assets[relation.from.idx].outgoing += 1;
data.relations.push({
source: relation.from.idx,
target: relation.to.idx,
type: typeString
});
}
});
// For some reason some data sets break if this is not done. WTF
data.relations.reverse();
var dataString = 'var assetgraph = ' + JSON.stringify(data, undefined, 4);
vizGraph.on('error', function (err) {
console.error((err.asset ? err.asset.urlOrDescription + ': ' : '') + err.stack);
process.exit(1);
})
.loadAssets([
'index.html'
])
.populate({
followRelations: {
type: query.not(['HtmlAnchor'])
}
})
.queue(function injectAssetData(assetGraph) {
assetGraph.findAssets({
type: 'JavaScript',
fileName: 'data.js'
})[0].text = dataString;
})
.inlineRelations({
type: ['CssImage', 'HtmlImage', 'HtmlShortcutIcon', 'HtmlAppleTouchStartupImage']
})
.inlineRelations({
type: ['CssFontFaceSrc']
})
.inlineRelations({
type: ['CssBehavior']
})
/*
.compressJavaScript({type: 'JavaScript'}, 'uglifyJs')
.minifyAssets({
type: ['Html', 'JavaScript', 'Css']
})
*/
// inline style and script
.inlineRelations({ type: ['HtmlStyle', 'HtmlScript'] })
.moveAssets({type: 'Html', isInline: false}, function (asset) {
return "file://" + Path.normalize(targetFileName);
})
.writeAssetsToDisc({url: /^file:/})
.run(function (err) {
if (err) {
console.error(err);
}
});
cb();
};
};