-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.js
83 lines (71 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
import { readFileSync, writeFileSync, mkdirSync } from 'fs'
import { execSync } from 'child_process'
import express from 'express'
import compression from 'compression'
import {minify as terserMinify} from 'terser'
async function getHtml({ minify = true }) {
let html = readFileSync('index.html').toString()
if (!minify) return html
let storyHtml = readFileSync('story.html').toString().replaceAll('\n', '')
const [htmlBodyAndStory, scripts] = html.split(/<!--\s*snip\s*-->/i)
const htmlBody = htmlBodyAndStory
.replace(/<!--\s*story\s*-->/i, storyHtml)
const scriptsObj = {}
scripts.replace(/"(.+?\.js)"/g, (_, $1) => {
scriptsObj[$1] = readFileSync($1).toString()
})
let { code } = await terserMinify(scriptsObj, {
compress: {
global_defs: {
'self.env': 'production'
},
// unsafe: true, // at the end
passes: 5,
pure_getters: true,
},
mangle: true,
toplevel: true,
format: {
ascii_only: true,
safari10: true,
wrap_func_args: false,
}
})
return `${htmlBody}<script>${code}</script>`
}
async function buildZip() {
const html = await getHtml({ minify: true })
mkdirSync('/tmp/js13k/.build', { recursive: true })
writeFileSync('/tmp/js13k/.build/index.html', html)
console.log(execSync(`zip --junk-paths -9 build.zip /tmp/js13k/.build/index.html`).toString())
console.log(execSync(`ls -al build.zip`).toString())
console.log(execSync(`ls -alh build.zip`).toString())
execSync(`mv build.zip /tmp/js13k/.build/`)
}
switch (process.argv[2]) {
case 'build': {
await buildZip()
break
}
case 'serve': {
const app = express()
app.use(compression())
app.on('error', err=>{console.error(err)})
app.get('/', (req, res) => {
getHtml({minify: process.argv.includes('--compressed')}).then(
html => res.contentType('html').end(html),
err => res.end(err.stack)
)
})
app.use('/', express.static('.'))
app.listen(3000, (err) => {
if (err) throw (err)
else console.log('listening on http://localhost:3000')
})
break
}
default: {
console.error(`usage:\nnode ./build.js build (requires 'zip' command)\nnode ./build.js serve`)
process.exit(1)
}
}