-
-
Notifications
You must be signed in to change notification settings - Fork 108
/
index.js
163 lines (139 loc) · 4.38 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
import url from 'url';
import async from 'async';
import RawSource from 'webpack-sources/lib/RawSource';
import ModuleFilenameHelpers from 'webpack/lib/ModuleFilenameHelpers';
import cacache from 'cacache';
import findCacheDir from 'find-cache-dir';
import serialize from 'serialize-javascript';
import pkg from '../package.json';
class CompressionPlugin {
constructor(options = {}) {
const {
asset = '[path].gz[query]',
test,
include,
exclude,
algorithm = 'gzip',
filename = false,
compressionOptions = {},
cache = false,
threshold = 0,
minRatio = 0.8,
deleteOriginalAssets = false,
} = options;
this.options = {
asset,
test,
include,
exclude,
algorithm,
filename,
compressionOptions,
cache,
threshold,
minRatio,
deleteOriginalAssets,
};
if (typeof algorithm === 'string') {
// eslint-disable-next-line global-require
const zlib = require('zlib');
this.options.algorithm = zlib[this.options.algorithm];
if (!this.options.algorithm) {
throw new Error('Algorithm not found in zlib');
}
this.options.compressionOptions = {
level: options.level || 9,
flush: options.flush,
chunkSize: options.chunkSize,
windowBits: options.windowBits,
memLevel: options.memLevel,
strategy: options.strategy,
dictionary: options.dictionary,
};
}
}
apply(compiler) {
compiler.plugin('emit', (compilation, callback) => {
const { cache, threshold, minRatio, asset: assetName, filename, deleteOriginalAssets } = this.options;
const cacheDir = cache === true ? findCacheDir({ name: 'compression-webpack-plugin' }) : cache;
const { assets } = compilation;
// eslint-disable-next-line consistent-return
async.forEach(Object.keys(assets), (file, cb) => {
if (!ModuleFilenameHelpers.matchObject(this.options, file)) {
return cb();
}
const asset = assets[file];
let content = asset.source();
if (!Buffer.isBuffer(content)) {
content = new Buffer(content, 'utf8');
}
const originalSize = content.length;
if (originalSize < threshold) {
return cb();
}
return Promise
.resolve()
.then(() => {
if (cache) {
const cacheKey = serialize({
// Invalidate cache after upgrade `zlib` module (build-in in `nodejs`)
node: process.version,
'compression-webpack-plugin': pkg.version,
'compression-webpack-plugin-options': this.options,
file,
content,
});
return cacache
.get(cacheDir, cacheKey)
.then(
result => result.data,
() => Promise
.resolve()
.then(() => this.compress(content))
.then(
data => cacache.put(cacheDir, cacheKey, data)
.then(() => data),
),
);
}
return this.compress(content);
})
.then((result) => {
if (result.length / originalSize > minRatio) { return cb(); }
const parse = url.parse(file);
const sub = {
file,
path: parse.pathname,
query: parse.query || '',
};
let newAssetName = assetName.replace(/\[(file|path|query)\]/g, (p0, p1) => sub[p1]);
if (typeof filename === 'function') {
newAssetName = filename(newAssetName);
}
assets[newAssetName] = new RawSource(result);
if (deleteOriginalAssets) {
delete assets[file];
}
return cb();
})
.catch(cb);
}, callback);
});
}
compress(content) {
return new Promise((resolve, reject) => {
const { algorithm, compressionOptions } = this.options;
algorithm(content, compressionOptions, (error, result) => {
if (error) {
return reject(error);
}
return resolve(result);
});
});
}
}
export default CompressionPlugin;