Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log warning instead of crash if image optimizer fails #7119

Merged
merged 4 commits into from Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/core/integration-tests/test/image.js
Expand Up @@ -113,6 +113,31 @@ describe('image', function() {
assert(output.length < input.length);
});

it('should lossless optimise progressive JPEGs', async function() {
let img = path.join(__dirname, '/integration/image/banana.jpg');
let b = await bundle(img, {
defaultTargetOptions: {
shouldOptimize: true,
},
logLevel: 'verbose',
});

const imagePath = b.getBundles().find(b => b.type === 'jpg').filePath;

// let input = await inputFS.readFile(img);
// let inputRaw = await sharp(input)
// .toFormat('raw')
// .toBuffer();
// Check validity of image
let output = await outputFS.readFile(imagePath);
await sharp(output)
.toFormat('raw')
.toBuffer();

// assert(outputRaw.equals(inputRaw));
// assert(output.length < input.length);
});

it('should lossless optimise PNGs', async function() {
let img = path.join(__dirname, '/integration/image/clock.png');
let b = await bundle(img, {
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions packages/optimizers/image/package.json
Expand Up @@ -34,6 +34,7 @@
"dependencies": {
"@parcel/plugin": "^2.0.0",
"@parcel/utils": "^2.0.0",
"@parcel/diagnostic": "^2.0.0",
"detect-libc": "^1.0.3"
},
"devDependencies": {
Expand Down
28 changes: 23 additions & 5 deletions packages/optimizers/image/src/ImageOptimizer.js
@@ -1,18 +1,36 @@
// @flow
import path from 'path';
import process from 'process';
import {Optimizer} from '@parcel/plugin';
import {blobToBuffer} from '@parcel/utils';
import {md} from '@parcel/diagnostic';
import {optimize} from '../native';

export default (new Optimizer({
async optimize({bundle, contents}) {
async optimize({bundle, contents, logger}) {
if (!bundle.env.shouldOptimize) {
return {contents};
}

let buffer = await blobToBuffer(contents);
let optimized = optimize(bundle.type, buffer);
return {
contents: optimized.length < buffer.length ? optimized : buffer,
};

// Attempt to optimize it, if the optimize fails we log a warning...
try {
let optimized = optimize(bundle.type, buffer);
return {
contents: optimized.length < buffer.length ? optimized : buffer,
};
} catch (err) {
const filepath = bundle.getMainEntry()?.filePath;
const filename = filepath
? path.relative(process.cwd(), filepath)
: 'unknown';
logger.warn({
message: md`Could not optimize image ${filename}: ${err.message}`,
stack: err.stack,
});
}

return {contents: buffer};
},
}): Optimizer);