web-template/eleventy.config.js

54 lines
1.3 KiB
JavaScript

const esbuild = require("esbuild");
const htmlmin = require("html-minifier");
//const npath = require("path");
//const afs = require("fs/promises");
const input = "src";
const output = "_site";
const serve = process.env.ELEVENTY_RUN_MODE === "serve";
// TODO minify css and js in html
module.exports = eleventyConfig => {
eleventyConfig.on("eleventy.after", async ({ dir, results, runMode, outputMode }) => {
const esbuildResult = await esbuild.build({
entryPoints: [
`${input}/style.css`,
`${input}/main.ts`,
],
outdir: output,
format: "esm",
bundle: true,
splitting: true,
minify: !serve,
sourcemap: true,
//write: serve,
});
});
eleventyConfig.addTransform("htmlmin", function (content) {
if (this.page.outputPath && this.page.outputPath.endsWith(".html")) {
let minified = htmlmin.minify(content, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true,
});
return minified;
}
return content;
});
//config.setServerPassthroughCopyBehavior("passthrough"); // doesn't work
eleventyConfig.addPassthroughCopy({ assets: "/" });
eleventyConfig.addWatchTarget("src/**/*.css");
eleventyConfig.addWatchTarget("src/**/*.js");
eleventyConfig.addWatchTarget("src/**/*.ts");
eleventyConfig.addWatchTarget("src/**/*.json");
return {
dir: {
input,
output,
},
};
};