Skip to content

Commit bd60650

Browse files
authored
feat: added the keep-source-maps value to the deleteOriginalAssets option (#216)
1 parent 37b53e7 commit bd60650

20 files changed

+366
-239
lines changed

README.md

+15-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ And run `webpack` via your preferred method.
5050
| **[`threshold`](#threshold)** | `{Number}` | `0` | Only assets bigger than this size are processed (in bytes) |
5151
| **[`minRatio`](#minratio)** | `{Number}` | `0.8` | Only assets that compress better than this ratio are processed (`minRatio = Compressed Size / Original Size`) |
5252
| **[`filename`](#filename)** | `{String\|Function}` | `[path][base].gz` | The target asset filename |
53-
| **[`deleteOriginalAssets`](#deleteoriginalassets)** | `{Boolean}` | `false` | Whether to delete the original assets or not |
53+
| **[`deleteOriginalAssets`](#deleteoriginalassets)** | `{Boolean\|'keep-source-map'}` | `false` | Whether to delete the original assets or not |
5454
| **[`cache`](#cache)** | `{Boolean}` | `true` | Enable file caching |
5555

5656
### `test`
@@ -289,7 +289,7 @@ module.exports = {
289289

290290
### `deleteOriginalAssets`
291291

292-
Type: `Boolean`
292+
Type: `Boolean | 'keep-source-map'`
293293
Default: `false`
294294

295295
Whether to delete the original assets or not.
@@ -306,6 +306,19 @@ module.exports = {
306306
};
307307
```
308308

309+
To exclude sourcemaps from compression
310+
311+
```js
312+
module.exports = {
313+
plugins: [
314+
new CompressionPlugin({
315+
exclude: /.map$/
316+
deleteOriginalAssets: 'keep-source-map',
317+
}),
318+
],
319+
};
320+
```
321+
309322
### `cache`
310323

311324
> ⚠ Ignored in webpack 5! Please use https://webpack.js.org/configuration/other-options/#cache.

package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
"npm-run-all": "^4.1.5",
7272
"prettier": "^2.1.2",
7373
"standard-version": "^9.0.0",
74-
"webpack": "^5.3.2"
74+
"webpack": "^5.4.0"
7575
},
7676
"keywords": [
7777
"webpack"

src/index.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,21 @@ class CompressionPlugin {
299299
}
300300

301301
if (this.options.deleteOriginalAssets) {
302-
// eslint-disable-next-line no-param-reassign
302+
if (this.options.deleteOriginalAssets === 'keep-source-map') {
303+
// TODO `...` required only for webpack@4
304+
const updatedAssetInfo = {
305+
...info,
306+
related: { ...info.related, sourceMap: null },
307+
};
308+
309+
CompressionPlugin.updateAsset(
310+
compilation,
311+
name,
312+
inputSource,
313+
updatedAssetInfo
314+
);
315+
}
316+
303317
CompressionPlugin.deleteAsset(compilation, name);
304318
} else {
305319
// TODO `...` required only for webpack@4

src/options.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,15 @@
8585
},
8686
"deleteOriginalAssets": {
8787
"description": "Whether to delete the original assets or not.",
88-
"type": "boolean"
88+
"anyOf": [
89+
{
90+
"type": "boolean"
91+
},
92+
{
93+
"type": "string",
94+
"enum": ["keep-source-map"]
95+
}
96+
]
8997
},
9098
"filename": {
9199
"description": "The target asset filename.",

0 commit comments

Comments
 (0)