Skip to content
Open
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
18 changes: 14 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,20 @@ class CompressionPlugin {
}

if (this.options.deleteOriginalAssets) {
if (this.options.deleteOriginalAssets === "keep-source-map") {
compilation.updateAsset(name, source, {
related: { sourceMap: null },
});
const needKeepSourceMap =
this.options.deleteOriginalAssets === "keep-source-map";
const isSameFilanem = name === newName;

if (needKeepSourceMap || isSameFilanem) {
const updatedAssetInfo = {};

if (isSameFilanem) {
updatedAssetInfo.related = null;
} else {
updatedAssetInfo.related = { sourceMap: null };
}

compilation.updateAsset(name, source, updatedAssetInfo);
}

compilation.deleteAsset(name);
Expand Down
81 changes: 81 additions & 0 deletions test/CompressionPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,87 @@ describe("CompressionPlugin", () => {
});
});

it("should work with multiple plugins and using same filename", async () => {
const compiler = getCompiler(
"./entry.js",
{},
{
output: {
path: `${__dirname}/dist`,
filename: "[name].js",
chunkFilename: "[id].[name].js",
},
}
);

new CompressionPlugin({
algorithm: "brotliCompress",
filename: "[path]/br/[file]",
minRatio: Infinity,
}).apply(compiler);
new CompressionPlugin({
algorithm: "gzip",
filename: "[file]",
minRatio: Infinity,
deleteOriginalAssets: true,
}).apply(compiler);

await new Promise(async (resolve) => {
const newStats = await compile(compiler);

// expect(newStats.compilation.emittedAssets.size).toBe(8);

expect(getAssetsNameAndSize(newStats, compiler)).toMatchSnapshot(
"assets"
);
expect(getWarnings(newStats)).toMatchSnapshot("errors");
expect(getErrors(newStats)).toMatchSnapshot("warnings");

resolve();
});
});

it("should work with multiple plugins and using same filename and keep source maps", async () => {
const compiler = getCompiler(
"./entry.js",
{},
{
devtool: "source-map",
output: {
path: `${__dirname}/dist`,
filename: "[name].js",
chunkFilename: "[id].[name].js",
},
}
);

new CompressionPlugin({
algorithm: "brotliCompress",
filename: "[path]/br/[file]",
minRatio: Infinity,
}).apply(compiler);
new CompressionPlugin({
algorithm: "gzip",
filename: "[file]",
minRatio: Infinity,
deleteOriginalAssets: "keep-source-map",
}).apply(compiler);

await new Promise(async (resolve) => {
const newStats = await compile(compiler);

// expect(newStats.compilation.emittedAssets.size).toBe(8);

expect(getAssetsNameAndSize(newStats, compiler)).toMatchSnapshot(
"assets"
);
expect(getWarnings(newStats)).toMatchSnapshot("errors");
expect(getErrors(newStats)).toMatchSnapshot("warnings");

resolve();
});
});

it('should work and do not use memory cache when the "cache" option is "false"', async () => {
const compiler = getCompiler(
"./entry.js",
Expand Down
170 changes: 170 additions & 0 deletions test/__snapshots__/CompressionPlugin.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1591,6 +1591,176 @@ exports[`CompressionPlugin should work with assets info: errors 1`] = `Array []`

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

exports[`CompressionPlugin should work with multiple plugins and using same filename and keep source maps: assets 1`] = `
Array [
Array [
"/br/09a1a1112c577c2794359715edfcb5ac.png",
73329,
Object {
"compressed": true,
"immutable": true,
"size": 73329,
},
],
Array [
"/br/23fc1d3ac606d117e05a140e0de79806.svg",
363,
Object {
"compressed": true,
"immutable": true,
"size": 363,
},
],
Array [
"/br/async.async.js",
123,
Object {
"compressed": true,
"size": 123,
},
],
Array [
"/br/main.js",
3465,
Object {
"compressed": true,
"size": 3465,
},
],
Array [
"/br/main.js.map",
3578,
Object {
"compressed": true,
"size": 3578,
},
],
Array [
"09a1a1112c577c2794359715edfcb5ac.png",
73160,
Object {
"compressed": true,
"immutable": true,
"size": 73160,
},
],
Array [
"23fc1d3ac606d117e05a140e0de79806.svg",
393,
Object {
"compressed": true,
"immutable": true,
"size": 393,
},
],
Array [
"async.async.js",
134,
Object {
"compressed": true,
"size": 134,
},
],
Array [
"main.js",
3969,
Object {
"compressed": true,
"size": 3969,
},
],
Array [
"main.js.map",
4062,
Object {
"compressed": true,
"size": 4062,
},
],
]
`;

exports[`CompressionPlugin should work with multiple plugins and using same filename and keep source maps: errors 1`] = `Array []`;

exports[`CompressionPlugin should work with multiple plugins and using same filename and keep source maps: warnings 1`] = `Array []`;

exports[`CompressionPlugin should work with multiple plugins and using same filename: assets 1`] = `
Array [
Array [
"/br/09a1a1112c577c2794359715edfcb5ac.png",
73329,
Object {
"compressed": true,
"immutable": true,
"size": 73329,
},
],
Array [
"/br/23fc1d3ac606d117e05a140e0de79806.svg",
363,
Object {
"compressed": true,
"immutable": true,
"size": 363,
},
],
Array [
"/br/async.async.js",
123,
Object {
"compressed": true,
"size": 123,
},
],
Array [
"/br/main.js",
3442,
Object {
"compressed": true,
"size": 3442,
},
],
Array [
"09a1a1112c577c2794359715edfcb5ac.png",
73160,
Object {
"compressed": true,
"immutable": true,
"size": 73160,
},
],
Array [
"23fc1d3ac606d117e05a140e0de79806.svg",
393,
Object {
"compressed": true,
"immutable": true,
"size": 393,
},
],
Array [
"async.async.js",
134,
Object {
"compressed": true,
"size": 134,
},
],
Array [
"main.js",
3946,
Object {
"compressed": true,
"size": 3946,
},
],
]
`;

exports[`CompressionPlugin should work with multiple plugins and using same filename: errors 1`] = `Array []`;

exports[`CompressionPlugin should work with multiple plugins and using same filename: warnings 1`] = `Array []`;

exports[`CompressionPlugin should work with multiple plugins: assets 1`] = `
Array [
Array [
Expand Down