forked from mynameiswhm/brotli-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
103 lines (86 loc) · 3.72 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
var async = require('async');
var url = require('url');
var RawSource = require('webpack-sources/lib/RawSource');
function CompressionPlugin(options) {
options = options || {};
const algorithmName = options.algorithm || 'brotli';
this.asset = options.asset || '[path].br[query]';
this.test = options.test || options.regExp;
this.threshold = options.threshold || 0;
this.minRatio = options.minRatio || 0.8;
if (algorithmName === 'brotli') {
const compress = require('./compress.js')();
const brotliOptions = {
mode: options.hasOwnProperty('mode') ? options.mode : 0,
quality: options.hasOwnProperty('quality') ? options.quality : 11,
lgwin: options.hasOwnProperty('lgwin') ? options.lgwin : 22,
lgblock: options.hasOwnProperty('lgblock') ? options.lgblock : 0,
enable_dictionary: options.hasOwnProperty('enable_dictionary') ? options.enable_dictionary : true,
enable_transforms: options.hasOwnProperty('enable_transforms') ? options.enable_transforms : false,
greedy_block_split: options.hasOwnProperty('greedy_block_split') ? options.greedy_block_split : false,
enable_context_modeling: options.hasOwnProperty('enable_context_modeling') ? options.enable_context_modeling : false
};
this.compress = function (content, callback) {
compress(content, brotliOptions, callback);
};
} else {
const zlib = require('zlib');
const algorithm = zlib[algorithmName];
if (!algorithm) {
throw new Error(`Algorithm ${algorithmName} not found in zlib`);
}
const compressionOptions = {
level: options.level || 9,
flush: options.flush,
chunkSize: options.chunkSize,
windowBits: options.windowBits,
memLevel: options.memLevel,
strategy: options.strategy,
dictionary: options.dictionary,
};
this.compress = function(content, callback) {
algorithm(content, compressionOptions, callback);
};
}
}
module.exports = CompressionPlugin;
CompressionPlugin.prototype.apply = function (compiler) {
compiler.plugin('emit', function (compilation, callback) {
var assets = compilation.assets;
async.forEach(Object.keys(assets), function (file, callback) {
if (this.test && !this.test.test(file)) {
return callback();
}
var asset = assets[file];
var content = asset.source();
if (!Buffer.isBuffer(content)) {
content = new Buffer(content, 'utf-8');
}
var originalSize = content.length;
if (originalSize < this.threshold) {
return callback();
}
this.compress(content, function (err, result) {
if (err) {
return callback(err);
}
if (result.length / originalSize > this.minRatio) {
return callback();
}
var parse = url.parse(file);
var sub = {
file: file,
fileWithoutExt: file.split('.').slice(0, -1).join('.'),
ext: file.split('.').slice(-1).join(''),
path: parse.pathname,
query: parse.query || ''
};
var newFile = this.asset.replace(/\[(file|fileWithoutExt|ext|path|query)]/g, function (p0, p1) {
return sub[p1];
});
assets[newFile] = new RawSource(result);
callback();
}.bind(this));
}.bind(this), callback);
}.bind(this));
};