Skip to content

Commit 4118b98

Browse files
committed
feat(03-final): add Eleventy-img optimized img.webc component
1 parent 61caf26 commit 4118b98

File tree

9 files changed

+4839
-0
lines changed

9 files changed

+4839
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
_site
3+
.cache

03-final/.eleventy.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const webc = require("@11ty/eleventy-plugin-webc");
2+
3+
module.exports = function(eleventyConfig) {
4+
eleventyConfig.addPlugin(webc, {
5+
components: "_includes/webc/*.webc"
6+
});
7+
}

03-final/_data/Image.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const Image = require("@11ty/eleventy-img");
2+
3+
module.exports = function() {
4+
return Image;
5+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Eleventy Images + WebC rocks!</title>
6+
</head>
7+
<body @html="content"></body>
8+
</html>

03-final/_includes/webc/img.webc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<script webc:type="render" webc:is="template">
2+
async function() {
3+
let metadata = await this.Image(this.src, {
4+
widths: [300],
5+
formats: ["avif", "jpeg"],
6+
outputDir: "_site/img/",
7+
});
8+
9+
let imageAttributes = {
10+
alt: this.alt,
11+
sizes: '100vw',
12+
loading: "lazy",
13+
decoding: "async",
14+
"webc:raw": true,
15+
};
16+
17+
// You bet we throw an error on missing alt in `imageAttributes` (alt="" works okay)
18+
return this.Image.generateHTML(metadata, imageAttributes);
19+
}
20+
</script>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<footer>
2+
<p>&copy; 2022 Yours Truly.</p>
3+
</footer>

03-final/index.webc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
layout: layouts/base.webc
3+
---
4+
5+
Fun with WebC
6+
7+
<img alt="possum hanging from a palm leaf" src="https://images.unsplash.com/photo-1627224286457-923944e5b159?auto=format&fit=crop&w=387&q=80">
8+
9+
<template webc:type="11ty" 11ty:type="md">
10+
11+
!['close-up of a baby possum among ferns'](https://images.pexels.com/photos/13960994/pexels-photo-13960994.jpeg)
12+
13+
</template>
14+
15+
<site-footer></site-footer>

0 commit comments

Comments
 (0)