From f015725c91deda13b9f3bb32e60cc5f57020d93c Mon Sep 17 00:00:00 2001 From: Cassie Jones Date: Tue, 12 Nov 2019 00:02:52 -0500 Subject: [PATCH] Add HTML minification --- .eleventy.js | 18 ++++++++++-- package-lock.json | 71 +++++++++++++++++++++++++++++++++++++++++++++-- package.json | 1 + 3 files changed, 85 insertions(+), 5 deletions(-) diff --git a/.eleventy.js b/.eleventy.js index 3a95329..7257ed9 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -4,13 +4,14 @@ const Nunjucks = require("nunjucks"); const dateFilter = require("nunjucks-date-filter"); const md = require("markdown-it")(); const CleanCSS = require("clean-css"); +const htmlMinifier = require("html-minifier"); module.exports = (eleventyConfig) => { eleventyConfig.addNunjucksFilter('date', dateFilter); eleventyConfig.addPlugin(pluginRss); eleventyConfig.addPlugin(pluginSyntaxHighlight); - eleventyConfig.addFilter("cssmin", code => - new CleanCSS({}).minify(code).styles); + + eleventyConfig.addTransform("html-minifier", htmlMinifierTransform); eleventyConfig.addPassthroughCopy("img"); eleventyConfig.addPassthroughCopy("static"); @@ -22,6 +23,8 @@ module.exports = (eleventyConfig) => { eleventyConfig.addFilter("markdown", value => md.renderInline(value)); eleventyConfig.addFilter("groupby", groupbyFilter); + eleventyConfig.addFilter("cssmin", css => + new CleanCSS({}).minify(css).styles); eleventyConfig.addCollection("years", collection => { const posts = collection.getFilteredByTag("posts"); @@ -110,3 +113,14 @@ function figureShortcode(content, { src, alt }) {
${captionHtml}
`; } + +function htmlMinifierTransform(content, outputPath) { + if (outputPath.endsWith(".html")) { + return htmlMinifier.minify(content, { + useShortDoctype: true, + removeComments: true, + collapseWhitespace: true, + }); + } + return content; +} diff --git a/package-lock.json b/package-lock.json index ef07624..87c8e42 100644 --- a/package-lock.json +++ b/package-lock.json @@ -869,6 +869,16 @@ "integrity": "sha1-KAOY5dZkvXQDi28JBRU+borxvCA=", "dev": true }, + "camel-case": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/camel-case/-/camel-case-3.0.0.tgz", + "integrity": "sha1-yjw2iKTpzzpM2nd9xNy8cTJJz3M=", + "dev": true, + "requires": { + "no-case": "^2.2.0", + "upper-case": "^1.1.1" + } + }, "camelcase": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-2.1.1.tgz", @@ -2903,12 +2913,33 @@ } } }, + "he": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz", + "integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==", + "dev": true + }, "hosted-git-info": { "version": "2.8.5", "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.8.5.tgz", "integrity": "sha512-kssjab8CvdXfcXMXVcvsXum4Hwdq9XGtRD3TteMEvEbq0LXyiNQr6AprqKqfeaDXze7SxWvRxdpwE6ku7ikLkg==", "dev": true }, + "html-minifier": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/html-minifier/-/html-minifier-4.0.0.tgz", + "integrity": "sha512-aoGxanpFPLg7MkIl/DDFYtb0iWz7jMFGqFhvEDZga6/4QTjneiD8I/NXL1x5aaoCp7FSIT6h/OhykDdPsbtMig==", + "dev": true, + "requires": { + "camel-case": "^3.0.0", + "clean-css": "^4.2.1", + "commander": "^2.19.0", + "he": "^1.2.0", + "param-case": "^2.1.1", + "relateurl": "^0.2.7", + "uglify-js": "^3.5.1" + } + }, "htmlparser2": { "version": "3.10.1", "resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-3.10.1.tgz", @@ -3504,6 +3535,12 @@ "integrity": "sha1-MKCy2jj3N3DoKUoNIuZiXtd9AJc=", "dev": true }, + "lower-case": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/lower-case/-/lower-case-1.1.4.tgz", + "integrity": "sha1-miyr0bno4K6ZOkv31YdcOcQujqw=", + "dev": true + }, "lru-cache": { "version": "4.1.5", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-4.1.5.tgz", @@ -3861,6 +3898,15 @@ "integrity": "sha512-iyam8fBuCUpWeKPGpaNMetEocMt364qkCsfL9JuhjXX6dRnguRVOfk2GZaDpPjcOKiiXCPINZC1GczQ7iTq3Zw==", "dev": true }, + "no-case": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/no-case/-/no-case-2.3.2.tgz", + "integrity": "sha512-rmTZ9kz+f3rCvK2TD1Ue/oZlns7OGoIWP4fc3llxxRXlOkHKoWPPWJOfFYpITabSow43QJbRIoHQXtt10VldyQ==", + "dev": true, + "requires": { + "lower-case": "^1.1.1" + } + }, "node-source-walk": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/node-source-walk/-/node-source-walk-4.2.0.tgz", @@ -4102,6 +4148,15 @@ "os-tmpdir": "^1.0.0" } }, + "param-case": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/param-case/-/param-case-2.1.1.tgz", + "integrity": "sha1-35T9jPZTHs915r75oIWPvHK+Ikc=", + "dev": true, + "requires": { + "no-case": "^2.2.0" + } + }, "parse-filepath": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/parse-filepath/-/parse-filepath-1.0.2.tgz", @@ -4765,6 +4820,12 @@ "safe-regex": "^1.1.0" } }, + "relateurl": { + "version": "0.2.7", + "resolved": "https://registry.npmjs.org/relateurl/-/relateurl-0.2.7.tgz", + "integrity": "sha1-VNvzd+UUQKypCkzSdGANP/LYiKk=", + "dev": true + }, "remove-trailing-separator": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/remove-trailing-separator/-/remove-trailing-separator-1.1.0.tgz", @@ -5868,7 +5929,6 @@ "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.6.6.tgz", "integrity": "sha512-q3Zusqd028P8MdbarqL0I1snTZ7+IbIWiKUXVZyXMVdOSxOG2FFqLXyGlgYSqYu46SR7tR3Sk0xqN1VtvxGWnQ==", "dev": true, - "optional": true, "requires": { "commander": "~2.20.3", "source-map": "~0.6.1" @@ -5878,8 +5938,7 @@ "version": "0.6.1", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", - "dev": true, - "optional": true + "dev": true } } }, @@ -5978,6 +6037,12 @@ "integrity": "sha512-aZwGpamFO61g3OlfT7OQCHqhGnW43ieH9WZeP7QxN/G/jS4jfqUkZxoryvJgVPEcrl5NL/ggHsSmLMHuH64Lhg==", "dev": true }, + "upper-case": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/upper-case/-/upper-case-1.1.3.tgz", + "integrity": "sha1-9rRQHC7EzdJrp4vnIilh3ndiFZg=", + "dev": true + }, "urix": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/urix/-/urix-0.1.0.tgz", diff --git a/package.json b/package.json index c52e3ad..ddea4ff 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "@11ty/eleventy-plugin-rss": "^1.0.7", "@11ty/eleventy-plugin-syntaxhighlight": "file:../../oss/eleventy-plugin-syntaxhighlight", "clean-css": "^4.2.1", + "html-minifier": "^4.0.0", "markdown-it": "^8.4.2", "nunjucks-date-filter": "^0.1.1" }, -- 2.47.0