@divriots/jampack 0.7.0 → 0.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1 -1
- package/dist/optimize.js +18 -21
- package/package.json +1 -1
package/dist/index.js
CHANGED
package/dist/optimize.js
CHANGED
|
@@ -273,27 +273,33 @@ async function processImage(htmlfile, $, imgElement, isAboveTheFold) {
|
|
|
273
273
|
while (valueW > config.image.srcset_min_width) {
|
|
274
274
|
const src = imageSrc(`@${valueW}w`);
|
|
275
275
|
const absoluteFilename = translateSrc($state.dir, path.dirname(htmlfile), src);
|
|
276
|
+
let doAddToSrcSet = true;
|
|
276
277
|
// Don't generate srcset file twice
|
|
277
278
|
if (!$state.compressedFiles.has(absoluteFilename)) {
|
|
278
|
-
// Add file to list avoid recompression
|
|
279
|
-
$state.compressedFiles.add(absoluteFilename);
|
|
280
279
|
const compressedImage = await compressImage(await originalImage.getData(), {
|
|
281
280
|
resize: { width: valueW, height: valueH },
|
|
282
281
|
toFormat: canBeProgressiveJpeg ? 'pjpg' : targetFormat,
|
|
283
282
|
});
|
|
284
283
|
if (!(compressedImage === null || compressedImage === void 0 ? void 0 : compressedImage.data) ||
|
|
285
284
|
compressedImage.data.length >= previousImageSize) {
|
|
286
|
-
//
|
|
287
|
-
//
|
|
288
|
-
|
|
285
|
+
// New image is not compressed or bigger than previous image in srcset
|
|
286
|
+
// Don't add to srcset
|
|
287
|
+
doAddToSrcSet = false;
|
|
289
288
|
}
|
|
290
|
-
|
|
291
|
-
|
|
289
|
+
else {
|
|
290
|
+
// Write file
|
|
291
|
+
if (!$state.args.nowrite) {
|
|
292
|
+
fs.writeFile(absoluteFilename, compressedImage.data);
|
|
293
|
+
}
|
|
294
|
+
// Set new previous size to beat
|
|
295
|
+
previousImageSize = compressedImage.data.length;
|
|
296
|
+
// Add file to list avoid recompression
|
|
297
|
+
$state.compressedFiles.add(absoluteFilename);
|
|
292
298
|
}
|
|
293
|
-
// Add previous smaller file size
|
|
294
|
-
previousImageSize = compressedImage.data.length;
|
|
295
299
|
}
|
|
296
|
-
|
|
300
|
+
if (doAddToSrcSet) {
|
|
301
|
+
new_srcset += `, ${src} ${valueW}w`;
|
|
302
|
+
}
|
|
297
303
|
// reduce size
|
|
298
304
|
valueW -= step;
|
|
299
305
|
valueH = Math.trunc(valueW / ratio);
|
|
@@ -322,7 +328,7 @@ async function setImageSize(htmlfile, img, image) {
|
|
|
322
328
|
if (!isNumeric(width)) {
|
|
323
329
|
$state.reportIssue(htmlfile, {
|
|
324
330
|
type: 'warn',
|
|
325
|
-
msg: `Invalid width attribute format: "${width}" - overriding`,
|
|
331
|
+
msg: `Invalid width attribute format: "${width}" - overriding for ${image.src}`,
|
|
326
332
|
});
|
|
327
333
|
width = undefined;
|
|
328
334
|
}
|
|
@@ -331,7 +337,7 @@ async function setImageSize(htmlfile, img, image) {
|
|
|
331
337
|
if (!isNumeric(height)) {
|
|
332
338
|
$state.reportIssue(htmlfile, {
|
|
333
339
|
type: 'warn',
|
|
334
|
-
msg: `Invalid height attribute format: "${height}" - overriding`,
|
|
340
|
+
msg: `Invalid height attribute format: "${height}" - overriding for ${image.src}`,
|
|
335
341
|
});
|
|
336
342
|
height = undefined;
|
|
337
343
|
}
|
|
@@ -349,15 +355,6 @@ async function setImageSize(htmlfile, img, image) {
|
|
|
349
355
|
// Both are provided
|
|
350
356
|
const w = parseInt(width, 10);
|
|
351
357
|
const h = parseInt(height, 10);
|
|
352
|
-
// Is ratio equal?
|
|
353
|
-
const providedRatio = Math.round((w / h) * 10) / 10;
|
|
354
|
-
const imageRatio = Math.round(originalRatio * 10) / 10;
|
|
355
|
-
if (providedRatio !== imageRatio) {
|
|
356
|
-
$state.reportIssue(htmlfile, {
|
|
357
|
-
type: 'warn',
|
|
358
|
-
msg: `Image aspect ratio in HTML (${providedRatio}) differs from image aspect ratio (${imageRatio}) - fix width and height or let jampack fill them.`,
|
|
359
|
-
});
|
|
360
|
-
}
|
|
361
358
|
return [w, h];
|
|
362
359
|
}
|
|
363
360
|
else if (width !== undefined && height === undefined) {
|