forked from mikechau/sri-stats-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
103 lines (87 loc) · 3.13 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
/**
* Script based on @roman01la's webpack-sri.
*
* https://github.com/roman01la/webpack-sri
*
*/
'use strict';
var fs = require('fs');
var path = require('path');
var getSriHash = require('./lib/utils').getSriHash;
var CustomStats = require('webpack-custom-stats-patch');
var DEFAULT_PARAMS = {
algorithm: 'sha384',
allow: (/\.(js|css)$/i),
customStatsKey: 'sris',
assetKey: 'integrity',
saveAs: path.join(process.cwd(), 'build', 'subresource-integrity-map.json'),
write: false,
writeDirectMapping: true,
resultsKey: '__RESULTS_SRIS',
runAfterEmit: 'true'
};
function SriStatsWebpackPlugin(options) {
var params = options || {};
this._algorithm = params.algorithm || DEFAULT_PARAMS.algorithm;
this._allow = params.allow || DEFAULT_PARAMS.allow;
this._customStatsKey = params.customStatsKey || DEFAULT_PARAMS.customStatsKey;
this._assetKey = params.assetKey || DEFAULT_PARAMS.assetKey;
this._saveAs = params.saveAs || DEFAULT_PARAMS.saveAs;
this._write = ((params.write === undefined) ? DEFAULT_PARAMS.write : params.write);
this._writeDirectMapping = ((params.writeDirectMapping === undefined) ? DEFAULT_PARAMS.writeDirectMapping : params.writeDirectMapping);
this._resultsKey = params.resultsKey || DEFAULT_PARAMS.resultsKey;
this._runAfterEmit = ((params.runAfterEmit === undefined) ? DEFAULT_PARAMS.runAfterEmit : params.runAfterEmit);
}
SriStatsWebpackPlugin.prototype.getAlgorithm = function getAlgorithm() {
return this._algorithm;
};
SriStatsWebpackPlugin.prototype.apply = function(compiler) {
var sriAlgorithm = this._algorithm;
var whitelistRegex = this._allow;
var customStatsKey = this._customStatsKey;
var assetKey = this._assetKey;
var savePath = this._saveAs;
var writeEnabled = this._write;
var writeDirectMapping = this._writeDirectMapping;
var resultsKey = this._resultsKey;
var stage = this._runAfterEmit ? 'after-emit' : 'emit';
var directMapping = {};
var sris = {};
compiler.plugin('this-compilation', function(compilation) {
compilation.plugin('optimize-assets', function(assets, callback) {
Object.keys(assets).forEach(function(file) {
var asset = assets[file];
var assetStat = {};
var content;
var integrity;
if (file.match(whitelistRegex)) {
content = asset.source();
integrity = getSriHash(sriAlgorithm, content);
assetStat[assetKey] = integrity;
sris[file] = assetStat;
directMapping[file] = integrity;
}
});
callback();
});
});
compiler.plugin(stage, function(compilation, callback) {
var stats = new CustomStats(compilation);
stats.addCustomStat(customStatsKey, sris);
compilation[resultsKey] = directMapping;
callback();
});
compiler.plugin('done', function(stats) {
var output;
if (writeEnabled) {
output = ((writeDirectMapping) ? stats.compilation[resultsKey] : sris);
fs.writeFile(savePath, JSON.stringify(output, null, ' '), function(err) {
if (err) {
console.error('Failed to write stats.', err);
throw err;
}
});
}
});
};
module.exports = SriStatsWebpackPlugin;