Skip to content

Commit 317e2c9

Browse files
fix: usage multiple plugins and deleteOriginalAssets
1 parent 38851ea commit 317e2c9

File tree

3 files changed

+265
-4
lines changed

3 files changed

+265
-4
lines changed

src/index.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,20 @@ class CompressionPlugin {
279279
}
280280

281281
if (this.options.deleteOriginalAssets) {
282-
if (this.options.deleteOriginalAssets === "keep-source-map") {
283-
compilation.updateAsset(name, source, {
284-
related: { sourceMap: null },
285-
});
282+
const needKeepSourceMap =
283+
this.options.deleteOriginalAssets === "keep-source-map";
284+
const isSameFilanem = name === newName;
285+
286+
if (needKeepSourceMap || isSameFilanem) {
287+
const updatedAssetInfo = {};
288+
289+
if (isSameFilanem) {
290+
updatedAssetInfo.related = null;
291+
} else {
292+
updatedAssetInfo.related = { sourceMap: null };
293+
}
294+
295+
compilation.updateAsset(name, source, updatedAssetInfo);
286296
}
287297

288298
compilation.deleteAsset(name);

test/CompressionPlugin.test.js

+81
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,87 @@ describe("CompressionPlugin", () => {
412412
});
413413
});
414414

415+
it("should work with multiple plugins and using same filename", async () => {
416+
const compiler = getCompiler(
417+
"./entry.js",
418+
{},
419+
{
420+
output: {
421+
path: `${__dirname}/dist`,
422+
filename: "[name].js",
423+
chunkFilename: "[id].[name].js",
424+
},
425+
}
426+
);
427+
428+
new CompressionPlugin({
429+
algorithm: "brotliCompress",
430+
filename: "[path]/br/[file]",
431+
minRatio: Infinity,
432+
}).apply(compiler);
433+
new CompressionPlugin({
434+
algorithm: "gzip",
435+
filename: "[file]",
436+
minRatio: Infinity,
437+
deleteOriginalAssets: true,
438+
}).apply(compiler);
439+
440+
await new Promise(async (resolve) => {
441+
const newStats = await compile(compiler);
442+
443+
// expect(newStats.compilation.emittedAssets.size).toBe(8);
444+
445+
expect(getAssetsNameAndSize(newStats, compiler)).toMatchSnapshot(
446+
"assets"
447+
);
448+
expect(getWarnings(newStats)).toMatchSnapshot("errors");
449+
expect(getErrors(newStats)).toMatchSnapshot("warnings");
450+
451+
resolve();
452+
});
453+
});
454+
455+
it("should work with multiple plugins and using same filename and keep source maps", async () => {
456+
const compiler = getCompiler(
457+
"./entry.js",
458+
{},
459+
{
460+
devtool: "source-map",
461+
output: {
462+
path: `${__dirname}/dist`,
463+
filename: "[name].js",
464+
chunkFilename: "[id].[name].js",
465+
},
466+
}
467+
);
468+
469+
new CompressionPlugin({
470+
algorithm: "brotliCompress",
471+
filename: "[path]/br/[file]",
472+
minRatio: Infinity,
473+
}).apply(compiler);
474+
new CompressionPlugin({
475+
algorithm: "gzip",
476+
filename: "[file]",
477+
minRatio: Infinity,
478+
deleteOriginalAssets: "keep-source-map",
479+
}).apply(compiler);
480+
481+
await new Promise(async (resolve) => {
482+
const newStats = await compile(compiler);
483+
484+
// expect(newStats.compilation.emittedAssets.size).toBe(8);
485+
486+
expect(getAssetsNameAndSize(newStats, compiler)).toMatchSnapshot(
487+
"assets"
488+
);
489+
expect(getWarnings(newStats)).toMatchSnapshot("errors");
490+
expect(getErrors(newStats)).toMatchSnapshot("warnings");
491+
492+
resolve();
493+
});
494+
});
495+
415496
it('should work and do not use memory cache when the "cache" option is "false"', async () => {
416497
const compiler = getCompiler(
417498
"./entry.js",

test/__snapshots__/CompressionPlugin.test.js.snap

+170
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,176 @@ exports[`CompressionPlugin should work with assets info: errors 1`] = `Array []`
15911591

15921592
exports[`CompressionPlugin should work with assets info: warnings 1`] = `Array []`;
15931593

1594+
exports[`CompressionPlugin should work with multiple plugins and using same filename and keep source maps: assets 1`] = `
1595+
Array [
1596+
Array [
1597+
"/br/09a1a1112c577c2794359715edfcb5ac.png",
1598+
73329,
1599+
Object {
1600+
"compressed": true,
1601+
"immutable": true,
1602+
"size": 73329,
1603+
},
1604+
],
1605+
Array [
1606+
"/br/23fc1d3ac606d117e05a140e0de79806.svg",
1607+
363,
1608+
Object {
1609+
"compressed": true,
1610+
"immutable": true,
1611+
"size": 363,
1612+
},
1613+
],
1614+
Array [
1615+
"/br/async.async.js",
1616+
123,
1617+
Object {
1618+
"compressed": true,
1619+
"size": 123,
1620+
},
1621+
],
1622+
Array [
1623+
"/br/main.js",
1624+
3493,
1625+
Object {
1626+
"compressed": true,
1627+
"size": 3493,
1628+
},
1629+
],
1630+
Array [
1631+
"/br/main.js.map",
1632+
3582,
1633+
Object {
1634+
"compressed": true,
1635+
"size": 3582,
1636+
},
1637+
],
1638+
Array [
1639+
"09a1a1112c577c2794359715edfcb5ac.png",
1640+
73160,
1641+
Object {
1642+
"compressed": true,
1643+
"immutable": true,
1644+
"size": 73160,
1645+
},
1646+
],
1647+
Array [
1648+
"23fc1d3ac606d117e05a140e0de79806.svg",
1649+
393,
1650+
Object {
1651+
"compressed": true,
1652+
"immutable": true,
1653+
"size": 393,
1654+
},
1655+
],
1656+
Array [
1657+
"async.async.js",
1658+
134,
1659+
Object {
1660+
"compressed": true,
1661+
"size": 134,
1662+
},
1663+
],
1664+
Array [
1665+
"main.js",
1666+
3978,
1667+
Object {
1668+
"compressed": true,
1669+
"size": 3978,
1670+
},
1671+
],
1672+
Array [
1673+
"main.js.map",
1674+
4081,
1675+
Object {
1676+
"compressed": true,
1677+
"size": 4081,
1678+
},
1679+
],
1680+
]
1681+
`;
1682+
1683+
exports[`CompressionPlugin should work with multiple plugins and using same filename and keep source maps: errors 1`] = `Array []`;
1684+
1685+
exports[`CompressionPlugin should work with multiple plugins and using same filename and keep source maps: warnings 1`] = `Array []`;
1686+
1687+
exports[`CompressionPlugin should work with multiple plugins and using same filename: assets 1`] = `
1688+
Array [
1689+
Array [
1690+
"/br/09a1a1112c577c2794359715edfcb5ac.png",
1691+
73329,
1692+
Object {
1693+
"compressed": true,
1694+
"immutable": true,
1695+
"size": 73329,
1696+
},
1697+
],
1698+
Array [
1699+
"/br/23fc1d3ac606d117e05a140e0de79806.svg",
1700+
363,
1701+
Object {
1702+
"compressed": true,
1703+
"immutable": true,
1704+
"size": 363,
1705+
},
1706+
],
1707+
Array [
1708+
"/br/async.async.js",
1709+
123,
1710+
Object {
1711+
"compressed": true,
1712+
"size": 123,
1713+
},
1714+
],
1715+
Array [
1716+
"/br/main.js",
1717+
3458,
1718+
Object {
1719+
"compressed": true,
1720+
"size": 3458,
1721+
},
1722+
],
1723+
Array [
1724+
"09a1a1112c577c2794359715edfcb5ac.png",
1725+
73160,
1726+
Object {
1727+
"compressed": true,
1728+
"immutable": true,
1729+
"size": 73160,
1730+
},
1731+
],
1732+
Array [
1733+
"23fc1d3ac606d117e05a140e0de79806.svg",
1734+
393,
1735+
Object {
1736+
"compressed": true,
1737+
"immutable": true,
1738+
"size": 393,
1739+
},
1740+
],
1741+
Array [
1742+
"async.async.js",
1743+
134,
1744+
Object {
1745+
"compressed": true,
1746+
"size": 134,
1747+
},
1748+
],
1749+
Array [
1750+
"main.js",
1751+
3954,
1752+
Object {
1753+
"compressed": true,
1754+
"size": 3954,
1755+
},
1756+
],
1757+
]
1758+
`;
1759+
1760+
exports[`CompressionPlugin should work with multiple plugins and using same filename: errors 1`] = `Array []`;
1761+
1762+
exports[`CompressionPlugin should work with multiple plugins and using same filename: warnings 1`] = `Array []`;
1763+
15941764
exports[`CompressionPlugin should work with multiple plugins: assets 1`] = `
15951765
Array [
15961766
Array [

0 commit comments

Comments
 (0)