forked from RatoGBM/Laserpointer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
executable file
·133 lines (111 loc) · 3.68 KB
/
Copy pathbuild.js
File metadata and controls
executable file
·133 lines (111 loc) · 3.68 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/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 USE_ROADROLLER = false; // enable for extra compression
const sourceFiles = [
'littlejs.js',
'game.js',
'gameObjects.js',
'level.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,
USE_ROADROLLER ? [closureCompilerStep, uglifyBuildStep, roadrollerBuildStep, htmlBuildStep, zipBuildStep] : [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...`);
// use closer compiler to minify the code
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 roadrollerBuildStep(filename) {
console.log(`Running roadroller...`);
child_process.execSync(`npx roadroller ${filename} -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 += '<meta charset=utf-8>';
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...`);
// zip the build folder using ect
const args = ['-9', '-strip', '-zip', `../${PROGRAM_NAME}.zip`, 'index.html', ...dataFiles];
child_process.spawnSync('ect', args, {
stdio: 'inherit',
cwd: BUILD_FOLDER
});
};