-
Notifications
You must be signed in to change notification settings - Fork 1
/
run-build.js
101 lines (88 loc) · 2.44 KB
/
run-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
// `run-build.js` is based off of the `run.js` script that
// Replit uses to build the game using esbuild.
// This script creates a `dist/` dir that is ready to host the game.
// Replit functionality including users & database are removed.
const fs = require("fs");
const fse = require("fs-extra");
const path = require("path");
const esbuild = require("esbuild");
let err = null;
// build user game
function buildGame() {
const template = fs.readFileSync("template.html", "utf-8");
let code = "";
code += `<script src="helper.js"></script>\n`;
code += `<script src="game.js"></script>\n`;
try {
// // clean
// try {
// let dir = 'dist';
// fs.rmSync(dir, { recursive: true, force: true });
// console.log(`${dir}/ deleted!`);
// } catch (err) {
// console.log(`wtffff`);
// console.error(`Error while deleting ${dir}.`);
// }
var dir = 'dist';
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
// build user code
esbuild.buildSync({
bundle: true,
sourcemap: true,
target: "es6",
keepNames: true,
// logLevel: "silent",
entryPoints: ["code/main.js"],
outfile: "dist/game.js",
});
esbuild.buildSync({
bundle: true,
sourcemap: true,
target: "es6",
keepNames: true,
entryPoints: ["helper.ts"],
outfile: "dist/helper.js",
});
console.log("buildGame esbuild.buildSync() steps complete, game.js & helper.js built");
} catch (e) {
const loc = e.errors[0].location;
err = {
msg: e.errors[0].text,
stack: [
{
line: loc.line,
col: loc.column,
file: loc.file,
},
],
};
let msg = "";
msg += "<pre>";
msg += `ERROR: ${err.msg}\n`;
if (err.stack) {
err.stack.forEach((trace) => {
msg += ` -> ${trace.file}:${trace.line}:${trace.col}\n`;
});
}
msg += "</pre>";
fs.writeFileSync("dist/index.html", msg);
return;
}
try {
fs.writeFileSync("dist/index.html", template.replace("{{kaboom}}", code));
console.log("Game & index built to dist/");
fse.copySync("sounds", "dist/sounds");
fse.copySync("sprites", "dist/sprites");
console.log("Copied sounds and sprites to dist/");
// // PWA
// fs.writeFileSync("dist/offline.html", template.replace("{{kaboom}}", code));
// fse.copySync("manifest.json", "dist/manifest.json");
// fse.copySync("pwabuilder-sw-register.js", "dist/pwabuilder-sw-register.js");
// fse.copySync("pwabuilder-sw.js", "dist/pwabuilder-sw.js");
} catch (err) {
console.error(err);
}
}
buildGame();