-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
48 lines (41 loc) · 1.28 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import path from "node:path";
import { readFileSync, writeFileSync } from "node:fs";
import minifier from "html-minifier-terser";
/** @type {import('.').default} */
export default function (insideAdapter, options) {
const name = "sveltekit-html-minifier";
return {
name: insideAdapter ? `${name}, ${insideAdapter.name}` : name,
async adapt(builder) {
if (insideAdapter) {
await insideAdapter.adapt(builder);
}
/** @type {import('.').Options} */
const mergedOptions = {
pages: "build",
...options,
minifierOptions: {
removeAttributeQuotes: true,
minifyJS: true,
minifyCSS: true,
collapseWhitespace: true,
useShortDoctype: true,
...options?.minifierOptions,
},
};
builder.prerendered.pages.forEach(async (page) => {
const htmlPath = path.join(mergedOptions.pages, page.file);
builder.log.info(`Minify HTML ${htmlPath}`);
// read HTML file
const html = readFileSync(htmlPath).toString();
// minify HTML
const minHTML = await minifier.minify(
html,
mergedOptions.minifierOptions
);
// write minified HTML
await writeFileSync(htmlPath, minHTML);
});
},
};
}