From 0e9ef0a63242037ebabb155fc11c8f0d51ea910a Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Wed, 2 Dec 2020 21:35:24 +0300 Subject: [PATCH] refactor: code (#225) --- src/index.js | 140 ++++++++++++++++++++++++++++----------------------- 1 file changed, 78 insertions(+), 62 deletions(-) diff --git a/src/index.js b/src/index.js index 54d6297..5f8a947 100644 --- a/src/index.js +++ b/src/index.js @@ -105,85 +105,101 @@ class CompressionPlugin { async compress(compiler, compilation, assets) { const cache = compilation.getCache("CompressionWebpackPlugin"); const assetsForMinify = await Promise.all( - Object.keys(assets) - .filter((name) => { - const { info } = compilation.getAsset(name); + Object.keys(assets).reduce((accumulator, name) => { + const { info, source } = compilation.getAsset(name); - // Skip double minimize assets from child compilation - if (info.compressed) { - return false; - } + // Skip double minimize assets from child compilation + if (info.compressed) { + return accumulator; + } - if ( - !compiler.webpack.ModuleFilenameHelpers.matchObject.bind( - // eslint-disable-next-line no-undefined - undefined, - this.options - )(name) - ) { - return false; - } + if ( + !compiler.webpack.ModuleFilenameHelpers.matchObject.bind( + // eslint-disable-next-line no-undefined + undefined, + this.options + )(name) + ) { + return accumulator; + } - return true; - }) - .map(async (name) => { - const { info, source } = compilation.getAsset(name); + let input = source.source(); - const eTag = cache.getLazyHashedEtag(source); - const cacheItem = cache.getItemCache( - serialize({ - name, - algorithm: this.options.algorithm, - compressionOptions: this.options.compressionOptions, - }), - eTag - ); - const output = await cacheItem.getPromise(); + if (!Buffer.isBuffer(input)) { + input = Buffer.from(input); + } - return { name, info, inputSource: source, output, cacheItem }; - }) - ); + if (input.length < this.options.threshold) { + return accumulator; + } - const { RawSource } = compiler.webpack.sources; - const scheduledTasks = []; + let relatedName; - for (const asset of assetsForMinify) { - scheduledTasks.push( - (async () => { - const { name, inputSource, cacheItem, info } = asset; - let { output } = asset; + if (typeof this.options.algorithm === "function") { + let filenameForRelatedName = this.options.filename; - let input = inputSource.source(); + const index = filenameForRelatedName.lastIndexOf("?"); - if (!Buffer.isBuffer(input)) { - input = Buffer.from(input); + if (index >= 0) { + filenameForRelatedName = filenameForRelatedName.substr(0, index); } - if (input.length < this.options.threshold) { - return; - } + relatedName = `${path.extname(filenameForRelatedName).slice(1)}ed`; + } else if (this.options.algorithm === "gzip") { + relatedName = "gzipped"; + } else { + relatedName = `${this.options.algorithm}ed`; + } - let relatedName; + if (info.related && info.related[relatedName]) { + return accumulator; + } - if (typeof this.options.algorithm === "function") { - let filenameForRelatedName = this.options.filename; + const eTag = cache.getLazyHashedEtag(source); + const cacheItem = cache.getItemCache( + serialize({ + name, + algorithm: this.options.algorithm, + compressionOptions: this.options.compressionOptions, + }), + eTag + ); - const index = filenameForRelatedName.lastIndexOf("?"); + accumulator.push( + (async () => { + const output = await cacheItem.getPromise(); - if (index >= 0) { - filenameForRelatedName = filenameForRelatedName.substr(0, index); - } + return { + name, + inputSource: source, + info, + input, + output, + cacheItem, + relatedName, + }; + })() + ); - relatedName = `${path.extname(filenameForRelatedName).slice(1)}ed`; - } else if (this.options.algorithm === "gzip") { - relatedName = "gzipped"; - } else { - relatedName = `${this.options.algorithm}ed`; - } + return accumulator; + }, []) + ); - if (info.related && info.related[relatedName]) { - return; - } + const { RawSource } = compiler.webpack.sources; + const scheduledTasks = []; + + for (const asset of assetsForMinify) { + scheduledTasks.push( + (async () => { + const { + name, + inputSource, + input, + cacheItem, + info, + relatedName, + } = asset; + let { output } = asset; if (!output) { try {