forked from increpare/PuzzleScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.js
319 lines (265 loc) · 12.2 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
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/env node
/*
creates a highly compressed release build in bin of the contents of src
packages used:
npm i rimraf compress-images web-resource-inliner ncp [email protected] concat ycssmin terser gzipper html-minifier-terser glob
*/
const fs = require("fs");
const path = require('path');
const { execFileSync } = require('child_process');
const rimraf = require('rimraf');
const compress_images = require("compress-images");
var webResourceInliner = require("web-resource-inliner");
const ncp = require('ncp').ncp;
const gifsicle = require('gifsicle');
const concat = require('concat');
const cssmin = require('ycssmin').cssmin;
const { minify } = require("terser");
const { Compress } = require('gzipper');
const htmlminify = require('html-minifier-terser').minify;
const glob = require("glob")
//print all paths to all modules above
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("removing bin")
rimraf.sync("./bin");
fs.mkdirSync('./bin');
console.log("Copying files")
ncp.limit = 16;
ncp("./src", "./bin/", function (err) {
if (err) {
return console.error(err);
}
console.log("echo optimizing pngs");
rimraf.sync('./bin/images/*.png');
(async () => {
compress_images(
"./src/images/*.png",
"./bin/images/",
{ compress_force: false, statistic: false, autoupdate: true }, false,
{ jpg: { engine: "mozjpeg", command: ["-quality", "60"] } },
{ png: { engine: "pngcrush", command: ["-reduce", "-brute"] } },
{ svg: { engine: "svgo", command: "--multipass" } },
{ gif: { engine: "gifsicle", command: ["--colors", "64", "--use-col=web"] } },
function (error, completed, statistic) {
// console.log("-------------");
// console.log(error);
// console.log(completed);
// console.log(statistic);
// console.log("-------------");
}
);
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');
glob("./bin/Documentation/images/*.gif", {}, async function (er, files) {
for (filename of files) {
execFileSync(gifsicle, ['-O2', '-o', filename, filename]);
}
console.log('Images optimized');
//remove ".bin/js dir if it exists"
rimraf.sync('./bin/js');
rimraf.sync('./bin/css');
rimraf.sync('./bin/tests');
fs.mkdirSync('./bin/js');
fs.mkdirSync('./bin/css');
console.log('compressing css');
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 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");
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/storagewrapper.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/storagewrapper.js",
"./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/stringstream.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(/<!--___SCRIPTINSERT___-->/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">');
d = new Date();
const monthname = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];
editor = editor.replace(/<!--BUILDNUMBER-->/g, `build ${buildnum.toString()}, ${d.getDate()}-${monthname[d.getMonth()]}-${d.getFullYear()}`);
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(/<!--___SCRIPTINSERT___-->/g, '<script src="js\/scripts_play_compiled.js"><\/script>');
fs.writeFileSync("./bin/play.html", player, encoding = 'utf8');
console.log("inlining standalone template")
//src one first:
var standalone_raw = fs.readFileSync("./src/standalone.html", 'utf8');
webResourceInliner.html({
fileContent: standalone_raw,
relativeTo: 'src/',
},
function (err, inlined) {
if (err) {
console.log(err)
} else {
fs.writeFileSync("./src/standalone_inlined.txt", inlined);
}
});
//then bin one:
standalone_raw = standalone_raw.replace(/<script src="js\/[A-Za-z0-9_\/-]*\.js"><\/script>/g, "");
standalone_raw = standalone_raw.replace(/<!--___SCRIPTINSERT___-->/g, '<script src="js\/scripts_play_compiled.js"><\/script>');
webResourceInliner.html({
fileContent: standalone_raw,
relativeTo: 'bin/',
},
async function (err, inlined) {
if (err) {
console.log(err)
} else {
var minified = await htmlminify(inlined,
{
collapseBooleanAttributes: true,
collapseWhitespace: true,
minifyCSS: true,
minifyURLs: true,
removeAttributeQuotes: true,
removeComments: true,
removeEmptyAttributes: true,
});
fs.writeFileSync("./bin/standalone_inlined.txt", minified);
}
//delete ./bin/standalone.html
fs.unlinkSync("./bin/standalone.html");
console.log("compressing html");
glob("./bin/*.html", {}, async function (er, files) {
for (filename of files) {
var lines = fs.readFileSync(filename, encoding = 'utf8');
var result = await htmlminify(lines);
fs.writeFileSync(filename, result);
}
});
(async function a() {
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"));
for (var i=0;i<files.length;i++) {
var file = files[i];
var comp = new Compress(file);
await comp.run();
}
console.log("Files compressed. All good!");
})();
});
});
})();
});