forked from gemini-hlsw/explore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
53 lines (49 loc) · 2.07 KB
/
build.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
const path = require("path")
const fs = require("fs")
const process = require("process")
const { build } = require("vite")
const { minify } = require("terser")
const humanFormat = require("human-format")
const brotliSize = require("brotli-size")
const config = require("./vite.config.js")
const outDir = path.resolve(__dirname, "heroku/static")
const terserOptions = {
sourceMap: false,
nameCache: {},
format: {
comments: false,
ecma: 2015
},
mangle: {
properties: {
debug: false,
reserved: ['$classData', 'main', 'toString', 'constructor', 'length', 'call', 'apply', 'NaN', 'Infinity', 'undefined'],
// Basically, every root package except Lcrystal and Lexplore. For some reason it breaks dynamic import of the help system.
regex: /^(\$m_|.*__f_|.*__F\d?_|.*__O_|.*Ljava|.*Lcats|.*Ljapgolly|.*Lfs2|.*Lorg|.*Lcom|.*Lio|.*Leu|.*Lclue|.*Llucuma|.*Lreact)/,
}
}
}
var i = 1
function runTerserOn(fileName, length) {
process.stdout.write(`Minifying ${i++}/${length}: ${fileName}...`)
const absolute = path.join(outDir, fileName)
const original = fs.readFileSync(absolute, "utf8")
minify(original, terserOptions).then( minified => {
fs.writeFileSync(absolute, minified.code, "utf8")
const fromSize = original.length
const toSize = minified.code.length
const ratio = (toSize / fromSize) * 100
const fromBrotli = brotliSize.sync(original)
const toBrotli = brotliSize.sync(minified.code)
const brotliRatio = (toBrotli / fromBrotli) * 100
process.stdout.write(` ${humanFormat.bytes(fromSize, { prefix: 'Ki' })} --> ${humanFormat.bytes(toSize, { prefix: 'Ki' })} (${ratio.toFixed(2)}%)` +
` / brotli: ${humanFormat.bytes(fromBrotli, { prefix: 'Ki' })} --> ${humanFormat.bytes(toBrotli, { prefix: 'Ki' })} (${brotliRatio.toFixed(2)}%) \n`)
})
}
;(async () => {
const rollupOutput = await build(config)
const jsChunks = rollupOutput.output.map(chunk => chunk.fileName).filter(fileName => fileName.endsWith(".js"))
for (const fileName of jsChunks) {
await runTerserOn(fileName, jsChunks.length)
}
})()