-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Log warning instead of crash if image optimizer fails (#7119)
- Loading branch information
1 parent
806bebe
commit da998f7
Showing
4 changed files
with
49 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |