Skip to content

Commit 59e2902

Browse files
committed
Use custom HTML minifier to fix build fail
1 parent b84f206 commit 59e2902

File tree

4 files changed

+212
-53
lines changed

4 files changed

+212
-53
lines changed

package-lock.json

Lines changed: 168 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"preview": "npm run build && vite preview"
88
},
99
"devDependencies": {
10-
"@sergeymakinen/vite-plugin-html-minimize": "^2.0.0",
10+
"@types/html-minifier-next": "^1.1.0",
11+
"html-minifier-next": "^1.1.5",
1112
"typescript": "^5.3.3",
1213
"vite": "^7.1.2"
1314
}

tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
"module": "ESNext",
66
"lib": ["ES2020", "DOM", "DOM.Iterable"],
77
"skipLibCheck": true,
8+
// Prevent Node.js types from overriding Browser types. See:
9+
// https://guilhermesimoes.github.io/blog/making-settimeout-return-number-in-typescript
10+
"types": [],
811

912
/* Bundler mode */
1013
"moduleResolution": "bundler",

vite.config.ts

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,47 @@
1+
import type { MinifierOptions } from "html-minifier-next";
12
import type { UserConfig } from "vite";
23

3-
import htmlMinimize from "@sergeymakinen/vite-plugin-html-minimize";
4+
import { minify } from "html-minifier-next";
5+
import { createFilter } from "vite";
6+
7+
const filter = createFilter("**/*.html");
8+
const options: MinifierOptions = {
9+
collapseWhitespace: true,
10+
html5: true,
11+
minifyCSS: true,
12+
minifyJS: true,
13+
removeAttributeQuotes: true,
14+
removeComments: true,
15+
removeRedundantAttributes: true,
16+
removeScriptTypeAttributes: true,
17+
removeStyleLinkTypeAttributes: true,
18+
useShortDoctype: true
19+
};
420

521
export default <UserConfig>{
622
plugins: [
7-
htmlMinimize({
8-
minifierOptions: {
9-
collapseWhitespace: true,
10-
html5: true,
11-
minifyCSS: true,
12-
minifyJS: true,
13-
removeAttributeQuotes: true,
14-
removeComments: true,
15-
removeRedundantAttributes: true,
16-
removeScriptTypeAttributes: true,
17-
removeStyleLinkTypeAttributes: true,
18-
useShortDoctype: true
23+
{
24+
// Yoinked from `@sergeymakinen/vite-plugin-html-minimize` with some
25+
// changes.
26+
name: "html-minifier",
27+
apply: "build",
28+
enforce: "post",
29+
generateBundle: {
30+
order: "post",
31+
async handler(_, bundle) {
32+
for (const assetOrChunk of Object.values(bundle)) {
33+
if (
34+
assetOrChunk.type === "asset" &&
35+
filter(assetOrChunk.fileName)
36+
) {
37+
assetOrChunk.source = await minify(
38+
assetOrChunk.source.toString(),
39+
options
40+
);
41+
}
42+
}
43+
}
1944
}
20-
})
45+
}
2146
]
2247
};

0 commit comments

Comments
 (0)