-
Notifications
You must be signed in to change notification settings - Fork 1
/
postbuild.js
44 lines (37 loc) · 1.04 KB
/
postbuild.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
const fs = require('fs')
const archiver = require('archiver')
const minify = require('html-minifier').minify
const MAX = 13 * 1024 // 13kb
fs.unlinkSync('./dist/bundle.js')
const content = fs.readFileSync('./dist/index.html')
const minified = minify(content.toString(), {
collapseWhitespace: true,
minifyCSS: true,
})
fs.writeFileSync('./dist/index.html', minified)
const output = fs.createWriteStream('./build.zip')
const archive = archiver('zip', { zlib: { level: 9 } })
output.on('close', function () {
const bytes = archive.pointer()
const percent = ((bytes / MAX) * 100).toFixed(2)
if (bytes > MAX) {
console.error(`Size overflow: ${bytes} bytes (${percent}%)`)
} else {
console.log(`Size: ${bytes} bytes (${percent}%)`)
}
})
archive.on('warning', function (err) {
if (err.code === 'ENOENT') {
console.warn(err)
} else {
throw err
}
})
archive.on('error', function (err) {
throw err
})
archive.pipe(output)
archive.append(fs.createReadStream('./dist/index.html'), {
name: 'index.html',
})
archive.finalize()