/
ImageOptimizer.js
36 lines (32 loc) 路 1 KB
/
ImageOptimizer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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, logger}) {
if (!bundle.env.shouldOptimize) {
return {contents};
}
let buffer = await blobToBuffer(contents);
// 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);