Skip to content

Commit

Permalink
Setup up build-time image generation pipeline with eleventy-img; Orga…
Browse files Browse the repository at this point in the history
…nised public folder
  • Loading branch information
vxncetxn committed Oct 10, 2022
1 parent d430856 commit 6f61134
Show file tree
Hide file tree
Showing 22 changed files with 383 additions and 14 deletions.
26 changes: 26 additions & 0 deletions generate-images.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const Image = require("@11ty/eleventy-img");
const fs = require("fs");
const path = require("path");

(async () => {
console.log(`Generating images...`);

let inputDir = "./images";
let outputDir = "./public/images";

fs.readdir(inputDir, (_, files) => {
files.forEach((file) => {
Image(`${inputDir}/${file}`, {
widths: [300],
formats: ["avif", "webp", "jpeg"],
outputDir,
filenameFormat: (_, src, width, format) => {
const extension = path.extname(src);
const name = path.basename(src, extension);
console.log(`Generated ${name}-${width}w.${format}`);
return `${name}-${width}w.${format}`;
},
});
});
});
})();
File renamed without changes
Binary file added images/test-image-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
"version": "0.0.1",
"private": true,
"scripts": {
"dev": "astro dev",
"generate-images": "node generate-images.js",
"dev": "pnpm generate-images && astro dev",
"start": "astro dev",
"build": "astro build",
"build": "pnpm generate-images && astro build",
"preview": "astro preview"
},
"devDependencies": {
"@11ty/eleventy-img": "^2.0.1",
"@astrojs/react": "^1.1.4",
"@astrojs/tailwind": "^1.0.0",
"@types/three": "^0.144.0",
Expand Down
Loading

0 comments on commit 6f61134

Please sign in to comment.