HTML Minify improvements

This commit is contained in:
Dustin Brett
2022-03-07 20:24:31 -08:00
parent 908fab9a46
commit 2fcbb54645
3 changed files with 50 additions and 21 deletions

View File

@@ -1,20 +0,0 @@
{
"collapseBooleanAttributes": true,
"collapseInlineTagWhitespace": true,
"collapseWhitespace": true,
"decodeEntities": true,
"includeAutoGeneratedTags": false,
"minifyJS": true,
"minifyURLs": true,
"removeAttributeQuotes": true,
"removeComments": true,
"removeEmptyAttributes": true,
"removeOptionalTags": true,
"removeRedundantAttributes": true,
"removeScriptTypeAttributes": true,
"removeStyleLinkTypeAttributes": true,
"sortAttributes": true,
"sortClassName": true,
"trimCustomFragments": true,
"useShortDoctype": true
}

View File

@@ -10,7 +10,7 @@
"scripts": {
"build": "npm run build:fs && npm run build:search && next build",
"build:fs": "cd public && node ../scripts/fs2json.js --exclude .index --out .index/fs.9p.json . && node ../node_modules/browserfs/dist/scripts/make_http_index.js > .index/fs.bfs.json",
"build:html": "html-minifier-terser --config-file .html-minifier.json --input-dir out --output-dir out --file-ext html",
"build:html": "node scripts/minifyHtml.js",
"build:robots": "node scripts/robots.js",
"build:search": "node scripts/searchIndex.js",
"deploy": "npm run build:robots && next export && npm run build:html",

49
scripts/minifyHtml.js Normal file
View File

@@ -0,0 +1,49 @@
const { readdirSync, readFileSync, writeFileSync } = require("fs");
const { minify } = require("html-minifier-terser");
const { extname, join } = require("path");
const OUT_PATH = "out";
const HTML_MINIFIER_CONFIG = {
collapseBooleanAttributes: true,
collapseInlineTagWhitespace: true,
collapseWhitespace: true,
decodeEntities: true,
includeAutoGeneratedTags: false,
minifyJS: true,
minifyURLs: true,
processConditionalComments: true,
processScripts: ["text/html"],
removeAttributeQuotes: true,
removeComments: true,
removeEmptyAttributes: true,
removeOptionalTags: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
sortAttributes: true,
sortClassName: true,
trimCustomFragments: true,
useShortDoctype: true,
};
const CODE_REMOVE_FUNCTIONS = [
(html) => html.replace(/<noscript (.*)><\/noscript>/, ""),
(html) => html.replace(/<script (.*) nomodule=""><\/script>/, ""),
(html) =>
html.replace(/<style data-styled="" data-styled-version=(.*)>/, "<style>"),
];
readdirSync(OUT_PATH).forEach(async (entry) => {
if (extname(entry) === ".html") {
const filPath = join(OUT_PATH, entry);
const html = await readFileSync(filPath);
let minifiedHtml = await minify(html.toString(), HTML_MINIFIER_CONFIG);
CODE_REMOVE_FUNCTIONS.forEach(
(codeFunction) => (minifiedHtml = codeFunction(minifiedHtml))
);
await writeFileSync(filPath, minifiedHtml);
}
});