diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ab786e..610ff0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +### [5.0.2](https://github.com/webpack-contrib/compression-webpack-plugin/compare/v5.0.1...v5.0.2) (2020-09-02) + + +### Bug Fixes + +* do not crash when the `algorithm` option return non `Buffer` ([#190](https://github.com/webpack-contrib/compression-webpack-plugin/issues/190)) ([81bf601](https://github.com/webpack-contrib/compression-webpack-plugin/commit/81bf60166caeeea9f5d5a18e1471ed0f7a28c312)) + ### [5.0.1](https://github.com/webpack-contrib/compression-webpack-plugin/compare/v5.0.0...v5.0.1) (2020-08-22) diff --git a/package-lock.json b/package-lock.json index 0c1e619..e860bcc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "compression-webpack-plugin", - "version": "5.0.1", + "version": "5.0.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 72934e1..b5cb54a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "compression-webpack-plugin", - "version": "5.0.1", + "version": "5.0.2", "description": "Prepare compressed versions of assets to serve them with Content-Encoding", "license": "MIT", "repository": "webpack-contrib/compression-webpack-plugin", diff --git a/src/Webpack4Cache.js b/src/Webpack4Cache.js index 144b0ca..e53831f 100644 --- a/src/Webpack4Cache.js +++ b/src/Webpack4Cache.js @@ -17,8 +17,8 @@ export default class Webpack4Cache { return findCacheDir({ name: 'compression-webpack-plugin' }) || os.tmpdir(); } - async get(task, sources) { - const weakOutput = this.weakCache.get(task.assetSource); + async get(cacheData, sources) { + const weakOutput = this.weakCache.get(cacheData.source); if (weakOutput) { return weakOutput; @@ -30,12 +30,13 @@ export default class Webpack4Cache { } // eslint-disable-next-line no-param-reassign - task.cacheIdent = task.cacheIdent || serialize(task.cacheKeys); + cacheData.cacheIdent = + cacheData.cacheIdent || serialize(cacheData.cacheKeys); let cachedResult; try { - cachedResult = await cacache.get(this.cache, task.cacheIdent); + cachedResult = await cacache.get(this.cache, cacheData.cacheIdent); } catch (ignoreError) { // eslint-disable-next-line no-undefined return undefined; @@ -46,9 +47,9 @@ export default class Webpack4Cache { ); } - async store(task) { - if (!this.weakCache.has(task.assetSource)) { - this.weakCache.set(task.assetSource, task.output); + async store(cacheData) { + if (!this.weakCache.has(cacheData.source)) { + this.weakCache.set(cacheData.source, cacheData.output); } if (!this.cache) { @@ -56,7 +57,7 @@ export default class Webpack4Cache { return undefined; } - const { cacheIdent, output } = task; + const { cacheIdent, output } = cacheData; return cacache.put(this.cache, cacheIdent, JSON.stringify(output.source())); } diff --git a/src/Webpack5Cache.js b/src/Webpack5Cache.js index 2bbcb89..2b9d32a 100644 --- a/src/Webpack5Cache.js +++ b/src/Webpack5Cache.js @@ -3,14 +3,19 @@ export default class Cache { this.cache = compilation.getCache('CompressionWebpackPlugin'); } - async get(task) { + async get(cacheData) { // eslint-disable-next-line no-param-reassign - task.eTag = task.eTag || this.cache.getLazyHashedEtag(task.assetSource); + cacheData.eTag = + cacheData.eTag || this.cache.getLazyHashedEtag(cacheData.source); - return this.cache.getPromise(task.assetName, task.eTag); + return this.cache.getPromise(cacheData.assetName, cacheData.eTag); } - async store(task) { - return this.cache.storePromise(task.assetName, task.eTag, task.output); + async store(cacheData) { + return this.cache.storePromise( + cacheData.assetName, + cacheData.eTag, + cacheData.output + ); } } diff --git a/src/index.js b/src/index.js index dc5b66e..c20a2c9 100644 --- a/src/index.js +++ b/src/index.js @@ -137,7 +137,7 @@ class CompressionPlugin { delete compilation.assets[name]; } - compress(input) { + runCompressionAlgorithm(input) { return new Promise((resolve, reject) => { const { algorithm, compressionOptions } = this; @@ -146,133 +146,131 @@ class CompressionPlugin { return reject(error); } + if (!Buffer.isBuffer(result)) { + // eslint-disable-next-line no-param-reassign + result = Buffer.from(result); + } + return resolve(result); }); }); } - *getTask(compilation, assetName) { - const { source: assetSource, info: assetInfo } = CompressionPlugin.getAsset( - compilation, - assetName + async compress(compilation, assets, CacheEngine, weakCache) { + const assetNames = Object.keys( + typeof assets === 'undefined' ? compilation.assets : assets + ).filter((assetName) => + // eslint-disable-next-line no-undefined + ModuleFilenameHelpers.matchObject.bind(undefined, this.options)(assetName) ); - if (assetInfo.compressed) { - yield false; - } - - let relatedName; - - if (typeof this.options.algorithm === 'function') { - let filenameForRelatedName = this.options.filename; - - const index = filenameForRelatedName.lastIndexOf('?'); - - if (index >= 0) { - filenameForRelatedName = filenameForRelatedName.substr(0, index); - } - - relatedName = `${path.extname(filenameForRelatedName).slice(1)}ed`; - } else { - relatedName = `${this.options.algorithm}ed`; - } - - if (assetInfo.related && assetInfo.related[relatedName]) { - yield false; + if (assetNames.length === 0) { + return Promise.resolve(); } - let input = assetSource.source(); - - if (!Buffer.isBuffer(input)) { - input = Buffer.from(input); - } + const scheduledTasks = []; + const cache = new CacheEngine( + compilation, + { cache: this.options.cache }, + weakCache + ); - if (input.length < this.options.threshold) { - yield false; - } + for (const assetName of assetNames) { + scheduledTasks.push( + (async () => { + const { source, info } = CompressionPlugin.getAsset( + compilation, + assetName + ); - const task = { assetName, assetSource, assetInfo, input, relatedName }; + if (info.compressed) { + return; + } - if (CompressionPlugin.isWebpack4()) { - task.cacheKeys = { - nodeVersion: process.version, - // eslint-disable-next-line global-require - 'compression-webpack-plugin': require('../package.json').version, - algorithm: this.algorithm, - originalAlgorithm: this.options.algorithm, - compressionOptions: this.compressionOptions, - assetName, - contentHash: crypto.createHash('md4').update(input).digest('hex'), - }; - } + let relatedName; - yield task; - } + if (typeof this.options.algorithm === 'function') { + let filenameForRelatedName = this.options.filename; - afterTask(compilation, task) { - const { output, input } = task; + const index = filenameForRelatedName.lastIndexOf('?'); - if (output.source().length / input.length > this.options.minRatio) { - return; - } + if (index >= 0) { + filenameForRelatedName = filenameForRelatedName.substr(0, index); + } - const { assetSource, assetName } = task; - const newAssetName = CompressionPlugin.interpolateName( - assetName, - this.options.filename - ); + relatedName = `${path.extname(filenameForRelatedName).slice(1)}ed`; + } else { + relatedName = `${this.options.algorithm}ed`; + } - CompressionPlugin.emitAsset(compilation, newAssetName, output, { - compressed: true, - }); + if (info.related && info.related[relatedName]) { + return; + } - if (this.options.deleteOriginalAssets) { - // eslint-disable-next-line no-param-reassign - CompressionPlugin.deleteAsset(compilation, assetName); - } else { - CompressionPlugin.updateAsset(compilation, assetName, assetSource, { - related: { [task.relatedName]: newAssetName }, - }); - } - } + let input = source.source(); - async runTasks(compilation, assetNames, CacheEngine, weakCache) { - const scheduledTasks = []; - const cache = new CacheEngine( - compilation, - { - cache: this.options.cache, - }, - weakCache - ); + if (!Buffer.isBuffer(input)) { + input = Buffer.from(input); + } - for (const assetName of assetNames) { - scheduledTasks.push( - (async () => { - const task = this.getTask(compilation, assetName).next().value; + if (input.length < this.options.threshold) { + return; + } - if (!task) { - return Promise.resolve(); + const cacheData = { source }; + + if (CompressionPlugin.isWebpack4()) { + cacheData.cacheKeys = { + nodeVersion: process.version, + // eslint-disable-next-line global-require + 'compression-webpack-plugin': require('../package.json').version, + algorithm: this.algorithm, + originalAlgorithm: this.options.algorithm, + compressionOptions: this.compressionOptions, + assetName, + contentHash: crypto.createHash('md4').update(input).digest('hex'), + }; + } else { + cacheData.assetName = assetName; } - task.output = await cache.get(task, { RawSource }); + let output = await cache.get(cacheData, { RawSource }); - if (!task.output) { + if (!output) { try { - // eslint-disable-next-line no-param-reassign - task.output = new RawSource(await this.compress(task.input)); + output = new RawSource(await this.runCompressionAlgorithm(input)); } catch (error) { compilation.errors.push(error); - return Promise.resolve(); + return; } - await cache.store(task); + cacheData.output = output; + + await cache.store(cacheData); } - this.afterTask(compilation, task); + if (output.source().length / input.length > this.options.minRatio) { + return; + } - return Promise.resolve(); + const newAssetName = CompressionPlugin.interpolateName( + assetName, + this.options.filename + ); + + CompressionPlugin.emitAsset(compilation, newAssetName, output, { + compressed: true, + }); + + if (this.options.deleteOriginalAssets) { + // eslint-disable-next-line no-param-reassign + CompressionPlugin.deleteAsset(compilation, assetName); + } else { + CompressionPlugin.updateAsset(compilation, assetName, source, { + related: { [relatedName]: newAssetName }, + }); + } })() ); } @@ -286,29 +284,6 @@ class CompressionPlugin { apply(compiler) { const pluginName = this.constructor.name; - const matchObject = ModuleFilenameHelpers.matchObject.bind( - // eslint-disable-next-line no-undefined - undefined, - this.options - ); - const compressionFn = async ( - compilation, - assets, - CacheEngine, - weakCache - ) => { - const assetNames = Object.keys( - typeof assets === 'undefined' ? compilation.assets : assets - ).filter((assetName) => matchObject(assetName)); - - if (assetNames.length === 0) { - return Promise.resolve(); - } - - await this.runTasks(compilation, assetNames, CacheEngine, weakCache); - - return Promise.resolve(); - }; if (CompressionPlugin.isWebpack4()) { // eslint-disable-next-line global-require @@ -317,7 +292,7 @@ class CompressionPlugin { compiler.hooks.emit.tapPromise({ name: pluginName }, (compilation) => // eslint-disable-next-line no-undefined - compressionFn(compilation, undefined, CacheEngine, weakCache) + this.compress(compilation, undefined, CacheEngine, weakCache) ); } else { // eslint-disable-next-line global-require @@ -332,7 +307,7 @@ class CompressionPlugin { name: pluginName, stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_TRANSFER, }, - (assets) => compressionFn(compilation, assets, CacheEngine) + (assets) => this.compress(compilation, assets, CacheEngine) ); compilation.hooks.statsPrinter.tap(pluginName, (stats) => { @@ -340,9 +315,9 @@ class CompressionPlugin { .for('asset.info.compressed') .tap( 'compression-webpack-plugin', - (minimized, { green, formatFlag }) => + (compressed, { green, formatFlag }) => // eslint-disable-next-line no-undefined - minimized ? green(formatFlag('compressed')) : undefined + compressed ? green(formatFlag('compressed')) : undefined ); }); }); diff --git a/test/CompressionPlugin.test.js b/test/CompressionPlugin.test.js index 706a937..9258b39 100644 --- a/test/CompressionPlugin.test.js +++ b/test/CompressionPlugin.test.js @@ -172,7 +172,7 @@ describe('CompressionPlugin', () => { expect(getErrors(stats)).toMatchSnapshot('warnings'); }); - it('should work in watch mode', async () => { + it('should work and use weak cache', async () => { const compiler = getCompiler('./entry.js'); new CompressionPlugin().apply(compiler); @@ -214,7 +214,7 @@ describe('CompressionPlugin', () => { }); }); - it('should work in watch mode when "cache" is "false"', async () => { + it('should work and use weak cache when "cache" is "false"', async () => { if (getCompiler.isWebpack4()) { expect(true).toBe(true); } else { @@ -243,4 +243,32 @@ describe('CompressionPlugin', () => { }); } }); + + it('should work and show compress assets in stats', async () => { + const compiler = getCompiler( + './entry.js', + {}, + { + stats: 'verbose', + output: { + path: `${__dirname}/dist`, + filename: '[name].js', + chunkFilename: '[id].[name].js', + }, + } + ); + + new CompressionPlugin().apply(compiler); + + const stats = await compile(compiler); + const stringStats = stats.toString({ relatedAssets: true }); + const printedCompressed = stringStats.match(/\[compressed]/g); + + expect(printedCompressed ? printedCompressed.length : 0).toBe( + getCompiler.isWebpack4() ? 0 : 3 + ); + expect(getAssetsNameAndSize(stats)).toMatchSnapshot('assets'); + expect(getWarnings(stats)).toMatchSnapshot('errors'); + expect(getErrors(stats)).toMatchSnapshot('warnings'); + }); }); diff --git a/test/__snapshots__/CompressionPlugin.test.js.snap.webpack4 b/test/__snapshots__/CompressionPlugin.test.js.snap.webpack4 index dd586a7..f731d33 100644 --- a/test/__snapshots__/CompressionPlugin.test.js.snap.webpack4 +++ b/test/__snapshots__/CompressionPlugin.test.js.snap.webpack4 @@ -1,87 +1,43 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`CompressionPlugin should work child compilations: assets 1`] = ` +exports[`CompressionPlugin should work and show compress assets in stats: assets 1`] = ` Array [ Array [ "09a1a1112c577c2794359715edfcb5ac.png", 78117, - Object {}, ], Array [ "23fc1d3ac606d117e05a140e0de79806.svg", 672, - Object { - "related": Object { - "gziped": "23fc1d3ac606d117e05a140e0de79806.svg.gz", - }, - }, ], Array [ "23fc1d3ac606d117e05a140e0de79806.svg.gz", 393, - Object { - "compressed": true, - }, - ], - Array [ - "async.async.js.gz", - 171, - Object { - "compressed": true, - }, ], Array [ - "async.async.js?ver=810493800e70254b803c", + "async.async.js", 249, - Object { - "immutable": true, - "related": Object { - "gziped": "async.async.js.gz", - }, - }, ], Array [ - "copied.js", - 400, - Object { - "copied": true, - "related": Object { - "gziped": "copied.js.gz", - }, - }, + "async.async.js.gz", + 171, ], Array [ - "copied.js.gz", - 29, - Object { - "compressed": true, - }, + "main.js", + 11595, ], Array [ "main.js.gz", - 3001, - Object { - "compressed": true, - }, - ], - Array [ - "main.js?var=810493800e70254b803c", - 11627, - Object { - "immutable": true, - "related": Object { - "gziped": "main.js.gz", - }, - }, + 2974, ], ] `; -exports[`CompressionPlugin should work child compilations: errors 1`] = `Array []`; +exports[`CompressionPlugin should work and show compress assets in stats: errors 1`] = `Array []`; -exports[`CompressionPlugin should work child compilations: warnings 1`] = `Array []`; +exports[`CompressionPlugin should work and show compress assets in stats: warnings 1`] = `Array []`; -exports[`CompressionPlugin should work in watch mode: assets 1`] = ` +exports[`CompressionPlugin should work and use weak cache: assets 1`] = ` Array [ Array [ "09a1a1112c577c2794359715edfcb5ac.png", @@ -114,7 +70,7 @@ Array [ ] `; -exports[`CompressionPlugin should work in watch mode: assets 2`] = ` +exports[`CompressionPlugin should work and use weak cache: assets 2`] = ` Array [ Array [ "09a1a1112c577c2794359715edfcb5ac.png", @@ -147,13 +103,94 @@ Array [ ] `; -exports[`CompressionPlugin should work in watch mode: errors 1`] = `Array []`; +exports[`CompressionPlugin should work and use weak cache: errors 1`] = `Array []`; + +exports[`CompressionPlugin should work and use weak cache: errors 2`] = `Array []`; -exports[`CompressionPlugin should work in watch mode: errors 2`] = `Array []`; +exports[`CompressionPlugin should work and use weak cache: warnings 1`] = `Array []`; + +exports[`CompressionPlugin should work and use weak cache: warnings 2`] = `Array []`; + +exports[`CompressionPlugin should work child compilations: assets 1`] = ` +Array [ + Array [ + "09a1a1112c577c2794359715edfcb5ac.png", + 78117, + Object {}, + ], + Array [ + "23fc1d3ac606d117e05a140e0de79806.svg", + 672, + Object { + "related": Object { + "gziped": "23fc1d3ac606d117e05a140e0de79806.svg.gz", + }, + }, + ], + Array [ + "23fc1d3ac606d117e05a140e0de79806.svg.gz", + 393, + Object { + "compressed": true, + }, + ], + Array [ + "async.async.js.gz", + 171, + Object { + "compressed": true, + }, + ], + Array [ + "async.async.js?ver=810493800e70254b803c", + 249, + Object { + "immutable": true, + "related": Object { + "gziped": "async.async.js.gz", + }, + }, + ], + Array [ + "copied.js", + 400, + Object { + "copied": true, + "related": Object { + "gziped": "copied.js.gz", + }, + }, + ], + Array [ + "copied.js.gz", + 29, + Object { + "compressed": true, + }, + ], + Array [ + "main.js.gz", + 3001, + Object { + "compressed": true, + }, + ], + Array [ + "main.js?var=810493800e70254b803c", + 11627, + Object { + "immutable": true, + "related": Object { + "gziped": "main.js.gz", + }, + }, + ], +] +`; -exports[`CompressionPlugin should work in watch mode: warnings 1`] = `Array []`; +exports[`CompressionPlugin should work child compilations: errors 1`] = `Array []`; -exports[`CompressionPlugin should work in watch mode: warnings 2`] = `Array []`; +exports[`CompressionPlugin should work child compilations: warnings 1`] = `Array []`; exports[`CompressionPlugin should work with assets info: assets 1`] = ` Array [ diff --git a/test/__snapshots__/CompressionPlugin.test.js.snap.webpack5 b/test/__snapshots__/CompressionPlugin.test.js.snap.webpack5 index 97924e7..9e1271a 100644 --- a/test/__snapshots__/CompressionPlugin.test.js.snap.webpack5 +++ b/test/__snapshots__/CompressionPlugin.test.js.snap.webpack5 @@ -1,99 +1,43 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`CompressionPlugin should work child compilations: assets 1`] = ` +exports[`CompressionPlugin should work and show compress assets in stats: assets 1`] = ` Array [ Array [ "09a1a1112c577c2794359715edfcb5ac.png", 78117, - Object { - "size": 78117, - }, ], Array [ "23fc1d3ac606d117e05a140e0de79806.svg", 672, - Object { - "related": Object { - "gziped": "23fc1d3ac606d117e05a140e0de79806.svg.gz", - }, - "size": 672, - }, ], Array [ "23fc1d3ac606d117e05a140e0de79806.svg.gz", 393, - Object { - "compressed": true, - "size": 393, - }, - ], - Array [ - "async.async.js.gz", - 181, - Object { - "compressed": true, - "size": 181, - }, ], Array [ - "async.async.js?ver=73440247c3513e8f18d5", - 269, - Object { - "fullhash": "73440247c3513e8f18d5", - "immutable": true, - "related": Object { - "gziped": "async.async.js.gz", - }, - "size": 269, - }, + "async.async.js", + 265, ], Array [ - "copied.js", - 400, - Object { - "copied": true, - "related": Object { - "gziped": "copied.js.gz", - }, - "size": 400, - }, + "async.async.js.gz", + 179, ], Array [ - "copied.js.gz", - 29, - Object { - "compressed": true, - "size": 29, - }, + "main.js", + 15026, ], Array [ "main.js.gz", - 3584, - Object { - "compressed": true, - "size": 3584, - }, - ], - Array [ - "main.js?var=73440247c3513e8f18d5", - 15312, - Object { - "fullhash": "73440247c3513e8f18d5", - "immutable": true, - "related": Object { - "gziped": "main.js.gz", - }, - "size": 15312, - }, + 3515, ], ] `; -exports[`CompressionPlugin should work child compilations: errors 1`] = `Array []`; +exports[`CompressionPlugin should work and show compress assets in stats: errors 1`] = `Array []`; -exports[`CompressionPlugin should work child compilations: warnings 1`] = `Array []`; +exports[`CompressionPlugin should work and show compress assets in stats: warnings 1`] = `Array []`; -exports[`CompressionPlugin should work in watch mode when "cache" is "false": assets 1`] = ` +exports[`CompressionPlugin should work and use weak cache when "cache" is "false": assets 1`] = ` Array [ Array [ "09a1a1112c577c2794359715edfcb5ac.png", @@ -108,25 +52,25 @@ Array [ 393, ], Array [ - "async.async.81ea6697ed82fa2176bb.js", - 269, + "async.async.1c2911c7c819fc826fb7.js", + 265, ], Array [ - "async.async.81ea6697ed82fa2176bb.js.gz", - 181, + "async.async.1c2911c7c819fc826fb7.js.gz", + 179, ], Array [ - "main.f1e836b35a2623bafe34.js", - 15156, + "main.c14d5dcb1534759d1d95.js", + 15057, ], Array [ - "main.f1e836b35a2623bafe34.js.gz", - 3556, + "main.c14d5dcb1534759d1d95.js.gz", + 3537, ], ] `; -exports[`CompressionPlugin should work in watch mode when "cache" is "false": assets 2`] = ` +exports[`CompressionPlugin should work and use weak cache when "cache" is "false": assets 2`] = ` Array [ Array [ "09a1a1112c577c2794359715edfcb5ac.png", @@ -141,33 +85,33 @@ Array [ 393, ], Array [ - "async.async.81ea6697ed82fa2176bb.js", - 269, + "async.async.1c2911c7c819fc826fb7.js", + 265, ], Array [ - "async.async.81ea6697ed82fa2176bb.js.gz", - 181, + "async.async.1c2911c7c819fc826fb7.js.gz", + 179, ], Array [ - "main.f1e836b35a2623bafe34.js", - 15156, + "main.c14d5dcb1534759d1d95.js", + 15057, ], Array [ - "main.f1e836b35a2623bafe34.js.gz", - 3556, + "main.c14d5dcb1534759d1d95.js.gz", + 3537, ], ] `; -exports[`CompressionPlugin should work in watch mode when "cache" is "false": errors 1`] = `Array []`; +exports[`CompressionPlugin should work and use weak cache when "cache" is "false": errors 1`] = `Array []`; -exports[`CompressionPlugin should work in watch mode when "cache" is "false": errors 2`] = `Array []`; +exports[`CompressionPlugin should work and use weak cache when "cache" is "false": errors 2`] = `Array []`; -exports[`CompressionPlugin should work in watch mode when "cache" is "false": warnings 1`] = `Array []`; +exports[`CompressionPlugin should work and use weak cache when "cache" is "false": warnings 1`] = `Array []`; -exports[`CompressionPlugin should work in watch mode when "cache" is "false": warnings 2`] = `Array []`; +exports[`CompressionPlugin should work and use weak cache when "cache" is "false": warnings 2`] = `Array []`; -exports[`CompressionPlugin should work in watch mode: assets 1`] = ` +exports[`CompressionPlugin should work and use weak cache: assets 1`] = ` Array [ Array [ "09a1a1112c577c2794359715edfcb5ac.png", @@ -182,25 +126,25 @@ Array [ 393, ], Array [ - "async.async.81ea6697ed82fa2176bb.js", - 269, + "async.async.1c2911c7c819fc826fb7.js", + 265, ], Array [ - "async.async.81ea6697ed82fa2176bb.js.gz", - 181, + "async.async.1c2911c7c819fc826fb7.js.gz", + 179, ], Array [ - "main.f1e836b35a2623bafe34.js", - 15156, + "main.c14d5dcb1534759d1d95.js", + 15057, ], Array [ - "main.f1e836b35a2623bafe34.js.gz", - 3556, + "main.c14d5dcb1534759d1d95.js.gz", + 3537, ], ] `; -exports[`CompressionPlugin should work in watch mode: assets 2`] = ` +exports[`CompressionPlugin should work and use weak cache: assets 2`] = ` Array [ Array [ "09a1a1112c577c2794359715edfcb5ac.png", @@ -215,31 +159,124 @@ Array [ 393, ], Array [ - "async.async.81ea6697ed82fa2176bb.js", - 269, + "async.async.1c2911c7c819fc826fb7.js", + 265, ], Array [ - "async.async.81ea6697ed82fa2176bb.js.gz", - 181, + "async.async.1c2911c7c819fc826fb7.js.gz", + 179, ], Array [ - "main.f1e836b35a2623bafe34.js", - 15156, + "main.c14d5dcb1534759d1d95.js", + 15057, ], Array [ - "main.f1e836b35a2623bafe34.js.gz", - 3556, + "main.c14d5dcb1534759d1d95.js.gz", + 3537, ], ] `; -exports[`CompressionPlugin should work in watch mode: errors 1`] = `Array []`; +exports[`CompressionPlugin should work and use weak cache: errors 1`] = `Array []`; + +exports[`CompressionPlugin should work and use weak cache: errors 2`] = `Array []`; -exports[`CompressionPlugin should work in watch mode: errors 2`] = `Array []`; +exports[`CompressionPlugin should work and use weak cache: warnings 1`] = `Array []`; + +exports[`CompressionPlugin should work and use weak cache: warnings 2`] = `Array []`; + +exports[`CompressionPlugin should work child compilations: assets 1`] = ` +Array [ + Array [ + "09a1a1112c577c2794359715edfcb5ac.png", + 78117, + Object { + "size": 78117, + }, + ], + Array [ + "23fc1d3ac606d117e05a140e0de79806.svg", + 672, + Object { + "related": Object { + "gziped": "23fc1d3ac606d117e05a140e0de79806.svg.gz", + }, + "size": 672, + }, + ], + Array [ + "23fc1d3ac606d117e05a140e0de79806.svg.gz", + 393, + Object { + "compressed": true, + "size": 393, + }, + ], + Array [ + "async.async.js.gz", + 179, + Object { + "compressed": true, + "size": 179, + }, + ], + Array [ + "async.async.js?ver=3993f67ababcffbe6f05", + 265, + Object { + "fullhash": "3993f67ababcffbe6f05", + "immutable": true, + "related": Object { + "gziped": "async.async.js.gz", + }, + "size": 265, + }, + ], + Array [ + "copied.js", + 400, + Object { + "copied": true, + "related": Object { + "gziped": "copied.js.gz", + }, + "size": 400, + }, + ], + Array [ + "copied.js.gz", + 29, + Object { + "compressed": true, + "size": 29, + }, + ], + Array [ + "main.js.gz", + 3558, + Object { + "compressed": true, + "size": 3558, + }, + ], + Array [ + "main.js?var=3993f67ababcffbe6f05", + 15213, + Object { + "fullhash": "3993f67ababcffbe6f05", + "immutable": true, + "related": Object { + "gziped": "main.js.gz", + }, + "size": 15213, + }, + ], +] +`; -exports[`CompressionPlugin should work in watch mode: warnings 1`] = `Array []`; +exports[`CompressionPlugin should work child compilations: errors 1`] = `Array []`; -exports[`CompressionPlugin should work in watch mode: warnings 2`] = `Array []`; +exports[`CompressionPlugin should work child compilations: warnings 1`] = `Array []`; exports[`CompressionPlugin should work with assets info: assets 1`] = ` Array [ @@ -277,7 +314,7 @@ Array [ }, ], Array [ - "async.async.js.map?ver=8cdf224bcea94552590b", + "async.async.js.map?ver=18beec7527af02981a38", 116, Object { "development": true, @@ -285,16 +322,16 @@ Array [ }, ], Array [ - "async.async.js?ver=8cdf224bcea94552590b", - 334, + "async.async.js?ver=18beec7527af02981a38", + 330, Object { - "fullhash": "8cdf224bcea94552590b", + "fullhash": "18beec7527af02981a38", "immutable": true, "related": Object { "gziped": "async.async.js.gz", - "sourceMap": "async.async.js.map?ver=8cdf224bcea94552590b", + "sourceMap": "async.async.js.map?ver=18beec7527af02981a38", }, - "size": 334, + "size": 330, }, ], Array [ @@ -318,42 +355,42 @@ Array [ ], Array [ "main.js.gz", - 3618, + 3594, Object { "compressed": true, - "size": 3618, + "size": 3594, }, ], Array [ "main.js.map.gz", - 3586, + 3564, Object { "compressed": true, - "size": 3586, + "size": 3564, }, ], Array [ - "main.js.map?var=8cdf224bcea94552590b", - 11216, + "main.js.map?var=18beec7527af02981a38", + 11139, Object { "development": true, "related": Object { "gziped": "main.js.map.gz", }, - "size": 11216, + "size": 11139, }, ], Array [ - "main.js?var=8cdf224bcea94552590b", - 15373, + "main.js?var=18beec7527af02981a38", + 15274, Object { - "fullhash": "8cdf224bcea94552590b", + "fullhash": "18beec7527af02981a38", "immutable": true, "related": Object { "gziped": "main.js.gz", - "sourceMap": "main.js.map?var=8cdf224bcea94552590b", + "sourceMap": "main.js.map?var=18beec7527af02981a38", }, - "size": 15373, + "size": 15274, }, ], ] @@ -419,41 +456,41 @@ Array [ ], Array [ "async.async.js.br", - 181, + 179, Object { "compressed": true, - "size": 181, + "size": 179, }, ], Array [ "async.async.js.compress", - 181, + 179, Object { "compressed": true, - "size": 181, + "size": 179, }, ], Array [ "async.async.js.custom?foo=bar#hash", - 181, + 179, Object { "compressed": true, - "size": 181, + "size": 179, }, ], Array [ "async.async.js.gz", - 181, + 179, Object { "compressed": true, - "size": 181, + "size": 179, }, ], Array [ - "async.async.js?ver=8cdf224bcea94552590b", - 269, + "async.async.js?ver=18beec7527af02981a38", + 265, Object { - "fullhash": "8cdf224bcea94552590b", + "fullhash": "18beec7527af02981a38", "immutable": true, "related": Object { "brotliCompressed": "async.async.js.br", @@ -461,46 +498,46 @@ Array [ "customed": "async.async.js.custom?foo=bar#hash", "gziped": "async.async.js.gz", }, - "size": 269, + "size": 265, }, ], Array [ "main.js.br", - 3587, + 3563, Object { "compressed": true, - "size": 3587, + "size": 3563, }, ], Array [ "main.js.compress", - 3587, + 3563, Object { "compressed": true, - "size": 3587, + "size": 3563, }, ], Array [ "main.js.custom?foo=bar#hash", - 3587, + 3563, Object { "compressed": true, - "size": 3587, + "size": 3563, }, ], Array [ "main.js.gz", - 3587, + 3563, Object { "compressed": true, - "size": 3587, + "size": 3563, }, ], Array [ - "main.js?var=8cdf224bcea94552590b", - 15315, + "main.js?var=18beec7527af02981a38", + 15216, Object { - "fullhash": "8cdf224bcea94552590b", + "fullhash": "18beec7527af02981a38", "immutable": true, "related": Object { "brotliCompressed": "main.js.br", @@ -508,7 +545,7 @@ Array [ "customed": "main.js.custom?foo=bar#hash", "gziped": "main.js.gz", }, - "size": 15315, + "size": 15216, }, ], ] @@ -534,19 +571,19 @@ Array [ ], Array [ "async.async.js.gz", - 181, + 179, ], Array [ - "async.async.js?ver=8cdf224bcea94552590b", - 269, + "async.async.js?ver=18beec7527af02981a38", + 265, ], Array [ "main.js.gz", - 3587, + 3563, ], Array [ - "main.js?var=8cdf224bcea94552590b", - 15315, + "main.js?var=18beec7527af02981a38", + 15216, ], ] `; diff --git a/test/__snapshots__/algorithm.test.js.snap.webpack5 b/test/__snapshots__/algorithm.test.js.snap.webpack5 index 3d26f5e..4fd2ff8 100644 --- a/test/__snapshots__/algorithm.test.js.snap.webpack5 +++ b/test/__snapshots__/algorithm.test.js.snap.webpack5 @@ -19,20 +19,20 @@ Array [ 393, ], Array [ - "async.async.81ea6697ed82fa2176bb.js", - 269, + "async.async.1c2911c7c819fc826fb7.js", + 265, ], Array [ - "async.async.81ea6697ed82fa2176bb.js.gz", - 181, + "async.async.1c2911c7c819fc826fb7.js.gz", + 179, ], Array [ - "main.f1e836b35a2623bafe34.js", - 15156, + "main.c14d5dcb1534759d1d95.js", + 15057, ], Array [ - "main.f1e836b35a2623bafe34.js.gz", - 3556, + "main.c14d5dcb1534759d1d95.js.gz", + 3537, ], ] `; @@ -62,20 +62,20 @@ Array [ 672, ], Array [ - "async.async.81ea6697ed82fa2176bb.js", - 269, + "async.async.1c2911c7c819fc826fb7.js", + 265, ], Array [ - "async.async.81ea6697ed82fa2176bb.js.gz", - 269, + "async.async.1c2911c7c819fc826fb7.js.gz", + 265, ], Array [ - "main.f1e836b35a2623bafe34.js", - 15156, + "main.c14d5dcb1534759d1d95.js", + 15057, ], Array [ - "main.f1e836b35a2623bafe34.js.gz", - 15156, + "main.c14d5dcb1534759d1d95.js.gz", + 15057, ], ] `; @@ -103,12 +103,12 @@ Array [ 672, ], Array [ - "async.async.81ea6697ed82fa2176bb.js", - 269, + "async.async.1c2911c7c819fc826fb7.js", + 265, ], Array [ - "main.f1e836b35a2623bafe34.js", - 15156, + "main.c14d5dcb1534759d1d95.js", + 15057, ], ] `; diff --git a/test/__snapshots__/cache-option.test.js.snap.webpack4 b/test/__snapshots__/cache-option.test.js.snap.webpack4 index 9586d3f..fcdf7c3 100644 --- a/test/__snapshots__/cache-option.test.js.snap.webpack4 +++ b/test/__snapshots__/cache-option.test.js.snap.webpack4 @@ -151,6 +151,116 @@ exports[`"cache" option matches snapshot for \`other-cache-directory\` value ({S exports[`"cache" option matches snapshot for \`other-cache-directory\` value ({String}): warnings 2`] = `Array []`; +exports[`"cache" option matches snapshot for \`other-other-cache-directory\` value ({String}) with the "algorithm" option: 09a1a1112c577c2794359715edfcb5ac.png 1`] = ` +Array [ + "09a1a1112c577c2794359715edfcb5ac.png", + "09a1a1112c577c2794359715edfcb5ac", +] +`; + +exports[`"cache" option matches snapshot for \`other-other-cache-directory\` value ({String}) with the "algorithm" option: 23fc1d3ac606d117e05a140e0de79806.svg 1`] = ` +Array [ + "23fc1d3ac606d117e05a140e0de79806.svg", + "23fc1d3ac606d117e05a140e0de79806", +] +`; + +exports[`"cache" option matches snapshot for \`other-other-cache-directory\` value ({String}) with the "algorithm" option: assets 1`] = ` +Array [ + Array [ + "09a1a1112c577c2794359715edfcb5ac.png", + 78117, + ], + Array [ + "09a1a1112c577c2794359715edfcb5ac.png.gz", + 73160, + ], + Array [ + "23fc1d3ac606d117e05a140e0de79806.svg", + 672, + ], + Array [ + "23fc1d3ac606d117e05a140e0de79806.svg.gz", + 393, + ], + Array [ + "async.async.dcbdb1dd40763448b635.js", + 249, + ], + Array [ + "async.async.dcbdb1dd40763448b635.js.gz", + 171, + ], + Array [ + "main.82c6786e32f9c77b44ef.js", + 11645, + ], + Array [ + "main.82c6786e32f9c77b44ef.js.gz", + 3000, + ], +] +`; + +exports[`"cache" option matches snapshot for \`other-other-cache-directory\` value ({String}) with the "algorithm" option: assets 2`] = ` +Array [ + Array [ + "09a1a1112c577c2794359715edfcb5ac.png", + 78117, + ], + Array [ + "09a1a1112c577c2794359715edfcb5ac.png.gz", + 73160, + ], + Array [ + "23fc1d3ac606d117e05a140e0de79806.svg", + 672, + ], + Array [ + "23fc1d3ac606d117e05a140e0de79806.svg.gz", + 393, + ], + Array [ + "async.async.dcbdb1dd40763448b635.js", + 249, + ], + Array [ + "async.async.dcbdb1dd40763448b635.js.gz", + 171, + ], + Array [ + "main.82c6786e32f9c77b44ef.js", + 11645, + ], + Array [ + "main.82c6786e32f9c77b44ef.js.gz", + 3000, + ], +] +`; + +exports[`"cache" option matches snapshot for \`other-other-cache-directory\` value ({String}) with the "algorithm" option: async.async.dcbdb1dd40763448b635.js 1`] = ` +Array [ + "async.async.dcbdb1dd40763448b635.js", + "0fb94f644e2d45a0d17031ab77459a35", +] +`; + +exports[`"cache" option matches snapshot for \`other-other-cache-directory\` value ({String}) with the "algorithm" option: errors 1`] = `Array []`; + +exports[`"cache" option matches snapshot for \`other-other-cache-directory\` value ({String}) with the "algorithm" option: errors 2`] = `Array []`; + +exports[`"cache" option matches snapshot for \`other-other-cache-directory\` value ({String}) with the "algorithm" option: main.82c6786e32f9c77b44ef.js 1`] = ` +Array [ + "main.82c6786e32f9c77b44ef.js", + "ef0eb5cabc4295a2bbcd320c66aecd89", +] +`; + +exports[`"cache" option matches snapshot for \`other-other-cache-directory\` value ({String}) with the "algorithm" option: warnings 1`] = `Array []`; + +exports[`"cache" option matches snapshot for \`other-other-cache-directory\` value ({String}) with the "algorithm" option: warnings 2`] = `Array []`; + exports[`"cache" option matches snapshot for \`true\` value ({Boolean}): 09a1a1112c577c2794359715edfcb5ac.png 1`] = ` Array [ "09a1a1112c577c2794359715edfcb5ac.png", diff --git a/test/__snapshots__/cache-option.test.js.snap.webpack5 b/test/__snapshots__/cache-option.test.js.snap.webpack5 index ab6a3aa..6644931 100644 --- a/test/__snapshots__/cache-option.test.js.snap.webpack5 +++ b/test/__snapshots__/cache-option.test.js.snap.webpack5 @@ -15,20 +15,20 @@ Array [ 393, ], Array [ - "async.async.81ea6697ed82fa2176bb.js", - 269, + "async.async.1c2911c7c819fc826fb7.js", + 265, ], Array [ - "async.async.81ea6697ed82fa2176bb.js.gz", - 181, + "async.async.1c2911c7c819fc826fb7.js.gz", + 179, ], Array [ - "main.f1e836b35a2623bafe34.js", - 15156, + "main.c14d5dcb1534759d1d95.js", + 15057, ], Array [ - "main.f1e836b35a2623bafe34.js.gz", - 3556, + "main.c14d5dcb1534759d1d95.js.gz", + 3537, ], ] `; @@ -48,20 +48,20 @@ Array [ 393, ], Array [ - "async.async.81ea6697ed82fa2176bb.js", - 269, + "async.async.1c2911c7c819fc826fb7.js", + 265, ], Array [ - "async.async.81ea6697ed82fa2176bb.js.gz", - 181, + "async.async.1c2911c7c819fc826fb7.js.gz", + 179, ], Array [ - "main.f1e836b35a2623bafe34.js", - 15156, + "main.c14d5dcb1534759d1d95.js", + 15057, ], Array [ - "main.f1e836b35a2623bafe34.js.gz", - 3556, + "main.c14d5dcb1534759d1d95.js.gz", + 3537, ], ] `; @@ -74,6 +74,80 @@ exports[`"cache" option should work when \`cache\` is \`false\`: warnings 1`] = exports[`"cache" option should work when \`cache\` is \`false\`: warnings 2`] = `Array []`; +exports[`"cache" option should work with "filesystem" value for the "cache.type" option and with the "algorithm" option: assets 1`] = ` +Array [ + Array [ + "09a1a1112c577c2794359715edfcb5ac.png", + 78117, + ], + Array [ + "23fc1d3ac606d117e05a140e0de79806.svg", + 672, + ], + Array [ + "23fc1d3ac606d117e05a140e0de79806.svg.gz", + 393, + ], + Array [ + "async.async.1c2911c7c819fc826fb7.js", + 265, + ], + Array [ + "async.async.1c2911c7c819fc826fb7.js.gz", + 179, + ], + Array [ + "main.c14d5dcb1534759d1d95.js", + 15057, + ], + Array [ + "main.c14d5dcb1534759d1d95.js.gz", + 3537, + ], +] +`; + +exports[`"cache" option should work with "filesystem" value for the "cache.type" option and with the "algorithm" option: assets 2`] = ` +Array [ + Array [ + "09a1a1112c577c2794359715edfcb5ac.png", + 78117, + ], + Array [ + "23fc1d3ac606d117e05a140e0de79806.svg", + 672, + ], + Array [ + "23fc1d3ac606d117e05a140e0de79806.svg.gz", + 393, + ], + Array [ + "async.async.1c2911c7c819fc826fb7.js", + 265, + ], + Array [ + "async.async.1c2911c7c819fc826fb7.js.gz", + 179, + ], + Array [ + "main.c14d5dcb1534759d1d95.js", + 15057, + ], + Array [ + "main.c14d5dcb1534759d1d95.js.gz", + 3537, + ], +] +`; + +exports[`"cache" option should work with "filesystem" value for the "cache.type" option and with the "algorithm" option: errors 1`] = `Array []`; + +exports[`"cache" option should work with "filesystem" value for the "cache.type" option and with the "algorithm" option: errors 2`] = `Array []`; + +exports[`"cache" option should work with "filesystem" value for the "cache.type" option and with the "algorithm" option: warnings 1`] = `Array []`; + +exports[`"cache" option should work with "filesystem" value for the "cache.type" option and with the "algorithm" option: warnings 2`] = `Array []`; + exports[`"cache" option should work with "filesystem" value for the "cache.type" option: assets 1`] = ` Array [ Array [ @@ -89,20 +163,20 @@ Array [ 393, ], Array [ - "async.async.81ea6697ed82fa2176bb.js", - 269, + "async.async.1c2911c7c819fc826fb7.js", + 265, ], Array [ - "async.async.81ea6697ed82fa2176bb.js.gz", - 181, + "async.async.1c2911c7c819fc826fb7.js.gz", + 179, ], Array [ - "main.f1e836b35a2623bafe34.js", - 15156, + "main.c14d5dcb1534759d1d95.js", + 15057, ], Array [ - "main.f1e836b35a2623bafe34.js.gz", - 3556, + "main.c14d5dcb1534759d1d95.js.gz", + 3537, ], ] `; @@ -122,20 +196,20 @@ Array [ 393, ], Array [ - "async.async.81ea6697ed82fa2176bb.js", - 269, + "async.async.1c2911c7c819fc826fb7.js", + 265, ], Array [ - "async.async.81ea6697ed82fa2176bb.js.gz", - 181, + "async.async.1c2911c7c819fc826fb7.js.gz", + 179, ], Array [ - "main.f1e836b35a2623bafe34.js", - 15156, + "main.c14d5dcb1534759d1d95.js", + 15057, ], Array [ - "main.f1e836b35a2623bafe34.js.gz", - 3556, + "main.c14d5dcb1534759d1d95.js.gz", + 3537, ], ] `; @@ -163,20 +237,20 @@ Array [ 393, ], Array [ - "async.async.81ea6697ed82fa2176bb.js", - 269, + "async.async.1c2911c7c819fc826fb7.js", + 265, ], Array [ - "async.async.81ea6697ed82fa2176bb.js.gz", - 181, + "async.async.1c2911c7c819fc826fb7.js.gz", + 179, ], Array [ - "main.f1e836b35a2623bafe34.js", - 15156, + "main.c14d5dcb1534759d1d95.js", + 15057, ], Array [ - "main.f1e836b35a2623bafe34.js.gz", - 3556, + "main.c14d5dcb1534759d1d95.js.gz", + 3537, ], ] `; @@ -196,20 +270,20 @@ Array [ 393, ], Array [ - "async.async.81ea6697ed82fa2176bb.js", - 269, + "async.async.1c2911c7c819fc826fb7.js", + 265, ], Array [ - "async.async.81ea6697ed82fa2176bb.js.gz", - 181, + "async.async.1c2911c7c819fc826fb7.js.gz", + 179, ], Array [ - "main.f1e836b35a2623bafe34.js", - 15156, + "main.c14d5dcb1534759d1d95.js", + 15057, ], Array [ - "main.f1e836b35a2623bafe34.js.gz", - 3556, + "main.c14d5dcb1534759d1d95.js.gz", + 3537, ], ] `; diff --git a/test/__snapshots__/compressionOptions-option.test.js.snap.webpack5 b/test/__snapshots__/compressionOptions-option.test.js.snap.webpack5 index 118d415..5dc73ea 100644 --- a/test/__snapshots__/compressionOptions-option.test.js.snap.webpack5 +++ b/test/__snapshots__/compressionOptions-option.test.js.snap.webpack5 @@ -19,20 +19,20 @@ Array [ 393, ], Array [ - "async.async.81ea6697ed82fa2176bb.js", - 269, + "async.async.1c2911c7c819fc826fb7.js", + 265, ], Array [ - "async.async.81ea6697ed82fa2176bb.js.gz", - 181, + "async.async.1c2911c7c819fc826fb7.js.gz", + 179, ], Array [ - "main.f1e836b35a2623bafe34.js", - 15156, + "main.c14d5dcb1534759d1d95.js", + 15057, ], Array [ - "main.f1e836b35a2623bafe34.js.gz", - 3556, + "main.c14d5dcb1534759d1d95.js.gz", + 3537, ], ] `; @@ -60,20 +60,20 @@ Array [ 393, ], Array [ - "async.async.81ea6697ed82fa2176bb.js", - 269, + "async.async.1c2911c7c819fc826fb7.js", + 265, ], Array [ - "async.async.81ea6697ed82fa2176bb.js.gz", - 181, + "async.async.1c2911c7c819fc826fb7.js.gz", + 179, ], Array [ - "main.f1e836b35a2623bafe34.js", - 15156, + "main.c14d5dcb1534759d1d95.js", + 15057, ], Array [ - "main.f1e836b35a2623bafe34.js.gz", - 3556, + "main.c14d5dcb1534759d1d95.js.gz", + 3537, ], ] `; diff --git a/test/__snapshots__/deleteOriginalAssets.test.js.snap.webpack5 b/test/__snapshots__/deleteOriginalAssets.test.js.snap.webpack5 index b0fe050..0d77233 100644 --- a/test/__snapshots__/deleteOriginalAssets.test.js.snap.webpack5 +++ b/test/__snapshots__/deleteOriginalAssets.test.js.snap.webpack5 @@ -19,20 +19,20 @@ Array [ 393, ], Array [ - "async.async.81ea6697ed82fa2176bb.js", - 269, + "async.async.1c2911c7c819fc826fb7.js", + 265, ], Array [ - "async.async.81ea6697ed82fa2176bb.js.gz", - 181, + "async.async.1c2911c7c819fc826fb7.js.gz", + 179, ], Array [ - "main.f1e836b35a2623bafe34.js", - 15156, + "main.c14d5dcb1534759d1d95.js", + 15057, ], Array [ - "main.f1e836b35a2623bafe34.js.gz", - 3556, + "main.c14d5dcb1534759d1d95.js.gz", + 3537, ], ] `; @@ -52,12 +52,12 @@ Array [ 393, ], Array [ - "async.async.81ea6697ed82fa2176bb.js.gz", - 181, + "async.async.1c2911c7c819fc826fb7.js.gz", + 179, ], Array [ - "main.f1e836b35a2623bafe34.js.gz", - 3556, + "main.c14d5dcb1534759d1d95.js.gz", + 3537, ], ] `; diff --git a/test/__snapshots__/exclude-option.test.js.snap.webpack5 b/test/__snapshots__/exclude-option.test.js.snap.webpack5 index f7fc78a..0c830b9 100644 --- a/test/__snapshots__/exclude-option.test.js.snap.webpack5 +++ b/test/__snapshots__/exclude-option.test.js.snap.webpack5 @@ -16,19 +16,19 @@ Array [ ], Array [ "async.async.js.gz", - 181, + 179, ], Array [ - "async.async.js?ver=8cdf224bcea94552590b", - 269, + "async.async.js?ver=18beec7527af02981a38", + 265, ], Array [ "main.js.gz", - 3587, + 3563, ], Array [ - "main.js?var=8cdf224bcea94552590b", - 15315, + "main.js?var=18beec7527af02981a38", + 15216, ], ] `; @@ -49,19 +49,19 @@ Array [ ], Array [ "async.async.js.gz", - 181, + 179, ], Array [ - "async.async.js?ver=8cdf224bcea94552590b", - 269, + "async.async.js?ver=18beec7527af02981a38", + 265, ], Array [ "main.js.gz", - 3587, + 3563, ], Array [ - "main.js?var=8cdf224bcea94552590b", - 15315, + "main.js?var=18beec7527af02981a38", + 15216, ], ] `; diff --git a/test/__snapshots__/filename-option.test.js.snap.webpack5 b/test/__snapshots__/filename-option.test.js.snap.webpack5 index ab241c0..cfadaa8 100644 --- a/test/__snapshots__/filename-option.test.js.snap.webpack5 +++ b/test/__snapshots__/filename-option.test.js.snap.webpack5 @@ -19,20 +19,20 @@ Array [ 672, ], Array [ - "async.async.js?ver=8cdf224bcea94552590b", - 269, + "async.async.js?ver=18beec7527af02981a38", + 265, ], Array [ - "async.async.super-compressed.gz.js?ver=8cdf224bcea94552590b", - 181, + "async.async.super-compressed.gz.js?ver=18beec7527af02981a38", + 179, ], Array [ - "main.js?var=8cdf224bcea94552590b", - 15315, + "main.js?var=18beec7527af02981a38", + 15216, ], Array [ - "main.super-compressed.gz.js?var=8cdf224bcea94552590b", - 3587, + "main.super-compressed.gz.js?var=18beec7527af02981a38", + 3563, ], ] `; @@ -60,20 +60,20 @@ Array [ 393, ], Array [ - "async.async.js.super-compressed.gz?ver=8cdf224bcea94552590b", - 181, + "async.async.js.super-compressed.gz?ver=18beec7527af02981a38", + 179, ], Array [ - "async.async.js?ver=8cdf224bcea94552590b", - 269, + "async.async.js?ver=18beec7527af02981a38", + 265, ], Array [ - "main.js.super-compressed.gz?var=8cdf224bcea94552590b", - 3587, + "main.js.super-compressed.gz?var=18beec7527af02981a38", + 3563, ], Array [ - "main.js?var=8cdf224bcea94552590b", - 15315, + "main.js?var=18beec7527af02981a38", + 15216, ], ] `; @@ -101,20 +101,20 @@ Array [ 393, ], Array [ - "async.async.js.gz?ver=8cdf224bcea94552590b", - 181, + "async.async.js.gz?ver=18beec7527af02981a38", + 179, ], Array [ - "async.async.js?ver=8cdf224bcea94552590b", - 269, + "async.async.js?ver=18beec7527af02981a38", + 265, ], Array [ - "main.js.gz?var=8cdf224bcea94552590b", - 3587, + "main.js.gz?var=18beec7527af02981a38", + 3563, ], Array [ - "main.js?var=8cdf224bcea94552590b", - 15315, + "main.js?var=18beec7527af02981a38", + 15216, ], ] `; diff --git a/test/__snapshots__/include-option.test.js.snap.webpack5 b/test/__snapshots__/include-option.test.js.snap.webpack5 index 019bf95..f443edb 100644 --- a/test/__snapshots__/include-option.test.js.snap.webpack5 +++ b/test/__snapshots__/include-option.test.js.snap.webpack5 @@ -12,19 +12,19 @@ Array [ ], Array [ "async.async.js.gz", - 181, + 179, ], Array [ - "async.async.js?ver=8cdf224bcea94552590b", - 269, + "async.async.js?ver=18beec7527af02981a38", + 265, ], Array [ "main.js.gz", - 3587, + 3563, ], Array [ - "main.js?var=8cdf224bcea94552590b", - 15315, + "main.js?var=18beec7527af02981a38", + 15216, ], ] `; @@ -49,19 +49,19 @@ Array [ ], Array [ "async.async.js.gz", - 181, + 179, ], Array [ - "async.async.js?ver=8cdf224bcea94552590b", - 269, + "async.async.js?ver=18beec7527af02981a38", + 265, ], Array [ "main.js.gz", - 3587, + 3563, ], Array [ - "main.js?var=8cdf224bcea94552590b", - 15315, + "main.js?var=18beec7527af02981a38", + 15216, ], ] `; diff --git a/test/__snapshots__/minRatio-option.test.js.snap.webpack5 b/test/__snapshots__/minRatio-option.test.js.snap.webpack5 index 08a7e39..016ae2d 100644 --- a/test/__snapshots__/minRatio-option.test.js.snap.webpack5 +++ b/test/__snapshots__/minRatio-option.test.js.snap.webpack5 @@ -11,12 +11,12 @@ Array [ 672, ], Array [ - "async.async.81ea6697ed82fa2176bb.js", - 269, + "async.async.1c2911c7c819fc826fb7.js", + 265, ], Array [ - "main.f1e836b35a2623bafe34.js", - 15156, + "main.c14d5dcb1534759d1d95.js", + 15057, ], ] `; @@ -44,20 +44,20 @@ Array [ 393, ], Array [ - "async.async.81ea6697ed82fa2176bb.js", - 269, + "async.async.1c2911c7c819fc826fb7.js", + 265, ], Array [ - "async.async.81ea6697ed82fa2176bb.js.gz", - 181, + "async.async.1c2911c7c819fc826fb7.js.gz", + 179, ], Array [ - "main.f1e836b35a2623bafe34.js", - 15156, + "main.c14d5dcb1534759d1d95.js", + 15057, ], Array [ - "main.f1e836b35a2623bafe34.js.gz", - 3556, + "main.c14d5dcb1534759d1d95.js.gz", + 3537, ], ] `; diff --git a/test/__snapshots__/test-option.test.js.snap.webpack4 b/test/__snapshots__/test-option.test.js.snap.webpack4 index 550f693..e3a81a2 100644 --- a/test/__snapshots__/test-option.test.js.snap.webpack4 +++ b/test/__snapshots__/test-option.test.js.snap.webpack4 @@ -102,3 +102,28 @@ Array [ exports[`"test" option matches snapshot with empty \`test\` value: errors 1`] = `Array []`; exports[`"test" option matches snapshot with empty \`test\` value: warnings 1`] = `Array []`; + +exports[`"test" option should work when no asset to compress : assets 1`] = ` +Array [ + Array [ + "09a1a1112c577c2794359715edfcb5ac.png", + 78117, + ], + Array [ + "23fc1d3ac606d117e05a140e0de79806.svg", + 672, + ], + Array [ + "async.async.js?ver=dc74724f7b13a2de5a20", + 249, + ], + Array [ + "main.js?var=dc74724f7b13a2de5a20", + 11630, + ], +] +`; + +exports[`"test" option should work when no asset to compress : errors 1`] = `Array []`; + +exports[`"test" option should work when no asset to compress : warnings 1`] = `Array []`; diff --git a/test/__snapshots__/test-option.test.js.snap.webpack5 b/test/__snapshots__/test-option.test.js.snap.webpack5 index 782405a..9444f28 100644 --- a/test/__snapshots__/test-option.test.js.snap.webpack5 +++ b/test/__snapshots__/test-option.test.js.snap.webpack5 @@ -15,12 +15,12 @@ Array [ 672, ], Array [ - "async.async.js?ver=8cdf224bcea94552590b", - 269, + "async.async.js?ver=18beec7527af02981a38", + 265, ], Array [ - "main.js?var=8cdf224bcea94552590b", - 15315, + "main.js?var=18beec7527af02981a38", + 15216, ], ] `; @@ -48,12 +48,12 @@ Array [ 393, ], Array [ - "async.async.js?ver=8cdf224bcea94552590b", - 269, + "async.async.js?ver=18beec7527af02981a38", + 265, ], Array [ - "main.js?var=8cdf224bcea94552590b", - 15315, + "main.js?var=18beec7527af02981a38", + 15216, ], ] `; @@ -82,19 +82,19 @@ Array [ ], Array [ "async.async.js.gz", - 181, + 179, ], Array [ - "async.async.js?ver=8cdf224bcea94552590b", - 269, + "async.async.js?ver=18beec7527af02981a38", + 265, ], Array [ "main.js.gz", - 3587, + 3563, ], Array [ - "main.js?var=8cdf224bcea94552590b", - 15315, + "main.js?var=18beec7527af02981a38", + 15216, ], ] `; @@ -102,3 +102,28 @@ Array [ exports[`"test" option matches snapshot with empty \`test\` value: errors 1`] = `Array []`; exports[`"test" option matches snapshot with empty \`test\` value: warnings 1`] = `Array []`; + +exports[`"test" option should work when no asset to compress : assets 1`] = ` +Array [ + Array [ + "09a1a1112c577c2794359715edfcb5ac.png", + 78117, + ], + Array [ + "23fc1d3ac606d117e05a140e0de79806.svg", + 672, + ], + Array [ + "async.async.js?ver=18beec7527af02981a38", + 265, + ], + Array [ + "main.js?var=18beec7527af02981a38", + 15216, + ], +] +`; + +exports[`"test" option should work when no asset to compress : errors 1`] = `Array []`; + +exports[`"test" option should work when no asset to compress : warnings 1`] = `Array []`; diff --git a/test/__snapshots__/threshold-option.test.js.snap.webpack5 b/test/__snapshots__/threshold-option.test.js.snap.webpack5 index 84434f0..cbdbd40 100644 --- a/test/__snapshots__/threshold-option.test.js.snap.webpack5 +++ b/test/__snapshots__/threshold-option.test.js.snap.webpack5 @@ -19,20 +19,20 @@ Array [ 393, ], Array [ - "async.async.81ea6697ed82fa2176bb.js", - 269, + "async.async.1c2911c7c819fc826fb7.js", + 265, ], Array [ - "async.async.81ea6697ed82fa2176bb.js.gz", - 181, + "async.async.1c2911c7c819fc826fb7.js.gz", + 179, ], Array [ - "main.f1e836b35a2623bafe34.js", - 15156, + "main.c14d5dcb1534759d1d95.js", + 15057, ], Array [ - "main.f1e836b35a2623bafe34.js.gz", - 3556, + "main.c14d5dcb1534759d1d95.js.gz", + 3537, ], ] `; @@ -56,16 +56,16 @@ Array [ 672, ], Array [ - "async.async.81ea6697ed82fa2176bb.js", - 269, + "async.async.1c2911c7c819fc826fb7.js", + 265, ], Array [ - "main.f1e836b35a2623bafe34.js", - 15156, + "main.c14d5dcb1534759d1d95.js", + 15057, ], Array [ - "main.f1e836b35a2623bafe34.js.gz", - 3556, + "main.c14d5dcb1534759d1d95.js.gz", + 3537, ], ] `; diff --git a/test/cache-option.test.js b/test/cache-option.test.js index 3acaa2f..a85c87c 100644 --- a/test/cache-option.test.js +++ b/test/cache-option.test.js @@ -1,4 +1,5 @@ import path from 'path'; +import zlib from 'zlib'; import cacache from 'cacache'; import findCacheDir from 'find-cache-dir'; @@ -18,6 +19,9 @@ import { const falseCacheDirectory = findCacheDir({ name: 'false-cache-directory' }); const cacheDir = findCacheDir({ name: 'compression-webpack-plugin' }); const otherCacheDir = findCacheDir({ name: 'other-cache-directory' }); +const otherOtherCacheDir = findCacheDir({ + name: 'other-other-cache-directory', +}); const uniqueCacheDirectory = findCacheDir({ name: 'unique-cache-directory' }); if (getCompiler.isWebpack4()) { @@ -28,12 +32,19 @@ if (getCompiler.isWebpack4()) { cacache.rm.all(cacheDir), cacache.rm.all(otherCacheDir), cacache.rm.all(uniqueCacheDirectory), + cacache.rm.all(otherOtherCacheDir), ]); }); - afterEach(() => - Promise.all([cacache.rm.all(cacheDir), cacache.rm.all(otherCacheDir)]) - ); + afterAll(() => { + return Promise.all([ + cacache.rm.all(falseCacheDirectory), + cacache.rm.all(cacheDir), + cacache.rm.all(otherCacheDir), + cacache.rm.all(uniqueCacheDirectory), + cacache.rm.all(otherOtherCacheDir), + ]); + }); it('matches snapshot for `false` value ({Boolean})', async () => { const compiler = getCompiler('./entry.js'); @@ -201,6 +212,89 @@ if (getCompiler.isWebpack4()) { expect(cacache.get.mock.calls.length).toBe(newCountAssets / 2); expect(cacache.put.mock.calls.length).toBe(0); }); + + it('matches snapshot for `other-other-cache-directory` value ({String}) with the "algorithm" option', async () => { + const beforeCacheCompiler = getCompiler('./entry.js'); + + new CompressionPlugin({ + cache: otherOtherCacheDir, + minRatio: 1, + algorithm: (input, compressionOptions, callback) => { + return zlib.gzip( + input, + { ...compressionOptions, ...{ level: 9 } }, + (error, buffer) => { + callback(error, Uint8Array.from(buffer)); + } + ); + }, + }).apply(beforeCacheCompiler); + + cacache.get = jest.fn(cacache.get); + cacache.put = jest.fn(cacache.put); + + const stats = await compile(beforeCacheCompiler); + + expect(getAssetsNameAndSize(stats)).toMatchSnapshot('assets'); + expect(getWarnings(stats)).toMatchSnapshot('errors'); + expect(getErrors(stats)).toMatchSnapshot('warnings'); + + const countAssets = Object.keys(stats.compilation.assets).length; + + // Try to found cached files, but we don't have their in cache + expect(cacache.get.mock.calls.length).toBe(countAssets / 2); + // Put files in cache + expect(cacache.put.mock.calls.length).toBe(countAssets / 2); + + const cacheEntriesList = await cacache.ls(otherOtherCacheDir); + const cacheKeys = Object.keys(cacheEntriesList); + + // Make sure that we cached files + expect(cacheKeys.length).toBe(countAssets / 2); + + cacheKeys.forEach((cacheEntry) => { + // eslint-disable-next-line no-new-func + const cacheEntryOptions = new Function( + `'use strict'\nreturn ${cacheEntry}` + )(); + const basename = path.basename(cacheEntryOptions.assetName); + + expect([basename, cacheEntryOptions.contentHash]).toMatchSnapshot( + basename + ); + }); + + cacache.get.mockClear(); + cacache.put.mockClear(); + + const afterCacheCompiler = getCompiler('./entry.js'); + + new CompressionPlugin({ + cache: otherOtherCacheDir, + minRatio: 1, + algorithm: (input, compressionOptions, callback) => { + return zlib.gzip( + input, + { ...compressionOptions, ...{ level: 9 } }, + (error, buffer) => { + callback(error, Uint8Array.from(buffer)); + } + ); + }, + }).apply(afterCacheCompiler); + + const newStats = await compile(afterCacheCompiler); + + expect(getAssetsNameAndSize(newStats)).toMatchSnapshot('assets'); + expect(getWarnings(newStats)).toMatchSnapshot('errors'); + expect(getErrors(newStats)).toMatchSnapshot('warnings'); + + const newCountAssets = Object.keys(newStats.compilation.assets).length; + + // Now we have cached files so we get their and don't put + expect(cacache.get.mock.calls.length).toBe(newCountAssets / 2); + expect(cacache.put.mock.calls.length).toBe(0); + }); }); } else { describe('"cache" option', () => { @@ -382,5 +476,75 @@ if (getCompiler.isWebpack4()) { expect(getErrors(newStats)).toMatchSnapshot('errors'); expect(getWarnings(newStats)).toMatchSnapshot('warnings'); }); + + it('should work with "filesystem" value for the "cache.type" option and with the "algorithm" option', async () => { + const compiler = getCompiler( + './entry.js', + {}, + { + cache: { + type: 'filesystem', + cacheDirectory: fileSystemCacheDirectory, + }, + } + ); + + new CompressionPlugin({ + algorithm: (input, compressionOptions, callback) => { + return zlib.gzip( + input, + { ...compressionOptions, ...{ level: 9 } }, + (error, buffer) => { + callback(error, Uint8Array.from(buffer)); + } + ); + }, + }).apply(compiler); + + let getCounter = 0; + + compiler.cache.hooks.get.tap( + { name: 'TestCache', stage: -100 }, + (identifier) => { + if (identifier.indexOf('CompressionWebpackPlugin') !== -1) { + getCounter += 1; + } + } + ); + + let storeCounter = 0; + + compiler.cache.hooks.store.tap( + { name: 'TestCache', stage: -100 }, + (identifier) => { + if (identifier.indexOf('CompressionWebpackPlugin') !== -1) { + storeCounter += 1; + } + } + ); + + const stats = await compile(compiler); + + // Get cache for assets + expect(getCounter).toBe(4); + // Store cached assets + expect(storeCounter).toBe(4); + expect(getAssetsNameAndSize(stats)).toMatchSnapshot('assets'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); + + getCounter = 0; + storeCounter = 0; + + const newStats = await compile(compiler); + + // Get cache for assets + expect(getCounter).toBe(4); + // No need to store, we got cached assets + expect(storeCounter).toBe(0); + expect(getAssetsNameAndSize(newStats)).toMatchSnapshot('assets'); + expect(getErrors(newStats)).toMatchSnapshot('errors'); + expect(getWarnings(newStats)).toMatchSnapshot('warnings'); + }); }); } diff --git a/test/test-option.test.js b/test/test-option.test.js index d349384..980e92d 100644 --- a/test/test-option.test.js +++ b/test/test-option.test.js @@ -65,4 +65,17 @@ describe('"test" option', () => { expect(getWarnings(stats)).toMatchSnapshot('errors'); expect(getErrors(stats)).toMatchSnapshot('warnings'); }); + + it('should work when no asset to compress ', async () => { + new CompressionPlugin({ + test: /\.(unknown)$/i, + minRatio: 1, + }).apply(compiler); + + const stats = await compile(compiler); + + expect(getAssetsNameAndSize(stats)).toMatchSnapshot('assets'); + expect(getWarnings(stats)).toMatchSnapshot('errors'); + expect(getErrors(stats)).toMatchSnapshot('warnings'); + }); });