-
Notifications
You must be signed in to change notification settings - Fork 1
/
minify.js
58 lines (54 loc) · 1.53 KB
/
minify.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
49
50
51
52
53
54
55
56
57
58
const fs = require('fs').promises
const matchURL = /https:\/\/[^.]+\.now\.sh/
const { minify } = require('html-minifier')
const Terser = require('terser')
const toKB = n => `${(n / 1000).toFixed(2)}kb`
const minifyOpts = {
caseSensitive: false,
collapseBooleanAttributes: true,
collapseInlineTagWhitespace: true,
collapseWhitespace: true,
conservativeCollapse: true,
decodeEntities: true,
html5: true,
includeAutoGeneratedTags: true,
keepClosingSlash: false,
maxLineLength: false,
minifyCSS: true,
minifyJS: js => Terser.minify(js).code,
minifyURLs: true,
preserveLineBreaks: false,
preventAttributesEscaping: true,
processConditionalComments: true,
removeAttributeQuotes: true,
removeComments: true,
removeEmptyAttributes: true,
removeEmptyElements: false,
removeOptionalTags: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
removeTagWhitespace: true,
sortAttributes: true,
sortClassName: true,
trimCustomFragments: true,
useShortDoctype: true,
}
module.exports.minify = async ({
src = 'index.template.html',
dest = 'index.html',
url,
silent,
...opts
} = {}) => {
const file = await fs.readFile(src, 'utf8')
const minified = minify(file, { ...minifyOpts, ...opts })
silent ||
console.log(
`${dest}:`,
`${toKB(minified.length)}`,
`(${toKB(file.length - minified.length)} saved)`,
)
const content = url ? minified.replace(matchURL, url) : minified
return fs.writeFile(dest, content, 'utf8')
}