-
Notifications
You must be signed in to change notification settings - Fork 0
/
eleventy.config.images.js
32 lines (30 loc) · 1.09 KB
/
eleventy.config.images.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
const path = require("path");
const eleventyImage = require("@11ty/eleventy-img");
module.exports = eleventyConfig => {
// Eleventy Image shortcode
// https://www.11ty.dev/docs/plugins/image/
eleventyConfig.addAsyncShortcode("image", async function imageShortcode(src, alt, widths, sizes, loading = 'lazy') {
// TODO loading=eager and fetchpriority=high
let imageAttributes = {
alt,
sizes,
loading,
decoding: "async",
};
if (src.startsWith("https://")) {
// If src is an external URL, skip optimization and return an img tag
return `<img src="${src}" alt="${alt}" loading="lazy" decoding="async">`;
} else {
// Full list of formats here: https://www.11ty.dev/docs/plugins/image/#output-formats
// Warning: Avif can be resource-intensive so take care!
let formats = ["avif", "webp", "auto"];
let file = path.join(__dirname, 'src', src);
let metadata = await eleventyImage(file, {
widths: widths || ["auto"],
formats,
outputDir: path.join(eleventyConfig.dir.output, "img"),
});
return eleventyImage.generateHTML(metadata, imageAttributes);
}
});
};