1 | "use strict";
|
2 |
|
3 | const os = require("os");
|
4 |
|
5 | const {
|
6 | validate
|
7 | } = require("schema-utils");
|
8 |
|
9 | const serialize = require("serialize-javascript");
|
10 |
|
11 | const {
|
12 | Worker
|
13 | } = require("jest-worker");
|
14 |
|
15 | const schema = require("./options.json");
|
16 |
|
17 | const {
|
18 | htmlMinifierTerser,
|
19 | throttleAll
|
20 | } = require("./utils");
|
21 |
|
22 | const {
|
23 | minify
|
24 | } = require("./minify");
|
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 | class HtmlMinimizerPlugin {
|
135 | |
136 |
|
137 |
|
138 | constructor(options) {
|
139 | validate(
|
140 |
|
141 | schema, options || {}, {
|
142 | name: "Html Minimizer Plugin",
|
143 | baseDataPath: "options"
|
144 | });
|
145 | const {
|
146 | minify = htmlMinifierTerser,
|
147 | minimizerOptions,
|
148 | parallel = true,
|
149 | test = /\.html(\?.*)?$/i,
|
150 | include,
|
151 | exclude
|
152 | } = options || {};
|
153 |
|
154 |
|
155 | let minimizer;
|
156 |
|
157 | if (Array.isArray(minify)) {
|
158 |
|
159 | minimizer =
|
160 |
|
161 | minify.map(
|
162 | |
163 |
|
164 |
|
165 |
|
166 |
|
167 | (item, i) => {
|
168 | return {
|
169 | implementation: item,
|
170 | options: Array.isArray(minimizerOptions) ? minimizerOptions[i] : minimizerOptions
|
171 | };
|
172 | });
|
173 | } else {
|
174 | minimizer =
|
175 |
|
176 | {
|
177 | implementation: minify,
|
178 | options: minimizerOptions
|
179 | };
|
180 | }
|
181 | |
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | this.options = {
|
188 | test,
|
189 | parallel,
|
190 | include,
|
191 | exclude,
|
192 | minimizer
|
193 | };
|
194 | }
|
195 | |
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 | static buildWarning(warning, file) {
|
204 | |
205 |
|
206 |
|
207 | const builtWarning = new Error(warning instanceof Error ? warning.message : typeof warning.message !== "undefined" ? warning.message : warning.toString());
|
208 | builtWarning.name = "Warning";
|
209 | builtWarning.hideStack = true;
|
210 | builtWarning.file = file;
|
211 | return builtWarning;
|
212 | }
|
213 | |
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 | static buildError(error, file) {
|
222 | |
223 |
|
224 |
|
225 | let builtError;
|
226 |
|
227 | if (typeof error === "string") {
|
228 | builtError = new Error(`${file} from Html Minimizer plugin\n${error}`);
|
229 | builtError.file = file;
|
230 | return builtError;
|
231 | }
|
232 |
|
233 | if (error.stack) {
|
234 |
|
235 | builtError = new Error(`${file} from Html Minimizer plugin\n${typeof error.message !== "undefined" ? error.message : ""}\n${error.stack}`);
|
236 | builtError.file = file;
|
237 | return builtError;
|
238 | }
|
239 |
|
240 | builtError = new Error(`${file} from Html Minimizer plugin\n${error.message}`);
|
241 | builtError.file = file;
|
242 | return builtError;
|
243 | }
|
244 | |
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 | static getAvailableNumberOfCores(parallel) {
|
252 |
|
253 |
|
254 | const cpus = os.cpus() || {
|
255 | length: 1
|
256 | };
|
257 | return parallel === true ? cpus.length - 1 : Math.min(Number(parallel) || 0, cpus.length - 1);
|
258 | }
|
259 | |
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 | async optimize(compiler, compilation, assets, optimizeOptions) {
|
270 | const cache = compilation.getCache("HtmlMinimizerWebpackPlugin");
|
271 | let numberOfAssets = 0;
|
272 | const assetsForMinify = await Promise.all(Object.keys(assets).filter(name => {
|
273 | const {
|
274 | info
|
275 | } =
|
276 |
|
277 | compilation.getAsset(name);
|
278 |
|
279 | if (info.minimized) {
|
280 | return false;
|
281 | }
|
282 |
|
283 | if (!compiler.webpack.ModuleFilenameHelpers.matchObject.bind(
|
284 | undefined, this.options)(name)) {
|
285 | return false;
|
286 | }
|
287 |
|
288 | return true;
|
289 | }).map(async name => {
|
290 | const {
|
291 | info,
|
292 | source
|
293 | } =
|
294 |
|
295 | compilation.getAsset(name);
|
296 | const eTag = cache.getLazyHashedEtag(source);
|
297 | const cacheItem = cache.getItemCache(name, eTag);
|
298 | const output = await cacheItem.getPromise();
|
299 |
|
300 | if (!output) {
|
301 | numberOfAssets += 1;
|
302 | }
|
303 |
|
304 | return {
|
305 | name,
|
306 | info,
|
307 | inputSource: source,
|
308 | output,
|
309 | cacheItem
|
310 | };
|
311 | }));
|
312 |
|
313 | if (assetsForMinify.length === 0) {
|
314 | return;
|
315 | }
|
316 |
|
317 |
|
318 |
|
319 | let getWorker;
|
320 |
|
321 |
|
322 | let initializedWorker;
|
323 |
|
324 |
|
325 | let numberOfWorkers;
|
326 |
|
327 | if (optimizeOptions.availableNumberOfCores > 0) {
|
328 |
|
329 | numberOfWorkers = Math.min(numberOfAssets, optimizeOptions.availableNumberOfCores);
|
330 |
|
331 | getWorker = () => {
|
332 | if (initializedWorker) {
|
333 | return initializedWorker;
|
334 | }
|
335 |
|
336 | initializedWorker =
|
337 |
|
338 | new Worker(require.resolve("./minify"), {
|
339 | numWorkers: numberOfWorkers,
|
340 | enableWorkerThreads: true
|
341 | });
|
342 |
|
343 | const workerStdout = initializedWorker.getStdout();
|
344 |
|
345 | if (workerStdout) {
|
346 | workerStdout.on("data", chunk => process.stdout.write(chunk));
|
347 | }
|
348 |
|
349 | const workerStderr = initializedWorker.getStderr();
|
350 |
|
351 | if (workerStderr) {
|
352 | workerStderr.on("data", chunk => process.stderr.write(chunk));
|
353 | }
|
354 |
|
355 | return initializedWorker;
|
356 | };
|
357 | }
|
358 |
|
359 | const {
|
360 | RawSource
|
361 | } = compiler.webpack.sources;
|
362 | const scheduledTasks = [];
|
363 |
|
364 | for (const asset of assetsForMinify) {
|
365 | scheduledTasks.push(async () => {
|
366 | const {
|
367 | name,
|
368 | inputSource,
|
369 | cacheItem
|
370 | } = asset;
|
371 | let {
|
372 | output
|
373 | } = asset;
|
374 | let input;
|
375 | const sourceFromInputSource = inputSource.source();
|
376 |
|
377 | if (!output) {
|
378 | input = sourceFromInputSource;
|
379 |
|
380 | if (Buffer.isBuffer(input)) {
|
381 | input = input.toString();
|
382 | }
|
383 | |
384 |
|
385 |
|
386 |
|
387 |
|
388 | const options = {
|
389 | name,
|
390 | input,
|
391 | minimizer: this.options.minimizer
|
392 | };
|
393 |
|
394 | try {
|
395 | output = await (getWorker ? getWorker().transform(serialize(options)) : minify(options));
|
396 | } catch (error) {
|
397 | compilation.errors.push(
|
398 |
|
399 | HtmlMinimizerPlugin.buildError(error, name));
|
400 | return;
|
401 | }
|
402 |
|
403 | output.source = new RawSource(output.code);
|
404 | await cacheItem.storePromise({
|
405 | source: output.source,
|
406 | errors: output.errors,
|
407 | warnings: output.warnings
|
408 | });
|
409 | }
|
410 |
|
411 | const newInfo = {
|
412 | minimized: true
|
413 | };
|
414 |
|
415 | if (output.warnings && output.warnings.length > 0) {
|
416 | for (const warning of output.warnings) {
|
417 | compilation.warnings.push(
|
418 |
|
419 | HtmlMinimizerPlugin.buildWarning(warning, name));
|
420 | }
|
421 | }
|
422 |
|
423 | if (output.errors && output.errors.length > 0) {
|
424 | for (const error of output.errors) {
|
425 | compilation.errors.push(
|
426 |
|
427 | HtmlMinimizerPlugin.buildError(error, name));
|
428 | }
|
429 | }
|
430 |
|
431 | compilation.updateAsset(name, output.source, newInfo);
|
432 | });
|
433 | }
|
434 |
|
435 | const limit = getWorker && numberOfAssets > 0 ?
|
436 |
|
437 | numberOfWorkers : scheduledTasks.length;
|
438 | await throttleAll(limit, scheduledTasks);
|
439 |
|
440 | if (initializedWorker) {
|
441 | await initializedWorker.end();
|
442 | }
|
443 | }
|
444 | |
445 |
|
446 |
|
447 |
|
448 |
|
449 |
|
450 | apply(compiler) {
|
451 | const pluginName = this.constructor.name;
|
452 | const availableNumberOfCores = HtmlMinimizerPlugin.getAvailableNumberOfCores(this.options.parallel);
|
453 | compiler.hooks.compilation.tap(pluginName, compilation => {
|
454 | compilation.hooks.processAssets.tapPromise({
|
455 | name: pluginName,
|
456 | stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
|
457 | additionalAssets: true
|
458 | }, assets => this.optimize(compiler, compilation, assets, {
|
459 | availableNumberOfCores
|
460 | }));
|
461 | compilation.hooks.statsPrinter.tap(pluginName, stats => {
|
462 | stats.hooks.print.for("asset.info.minimized").tap("html-minimizer-webpack-plugin", (minimized, {
|
463 | green,
|
464 | formatFlag
|
465 | }) => minimized ?
|
466 |
|
467 | green(
|
468 |
|
469 | formatFlag("minimized")) : "");
|
470 | });
|
471 | });
|
472 | }
|
473 |
|
474 | }
|
475 |
|
476 | HtmlMinimizerPlugin.htmlMinifierTerser = htmlMinifierTerser;
|
477 | module.exports = HtmlMinimizerPlugin; |
\ | No newline at end of file |