-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtsdown.config.ts
More file actions
54 lines (50 loc) · 1.27 KB
/
tsdown.config.ts
File metadata and controls
54 lines (50 loc) · 1.27 KB
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
49
50
51
52
53
54
import { minify } from 'html-minifier-terser'
import { defineConfig, type Rolldown } from 'tsdown'
export default defineConfig({
dts: true,
attw: {
profile: 'esm-only',
},
publint: true,
entry: {
index: 'src/index.ts',
core: 'src/core.ts',
cli: 'src/cli.ts',
},
plugins: [minifyHTMLPlugin()],
nodeProtocol: true,
})
function minifyHTMLPlugin(): Rolldown.Plugin {
return {
name: 'minify-html-plugin',
transform: {
filter: {
id: /\.html$/,
},
async handler(code) {
return {
code: await minifyHTML(code),
moduleType: 'text',
}
},
},
}
}
async function minifyHTML(html: string) {
const minifiedHTML = await minify(html, {
removeComments: true,
removeEmptyAttributes: true,
html5: true,
decodeEntities: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeAttributeQuotes: true,
removeRedundantAttributes: true,
})
console.log(
`HTML size reduced by ${Math.round(
100 - (minifiedHTML.length / html.length) * 100,
)}%`,
)
return minifiedHTML
}