-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbuild.js
153 lines (129 loc) · 3.66 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const rollup = require('rollup')
const fs = require('fs')
const tempfile = require('tempfile')
const ClosureCompiler = require('google-closure-compiler').compiler
const rollupPluginJson = require('rollup-plugin-json')
const rollupPluginUrl = require('rollup-plugin-url')
const childProcess = require('child_process')
const minifyHtml = require('html-minifier').minify
function asyncCompile (compiler) {
return new Promise(resolve => compiler.run((...args) => resolve(args)))
}
const closureCompilerPlugin = {
name: 'closure-compiler',
async transformBundle (code) {
const jsFilename = tempfile()
const mapFilename = tempfile()
fs.writeFileSync(jsFilename, code)
const compiler = new ClosureCompiler({
js: jsFilename,
create_source_map: mapFilename,
process_common_js_modules: true,
language_out: 'ECMASCRIPT_NEXT',
compilation_level: 'ADVANCED'
})
const [exitCode, stdOut, stdErr] = await asyncCompile(compiler)
if (exitCode != 0) {
throw new Error(`closure compiler exited ${exitCode}: ${stdErr}`)
}
return {
code: stdOut,
map: JSON.parse(fs.readFileSync(mapFilename))
}
}
}
const transformConstToLet = {
transformBundle (code) {
return code.replace(/\bconst\b/g, 'let')
}
}
// Rename certain words and rewrite patterns which closure compiler usually doesn't mangle, such that
// it actually does mangle them.
const preMangle = {
transformBundle (code) {
code = code.replace(/"maps":/g, 'maps:')
code = code.replace(/"entities":/g, 'entities:')
for (let word of [
'frames',
'facing',
'detach',
'step',
'entities',
'maps'
]) {
code = code.replace(new RegExp(`\\b${word}\\b`, 'g'), 'M' + word)
}
return code
}
}
const plugins = [
rollupPluginJson(),
rollupPluginUrl({
limit: Infinity
}),
transformConstToLet,
preMangle,
closureCompilerPlugin
]
const inputOptions = {
input: 'src/entry.js',
plugins
}
const outputOptions = {
file: 'dist/build.js',
format: 'es'
}
function advZipWindows () {
return new Promise((resolve, reject) => {
const command = `.\\bin\\advzip.exe -4 -a ./dist/dist.zip ./dist/index.html`
childProcess.exec(command, { cwd: __dirname }, (error, stdout, stderr) => {
if (error) {
return reject(stderr)
}
resolve(stdout)
})
})
}
function advZipFallback () {
return new Promise((resolve, reject) => {
const command = `advzip -4 -a ./dist/dist.zip ./dist/index.html`
childProcess.exec(command, { cwd: __dirname }, (error, stdout, stderr) => {
if (error) {
return reject(stderr)
}
resolve(stdout)
})
})
}
async function build() {
const bundle = await rollup.rollup(inputOptions)
const { code } = await bundle.generate(outputOptions)
let minifiedHtml = minifyHtml(
fs.readFileSync('index.html', { encoding: 'utf-8' }),
{
collapseWhitespace: true,
minifyCSS: true,
removeAttributeQuotes: true
}
)
let newScriptTag = `<script>${code}</script>`
minifiedHtml = minifiedHtml.replace(/<script[^>]+><\/script>/, m => newScriptTag)
fs.writeFileSync('dist/index.html', minifiedHtml, { encoding: 'utf-8' })
try {
await advZipWindows()
} catch (e) {
try {
await advZipFallback()
} catch (e) {
console.log('Could not zip index.html using advzip. Does the advzip binary even exist?')
return
}
}
const finalFileSize = fs.readFileSync('./dist/dist.zip').byteLength
console.log('Final file size:', finalFileSize)
const limit = 13 * 1024
if (finalFileSize > limit) {
console.error(`That's ${finalFileSize - limit} too many bytes!`)
}
}
build()