-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.js
307 lines (232 loc) · 10.3 KB
/
compile.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env node
/*
creates a highly compressed release build in bin of the contents of src
packages used:
npm i tar html-minifier-terser ycssmin google-closure-compiler concat pngcrush-bin inliner ncp rimraf gifsicle terser gzipper
*/
var fs = require("fs");
var path = require('path');
var lines = fs.readFileSync(".build/buildnumber.txt",encoding='utf-8');
var buildnum = parseInt(lines);
buildnum++;
fs.writeFileSync(".build/buildnumber.txt",buildnum.toString(),encoding='utf-8');
//#node-qunit-phantomjs tests/tests.html --timeout 40
console.log("===========================");
console.log('build number '+buildnum)
var start = new Date()
// console.log("clearing whitepsace from demos")
// cd demo
// find . -type f \( -name "*.txt" \) -exec perl -p -i -e "s/[ \t]*$//g" {} \;
// cd ..
console.log("removing bin")
fs.rmdirSync("./bin", { recursive: true });
fs.mkdirSync('./bin');
console.log("inlining standalone template")
var Inliner = require('inliner');
new Inliner('./src/standalone.html', function (error, html) {
// compressed and inlined HTML page
fs.writeFileSync("./src/standalone_inlined.txt",html,'utf8');
console.log("Copying files")
var ncp = require('ncp').ncp;
ncp.limit = 16;
ncp("./src", "./bin/", function (err) {
if (err) {
return console.error(err);
}
console.log("echo optimizing pngs");
const rimraf = require('rimraf');
rimraf.sync('./bin/images/*.png');
const imagemin = require('imagemin');
const imageminPngcrush = require('imagemin-pngcrush');
(async () => {
await imagemin(['./src/images/*.png'], {
destination: './bin/images/',
plugins: [
imageminPngcrush(["-brute","-reduce","-rem allb"])
]
});
const {execFileSync} = require('child_process');
const gifsicle = require('gifsicle');
console.log('Optimizing gallery gifs');
const galGifDir = "./bin/Gallery/gifs";
fs.readdirSync(galGifDir).forEach(file => {
if (fs.lstatSync(path.resolve(galGifDir, file)).isDirectory()) {
} else {
if (path.extname(file).toLowerCase()=== ".gif"){
execFileSync(gifsicle, ['--batch','-O2',galGifDir+"/"+file])
}
}
});
console.log('Optimizing documentation gifs');
var glob = require("glob")
glob("./bin/Documentation/images/*.gif", {}, async function (er, files) {
for (filename of files){
execFileSync(gifsicle, ['-O2','-o', filename, filename]);
}
console.log('Images optimized');
fs.rmdirSync("./bin/js", { recursive: true });
fs.mkdirSync('./bin/js');
fs.rmdirSync("./bin/css", { recursive: true });
fs.mkdirSync('./bin/css');
fs.rmdirSync("./bin/tests", { recursive: true });
fs.rmdirSync("./bin/Levels", { recursive: true });
console.log('compressing css');
const concat = require('concat');
await concat(["./src/css/docs.css",
"./src/css/codemirror.css",
"./src/css/midnight.css",
"./src/css/console.css",
"./src/css/gamecanvas.css",
"./src/css/soundbar.css",
"./src/css/layout.css",
"./src/css/toolbar.css",
"./src/css/dialog.css",
"./src/css/show-hint.css"],
"./bin/css/combined.css");
console.log('css files concatenated')
var cssmin = require('ycssmin').cssmin;
var css = fs.readFileSync("./bin/css/combined.css", encoding='utf8');
var min = cssmin(css);
fs.writeFileSync("./bin/css/combined.css",min,encoding="utf8");
var css = fs.readFileSync("./bin/Documentation/css/bootstrap.css", encoding='utf8');
var min = cssmin(css);
fs.writeFileSync("./bin/Documentation/css/bootstrap.css",min,encoding="utf8");
console.log("running js minification");
const { minify } = require("terser");
var files = [
"./src/js/Blob.js",
"./src/js/FileSaver.js",
"./src/js/jsgif/LZWEncoder.js",
"./src/js/jsgif/NeuQuant.js",
"./src/js/jsgif/GIFEncoder.js",
"./src/js/debug.js",
"./src/js/globalVariables.js",
"./src/js/font.js",
"./src/js/rng.js",
"./src/js/riffwave.js",
"./src/js/sfxr.js",
"./src/js/codemirror/codemirror.js",
"./src/js/codemirror/active-line.js",
"./src/js/codemirror/dialog.js",
"./src/js/codemirror/search.js",
"./src/js/codemirror/searchcursor.js",
"./src/js/codemirror/match-highlighter.js",
"./src/js/codemirror/show-hint.js",
"./src/js/codemirror/anyword-hint.js",
"./src/js/codemirror/comment.js",
"./src/js/colors.js",
"./src/js/graphics.js",
"./src/js/inputoutput.js",
"./src/js/mobile.js",
"./src/js/buildStandalone.js",
"./src/js/engine.js",
"./src/js/parser.js",
"./src/js/editor.js",
"./src/js/compiler.js",
"./src/js/console.js",
"./src/js/soundbar.js",
"./src/js/toolbar.js",
"./src/js/layout.js",
"./src/js/addlisteners.js",
"./src/js/addlisteners_editor.js",
"./src/js/makegif.js"];
var corpus={};
for (var i=0;i<files.length;i++){
var fpath=files[i];
corpus["source/"+fpath.slice(9)]=fs.readFileSync(fpath,encoding='utf-8');
}
var result = await minify(
corpus,
{
sourceMap: {
filename: "scripts_compiled.js",
url: "scripts_compiled.js.map"
}
});
fs.writeFileSync('./bin/js/scripts_compiled.js',result.code);
fs.writeFileSync('./bin/js/scripts_compiled.js.map',result.map);
files = [
"./src/js/globalVariables.js",
"./src/js/debug_off.js",
"./src/js/font.js",
"./src/js/rng.js",
"./src/js/riffwave.js",
"./src/js/sfxr.js",
"./src/js/codemirror/codemirror.js",
"./src/js/colors.js",
"./src/js/graphics.js",
"./src/js/engine.js",
"./src/js/parser.js",
"./src/js/compiler.js",
"./src/js/inputoutput.js",
"./src/js/mobile.js"];
corpus={};
for (var i=0;i<files.length;i++){
var fpath=files[i];
corpus["source/"+fpath.slice(9)]=fs.readFileSync(fpath,encoding='utf-8');
}
var result = await minify(
corpus,
{
sourceMap: {
filename: "scripts_play_compiled.js",
url: "scripts_play_compiled.js.map"
}
});
fs.writeFileSync('./bin/js/scripts_play_compiled.js',result.code);
fs.writeFileSync('./bin/js/scripts_play_compiled.js.map',result.map);
await ncp("./src/js", "./bin/js/source", function (err) {
if (err) {
return console.error(err);
}});
console.log("compilation done");
var editor = fs.readFileSync("./bin/editor.html", encoding='utf8');
editor = editor.replace(/<script src="js\/[A-Za-z0-9_\/-]*\.js"><\/script>/g, "");
editor = editor.replace(/<!--TOREPLACE-->/g, '<script src="js\/scripts_compiled.js"><\/script>');
editor = editor.replace(/<link rel="stylesheet" href="[A-Za-z0-9_\/-]*\.css">/g, '');
editor = editor.replace(/<!--CSSREPLACE-->/g, '<link rel="stylesheet" href="css\/combined.css">');
editor = editor.replace(/<!--BUILDNUMBER-->/g,'(build '+buildnum.toString()+')');
fs.writeFileSync("./bin/editor.html",editor, encoding='utf8');
var player = fs.readFileSync("./bin/play.html", encoding='utf8');
player = player.replace(/<script src="js\/[A-Za-z0-9_\/-]*\.js"><\/script>/g, "");
player = player.replace(/<!--TOREPLACE-->/g, '<script src="js\/scripts_play_compiled.js"><\/script>');
fs.writeFileSync("./bin/play.html",player, encoding='utf8');
console.log("compressing html");
var htmlminify = require('html-minifier-terser').minify;
glob("./bin/*.html", {}, async function (er, files) {
for (filename of files){
var lines=fs.readFileSync(filename, encoding='utf8');
var result = htmlminify(lines);
fs.writeFileSync(filename,result);
}
});
(async function a() {
const { Compress } = require('gzipper');
var glob = require("glob")
files = glob.sync("./bin/**/*.js");
files = files.concat(glob.sync("./bin/**/*.html"));
files = files.concat(glob.sync("./bin/**/*.css"));
files = files.concat(glob.sync("./bin/**/*.txt"));
var compressionTasks = files.map( fn=>new Compress(fn));
var compressed = await Promise.all(compressionTasks.map(gzip=>gzip.run()));
console.log("Files compressed. All good!");
})();
});
})();
});
});
/*
echo gzipping site
cd ../bin
./gzipper
rm README.md
rm gzipper
rm commit
cd ../src
end=`date +%s`
runtime=$((end-start))
time=`date "+%H:%M:%S"`
echo script end time : $time
echo script took $runtime seconds
echo ===========================
*/