@divriots/jampack 0.9.0 → 0.9.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/cache.js +1 -1
- package/dist/optimize.js +33 -4
- package/package.json +1 -1
package/dist/cache.js
CHANGED
|
@@ -26,7 +26,7 @@ async function cleanCache(full) {
|
|
|
26
26
|
// Delete old cache versions
|
|
27
27
|
await files
|
|
28
28
|
.filter((f) => f !== VERSION)
|
|
29
|
-
.forEach(async (f) => fs.
|
|
29
|
+
.forEach(async (f) => fs.rm(path.join(CACHE_FOLDER, f), { recursive: true }));
|
|
30
30
|
}
|
|
31
31
|
function computeCacheHash(buffer, options) {
|
|
32
32
|
if ($state.args.nocache) {
|
package/dist/optimize.js
CHANGED
|
@@ -28,7 +28,7 @@ async function analyse(file) {
|
|
|
28
28
|
const spinnerImg = ora({ prefixText: ' ' }).start();
|
|
29
29
|
for (let i = 0; i < imgsArray.length; i++) {
|
|
30
30
|
const imgElement = imgsArray[i];
|
|
31
|
-
spinnerImg.text = kleur.dim(`<img> [${i + 1}/${imgsArray.length}] ${$(imgElement).attr('src')}
|
|
31
|
+
spinnerImg.text = kleur.dim(`<img> [${i + 1}/${imgsArray.length}] ${$(imgElement).attr('src')}`);
|
|
32
32
|
const isAboveTheFold = imgElement.startIndex < theFold;
|
|
33
33
|
try {
|
|
34
34
|
await processImage(file, $, imgElement, isAboveTheFold);
|
|
@@ -43,6 +43,30 @@ async function analyse(file) {
|
|
|
43
43
|
}
|
|
44
44
|
// Reset spinner
|
|
45
45
|
spinnerImg.text = kleur.dim(`<img> [${imgsArray.length}/${imgsArray.length}]`);
|
|
46
|
+
const iframes = $('iframe');
|
|
47
|
+
const iframesArray = [];
|
|
48
|
+
iframes.each(async (index, ifElement) => {
|
|
49
|
+
iframesArray.push(ifElement);
|
|
50
|
+
});
|
|
51
|
+
// Process iframes sequentially
|
|
52
|
+
const spinnerIframe = ora({ prefixText: ' ' }).start();
|
|
53
|
+
for (let i = 0; i < iframesArray.length; i++) {
|
|
54
|
+
const ifElement = iframesArray[i];
|
|
55
|
+
spinnerIframe.text = kleur.dim(`<iframe> [${i + 1}/${iframesArray.length}] ${$(ifElement).attr('src')}`);
|
|
56
|
+
const isAboveTheFold = ifElement.startIndex < theFold;
|
|
57
|
+
try {
|
|
58
|
+
await processIframe(file, $, ifElement, isAboveTheFold);
|
|
59
|
+
}
|
|
60
|
+
catch (e) {
|
|
61
|
+
$state.reportIssue(file, {
|
|
62
|
+
type: 'erro',
|
|
63
|
+
msg: e.message ||
|
|
64
|
+
`Unexpected error while processing image: ${JSON.stringify(e)}`,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
// Reset spinner
|
|
69
|
+
spinnerIframe.text = kleur.dim(`<iframe> [${imgsArray.length}/${imgsArray.length}]`);
|
|
46
70
|
// Notify issues
|
|
47
71
|
const issues = $state.issues.get(file);
|
|
48
72
|
if (issues) {
|
|
@@ -249,9 +273,8 @@ async function processImage(htmlfile, $, imgElement, isAboveTheFold) {
|
|
|
249
273
|
* Only above the fold
|
|
250
274
|
*/
|
|
251
275
|
let isEmbed = false;
|
|
252
|
-
let imageToEmbed;
|
|
253
276
|
if (isAboveTheFold) {
|
|
254
|
-
imageToEmbed = newImage;
|
|
277
|
+
let imageToEmbed = newImage;
|
|
255
278
|
// If new image was not generated then take the old image
|
|
256
279
|
if (!imageToEmbed) {
|
|
257
280
|
imageToEmbed = {
|
|
@@ -287,7 +310,7 @@ async function processImage(htmlfile, $, imgElement, isAboveTheFold) {
|
|
|
287
310
|
}
|
|
288
311
|
}
|
|
289
312
|
}
|
|
290
|
-
if (isEmbed && (
|
|
313
|
+
if (isEmbed && (await originalImage.getExt()) === 'svg') {
|
|
291
314
|
// SVG has a different behaviour with image size
|
|
292
315
|
// Needs to be tested more
|
|
293
316
|
// For the moment skip image size for SVG
|
|
@@ -573,6 +596,12 @@ async function setImageSize(htmlfile, img, image) {
|
|
|
573
596
|
// Something when wrong
|
|
574
597
|
throw new Error(`Unexpected issue when resolving image size "${image.src}"`);
|
|
575
598
|
}
|
|
599
|
+
async function processIframe(htmlfile, $, imgElement, isAboveTheFold) {
|
|
600
|
+
const iframe = $(imgElement);
|
|
601
|
+
if (!isAboveTheFold) {
|
|
602
|
+
iframe.attr('loading', 'lazy');
|
|
603
|
+
}
|
|
604
|
+
}
|
|
576
605
|
export async function optimize(include, exclude) {
|
|
577
606
|
const glob = include ? [include] : ['**/*.{htm,html}'];
|
|
578
607
|
if (exclude)
|