This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
build.js
104 lines (90 loc) · 3.26 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
const fs = require("fs");
const path = require("path");
const yargs = require("yargs");
const { hideBin } = require('yargs/helpers')
const browserify = require("browserify");
const minifier = require("babel-minify");
const MAIN_IN = path.resolve(__dirname, "webgl", "game", "main.js");
const MAIN_OUT = path.resolve(__dirname, "public", "js", "main.min.js");
const RENDERER_IN = path.resolve(__dirname, "webgl", "game", "renderer.js");
const RENDERER_OUT = path.resolve(__dirname, "public", "js", "renderer.min.js");
const LOADER_IN = path.resolve(__dirname, "webgl", "game", "loader.js");
const LOADER_OUT = path.resolve(__dirname, "public", "js", "loader.min.js");
const CONTROL_IN = path.resolve(__dirname, "webgl", "control", "control.js");
const CONTROL_OUT = path.resolve(__dirname, "public", "js", "control.min.js");
const SW_IN = path.resolve(__dirname, "src", "worker.js");
const SW_OUT = path.resolve(__dirname, "public", "js", "sw.min.js");
/** @returns {Promise<string>} */
const streamToString = stream => {
const chunks = [];
return new Promise((resolve, reject) => {
stream.on('data', chunk => chunks.push(chunk))
stream.on('error', reject)
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')))
});
}
const argv = yargs(hideBin(process.argv))
.option('renderer', {
alias: 'r',
type: 'boolean',
description: 'Build renderer'
})
.option('sharedworker', {
alias: 's',
type: 'boolean',
description: 'Build sharedworker'
})
.option('main', {
alias: 'm',
type: 'boolean',
description: 'Build entry point'
})
.option('control', {
alias: 'c',
type: 'boolean',
description: 'Build controller'
})
.option('loader', {
alias: 'l',
type: 'boolean',
description: 'Build loader'
})
.option('all', {
alias: 'a',
type: 'boolean',
description: 'Build all js files'
})
.argv;
(async () => {
let code;
let bundled = 0;
if (argv.renderer || argv.all) {
console.log("Building renderer"); bundled++;
code = await streamToString(browserify(RENDERER_IN).bundle());
fs.writeFileSync(RENDERER_OUT, minifier(code).code);
}
if (argv.main || argv.all) {
console.log("Building main"); bundled++;
code = await streamToString(browserify(MAIN_IN).bundle());
fs.writeFileSync(MAIN_OUT, minifier(code).code);
}
if (argv.control || argv.all) {
console.log("Building web console"); bundled++;
code = await streamToString(browserify(CONTROL_IN).bundle());
fs.writeFileSync(CONTROL_OUT, minifier(code).code);
}
if (argv.loader || argv.all) {
console.log("Building loader"); bundled++;
code = await streamToString(browserify(LOADER_IN).bundle());
fs.writeFileSync(LOADER_OUT, minifier(code).code);
}
if (argv.sharedworker || argv.all) {
console.log("Building sharedworker"); bundled++;
code = await streamToString(browserify(SW_IN).bundle());
fs.writeFileSync(SW_OUT, minifier(code, {
mangle: false,
evaluate: false
}).code);
}
if (!bundled) console.log("Nothing was bundled");
})();