/
image.js
256 lines (210 loc) Β· 7.24 KB
/
image.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import assert from 'assert';
import {bundle, distDir, inputFS, outputFS} from '@parcel/test-utils';
import exifReader from 'exif-reader';
import path from 'path';
import sharp from 'sharp';
describe('image', function() {
this.timeout(10000);
it('Should be able to resize images', async () => {
await bundle(path.join(__dirname, '/integration/image/resized.js'));
let dirContent = await outputFS.readdir(distDir);
let imagePath = '';
let foundExtensions = [];
for (let filename of dirContent) {
let ext = path.extname(filename);
foundExtensions.push(ext);
if (ext === '.jpeg') {
imagePath = path.join(distDir, filename);
}
}
assert.deepStrictEqual(
foundExtensions.sort(),
['.jpeg', '.js', '.map'].sort(),
);
let buffer = await outputFS.readFile(imagePath);
let image = await sharp(buffer).metadata();
assert.equal(image.width, 600);
});
it('Should be able to import an image using multiple varying query parameters', async () => {
await bundle(
path.join(__dirname, '/integration/image-multiple-queries/index.html'),
);
let dirContent = await outputFS.readdir(distDir);
let foundExtensions = [];
for (let filename of dirContent) {
const foundExt = path.extname(filename);
if (foundExt !== '.map') {
foundExtensions.push(foundExt);
}
}
assert.deepStrictEqual(
foundExtensions.sort(),
['.jpeg', '.jpeg', '.webp', '.html'].sort(),
);
});
describe('Should be able to change image format', () => {
function testCase(ext) {
return async () => {
await bundle(
path.join(__dirname, `/integration/image/reformat.${ext}`),
);
let dirContent = await outputFS.readdir(distDir);
let foundExtensions = [];
for (let filename of dirContent) {
const foundExt = path.extname(filename);
if (foundExt !== '.map') {
foundExtensions.push(foundExt);
}
}
assert.deepStrictEqual(
foundExtensions.sort(),
['.webp', `.${ext}`].sort(),
);
};
}
it('from JS', testCase('js'));
it('from HTML', testCase('html'));
it('from CSS', testCase('css'));
it('all formats', async () => {
let b = await bundle(
path.join(__dirname, `/integration/image/reformat-all.html`),
);
let foundExtensions = new Set(b.getBundles().map(({type}) => type));
assert.deepStrictEqual(
foundExtensions,
new Set(['html', 'webp', 'avif', 'jpeg', 'png', 'tiff']),
);
});
});
it('should lossless optimise JPEGs', async function() {
let img = path.join(__dirname, '/integration/image/image.jpg');
let b = await bundle(img, {
defaultTargetOptions: {
shouldOptimize: true,
},
});
const imagePath = b.getBundles().find(b => b.type === 'jpg').filePath;
let input = await inputFS.readFile(img);
let inputRaw = await sharp(input)
.toFormat('raw')
.toBuffer();
let output = await outputFS.readFile(imagePath);
let outputRaw = await sharp(output)
.toFormat('raw')
.toBuffer();
assert(outputRaw.equals(inputRaw));
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, {
defaultTargetOptions: {
shouldOptimize: true,
},
});
const imagePath = b.getBundles().find(b => b.type === 'png').filePath;
let input = await inputFS.readFile(img);
let inputRaw = await sharp(input)
.toFormat('raw')
.toBuffer();
let output = await outputFS.readFile(imagePath);
let outputRaw = await sharp(output)
.toFormat('raw')
.toBuffer();
assert(outputRaw.equals(inputRaw));
assert(output.length < input.length);
});
it('support config files for jpeg files', async function() {
let b = await bundle(
path.join(__dirname, '/integration/image-config/image.jpg'),
{
defaultTargetOptions: {
shouldOptimize: false,
},
},
);
const originalSize = 549196;
const imagePath = b.getBundles().find(b => b.type === 'jpeg').filePath;
const buffer = await outputFS.readFile(imagePath);
const image = await sharp(buffer).metadata();
assert.strictEqual(image.width, 1920);
assert.strictEqual(image.chromaSubsampling, '4:4:4');
assert(image.size < originalSize);
});
it('support config files for png files', async function() {
let b = await bundle(
path.join(__dirname, '/integration/image-config/clock.png'),
{
defaultTargetOptions: {
shouldOptimize: false,
},
},
);
const originalSize = 84435;
const imagePath = b.getBundles().find(b => b.type === 'png').filePath;
const buffer = await outputFS.readFile(imagePath);
const image = await sharp(buffer).metadata();
assert.strictEqual(image.width, 200);
assert.strictEqual(image.paletteBitDepth, 8);
assert(image.size < originalSize);
});
it('should retain EXIF data', async () => {
const b = await bundle(
path.join(__dirname, '/integration/image-exif/resized.html'),
);
const imagePath = b.getBundles().find(b => b.type === 'jpeg').filePath;
const buffer = await outputFS.readFile(imagePath);
const image = await sharp(buffer).metadata();
const {exif} = exifReader(image.exif);
assert.strictEqual(
exif.UserComment.toString(),
'ASCII\u0000\u0000\u0000This is a comment',
);
});
it('should remove EXIF data when optimizing', async () => {
const b = await bundle(
path.join(__dirname, '/integration/image-exif/resized.html'),
{
defaultTargetOptions: {
shouldOptimize: true,
},
},
);
const imagePath = b.getBundles().find(b => b.type === 'jpeg').filePath;
const buffer = await outputFS.readFile(imagePath);
const image = await sharp(buffer).metadata();
assert.strictEqual(image.exif, undefined);
});
it('should use the EXIF orientation tag when resizing', async () => {
const b = await bundle(
path.join(__dirname, '/integration/image-exif/resized.html'),
);
const imagePath = b.getBundles().find(b => b.type === 'jpeg').filePath;
const buffer = await outputFS.readFile(imagePath);
const image = await sharp(buffer).metadata();
assert.strictEqual(image.orientation, 1);
assert.strictEqual(image.width, 240);
assert.strictEqual(image.height, 320);
});
});