Skip to content

Commit 3e889b8

Browse files
committed
Minify HTML in production
1 parent 03fa032 commit 3e889b8

4 files changed

Lines changed: 179 additions & 6 deletions

File tree

.eleventy.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
const filters = require("./utils/filters.js")
22
const esbuild = require("esbuild");
33
const { sassPlugin } = require("esbuild-sass-plugin");
4+
const htmlmin = require("html-minifier");
45

56
module.exports = function (eleventyConfig) {
67

78
eleventyConfig.addGlobalData("generated", () => {
89
let now = new Date();
910
return new Intl.DateTimeFormat(
10-
"en-GB", { dateStyle: "medium", timeStyle: "long", timeZone: "UTC"}
11+
"en-GB", { dateStyle: "medium", timeStyle: "long", timeZone: "UTC" }
1112
).format(now);
1213
});
1314

@@ -16,17 +17,29 @@ module.exports = function (eleventyConfig) {
1617
eleventyConfig.addFilter(filterName, filters[filterName])
1718
})
1819

20+
eleventyConfig.addTransform("htmlmin", (content, outputPath) => {
21+
if (outputPath && outputPath.endsWith(".html") && process.env.NODE_ENV === "production") {
22+
return htmlmin.minify(content, {
23+
useShortDoctype: true,
24+
removeComments: true,
25+
collapseWhitespace: true
26+
});
27+
}
28+
return content;
29+
});
30+
1931
eleventyConfig.on("afterBuild", () => {
2032
return esbuild.build({
2133
entryPoints: ["_site/_esbuild/app.scss", "_site/_esbuild/app.js"],
2234
outdir: "_site/assets",
2335
bundle: true,
36+
legalComments: "linked",
2437
minify: process.env.NODE_ENV === "production",
2538
sourcemap: process.env.NODE_ENV !== "production",
2639
plugins: [sassPlugin()]
2740
});
2841
});
29-
42+
3043
eleventyConfig.addWatchTarget("./src/sass/");
3144
eleventyConfig.addWatchTarget("./src/js/");
3245

package-lock.json

Lines changed: 163 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"esbuild": "^0.14.47",
1919
"esbuild-sass-plugin": "^2.2.6",
2020
"flexsearch": "^0.7.21",
21+
"html-minifier": "^4.0.0",
2122
"markdown-it": "^13.0.1",
2223
"p-limit": "^3.1.0"
2324
}

src/assets/app.scss.njk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
permalink: /_esbuild/app.scss
33
---
4-
54
@import "../../node_modules/bootstrap/scss/functions";
65

76
// variable overrides

0 commit comments

Comments
 (0)