Skip to content

Commit

Permalink
feat: Minification (#94)
Browse files Browse the repository at this point in the history
* added minifications for html, and css built from scss

* cleanup css themes to keep only the first comment
  • Loading branch information
uroybd authored Mar 6, 2023
1 parent 59c3cde commit 8026d6f
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 10 deletions.
18 changes: 18 additions & 0 deletions .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const matter = require("gray-matter");
const faviconPlugin = require("eleventy-favicon");
const tocPlugin = require("eleventy-plugin-nesting-toc");
const { parse } = require("node-html-parser");
const htmlMinifier = require("html-minifier");

const { headerToId, namedHeadingsFilter } = require("./src/helpers/utils");
const {
Expand Down Expand Up @@ -304,6 +305,23 @@ module.exports = function (eleventyConfig) {
return str && parsed.innerHTML;
});

eleventyConfig.addTransform("htmlMinifier", (content, outputPath) => {
if (
process.env.NODE_ENV === "production" &&
outputPath &&
outputPath.endsWith(".html")
) {
return htmlMinifier.minify(content, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true,
minifyCSS: true,
minifyJS: true,
});
}
return content;
});

eleventyConfig.addPassthroughCopy("src/site/img");
eleventyConfig.addPassthroughCopy("src/site/scripts");
eleventyConfig.addPassthroughCopy("src/site/styles/_theme.*.css");
Expand Down
171 changes: 168 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"watch:sass": "sass --watch src/site/styles:dist/styles",
"watch:eleventy": "cross-env ELEVENTY_ENV=dev eleventy --serve",
"build:eleventy": "cross-env ELEVENTY_ENV=prod NODE_OPTIONS=--max-old-space-size=4096 eleventy",
"build:sass": "sass src/site/styles:dist/styles",
"build:sass": "sass src/site/styles:dist/styles --style compressed",
"get-theme": "node src/site/get-theme.js",
"build": "npm-run-all get-theme build:*",
"postbuild": "node src/site/lunr-index.js"
Expand All @@ -19,6 +19,7 @@
"devDependencies": {
"@11ty/eleventy": "^2.0.0",
"cross-env": "^7.0.3",
"html-minifier": "^4.0.0",
"node-html-parser": "^6.1.4",
"npm-run-all": "^4.1.5",
"sass": "^1.49.9"
Expand All @@ -43,4 +44,4 @@
"markdown-it-plantuml": "^1.4.1",
"markdown-it-task-checkbox": "^1.0.6"
}
}
}
18 changes: 13 additions & 5 deletions src/site/get-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const fs = require("fs");
const crypto = require("crypto");
const glob = require("glob");

const themeCommentRegex = /\/\*[\s\S]*?\*\//g;

async function getTheme() {
let themeUrl = process.env.THEME;
if (themeUrl) {
Expand All @@ -26,13 +28,19 @@ async function getTheme() {
fs.rmSync(file);
});
} catch {}
let skippedFirstComment = false;
const data = res.data.replace(themeCommentRegex, (match) => {
if (skippedFirstComment) {
return "";
} else {
skippedFirstComment = true;
return match;
}
});
const hashSum = crypto.createHash("sha256");
hashSum.update(res.data);
hashSum.update(data);
const hex = hashSum.digest("hex");
fs.writeFileSync(
`src/site/styles/_theme.${hex.substring(0, 8)}.css`,
res.data
);
fs.writeFileSync(`src/site/styles/_theme.${hex.substring(0, 8)}.css`, data);
}
}

Expand Down

0 comments on commit 8026d6f

Please sign in to comment.