Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option to keep source maps #216

Merged
merged 6 commits into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 },
};
Copy link
Member

@alexander-akait alexander-akait Nov 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do it better:

delete updatedAssetInfo.related.sourceMap;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this, this is not working. The sourceMap is getting added back if a value is not provided.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Webpack I think is doing a smart merge there. 🤔


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