-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
88 lines (74 loc) · 2.43 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { readFileSync, writeFileSync } from 'fs';
import { minify as minifyJs } from "terser";
import { minify as minifyHtml } from 'html-minifier';
import { cmdRegPack } from 'regpack';
const options = {
toplevel: true,
compress: {
passes: 2,
// unsafe: true,
// unsafe_arrows: true,
// unsafe_comps: true,
// unsafe_math: true,
booleans_as_integers: true,
},
mangle: {
properties: {
keep_quoted: true,
},
},
format: {
wrap_func_args: false,
},
};
let js = readFileSync('src/main.js', 'utf8');
// Some custom mangling of JS to assist / work around Terser
js = js
// Remove whitespace in CSS template literals
.replace(/ = `[^`]+`/g, tag => tag.replace(/\s+/g, ''))
// Replace const with let
.replaceAll('const', 'let')
// Replace all strict equality comparison with abstract equality comparison
.replaceAll('===', '==')
// Remove the last semicolon at the end of a CSS string
.replaceAll(':0;`', ':0`');
const minifiedJs = await minifyJs(js, options);
const code = minifiedJs.code
// Replace all double quotes with backticks for consistency
.replaceAll('"', '`')
.replace('let e=[]', 'e=[]')
.replaceAll('for(let ', 'for(')
// Remove final semicolon
.replace(/;$/, '');
const packed = cmdRegPack(code, {
crushLengthFactor : parseFloat(1),
});
const html = readFileSync('src/index.html', 'utf8');
const inlined = html.replace(
/<script[^>]*><\/script>/,
`<script>${packed}</script>`,
);
const inlinedNonPacked = html.replace(
/<script[^>]*><\/script>/,
`<script>${code}</script>`,
);
const minifiedInlined = minifyHtml(inlined, {
removeAttributeQuotes: true,
collapseWhitespace: true,
});
const minifiedInlinedNonPacked = minifyHtml(inlinedNonPacked, {
removeAttributeQuotes: true,
collapseWhitespace: true,
});
const mangled = minifiedInlined
.replace('<!DOCTYPE html><html>', '') // Remove doctype & HTML opening tags
.replace(';</script>', '</script>') // Remove final semicolon
.replace('<head>', '') // Remove head opening tag
.replace('</head>', '') // Remove head closing tag
.replace('"initial-scale=1"', 'initial-scale=1') // Remove initial-scale quotes
.replace('<body>', '') // Remove body opening tag
.replace('</body></html>', ''); // Remove closing tags
console.log(`Mangled: ${mangled.length}B`);
writeFileSync('index.nonpacked.html', minifiedInlinedNonPacked);
writeFileSync('index.watch.html', minifiedInlined);
writeFileSync('index.html', mangled);