From 2910282b1a66ea158dbb939da7987bf60769d68c Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Wed, 16 Sep 2020 14:13:56 +0300 Subject: [PATCH] fix: respect directories in output --- package-lock.json | 6 +-- package.json | 2 +- src/index.js | 2 +- .../filename-option.test.js.snap.webpack4 | 41 +++++++++++++++++++ .../filename-option.test.js.snap.webpack5 | 41 +++++++++++++++++++ test/filename-option.test.js | 24 +++++++++++ 6 files changed, 111 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7965f55..30712c7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11107,9 +11107,9 @@ "dev": true }, "prettier": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.1.1.tgz", - "integrity": "sha512-9bY+5ZWCfqj3ghYBLxApy2zf6m+NJo5GzmLTpr9FsApsfjriNnS2dahWReHMi7qNPhhHl9SYHJs2cHZLgexNIw==", + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.1.2.tgz", + "integrity": "sha512-16c7K+x4qVlJg9rEbXl7HEGmQyZlG4R9AgP+oHKRMsMsuk8s+ATStlf1NpDqyBI1HpVyfjLOeMhH2LvuNvV5Vg==", "dev": true }, "pretty-format": { diff --git a/package.json b/package.json index aa76184..98fa5de 100644 --- a/package.json +++ b/package.json @@ -69,7 +69,7 @@ "lint-staged": "^10.3.0", "memfs": "^3.2.0", "npm-run-all": "^4.1.5", - "prettier": "^2.1.1", + "prettier": "^2.1.2", "standard-version": "^9.0.0", "webpack": "^4.44.1" }, diff --git a/src/index.js b/src/index.js index 1df37bb..9b7d880 100644 --- a/src/index.js +++ b/src/index.js @@ -32,7 +32,7 @@ class CompressionPlugin { cache = true, algorithm = 'gzip', compressionOptions = {}, - filename = '[base].gz', + filename = '[path][base].gz', threshold = 0, minRatio = 0.8, deleteOriginalAssets = false, diff --git a/test/__snapshots__/filename-option.test.js.snap.webpack4 b/test/__snapshots__/filename-option.test.js.snap.webpack4 index 700c7c4..7ba1ee2 100644 --- a/test/__snapshots__/filename-option.test.js.snap.webpack4 +++ b/test/__snapshots__/filename-option.test.js.snap.webpack4 @@ -122,3 +122,44 @@ Array [ exports[`"filename" option matches snapshot for custom function ({Function}): errors 1`] = `Array []`; exports[`"filename" option matches snapshot for custom function ({Function}): warnings 1`] = `Array []`; + +exports[`"filename" option show work: assets 1`] = ` +Array [ + Array [ + "09a1a1112c577c2794359715edfcb5ac.png", + 78117, + ], + Array [ + "09a1a1112c577c2794359715edfcb5ac.png.gz", + 73160, + ], + Array [ + "23fc1d3ac606d117e05a140e0de79806.svg", + 672, + ], + Array [ + "23fc1d3ac606d117e05a140e0de79806.svg.gz", + 393, + ], + Array [ + "assets/scripts/async.async.js.gz", + 171, + ], + Array [ + "assets/scripts/async.async.js?ver=f8840db1f464954a153b#hash", + 249, + ], + Array [ + "assets/scripts/main.js.gz", + 3016, + ], + Array [ + "assets/scripts/main.js?var=f8840db1f464954a153b#hash", + 11650, + ], +] +`; + +exports[`"filename" option show work: errors 1`] = `Array []`; + +exports[`"filename" option show work: warnings 1`] = `Array []`; diff --git a/test/__snapshots__/filename-option.test.js.snap.webpack5 b/test/__snapshots__/filename-option.test.js.snap.webpack5 index d613919..8b02240 100644 --- a/test/__snapshots__/filename-option.test.js.snap.webpack5 +++ b/test/__snapshots__/filename-option.test.js.snap.webpack5 @@ -122,3 +122,44 @@ Array [ exports[`"filename" option matches snapshot for custom function ({Function}): errors 1`] = `Array []`; exports[`"filename" option matches snapshot for custom function ({Function}): warnings 1`] = `Array []`; + +exports[`"filename" option show work: assets 1`] = ` +Array [ + Array [ + "09a1a1112c577c2794359715edfcb5ac.png", + 78117, + ], + Array [ + "09a1a1112c577c2794359715edfcb5ac.png.gz", + 73160, + ], + Array [ + "23fc1d3ac606d117e05a140e0de79806.svg", + 672, + ], + Array [ + "23fc1d3ac606d117e05a140e0de79806.svg.gz", + 393, + ], + Array [ + "assets/scripts/async.async.js.gz", + 179, + ], + Array [ + "assets/scripts/async.async.js?ver=50393f48e1956509615a#hash", + 265, + ], + Array [ + "assets/scripts/main.js.gz", + 3581, + ], + Array [ + "assets/scripts/main.js?var=50393f48e1956509615a#hash", + 15256, + ], +] +`; + +exports[`"filename" option show work: errors 1`] = `Array []`; + +exports[`"filename" option show work: warnings 1`] = `Array []`; diff --git a/test/filename-option.test.js b/test/filename-option.test.js index 3d714f5..82a8415 100644 --- a/test/filename-option.test.js +++ b/test/filename-option.test.js @@ -18,6 +18,30 @@ describe('"filename" option', () => { return removeCache(); }); + it('show work', async () => { + compiler = getCompiler( + './entry.js', + {}, + { + output: { + path: path.resolve(__dirname, './outputs'), + filename: 'assets/scripts/[name].js?var=[hash]#hash', + chunkFilename: 'assets/scripts/[id].[name].js?ver=[hash]#hash', + }, + } + ); + + new CompressionPlugin({ + minRatio: 1, + }).apply(compiler); + + const stats = await compile(compiler); + + expect(getAssetsNameAndSize(stats)).toMatchSnapshot('assets'); + expect(getWarnings(stats)).toMatchSnapshot('errors'); + expect(getErrors(stats)).toMatchSnapshot('warnings'); + }); + it('matches snapshot for `[path][base].super-compressed.gz[query][fragment]` value ({String})', async () => { compiler = getCompiler( './entry.js',