daedalOS/scripts/minifyHtml.js
2022-03-12 23:44:41 -08:00

58 lines
1.8 KiB
JavaScript

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 commit = process.env.npm_package_gitHead.slice(0, 8);
const CODE_REPLACE_FUNCTIONS = [
(html) => html.replace(/<noscript (.*)><\/noscript>/, ""),
(html) => html.replace(/><\/path>/, "/>"),
(html) => html.replace(/<script (.*) nomodule=""><\/script>/, ""),
(html) =>
html.replace(/<style data-styled="" data-styled-version=(.*)>/, "<style>"),
(html) =>
html.replace(
/<script id=__NEXT_DATA__ type=application\/json>(.*)<\/script>/,
`<script id=__NEXT_DATA__ type=application/json>{"buildId":"${commit}","page":"/","props":{}}</script>`
),
];
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_REPLACE_FUNCTIONS.forEach((codeFunction) => {
minifiedHtml = codeFunction(minifiedHtml);
});
await writeFileSync(filPath, minifiedHtml);
}
});