Skip to content

Commit

Permalink
feat: support the deleteOriginalAssets option as a function (#380)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Feb 27, 2024
1 parent 4c9f6f4 commit 1be8955
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 66 deletions.
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,10 @@ module.exports = {
Type:

```ts
type deleteOriginalAssets = boolean | "keep-source-map";
type deleteOriginalAssets =
| boolean
| "keep-source-map"
| ((name: string) => boolean);
```

Default: `false`
Expand All @@ -400,7 +403,7 @@ module.exports = {
};
```

To exclude sourcemaps from compression
To exclude sourcemaps from compression:

```js
module.exports = {
Expand All @@ -413,6 +416,25 @@ module.exports = {
};
```

Using a custom function:

```js
module.exports = {
plugins: [
new CompressionPlugin({
exclude: /.map$/,
deleteOriginalAssets: (name) => {
if (/\.js$/.test(name)) {
return false;
}

return true;
},
}),
],
};
```

## Examples

### Using Zopfli
Expand Down
14 changes: 11 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const schema = require("./options.json");
*/

/**
* @typedef {boolean | "keep-source-map"} DeleteOriginalAssets
* @typedef {boolean | "keep-source-map" | ((name: string) => boolean)} DeleteOriginalAssets
*/

/**
Expand Down Expand Up @@ -378,9 +378,17 @@ class CompressionPlugin {
// @ts-ignore
related: { sourceMap: null },
});
}

compilation.deleteAsset(name);
compilation.deleteAsset(name);
} else if (
typeof this.options.deleteOriginalAssets === "function"
) {
if (this.options.deleteOriginalAssets(name)) {
compilation.deleteAsset(name);
}
} else {
compilation.deleteAsset(name);
}
} else {
compilation.updateAsset(name, source, {
related: { [relatedName]: newFilename },
Expand Down
3 changes: 3 additions & 0 deletions src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@
{
"type": "boolean"
},
{
"instanceof": "Function"
},
{
"enum": ["keep-source-map"]
}
Expand Down
24 changes: 12 additions & 12 deletions test/CompressionPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ describe("CompressionPlugin", () => {
{
output: {
path: `${__dirname}/dist`,
filename: "[name].js?var=[hash]",
chunkFilename: "[id].[name].js?ver=[hash]",
filename: "[name].js?var=[contenthash]",
chunkFilename: "[id].[name].js?ver=[contenthash]",
},
},
);
Expand All @@ -48,8 +48,8 @@ describe("CompressionPlugin", () => {
devtool: "source-map",
output: {
path: `${__dirname}/dist`,
filename: "[name].js?var=[hash]",
chunkFilename: "[id].[name].js?ver=[hash]",
filename: "[name].js?var=[contenthash]",
chunkFilename: "[id].[name].js?ver=[contenthash]",
},
},
);
Expand All @@ -72,8 +72,8 @@ describe("CompressionPlugin", () => {
{
output: {
path: `${__dirname}/dist`,
filename: "[name].js?var=[hash]",
chunkFilename: "[id].[name].js?ver=[hash]",
filename: "[name].js?var=[contenthash]",
chunkFilename: "[id].[name].js?ver=[contenthash]",
},
module: {
rules: [
Expand Down Expand Up @@ -119,8 +119,8 @@ describe("CompressionPlugin", () => {
{
output: {
path: `${__dirname}/dist`,
filename: "[name].js?var=[hash]",
chunkFilename: "[id].[name].js?ver=[hash]",
filename: "[name].js?var=[contenthash]",
chunkFilename: "[id].[name].js?ver=[contenthash]",
},
},
);
Expand Down Expand Up @@ -484,8 +484,8 @@ describe("CompressionPlugin", () => {
{
output: {
path: `${__dirname}/dist`,
filename: "[name].js?var=[hash]",
chunkFilename: "[id].[name].js?ver=[hash]",
filename: "[name].js?var=[contenthash]",
chunkFilename: "[id].[name].js?ver=[contenthash]",
},
},
);
Expand All @@ -508,8 +508,8 @@ describe("CompressionPlugin", () => {
{
output: {
path: `${__dirname}/dist`,
filename: "[name].js?var=[hash]",
chunkFilename: "[id].[name].js?ver=[hash]",
filename: "[name].js?var=[contenthash]",
chunkFilename: "[id].[name].js?ver=[contenthash]",
},
},
);
Expand Down
88 changes: 44 additions & 44 deletions test/__snapshots__/CompressionPlugin.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1319,10 +1319,10 @@ exports[`CompressionPlugin should work child compilations: assets 1`] = `
},
],
[
"async.async.js?ver=59fa7d38343840d3f6ca",
"async.async.js?ver=cd3aed5809a2faa4eb5a",
194,
{
"fullhash": "59fa7d38343840d3f6ca",
"contenthash": "cd3aed5809a2faa4eb5a",
"immutable": true,
"javascriptModule": false,
"related": {
Expand Down Expand Up @@ -1352,24 +1352,24 @@ exports[`CompressionPlugin should work child compilations: assets 1`] = `
],
[
"main.js.gz",
4042,
4018,
{
"compressed": true,
"immutable": true,
"size": 4042,
"size": 4018,
},
],
[
"main.js?var=59fa7d38343840d3f6ca",
16258,
"main.js?var=87e4bf99b728fffc08a0",
16101,
{
"fullhash": "59fa7d38343840d3f6ca",
"contenthash": "87e4bf99b728fffc08a0",
"immutable": true,
"javascriptModule": false,
"related": {
"gzipped": "main.js.gz",
},
"size": 16258,
"size": 16101,
},
],
]
Expand Down Expand Up @@ -1511,10 +1511,10 @@ exports[`CompressionPlugin should work with assets info: assets 1`] = `
},
],
[
"async.async.js?ver=e30bb673a67a1e8f00d2",
"async.async.js?ver=ff0ad4032ae148f14da4",
194,
{
"fullhash": "e30bb673a67a1e8f00d2",
"contenthash": "ff0ad4032ae148f14da4",
"immutable": true,
"javascriptModule": false,
"related": {
Expand Down Expand Up @@ -1544,44 +1544,44 @@ exports[`CompressionPlugin should work with assets info: assets 1`] = `
],
[
"main.js.gz",
4078,
4070,
{
"compressed": true,
"immutable": true,
"size": 4078,
"size": 4070,
},
],
[
"main.js.map.gz",
4164,
4158,
{
"compressed": true,
"size": 4164,
"size": 4158,
},
],
[
"main.js.map?var=e30bb673a67a1e8f00d2",
13212,
"main.js.map?var=73bfdc1831ffc69a2d3f",
13104,
{
"development": true,
"related": {
"gzipped": "main.js.map.gz",
},
"size": 13212,
"size": 13104,
},
],
[
"main.js?var=e30bb673a67a1e8f00d2",
16319,
"main.js?var=73bfdc1831ffc69a2d3f",
16162,
{
"fullhash": "e30bb673a67a1e8f00d2",
"contenthash": "73bfdc1831ffc69a2d3f",
"immutable": true,
"javascriptModule": false,
"related": {
"gzipped": "main.js.gz",
"sourceMap": "main.js.map?var=e30bb673a67a1e8f00d2",
"sourceMap": "main.js.map?var=73bfdc1831ffc69a2d3f",
},
"size": 16319,
"size": 16162,
},
],
]
Expand Down Expand Up @@ -1712,10 +1712,10 @@ exports[`CompressionPlugin should work with multiple plugins: assets 1`] = `
},
],
[
"async.async.js?ver=5bff95cff1ab52803d05",
"async.async.js?ver=cd3aed5809a2faa4eb5a",
194,
{
"fullhash": "5bff95cff1ab52803d05",
"contenthash": "cd3aed5809a2faa4eb5a",
"immutable": true,
"javascriptModule": false,
"related": {
Expand All @@ -1729,45 +1729,45 @@ exports[`CompressionPlugin should work with multiple plugins: assets 1`] = `
],
[
"main.js.br",
3538,
3514,
{
"compressed": true,
"immutable": true,
"size": 3538,
"size": 3514,
},
],
[
"main.js.compress",
16261,
16104,
{
"compressed": true,
"immutable": true,
"size": 16261,
"size": 16104,
},
],
[
"main.js.custom?foo=bar#hash",
16261,
16104,
{
"compressed": true,
"immutable": true,
"size": 16261,
"size": 16104,
},
],
[
"main.js.gz",
4045,
4021,
{
"compressed": true,
"immutable": true,
"size": 4045,
"size": 4021,
},
],
[
"main.js?var=5bff95cff1ab52803d05",
16261,
"main.js?var=6d8b5bc920f6f6bff225",
16104,
{
"fullhash": "5bff95cff1ab52803d05",
"contenthash": "6d8b5bc920f6f6bff225",
"immutable": true,
"javascriptModule": false,
"related": {
Expand All @@ -1776,7 +1776,7 @@ exports[`CompressionPlugin should work with multiple plugins: assets 1`] = `
"customed": "main.js.custom?foo=bar#hash",
"gzipped": "main.js.gz",
},
"size": 16261,
"size": 16104,
},
],
]
Expand Down Expand Up @@ -1828,10 +1828,10 @@ exports[`CompressionPlugin should work: assets 1`] = `
},
],
[
"async.async.js?ver=5bff95cff1ab52803d05",
"async.async.js?ver=cd3aed5809a2faa4eb5a",
194,
{
"fullhash": "5bff95cff1ab52803d05",
"contenthash": "cd3aed5809a2faa4eb5a",
"immutable": true,
"javascriptModule": false,
"related": {
Expand All @@ -1842,24 +1842,24 @@ exports[`CompressionPlugin should work: assets 1`] = `
],
[
"main.js.gz",
4045,
4021,
{
"compressed": true,
"immutable": true,
"size": 4045,
"size": 4021,
},
],
[
"main.js?var=5bff95cff1ab52803d05",
16261,
"main.js?var=6d8b5bc920f6f6bff225",
16104,
{
"fullhash": "5bff95cff1ab52803d05",
"contenthash": "6d8b5bc920f6f6bff225",
"immutable": true,
"javascriptModule": false,
"related": {
"gzipped": "main.js.gz",
},
"size": 16261,
"size": 16104,
},
],
]
Expand Down
Loading

0 comments on commit 1be8955

Please sign in to comment.