Skip to content

Commit

Permalink
feat: added the keep-source-maps value to the `deleteOriginalAssets…
Browse files Browse the repository at this point in the history
…` option (#216)
  • Loading branch information
sibiraj-s authored Nov 9, 2020
1 parent 37b53e7 commit bd60650
Show file tree
Hide file tree
Showing 20 changed files with 366 additions and 239 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ And run `webpack` via your preferred method.
| **[`threshold`](#threshold)** | `{Number}` | `0` | Only assets bigger than this size are processed (in bytes) |
| **[`minRatio`](#minratio)** | `{Number}` | `0.8` | Only assets that compress better than this ratio are processed (`minRatio = Compressed Size / Original Size`) |
| **[`filename`](#filename)** | `{String\|Function}` | `[path][base].gz` | The target asset filename |
| **[`deleteOriginalAssets`](#deleteoriginalassets)** | `{Boolean}` | `false` | Whether to delete the original assets or not |
| **[`deleteOriginalAssets`](#deleteoriginalassets)** | `{Boolean\|'keep-source-map'}` | `false` | Whether to delete the original assets or not |
| **[`cache`](#cache)** | `{Boolean}` | `true` | Enable file caching |

### `test`
Expand Down Expand Up @@ -289,7 +289,7 @@ module.exports = {

### `deleteOriginalAssets`

Type: `Boolean`
Type: `Boolean | 'keep-source-map'`
Default: `false`

Whether to delete the original assets or not.
Expand All @@ -306,6 +306,19 @@ module.exports = {
};
```

To exclude sourcemaps from compression

```js
module.exports = {
plugins: [
new CompressionPlugin({
exclude: /.map$/
deleteOriginalAssets: 'keep-source-map',
}),
],
};
```

### `cache`

> ⚠ Ignored in webpack 5! Please use https://webpack.js.org/configuration/other-options/#cache.
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"npm-run-all": "^4.1.5",
"prettier": "^2.1.2",
"standard-version": "^9.0.0",
"webpack": "^5.3.2"
"webpack": "^5.4.0"
},
"keywords": [
"webpack"
Expand Down
16 changes: 15 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,21 @@ class CompressionPlugin {
}

if (this.options.deleteOriginalAssets) {
// eslint-disable-next-line no-param-reassign
if (this.options.deleteOriginalAssets === 'keep-source-map') {
// TODO `...` required only for webpack@4
const updatedAssetInfo = {
...info,
related: { ...info.related, sourceMap: null },
};

CompressionPlugin.updateAsset(
compilation,
name,
inputSource,
updatedAssetInfo
);
}

CompressionPlugin.deleteAsset(compilation, name);
} else {
// TODO `...` required only for webpack@4
Expand Down
10 changes: 9 additions & 1 deletion src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,15 @@
},
"deleteOriginalAssets": {
"description": "Whether to delete the original assets or not.",
"type": "boolean"
"anyOf": [
{
"type": "boolean"
},
{
"type": "string",
"enum": ["keep-source-map"]
}
]
},
"filename": {
"description": "The target asset filename.",
Expand Down
Loading

0 comments on commit bd60650

Please sign in to comment.