Skip to content

Commit

Permalink
fix: avoid renaming unsupported formats (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Sep 5, 2022
1 parent c897d30 commit 18e30ef
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ async function imageminGenerate(original, minimizerOptions) {
`Error with '${original.filename}': ${originalError.message}`
);

original.info.original = true;
original.errors.push(newError);

return original;
Expand Down Expand Up @@ -645,6 +646,7 @@ async function imageminMinify(original, options) {
`Error with '${original.filename}': ${originalError.message}`
);

original.info.original = true;
original.errors.push(newError);

return original;
Expand All @@ -655,6 +657,7 @@ async function imageminMinify(original, options) {
const { ext: extOutput } = fileTypeFromBuffer(result) || {};

if (extOutput && extInput !== extOutput) {
original.info.original = true;
original.warnings.push(
new Error(
`"imageminMinify" function do not support generate to "${extOutput}" from "${original.filename}". Please use "imageminGenerate" function.`
Expand Down Expand Up @@ -778,6 +781,7 @@ async function squooshGenerate(original, minifyOptions) {
`Error with '${original.filename}': ${originalError.message}`
);

original.info.original = true;
original.errors.push(newError);

return original;
Expand All @@ -788,6 +792,8 @@ async function squooshGenerate(original, minifyOptions) {
}

if (Object.keys(image.encodedWith).length === 0) {
// eslint-disable-next-line require-atomic-updates
original.info.original = true;
original.errors.push(
new Error(
`No result from 'squoosh' for '${original.filename}', please configure the 'encodeOptions' option to generate images`
Expand All @@ -798,6 +804,8 @@ async function squooshGenerate(original, minifyOptions) {
}

if (Object.keys(image.encodedWith).length > 1) {
// eslint-disable-next-line require-atomic-updates
original.info.original = true;
original.errors.push(
new Error(
`Multiple values for the 'encodeOptions' option is not supported for '${original.filename}', specify only one codec for the generator`
Expand Down Expand Up @@ -864,6 +872,8 @@ async function squooshMinify(original, options) {
const targetCodec = targets[ext];

if (!targetCodec) {
original.info.original = true;

return original;
}

Expand Down Expand Up @@ -909,6 +919,7 @@ async function squooshMinify(original, options) {
`Error with '${original.filename}': ${originalError.message}`
);

original.info.original = true;
original.errors.push(newError);

return original;
Expand Down Expand Up @@ -1010,6 +1021,8 @@ async function sharpTransform(
const inputExt = path.extname(original.filename).slice(1).toLowerCase();

if (!SHARP_FORMATS.has(inputExt)) {
original.info.original = true;

return original;
}

Expand Down Expand Up @@ -1112,6 +1125,7 @@ function sharpGenerate(original, minimizerOptions) {
`No result from 'sharp' for '${original.filename}', please configure the 'encodeOptions' option to generate images`
);

original.info.original = true;
original.errors.push(error);

return Promise.resolve(original);
Expand All @@ -1122,6 +1136,7 @@ function sharpGenerate(original, minimizerOptions) {
`Multiple values for the 'encodeOptions' option is not supported for '${original.filename}', specify only one codec for the generator`
);

original.info.original = true;
original.errors.push(error);

return Promise.resolve(original);
Expand Down
7 changes: 6 additions & 1 deletion src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,18 @@ async function worker(options) {

if (
typeof filename !== "undefined" &&
typeof options.generateFilename !== "undefined"
typeof options.generateFilename !== "undefined" &&
!item.info.original
) {
item.filename = options.generateFilename(filename, {
filename: item.filename,
});
}

if (item.info.original) {
delete item.info.original;
}

return item;
};

Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/svg-and-jpg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.log(new URL("../fixtures/loader-test.svg", import.meta.url));
console.log(new URL("../fixtures/loader-test.jpg", import.meta.url));
30 changes: 30 additions & 0 deletions test/plugin-minimizer-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -633,4 +633,34 @@ describe("plugin minify option", () => {
expect(warnings).toHaveLength(0);
expect(errors).toHaveLength(0);
});

it("should work with 'sharpMinify' minifier and don't rename unsupported asset", async () => {
const stats = await runWebpack({
entry: path.join(fixturesPath, "./svg-and-jpg.js"),
fileLoaderOff: true,
imageminPluginOptions: {
minimizer: {
implementation: ImageMinimizerPlugin.sharpMinify,
filename: "minified-xxx-[name]-yyy[ext]",
options: {
encodeOptions: { jpeg: { quality: 90 } },
},
},
},
});
const { compilation } = stats;
const { warnings, errors, assets } = compilation;

const originalAsset = Object.keys(assets).filter((asset) =>
asset.includes("loader-test.svg")
);
const minifiedAsset = Object.keys(assets).filter((asset) =>
asset.includes("minified-xxx-loader-test-yyy.jpg")
);

expect(originalAsset).toHaveLength(1);
expect(minifiedAsset).toHaveLength(1);
expect(warnings).toHaveLength(0);
expect(errors).toHaveLength(0);
});
});

0 comments on commit 18e30ef

Please sign in to comment.