Compare commits
4 Commits
e8915df1cc
...
3dd7d307b2
Author | SHA1 | Date |
---|---|---|
Robert McGovern | 3dd7d307b2 | |
Robert McGovern | 0140cb3324 | |
Robert McGovern | 063dbdd28e | |
Robert McGovern | 70ad2cec50 |
158
.eleventy.js
|
@ -1,4 +1,6 @@
|
|||
const { EleventyRenderPlugin } = require("@11ty/eleventy");
|
||||
const Image = require("@11ty/eleventy-img");
|
||||
const CleanCSS = require("clean-css");
|
||||
|
||||
module.exports = function (eleventyConfig) {
|
||||
eleventyConfig.addPassthroughCopy("./src/css");
|
||||
|
@ -27,9 +29,20 @@ module.exports = function (eleventyConfig) {
|
|||
// "./src/assets/images": "img",
|
||||
//});
|
||||
|
||||
// FILTERS
|
||||
eleventyConfig.addFilter("cssmin", function (code) {
|
||||
return new CleanCSS({}).minify(code).styles;
|
||||
});
|
||||
|
||||
// PLUGINS
|
||||
eleventyConfig.addPlugin(EleventyRenderPlugin);
|
||||
|
||||
// SHORTCODES
|
||||
// eleventyConfig.addShortcode("image", imageShortcode);
|
||||
eleventyConfig.addAsyncShortcode("imageGen", officialImageShortcode);
|
||||
eleventyConfig.addNunjucksShortcode("imageGenSync", imageShortcodeSync);
|
||||
// eleventyConfig.addShortcode("imageGenSync", imageShortcodeSync);
|
||||
|
||||
// WATCH Targets
|
||||
eleventyConfig.addWatchTarget("./src/css/");
|
||||
eleventyConfig.addWatchTarget("./src/js/");
|
||||
|
@ -51,3 +64,148 @@ module.exports = function (eleventyConfig) {
|
|||
},
|
||||
};
|
||||
};
|
||||
|
||||
const imageShortcode = async (
|
||||
src,
|
||||
alt,
|
||||
className = undefined,
|
||||
widths = [400, 800, 1280],
|
||||
formats = ["webp", "jpeg"],
|
||||
sizes = "100vw"
|
||||
) => {
|
||||
const imageMetadata = await Image(src, {
|
||||
widths: [...widths, null],
|
||||
formats: [...formats, null],
|
||||
outputDir: "_site/assets/images",
|
||||
urlPath: "/assets/images",
|
||||
});
|
||||
|
||||
const sourceHtmlString = Object.values(imageMetadata)
|
||||
// Map each format to the source HTML markup
|
||||
.map((images) => {
|
||||
// The first entry is representative of all the others
|
||||
// since they each have the same shape
|
||||
const { sourceType } = images[0];
|
||||
|
||||
// Use our util from earlier to make our lives easier
|
||||
const sourceAttributes = stringifyAttributes({
|
||||
type: sourceType,
|
||||
// srcset needs to be a comma-separated attribute
|
||||
srcset: images.map((image) => image.srcset).join(", "),
|
||||
sizes,
|
||||
});
|
||||
|
||||
// Return one <source> per format
|
||||
return `<source ${sourceAttributes}>`;
|
||||
})
|
||||
.join("\n");
|
||||
|
||||
const getLargestImage = (format) => {
|
||||
const images = imageMetadata[format];
|
||||
return images[images.length - 1];
|
||||
};
|
||||
|
||||
const largestUnoptimizedImg = getLargestImage(formats[0]);
|
||||
const imgAttributes = stringifyAttributes({
|
||||
src: largestUnoptimizedImg.url,
|
||||
width: largestUnoptimizedImg.width,
|
||||
height: largestUnoptimizedImg.height,
|
||||
alt,
|
||||
loading: "lazy",
|
||||
decoding: "async",
|
||||
});
|
||||
const imgHtmlString = `<img ${imgAttributes}>`;
|
||||
|
||||
const pictureAttributes = stringifyAttributes({
|
||||
class: className,
|
||||
});
|
||||
const picture = `<picture ${pictureAttributes}>
|
||||
${sourceHtmlString}
|
||||
${imgHtmlString}
|
||||
</picture>`;
|
||||
|
||||
return outdent`${picture}`;
|
||||
};
|
||||
|
||||
async function officialImageShortcode(src, alt, sizes = "100vw") {
|
||||
if (alt === undefined) {
|
||||
// You bet we throw an error on missing alt (alt="" works okay)
|
||||
throw new Error(`Missing \`alt\` on responsiveimage from: ${src}`);
|
||||
}
|
||||
|
||||
if (src.startsWith("/screenshots")) {
|
||||
src = "./src/" + src;
|
||||
} else {
|
||||
src = "./projects/" + src;
|
||||
}
|
||||
|
||||
console.log("officalImage: " + src + " " + alt);
|
||||
|
||||
let metadata = await Image(src, {
|
||||
widths: [300, 600],
|
||||
formats: ["webp", "jpeg"],
|
||||
outputDir: "www/assets/images",
|
||||
urlPath: "/assets/images",
|
||||
});
|
||||
|
||||
console.log(metadata);
|
||||
|
||||
let lowsrc = metadata.jpeg[0];
|
||||
let highsrc = metadata.jpeg[metadata.jpeg.length - 1];
|
||||
|
||||
return `<picture>
|
||||
${Object.values(metadata)
|
||||
.map((imageFormat) => {
|
||||
return ` <source type="${
|
||||
imageFormat[0].sourceType
|
||||
}" srcset="${imageFormat
|
||||
.map((entry) => entry.srcset)
|
||||
.join(", ")}" sizes="${sizes}">`;
|
||||
})
|
||||
.join("\n")}
|
||||
<img
|
||||
src="${lowsrc.url}"
|
||||
width="${highsrc.width}"
|
||||
height="${highsrc.height}"
|
||||
alt="${alt}"
|
||||
class="card__img"
|
||||
loading="lazy"
|
||||
decoding="async">
|
||||
</picture>`;
|
||||
}
|
||||
|
||||
function imageShortcodeSync(
|
||||
src,
|
||||
alt,
|
||||
sizes = "100vw",
|
||||
widths = [300, 600],
|
||||
cls = "card__img"
|
||||
) {
|
||||
let options = {
|
||||
widths: widths,
|
||||
formats: ["webp", "jpeg"],
|
||||
outputDir: "www/assets/images",
|
||||
urlPath: "/assets/images",
|
||||
};
|
||||
|
||||
if (src.startsWith("/screenshots")) {
|
||||
src = "./src/" + src;
|
||||
} else {
|
||||
src = "./projects/" + src;
|
||||
}
|
||||
|
||||
// console.log("Sync: " + src + " " + alt);
|
||||
// generate images, while this is async we don’t wait
|
||||
Image(src, options);
|
||||
|
||||
let imageAttributes = {
|
||||
class: cls,
|
||||
alt,
|
||||
sizes,
|
||||
loading: "lazy",
|
||||
decoding: "async",
|
||||
};
|
||||
// get metadata even if the images are not fully generated yet
|
||||
let metadata = Image.statsSync(src, options);
|
||||
return Image.generateHTML(metadata, imageAttributes);
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
"path": "."
|
||||
},
|
||||
{
|
||||
"path": "projects/FrontendMentor/junior/advice-generator-app"
|
||||
"path": "projects/FrontendMentor/junior/github-user-search-app"
|
||||
}
|
||||
],
|
||||
"settings": {}
|
||||
|
|
After Width: | Height: | Size: 17 KiB |
After Width: | Height: | Size: 12 KiB |
After Width: | Height: | Size: 49 KiB |
After Width: | Height: | Size: 32 KiB |
After Width: | Height: | Size: 2.5 KiB |
After Width: | Height: | Size: 1.3 KiB |
After Width: | Height: | Size: 7.4 KiB |
After Width: | Height: | Size: 3.6 KiB |
After Width: | Height: | Size: 21 KiB |
After Width: | Height: | Size: 13 KiB |
After Width: | Height: | Size: 15 KiB |
After Width: | Height: | Size: 10 KiB |
After Width: | Height: | Size: 47 KiB |
After Width: | Height: | Size: 31 KiB |
After Width: | Height: | Size: 27 KiB |
After Width: | Height: | Size: 15 KiB |
After Width: | Height: | Size: 68 KiB |
After Width: | Height: | Size: 36 KiB |
After Width: | Height: | Size: 16 KiB |
After Width: | Height: | Size: 9.0 KiB |
After Width: | Height: | Size: 41 KiB |
After Width: | Height: | Size: 21 KiB |
After Width: | Height: | Size: 4.2 KiB |
After Width: | Height: | Size: 2.1 KiB |
After Width: | Height: | Size: 12 KiB |
After Width: | Height: | Size: 6.8 KiB |
After Width: | Height: | Size: 7.8 KiB |
After Width: | Height: | Size: 5.1 KiB |
After Width: | Height: | Size: 24 KiB |
After Width: | Height: | Size: 14 KiB |
After Width: | Height: | Size: 3.9 KiB |
After Width: | Height: | Size: 2.1 KiB |
After Width: | Height: | Size: 11 KiB |
After Width: | Height: | Size: 5.7 KiB |
After Width: | Height: | Size: 8.5 KiB |
After Width: | Height: | Size: 5.8 KiB |
After Width: | Height: | Size: 23 KiB |
After Width: | Height: | Size: 14 KiB |
After Width: | Height: | Size: 2.1 KiB |
After Width: | Height: | Size: 1.0 KiB |
After Width: | Height: | Size: 5.9 KiB |
After Width: | Height: | Size: 2.6 KiB |
After Width: | Height: | Size: 17 KiB |
After Width: | Height: | Size: 9.6 KiB |
After Width: | Height: | Size: 46 KiB |
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 8.6 KiB |
After Width: | Height: | Size: 4.6 KiB |
After Width: | Height: | Size: 22 KiB |
After Width: | Height: | Size: 12 KiB |
After Width: | Height: | Size: 2.8 KiB |
After Width: | Height: | Size: 1.3 KiB |
After Width: | Height: | Size: 7.0 KiB |
After Width: | Height: | Size: 3.4 KiB |
After Width: | Height: | Size: 3.0 KiB |
After Width: | Height: | Size: 1.7 KiB |
After Width: | Height: | Size: 8.5 KiB |
After Width: | Height: | Size: 5.3 KiB |
After Width: | Height: | Size: 4.3 KiB |
After Width: | Height: | Size: 2.5 KiB |
After Width: | Height: | Size: 12 KiB |
After Width: | Height: | Size: 6.8 KiB |
After Width: | Height: | Size: 4.6 KiB |
After Width: | Height: | Size: 2.4 KiB |
After Width: | Height: | Size: 12 KiB |
After Width: | Height: | Size: 6.1 KiB |
After Width: | Height: | Size: 6.4 KiB |
After Width: | Height: | Size: 3.2 KiB |
After Width: | Height: | Size: 16 KiB |
After Width: | Height: | Size: 7.2 KiB |
After Width: | Height: | Size: 5.7 KiB |
After Width: | Height: | Size: 3.9 KiB |
After Width: | Height: | Size: 17 KiB |
After Width: | Height: | Size: 10 KiB |
After Width: | Height: | Size: 3.1 KiB |
After Width: | Height: | Size: 1.5 KiB |
After Width: | Height: | Size: 9.5 KiB |
After Width: | Height: | Size: 5.0 KiB |
After Width: | Height: | Size: 20 KiB |
After Width: | Height: | Size: 16 KiB |
After Width: | Height: | Size: 76 KiB |
After Width: | Height: | Size: 58 KiB |
After Width: | Height: | Size: 5.0 KiB |
After Width: | Height: | Size: 2.6 KiB |
After Width: | Height: | Size: 14 KiB |
After Width: | Height: | Size: 7.2 KiB |
After Width: | Height: | Size: 11 KiB |
After Width: | Height: | Size: 8.9 KiB |
After Width: | Height: | Size: 46 KiB |
After Width: | Height: | Size: 36 KiB |
After Width: | Height: | Size: 3.6 KiB |
After Width: | Height: | Size: 2.1 KiB |
After Width: | Height: | Size: 11 KiB |
After Width: | Height: | Size: 6.8 KiB |
After Width: | Height: | Size: 7.0 KiB |
After Width: | Height: | Size: 4.2 KiB |
After Width: | Height: | Size: 19 KiB |
After Width: | Height: | Size: 10 KiB |