2023-01-03 12:26:44 +01:00
|
|
|
import type { AstroIntegration } from "astro";
|
2022-12-27 19:41:12 +01:00
|
|
|
import { minify } from "html-minifier";
|
|
|
|
import { writeFile, readFile } from "node:fs/promises";
|
|
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
|
2023-01-03 12:26:44 +01:00
|
|
|
export default (): AstroIntegration => {
|
2022-12-27 19:41:12 +01:00
|
|
|
return {
|
|
|
|
name: "hyro-minify",
|
|
|
|
hooks: {
|
|
|
|
"astro:build:done": async (opts) => {
|
|
|
|
for (const route of opts.routes) {
|
2023-01-03 12:26:44 +01:00
|
|
|
const content = await readFile(fileURLToPath(route.distURL!), "utf-8");
|
2022-12-27 19:41:12 +01:00
|
|
|
const minified = await minify(content, {
|
|
|
|
removeAttributeQuotes: true,
|
|
|
|
collapseWhitespace: true,
|
|
|
|
minifyCSS: true,
|
|
|
|
html5: true,
|
|
|
|
removeComments: true,
|
|
|
|
minifyJS: true,
|
|
|
|
minifyURLs: true,
|
|
|
|
});
|
|
|
|
|
2023-01-03 12:26:44 +01:00
|
|
|
await writeFile(fileURLToPath(route.distURL!), minified, "utf-8");
|
|
|
|
console.log(`Minified ${route.distURL!.pathname}`)
|
2022-12-27 19:41:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|