Skip to content

Commit b858801

Browse files
authored
Lossless image optimizer for PNGs and JPEGs (#6762)
1 parent 1452a30 commit b858801

File tree

15 files changed

+820
-6
lines changed

15 files changed

+820
-6
lines changed

β€ŽCargo.lockβ€Ž

Lines changed: 521 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€ŽCargo.tomlβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ members = [
55
"packages/transformers/js/wasm",
66
"packages/utils/fs-search",
77
"packages/utils/hash",
8+
"packages/optimizers/image"
89
]

β€Žpackages/configs/default/index.jsonβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@
5454
"*.css": ["@parcel/optimizer-cssnano"],
5555
"*.html": ["@parcel/optimizer-htmlnano"],
5656
"*.{js,mjs,cjs}": ["@parcel/optimizer-terser"],
57-
"*.svg": ["@parcel/optimizer-svgo"]
57+
"*.svg": ["@parcel/optimizer-svgo"],
58+
"*.{jpg,jpeg,png}": ["@parcel/optimizer-image"]
5859
},
5960
"packagers": {
6061
"*.html": "@parcel/packager-html",

β€Žpackages/configs/default/package.jsonβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"@parcel/namer-default": "2.0.0-rc.0",
2323
"@parcel/optimizer-cssnano": "2.0.0-rc.0",
2424
"@parcel/optimizer-htmlnano": "2.0.0-rc.0",
25+
"@parcel/optimizer-image": "2.0.0-rc.0",
2526
"@parcel/optimizer-svgo": "2.0.0-rc.0",
2627
"@parcel/optimizer-terser": "2.0.0-rc.0",
2728
"@parcel/packager-css": "2.0.0-rc.0",

β€Žpackages/core/integration-tests/test/image.jsβ€Ž

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from 'assert';
2-
import {bundle, distDir, outputFS} from '@parcel/test-utils';
2+
import {bundle, distDir, inputFS, outputFS} from '@parcel/test-utils';
33
import path from 'path';
44
import sharp from 'sharp';
55

@@ -88,4 +88,50 @@ describe('image', function() {
8888
);
8989
});
9090
});
91+
92+
it('should optimise JPEGs', async function() {
93+
let img = path.join(__dirname, '/integration/image/image.jpg');
94+
let b = await bundle(img, {
95+
defaultTargetOptions: {
96+
shouldOptimize: true,
97+
},
98+
});
99+
100+
const imagePath = b.getBundles().find(b => b.type === 'jpg').filePath;
101+
102+
let input = await inputFS.readFile(img);
103+
let inputRaw = await sharp(input)
104+
.toFormat('raw')
105+
.toBuffer();
106+
let output = await outputFS.readFile(imagePath);
107+
let outputRaw = await sharp(output)
108+
.toFormat('raw')
109+
.toBuffer();
110+
111+
assert(outputRaw.equals(inputRaw));
112+
assert(output.length < input.length);
113+
});
114+
115+
it('should optimise PNGs', async function() {
116+
let img = path.join(__dirname, '/integration/image/clock.png');
117+
let b = await bundle(img, {
118+
defaultTargetOptions: {
119+
shouldOptimize: true,
120+
},
121+
});
122+
123+
const imagePath = b.getBundles().find(b => b.type === 'png').filePath;
124+
125+
let input = await inputFS.readFile(img);
126+
let inputRaw = await sharp(input)
127+
.toFormat('raw')
128+
.toBuffer();
129+
let output = await outputFS.readFile(imagePath);
130+
let outputRaw = await sharp(output)
131+
.toFormat('raw')
132+
.toBuffer();
133+
134+
assert(outputRaw.equals(inputRaw));
135+
assert(output.length < input.length);
136+
});
91137
});
82.5 KB
Loading
255 Bytes
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.node
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
authors = ["Devon Govett <[email protected]>"]
3+
name = "parcel-image"
4+
version = "0.1.0"
5+
6+
[lib]
7+
crate-type = ["cdylib"]
8+
9+
[dependencies]
10+
napi = "1"
11+
napi-derive = "1"
12+
oxipng = "5.0.0"
13+
mozjpeg-sys = "1.0.0"
14+
libc = "0.2"
15+
16+
[build-dependencies]
17+
napi-build = "1"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
extern crate napi_build;
2+
3+
fn main() {
4+
napi_build::setup();
5+
}

0 commit comments

Comments
Β (0)