-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
112 lines (91 loc) · 3.14 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
#!/usr/bin/env node
/**
* LittleJS Build System
*/
'use strict';
const PROGRAM_TITLE = 'Little JS Starter Project';
const PROGRAM_NAME = 'game';
const BUILD_FOLDER = 'build';
const sourceFiles =
[
'../../dist/littlejs.release.js',
'game.js',
// add your game's files here
];
const dataFiles =
[
'tiles.png',
// add your game's data files here
];
console.log(`Building ${PROGRAM_NAME}...`);
const startTime = Date.now();
const fs = require('node:fs');
const child_process = require('node:child_process');
// rebuild engine
//child_process.execSync(`npm run build`, { stdio: 'inherit' });
// remove old files and setup build folder
fs.rmSync(BUILD_FOLDER, { recursive: true, force: true });
fs.rmSync(`${PROGRAM_NAME}.zip`, { force: true });
fs.mkdirSync(BUILD_FOLDER);
// copy data files
for(const file of dataFiles)
fs.copyFileSync(file, `${BUILD_FOLDER}/${file}`);
Build
(
`${BUILD_FOLDER}/index.js`,
sourceFiles,
[closureCompilerStep, uglifyBuildStep, htmlBuildStep, zipBuildStep]
);
console.log('');
console.log(`Build Completed in ${((Date.now() - startTime)/1e3).toFixed(2)} seconds!`);
console.log(`Size of ${PROGRAM_NAME}.zip: ${fs.statSync(`${PROGRAM_NAME}.zip`).size} bytes`);
///////////////////////////////////////////////////////////////////////////////
// A single build with its own source files, build steps, and output file
// - each build step is a callback that accepts a single filename
function Build(outputFile, files=[], buildSteps=[])
{
// copy files into a buffer
let buffer = '';
for (const file of files)
buffer += fs.readFileSync(file) + '\n';
// output file
fs.writeFileSync(outputFile, buffer, {flag: 'w+'});
// execute build steps in order
for (const buildStep of buildSteps)
buildStep(outputFile);
}
function closureCompilerStep(filename)
{
console.log(`Running closure compiler...`);
const filenameTemp = filename + '.tmp';
fs.copyFileSync(filename, filenameTemp);
child_process.execSync(`npx google-closure-compiler --js=${filenameTemp} --js_output_file=${filename} --compilation_level=ADVANCED --warning_level=VERBOSE --jscomp_off=* --assume_function_wrapper`, {stdio: 'inherit'});
fs.rmSync(filenameTemp);
};
function uglifyBuildStep(filename)
{
console.log(`Running uglify...`);
child_process.execSync(`npx uglifyjs ${filename} -c -m -o ${filename}`, {stdio: 'inherit'});
};
function htmlBuildStep(filename)
{
console.log(`Building html...`);
// create html file
let buffer = '<!DOCTYPE html>';
buffer += '<head>';
buffer += `<title>${PROGRAM_TITLE}</title>`;
buffer += '</head>';
buffer += '<body>';
buffer += '<script>';
buffer += fs.readFileSync(filename) + '\n';
buffer += '</script>';
// output html file
fs.writeFileSync(`${BUILD_FOLDER}/index.html`, buffer, {flag: 'w+'});
};
function zipBuildStep(filename)
{
console.log(`Zipping...`);
const ect = '../../../node_modules/ect-bin/vendor/win32/ect.exe';
const args = ['-9', '-strip', '-zip', `../${PROGRAM_NAME}.zip`, 'index.html', ...dataFiles];
child_process.spawnSync(ect, args, {stdio: 'inherit', cwd: BUILD_FOLDER});
};