Skip to content

Commit 1be8955

Browse files
feat: support the deleteOriginalAssets option as a function (#380)
1 parent 4c9f6f4 commit 1be8955

10 files changed

+186
-66
lines changed

README.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,10 @@ module.exports = {
381381
Type:
382382

383383
```ts
384-
type deleteOriginalAssets = boolean | "keep-source-map";
384+
type deleteOriginalAssets =
385+
| boolean
386+
| "keep-source-map"
387+
| ((name: string) => boolean);
385388
```
386389

387390
Default: `false`
@@ -400,7 +403,7 @@ module.exports = {
400403
};
401404
```
402405

403-
To exclude sourcemaps from compression
406+
To exclude sourcemaps from compression:
404407

405408
```js
406409
module.exports = {
@@ -413,6 +416,25 @@ module.exports = {
413416
};
414417
```
415418

419+
Using a custom function:
420+
421+
```js
422+
module.exports = {
423+
plugins: [
424+
new CompressionPlugin({
425+
exclude: /.map$/,
426+
deleteOriginalAssets: (name) => {
427+
if (/\.js$/.test(name)) {
428+
return false;
429+
}
430+
431+
return true;
432+
},
433+
}),
434+
],
435+
};
436+
```
437+
416438
## Examples
417439

418440
### Using Zopfli

src/index.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const schema = require("./options.json");
5858
*/
5959

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

6464
/**
@@ -378,9 +378,17 @@ class CompressionPlugin {
378378
// @ts-ignore
379379
related: { sourceMap: null },
380380
});
381-
}
382381

383-
compilation.deleteAsset(name);
382+
compilation.deleteAsset(name);
383+
} else if (
384+
typeof this.options.deleteOriginalAssets === "function"
385+
) {
386+
if (this.options.deleteOriginalAssets(name)) {
387+
compilation.deleteAsset(name);
388+
}
389+
} else {
390+
compilation.deleteAsset(name);
391+
}
384392
} else {
385393
compilation.updateAsset(name, source, {
386394
related: { [relatedName]: newFilename },

src/options.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@
9898
{
9999
"type": "boolean"
100100
},
101+
{
102+
"instanceof": "Function"
103+
},
101104
{
102105
"enum": ["keep-source-map"]
103106
}

test/CompressionPlugin.test.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ describe("CompressionPlugin", () => {
2525
{
2626
output: {
2727
path: `${__dirname}/dist`,
28-
filename: "[name].js?var=[hash]",
29-
chunkFilename: "[id].[name].js?ver=[hash]",
28+
filename: "[name].js?var=[contenthash]",
29+
chunkFilename: "[id].[name].js?ver=[contenthash]",
3030
},
3131
},
3232
);
@@ -48,8 +48,8 @@ describe("CompressionPlugin", () => {
4848
devtool: "source-map",
4949
output: {
5050
path: `${__dirname}/dist`,
51-
filename: "[name].js?var=[hash]",
52-
chunkFilename: "[id].[name].js?ver=[hash]",
51+
filename: "[name].js?var=[contenthash]",
52+
chunkFilename: "[id].[name].js?ver=[contenthash]",
5353
},
5454
},
5555
);
@@ -72,8 +72,8 @@ describe("CompressionPlugin", () => {
7272
{
7373
output: {
7474
path: `${__dirname}/dist`,
75-
filename: "[name].js?var=[hash]",
76-
chunkFilename: "[id].[name].js?ver=[hash]",
75+
filename: "[name].js?var=[contenthash]",
76+
chunkFilename: "[id].[name].js?ver=[contenthash]",
7777
},
7878
module: {
7979
rules: [
@@ -119,8 +119,8 @@ describe("CompressionPlugin", () => {
119119
{
120120
output: {
121121
path: `${__dirname}/dist`,
122-
filename: "[name].js?var=[hash]",
123-
chunkFilename: "[id].[name].js?ver=[hash]",
122+
filename: "[name].js?var=[contenthash]",
123+
chunkFilename: "[id].[name].js?ver=[contenthash]",
124124
},
125125
},
126126
);
@@ -484,8 +484,8 @@ describe("CompressionPlugin", () => {
484484
{
485485
output: {
486486
path: `${__dirname}/dist`,
487-
filename: "[name].js?var=[hash]",
488-
chunkFilename: "[id].[name].js?ver=[hash]",
487+
filename: "[name].js?var=[contenthash]",
488+
chunkFilename: "[id].[name].js?ver=[contenthash]",
489489
},
490490
},
491491
);
@@ -508,8 +508,8 @@ describe("CompressionPlugin", () => {
508508
{
509509
output: {
510510
path: `${__dirname}/dist`,
511-
filename: "[name].js?var=[hash]",
512-
chunkFilename: "[id].[name].js?ver=[hash]",
511+
filename: "[name].js?var=[contenthash]",
512+
chunkFilename: "[id].[name].js?ver=[contenthash]",
513513
},
514514
},
515515
);

test/__snapshots__/CompressionPlugin.test.js.snap

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,10 +1319,10 @@ exports[`CompressionPlugin should work child compilations: assets 1`] = `
13191319
},
13201320
],
13211321
[
1322-
"async.async.js?ver=59fa7d38343840d3f6ca",
1322+
"async.async.js?ver=cd3aed5809a2faa4eb5a",
13231323
194,
13241324
{
1325-
"fullhash": "59fa7d38343840d3f6ca",
1325+
"contenthash": "cd3aed5809a2faa4eb5a",
13261326
"immutable": true,
13271327
"javascriptModule": false,
13281328
"related": {
@@ -1352,24 +1352,24 @@ exports[`CompressionPlugin should work child compilations: assets 1`] = `
13521352
],
13531353
[
13541354
"main.js.gz",
1355-
4042,
1355+
4018,
13561356
{
13571357
"compressed": true,
13581358
"immutable": true,
1359-
"size": 4042,
1359+
"size": 4018,
13601360
},
13611361
],
13621362
[
1363-
"main.js?var=59fa7d38343840d3f6ca",
1364-
16258,
1363+
"main.js?var=87e4bf99b728fffc08a0",
1364+
16101,
13651365
{
1366-
"fullhash": "59fa7d38343840d3f6ca",
1366+
"contenthash": "87e4bf99b728fffc08a0",
13671367
"immutable": true,
13681368
"javascriptModule": false,
13691369
"related": {
13701370
"gzipped": "main.js.gz",
13711371
},
1372-
"size": 16258,
1372+
"size": 16101,
13731373
},
13741374
],
13751375
]
@@ -1511,10 +1511,10 @@ exports[`CompressionPlugin should work with assets info: assets 1`] = `
15111511
},
15121512
],
15131513
[
1514-
"async.async.js?ver=e30bb673a67a1e8f00d2",
1514+
"async.async.js?ver=ff0ad4032ae148f14da4",
15151515
194,
15161516
{
1517-
"fullhash": "e30bb673a67a1e8f00d2",
1517+
"contenthash": "ff0ad4032ae148f14da4",
15181518
"immutable": true,
15191519
"javascriptModule": false,
15201520
"related": {
@@ -1544,44 +1544,44 @@ exports[`CompressionPlugin should work with assets info: assets 1`] = `
15441544
],
15451545
[
15461546
"main.js.gz",
1547-
4078,
1547+
4070,
15481548
{
15491549
"compressed": true,
15501550
"immutable": true,
1551-
"size": 4078,
1551+
"size": 4070,
15521552
},
15531553
],
15541554
[
15551555
"main.js.map.gz",
1556-
4164,
1556+
4158,
15571557
{
15581558
"compressed": true,
1559-
"size": 4164,
1559+
"size": 4158,
15601560
},
15611561
],
15621562
[
1563-
"main.js.map?var=e30bb673a67a1e8f00d2",
1564-
13212,
1563+
"main.js.map?var=73bfdc1831ffc69a2d3f",
1564+
13104,
15651565
{
15661566
"development": true,
15671567
"related": {
15681568
"gzipped": "main.js.map.gz",
15691569
},
1570-
"size": 13212,
1570+
"size": 13104,
15711571
},
15721572
],
15731573
[
1574-
"main.js?var=e30bb673a67a1e8f00d2",
1575-
16319,
1574+
"main.js?var=73bfdc1831ffc69a2d3f",
1575+
16162,
15761576
{
1577-
"fullhash": "e30bb673a67a1e8f00d2",
1577+
"contenthash": "73bfdc1831ffc69a2d3f",
15781578
"immutable": true,
15791579
"javascriptModule": false,
15801580
"related": {
15811581
"gzipped": "main.js.gz",
1582-
"sourceMap": "main.js.map?var=e30bb673a67a1e8f00d2",
1582+
"sourceMap": "main.js.map?var=73bfdc1831ffc69a2d3f",
15831583
},
1584-
"size": 16319,
1584+
"size": 16162,
15851585
},
15861586
],
15871587
]
@@ -1712,10 +1712,10 @@ exports[`CompressionPlugin should work with multiple plugins: assets 1`] = `
17121712
},
17131713
],
17141714
[
1715-
"async.async.js?ver=5bff95cff1ab52803d05",
1715+
"async.async.js?ver=cd3aed5809a2faa4eb5a",
17161716
194,
17171717
{
1718-
"fullhash": "5bff95cff1ab52803d05",
1718+
"contenthash": "cd3aed5809a2faa4eb5a",
17191719
"immutable": true,
17201720
"javascriptModule": false,
17211721
"related": {
@@ -1729,45 +1729,45 @@ exports[`CompressionPlugin should work with multiple plugins: assets 1`] = `
17291729
],
17301730
[
17311731
"main.js.br",
1732-
3538,
1732+
3514,
17331733
{
17341734
"compressed": true,
17351735
"immutable": true,
1736-
"size": 3538,
1736+
"size": 3514,
17371737
},
17381738
],
17391739
[
17401740
"main.js.compress",
1741-
16261,
1741+
16104,
17421742
{
17431743
"compressed": true,
17441744
"immutable": true,
1745-
"size": 16261,
1745+
"size": 16104,
17461746
},
17471747
],
17481748
[
17491749
"main.js.custom?foo=bar#hash",
1750-
16261,
1750+
16104,
17511751
{
17521752
"compressed": true,
17531753
"immutable": true,
1754-
"size": 16261,
1754+
"size": 16104,
17551755
},
17561756
],
17571757
[
17581758
"main.js.gz",
1759-
4045,
1759+
4021,
17601760
{
17611761
"compressed": true,
17621762
"immutable": true,
1763-
"size": 4045,
1763+
"size": 4021,
17641764
},
17651765
],
17661766
[
1767-
"main.js?var=5bff95cff1ab52803d05",
1768-
16261,
1767+
"main.js?var=6d8b5bc920f6f6bff225",
1768+
16104,
17691769
{
1770-
"fullhash": "5bff95cff1ab52803d05",
1770+
"contenthash": "6d8b5bc920f6f6bff225",
17711771
"immutable": true,
17721772
"javascriptModule": false,
17731773
"related": {
@@ -1776,7 +1776,7 @@ exports[`CompressionPlugin should work with multiple plugins: assets 1`] = `
17761776
"customed": "main.js.custom?foo=bar#hash",
17771777
"gzipped": "main.js.gz",
17781778
},
1779-
"size": 16261,
1779+
"size": 16104,
17801780
},
17811781
],
17821782
]
@@ -1828,10 +1828,10 @@ exports[`CompressionPlugin should work: assets 1`] = `
18281828
},
18291829
],
18301830
[
1831-
"async.async.js?ver=5bff95cff1ab52803d05",
1831+
"async.async.js?ver=cd3aed5809a2faa4eb5a",
18321832
194,
18331833
{
1834-
"fullhash": "5bff95cff1ab52803d05",
1834+
"contenthash": "cd3aed5809a2faa4eb5a",
18351835
"immutable": true,
18361836
"javascriptModule": false,
18371837
"related": {
@@ -1842,24 +1842,24 @@ exports[`CompressionPlugin should work: assets 1`] = `
18421842
],
18431843
[
18441844
"main.js.gz",
1845-
4045,
1845+
4021,
18461846
{
18471847
"compressed": true,
18481848
"immutable": true,
1849-
"size": 4045,
1849+
"size": 4021,
18501850
},
18511851
],
18521852
[
1853-
"main.js?var=5bff95cff1ab52803d05",
1854-
16261,
1853+
"main.js?var=6d8b5bc920f6f6bff225",
1854+
16104,
18551855
{
1856-
"fullhash": "5bff95cff1ab52803d05",
1856+
"contenthash": "6d8b5bc920f6f6bff225",
18571857
"immutable": true,
18581858
"javascriptModule": false,
18591859
"related": {
18601860
"gzipped": "main.js.gz",
18611861
},
1862-
"size": 16261,
1862+
"size": 16104,
18631863
},
18641864
],
18651865
]

0 commit comments

Comments
 (0)