forked from nickjj/manifest-revision-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.js
90 lines (73 loc) · 2.19 KB
/
format.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
/**
* Format raw webpack stats in a way that suites you.
* @param {object} data - The raw stats returned by webpack.
* @param {object} parsedAssets - List of assets that have already been parsed.
* @returns {object}
*/
var Format = function (data, parsedAssets) {
this.data = data;
this.assets = parsedAssets;
};
/**
* CSS assets will get a Javascript chunk output with it, this function removes
* that and leaves you with just a single chunk.
* @returns {object}
*/
Format.prototype.normalizeChunks = function () {
var output = {};
for (var chunk in this.data.assetsByChunkName) {
var chunkValue = this.data.assetsByChunkName[chunk];
if (typeof chunkValue === 'string') {
output[chunk] = [chunkValue];
}
else {
chunkValue = chunkValue.filter(function(item) {
return item.indexOf('hot-update.js') === -1;
});
output[chunk] = chunkValue;
}
}
return output;
};
/**
* At the end of the day the chunks are assets so combine them into the assets.
* @returns {object}
*/
Format.prototype.mergeChunksIntoAssets = function () {
var output = {};
var assetsByChunkName = this.normalizeChunks();
output.assets = this.assets;
for (var chunk in assetsByChunkName) {
for (var asset in assetsByChunkName[chunk]) {
asset = assetsByChunkName[chunk][asset];
var fileExtension = asset.split('.').slice(-1)[0];
var chunkWithExtension = chunk + '.' + fileExtension;
output.assets[chunkWithExtension] = asset;
}
}
return output;
};
/**
* Return the data back formatted in a general way.
* @returns {object}
*/
Format.prototype.general = function () {
var output = this.mergeChunksIntoAssets();
output.publicPath = this.data.publicPath;
return output;
};
/**
* Return the data back formatted to work with Ruby on Rails.
* @returns {object}
*/
Format.prototype.rails = function () {
var output = this.general();
output.files = {};
for (var asset in this.assets) {
output.files[this.assets[asset]] = {
'logical_path': asset
};
}
return output;
};
module.exports = Format;