-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathbuild.mjs
96 lines (85 loc) · 3.42 KB
/
build.mjs
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
// This script deploys all files from /src/client to /dist in order to run the website.
// Depending on the value of DEV_BUILD in /src/server/config/config.js, this happens either in development mode or in production mode.
// Development mode: All files are simply copied over unmodified.
// Production mode: All non-script assets are copied over unmodified,
// but all game scripts in /src/client/scripts/game are concatenated into app.js.
// Further, all scripts are minified with the use of terser.
import { readdir, cp as copy, rm as remove, readFile, writeFile } from "node:fs/promises";
import { minify } from "terser";
import { injectScriptsIntoPlayEjs } from "./src/server/utility/HTMLScriptInjector.js"
import { DEV_BUILD } from "./src/server/config/config.js";
/**
*
* @param {string} path
* @param {string} ext
* @returns {Promise<string[]>}
*/
async function getExtFiles(path, ext) {
const filesNFolder = await readdir(path);
const folders = filesNFolder.filter(v => !v.endsWith(ext));
let files = filesNFolder.filter(v => v.endsWith(ext));
for (const folder of folders) {
try {
const newFiles = await getExtFiles(`${path}/${folder}`, ext);
files.push(...newFiles.map(v => `${folder}/${v}`));
} catch (e) {
if (e.code) continue;
console.log(e);
}
}
return files;
}
// remove dist
await remove("./dist", {
recursive: true,
force: true,
});
if (DEV_BUILD){
// in dev mode, copy all clientside files over to dist and exit
await copy("./src/client", "./dist", {
recursive: true,
force: true
});
// overwrite play.ejs by injecting all needed scripts into it:
await writeFile(`./dist/views/play.ejs`, injectScriptsIntoPlayEjs(), 'utf8');
} else{
// in prod mode, copy all clientside files over to dist, except for those contained in scripts
await copy("./src/client", "./dist", {
recursive: true,
force: true,
filter: filename => {
return !/(\\|\/)scripts(\\|\/)/.test(filename) || /(\\|\/)game$/.test(filename) // make sure to create the scripts/game/folder
}
});
// make a list of all client scripts:
const clientFiles = [];
const clientScripts = await getExtFiles("./src/client/scripts", ".js");
clientFiles.push(...clientScripts.map(v => `scripts/${v}`));
// string containing all code in /game except for htmlscript.js:
let gamecode = "";
for (const file of clientFiles) {
// If the client script is htmlscript.js or not in scripts/game, then minify it and copy it over
if (/\/htmlscript\.js$/.test(file) || !/scripts(\\|\/)+game(\\|\/)/.test(file) ){
const code = await readFile(`./src/client/${file}`, 'utf8');
const minified = await minify(code, {
mangle: true, // Enable variable name mangling
compress: true, // Enable compression
sourceMap: false
});
await writeFile(`./dist/${file}`, minified.code, 'utf8');
}
// Collect the code of all js files in /game except for htmlscript.js:
else{
gamecode += await readFile(`./src/client/${file}`, 'utf8');
}
}
// Combine all gamecode files into app.js
const minifiedgame = await minify(gamecode, {
mangle: true,
compress: true,
sourceMap: false
});
await writeFile(`./dist/scripts/game/app.js`, minifiedgame.code, 'utf8');
// overwrite play.ejs by injecting all needed scripts into it:
await writeFile(`./dist/views/play.ejs`, injectScriptsIntoPlayEjs(), 'utf8');
}