@divriots/jampack 0.11.1 → 0.11.3
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/README.md +70 -8
- package/dist/compressors/html.js +4 -1
- package/dist/config-default.js +8 -0
- package/dist/config-types.d.ts +8 -0
- package/dist/optimize.js +30 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -35,16 +35,78 @@ Optimizes static websites for best user experience and best Core Web Vitals scor
|
|
|
35
35
|
|
|
36
36
|
`jampack` is a post-processing tool that takes the output of your [Static Site Generator (aka SSG)](https://jamstack.org/generators/) and optimizes it for best user experience and best [Core Web Vitals](https://web.dev/learn-core-web-vitals/) scores.
|
|
37
37
|
|
|
38
|
-
[Read the introduction blog post](https://divriots.com/blog/introducing-jampack)
|
|
38
|
+
[Read the introduction blog post](https://divriots.com/blog/introducing-jampack/)
|
|
39
39
|
|
|
40
40
|
## What can `jampack` do?
|
|
41
41
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
42
|
+
### `<img>`
|
|
43
|
+
|
|
44
|
+
```html
|
|
45
|
+
<img src="./redpanda.jpg" alt="Red panda">
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
becomes responsive with dimensions:
|
|
49
|
+
|
|
50
|
+
```html
|
|
51
|
+
<img src="./redpanda.jpg.webp" alt="Red panda"
|
|
52
|
+
srcset="./redpanda.jpg.webp 3872w, ./redpanda@3572w.jpg.webp 3572w, ./redpanda@3272w.jpg.webp 3272w, ./redpanda@2972w.jpg.webp 2972w, ./redpanda@2672w.jpg.webp 2672w, ./redpanda@2372w.jpg.webp 2372w, ./redpanda@2072w.jpg.webp 2072w, ./redpanda@1772w.jpg.webp 1772w, ./redpanda@1472w.jpg.webp 1472w, ./redpanda@1172w.jpg.webp 1172w, ./redpanda@872w.jpg.webp 872w"
|
|
53
|
+
sizes="100vw"
|
|
54
|
+
loading="lazy"
|
|
55
|
+
decoding="async"
|
|
56
|
+
width="3872"
|
|
57
|
+
height="2592">
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### `<picture>`
|
|
61
|
+
|
|
62
|
+
```html
|
|
63
|
+
<picture>
|
|
64
|
+
<img src="./redpanda.jpg" alt="Red panda">
|
|
65
|
+
</picture>
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
becomes responsive with multiple formats including AVIF:
|
|
69
|
+
|
|
70
|
+
```html
|
|
71
|
+
<picture>
|
|
72
|
+
<source type="image/avif" srcset="./redpanda@1936w.avif 1936w, ./redpanda@1636w.avif 1636w, ./redpanda@1336w.avif 1336w, ./redpanda@1036w.avif 1036w, ./redpanda@736w.avif 736w">
|
|
73
|
+
<source type="image/webp" srcset="./redpanda@1936w.webp 1936w, ./redpanda@1636w.webp 1636w, ./redpanda@1336w.webp 1336w, ./redpanda@1036w.webp 1036w, ./redpanda@736w.webp 736w">
|
|
74
|
+
<img src="./redpanda.jpg" alt="Red panda" loading="lazy" decoding="async" width="1936" height="1296" srcset="./redpanda.jpg 1936w, ./redpanda@1636w.jpg 1636w, ./redpanda@1336w.jpg 1336w, ./redpanda@1036w.jpg 1036w, ./redpanda@736w.jpg 736w" sizes="100vw">
|
|
75
|
+
</picture>
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Above & below-the-fold
|
|
79
|
+
|
|
80
|
+
`jampack` optimizes assets above-the-fold ⬆️.
|
|
81
|
+
|
|
82
|
+
- Images are loaded with higher priority.
|
|
83
|
+
- Images are compress as Progressive JPEG.
|
|
84
|
+
- Small images are embedded in HTML.
|
|
85
|
+
|
|
86
|
+
Lazy-load assets below-the-fold ⬇️.
|
|
87
|
+
|
|
88
|
+
- Images and Iframes are lazy loaded.
|
|
89
|
+
|
|
90
|
+
See [details](https://jampack.divriots.com/features/optimize-above-the-fold/).
|
|
91
|
+
|
|
92
|
+
### All assets are compressed
|
|
93
|
+
|
|
94
|
+
In a 2nd PASS, `jampack` compresses all untouched assets and keep the same name and the same format.
|
|
95
|
+
|
|
96
|
+
| Extension | Compressor |
|
|
97
|
+
| --------------- | --------------------- |
|
|
98
|
+
| `.html`,`.htm` | [`html-minifier-terser`](https://github.com/terser/html-minifier-terser) |
|
|
99
|
+
| `.css` | Best of [`csso`](https://github.com/css/csso) & [`lightningCSS`](https://lightningcss.dev) |
|
|
100
|
+
| `.js` | [`swc`](https://swc.rs/) |
|
|
101
|
+
| `.svg` | [`svgo`](https://github.com/svg/svgo) |
|
|
102
|
+
| `.jpg`,`.jpeg` | [`sharp`](https://sharp.pixelplumbing.com/) |
|
|
103
|
+
| `.png` | [`sharp`](https://sharp.pixelplumbing.com/) |
|
|
104
|
+
| `.webp` | [`sharp`](https://sharp.pixelplumbing.com/) |
|
|
105
|
+
| `.avif` | [`sharp`](https://sharp.pixelplumbing.com/) |
|
|
106
|
+
|
|
107
|
+
### And more!
|
|
108
|
+
|
|
109
|
+
See [Documentation](https://jampack.divriots.com/) for all feature list and examples.
|
|
48
110
|
|
|
49
111
|
## Quick use
|
|
50
112
|
|
|
@@ -53,7 +115,7 @@ Optimizes static websites for best user experience and best Core Web Vitals scor
|
|
|
53
115
|
npx @divriots/jampack ./dist
|
|
54
116
|
```
|
|
55
117
|
|
|
56
|
-
For more options see [CLI options](https://jampack.divriots.com/cli-options).
|
|
118
|
+
For more options see [CLI options](https://jampack.divriots.com/cli-options/).
|
|
57
119
|
|
|
58
120
|
## `jampack` used in the wild
|
|
59
121
|
|
package/dist/compressors/html.js
CHANGED
|
@@ -8,9 +8,12 @@ async function minifyJSinHTML(originalCode) {
|
|
|
8
8
|
return originalCode;
|
|
9
9
|
}
|
|
10
10
|
async function minifyCSSinHTML(originalCode, type) {
|
|
11
|
+
// Don't compress media
|
|
12
|
+
if (type === 'media')
|
|
13
|
+
return originalCode;
|
|
11
14
|
const originalBuffer = Buffer.from(originalCode);
|
|
12
15
|
const newCSS = await compressCSS(originalBuffer, type);
|
|
13
|
-
if (newCSS && newCSS.length < originalBuffer.length)
|
|
16
|
+
if (newCSS && newCSS.length > 0 && newCSS.length < originalBuffer.length)
|
|
14
17
|
return newCSS.toString();
|
|
15
18
|
return originalCode;
|
|
16
19
|
}
|
package/dist/config-default.js
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
const default_options = {
|
|
2
|
+
html: {
|
|
3
|
+
add_css_reset_as: 'inline',
|
|
4
|
+
},
|
|
2
5
|
image: {
|
|
3
6
|
embed_size: 1500,
|
|
4
7
|
srcset_min_width: 640,
|
|
8
|
+
external: {
|
|
9
|
+
process: 'download',
|
|
10
|
+
src_include: /.*/,
|
|
11
|
+
src_exclude: null,
|
|
12
|
+
},
|
|
5
13
|
compress: true,
|
|
6
14
|
jpeg: {
|
|
7
15
|
options: {
|
package/dist/config-types.d.ts
CHANGED
|
@@ -4,9 +4,17 @@ export type WebpOptions = {
|
|
|
4
4
|
quality: number;
|
|
5
5
|
};
|
|
6
6
|
export type Options = {
|
|
7
|
+
html: {
|
|
8
|
+
add_css_reset_as: 'inline' | 'off';
|
|
9
|
+
};
|
|
7
10
|
image: {
|
|
8
11
|
embed_size: number;
|
|
9
12
|
srcset_min_width: number;
|
|
13
|
+
external: {
|
|
14
|
+
process: 'download' | 'add-dimensions-only' | 'off';
|
|
15
|
+
src_include: RegExp | undefined;
|
|
16
|
+
src_exclude: RegExp | null;
|
|
17
|
+
};
|
|
10
18
|
compress: boolean;
|
|
11
19
|
jpeg: {
|
|
12
20
|
options: {
|
package/dist/optimize.js
CHANGED
|
@@ -79,13 +79,15 @@ async function analyse(file) {
|
|
|
79
79
|
$('the-fold').remove();
|
|
80
80
|
}
|
|
81
81
|
// Add CSS to head
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
heads.
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
82
|
+
if (config.html.add_css_reset_as === 'inline') {
|
|
83
|
+
const CSS = `<style>:where(img){height:auto;width:auto}</style>`;
|
|
84
|
+
const heads = $('head');
|
|
85
|
+
if (heads.length > 0) {
|
|
86
|
+
heads.prepend(CSS);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
(_a = $('html')) === null || _a === void 0 ? void 0 : _a.prepend(`<head>${CSS}</head>`);
|
|
90
|
+
}
|
|
89
91
|
}
|
|
90
92
|
if (!$state.args.nowrite) {
|
|
91
93
|
await fs.writeFile(path.join($state.dir, file), $.html());
|
|
@@ -122,6 +124,13 @@ async function processImage(htmlfile, $, imgElement, isAboveTheFold) {
|
|
|
122
124
|
*/
|
|
123
125
|
const attrib_src = img.attr('src');
|
|
124
126
|
if (!attrib_src) {
|
|
127
|
+
const attrib_data_src = img.attr('data-src');
|
|
128
|
+
if (attrib_data_src) {
|
|
129
|
+
// Contains attribute [data-src]
|
|
130
|
+
// Most likely, this is a Javascript lazy-loaded image
|
|
131
|
+
// Let's not mess with it and hope it's well done
|
|
132
|
+
return; // No warnings
|
|
133
|
+
}
|
|
125
134
|
$state.reportIssue(htmlfile, {
|
|
126
135
|
type: 'warn',
|
|
127
136
|
msg: `Missing [src] on img - processing skipped.`,
|
|
@@ -175,9 +184,21 @@ async function processImage(htmlfile, $, imgElement, isAboveTheFold) {
|
|
|
175
184
|
* Attribute 'decoding'
|
|
176
185
|
*/
|
|
177
186
|
img.attr('decoding', 'async');
|
|
187
|
+
/*
|
|
188
|
+
* Check for external images
|
|
189
|
+
*/
|
|
178
190
|
if (!isLocal(attrib_src)) {
|
|
179
|
-
|
|
180
|
-
|
|
191
|
+
switch (config.image.external.process) {
|
|
192
|
+
case 'off':
|
|
193
|
+
// Don't process external images
|
|
194
|
+
return;
|
|
195
|
+
case 'download':
|
|
196
|
+
// Download external image for local processing
|
|
197
|
+
break;
|
|
198
|
+
case 'add-dimensions-only':
|
|
199
|
+
// Only add dimensions of image
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
181
202
|
}
|
|
182
203
|
/*
|
|
183
204
|
* Loading image
|