|
| 1 | +import { minify } from "html-minifier"; |
| 2 | +import sharp from "sharp"; |
| 3 | +import path from "path"; |
| 4 | +import fs from "fs"; |
| 5 | +import { fileURLToPath } from "url"; |
| 6 | + |
| 7 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 8 | + |
| 9 | +export default function (eleventyConfig) { |
| 10 | + // Exclude data helper modules from template processing |
| 11 | + eleventyConfig.ignores.add("src/pages/_helpers/**"); |
| 12 | + |
| 13 | + // Copy assets to output |
| 14 | + eleventyConfig.addPassthroughCopy({ "src/assets": "assets" }); |
| 15 | + |
| 16 | + // Watch content YAML files for changes |
| 17 | + eleventyConfig.addWatchTarget("src/content/"); |
| 18 | + |
| 19 | + // Minify HTML output |
| 20 | + eleventyConfig.addTransform("htmlmin", function (content, outputPath) { |
| 21 | + if (outputPath && outputPath.endsWith(".html")) { |
| 22 | + return minify(content, { |
| 23 | + removeComments: true, |
| 24 | + collapseWhitespace: true, |
| 25 | + }); |
| 26 | + } |
| 27 | + return content; |
| 28 | + }); |
| 29 | + |
| 30 | + // Convert PNG images to WebP after build |
| 31 | + eleventyConfig.on("eleventy.after", async () => { |
| 32 | + const assetsDir = path.resolve(__dirname, "dist/assets"); |
| 33 | + if (!fs.existsSync(assetsDir)) return; |
| 34 | + await convertPngsToWebp(assetsDir); |
| 35 | + }); |
| 36 | + |
| 37 | + return { |
| 38 | + dir: { |
| 39 | + input: "src/pages", |
| 40 | + output: "dist", |
| 41 | + }, |
| 42 | + }; |
| 43 | +} |
| 44 | + |
| 45 | +async function convertPngsToWebp(dir) { |
| 46 | + const entries = fs.readdirSync(dir, { withFileTypes: true }); |
| 47 | + for (const entry of entries) { |
| 48 | + const fullPath = path.join(dir, entry.name); |
| 49 | + if (entry.isDirectory()) { |
| 50 | + await convertPngsToWebp(fullPath); |
| 51 | + } else if (entry.name.endsWith(".png")) { |
| 52 | + const outFile = fullPath.replace(/\.png$/, ".webp"); |
| 53 | + await sharp(fullPath).toFile(outFile); |
| 54 | + } |
| 55 | + } |
| 56 | +} |
0 commit comments