-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild
executable file
·103 lines (90 loc) · 3.47 KB
/
build
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
#!/usr/bin/env node
"use strict";
var fs = require('fs');
var assert = require('assert');
var uglifyjs = require('uglify-js');
var cheerio = require('cheerio');
var hasOwn = Object.prototype.hasOwnProperty;
var devMode = ~process.argv.indexOf('--dev');
// generally a really bad idea, but a fun experiment.
var obscure = {
// textures:
'wrap':'u', 'opaque':'o', 'width':'w', 'height':'h',
// frames:
'iofs':'a', 'inum':'b',
// entities: (AVOID 'x' and 'y')
'onground':'g', 'onrope':'r', 'frames':'f', 'tex':'t', 'anim':'a', 'animOfs':'b', 'animTr':'c',
'geom':'e', 'mins':'i', 'maxs':'j', 'pos':'k', 'speed':'s', 'update':'u', 'flip':'l',
'walkAnim':'w', 'walkIdle':'m', 'climbAnim':'n', 'climbIdle':'o', 'jumpAnim':'p',
'accX':'q', 'accY':'v', 'velX':'h', 'velY':'d', 'jumpHeld':'z', 'is_rope':'A',
'climbing':'B', 'lastDmg':'C', 'is_enemy':'D', 'health':'E', 'color':'F', 'thread':'G',
// geometry:
'draw':'H',
// hit-test:
'hitL':'a','hitR':'b','hitB':'c','hitT':'d','ladder':'e','pain':'f',
};
// minify HTML and inline CSS.
var $ = cheerio.load(read('web/index.html'), devMode?{}:{ignoreWhitespace:true});
$('style').each(function (i,elem) {
assert(elem.children && elem.children[0] && elem.children[0].type == 'text');
elem.children[0].data = minCSS(elem.children[0].data);
});
// minify the loader and insert at the end of the document.
var loader = minify(wrap(read('web/loader.js')));
$('body').append('<script>'+loader+'</script>');
// write the static web files.
fs.writeFileSync('out/index.html', $.html(), 'utf8');
// copy the logo image.
copy('out/esme.png', 'web/esme.png');
// read and minify shaders.
var shaders = 'var ShaderLibrary='+JSON.stringify({
vertex: norm_ws(read('src/shader.vertex', 'utf8')),
fragment: norm_ws(read('src/shader.fragment', 'utf8')),
})+';\n';
// write the game-server data files.
write('gen/game.js', minify(wrap(
'"use strict";\n' +
shaders +
untrace(concat([
'src/raf-shim.js',
'src/defs.js',
'src/cache.js',
'src/audio.js',
'src/controls.js',
'src/glr.js',
'src/geom.js',
'src/engine.js',
'src/images.js',
'src/spawn.js',
'src/start.js'
]))
)));
function minify(text) {
if (devMode) return text;
if (1) {
// obscure object fields (closure compiler kind of thing)
text = text.replace(/\.[A-Za-z_]+\b/g, function (word) { word=word.substring(1); return '.' + (hasOwn.call(obscure,word) ? obscure[word] : word); });
text = text.replace(/\b[A-Za-z_]+:/g, function (word) { word=word.substring(0,word.length-1); return (hasOwn.call(obscure,word) ? obscure[word] : word) + ':'; });
}
return uglifyjs.minify(text, {fromString:true}).code; // TODO: outSourceMap
}
function untrace(text) {
if (devMode) return text;
return text.replace(/\btrace\([^\)]+\);/g,';');
}
function concat(files) { return files.map(read).join('\n;'); }
function read(from) { return fs.readFileSync(from, 'utf8'); }
function write(to, text) { fs.writeFileSync(to, text, 'utf8'); }
function copy(to, from) { fs.writeFileSync(to, fs.readFileSync(from)); }
function norm_ws(text) { return text.replace(/[ \t]+/g,' ').replace(/\n\s*/g,'\n'); }
function wrap(text) { return '(function(){\n' + text + '\n})()'; }
function minCSS(css) {
return css.replace(/\s+{/g, '{')
.replace(/{\s+/g, '{')
.replace(/\s+}/g, '}')
.replace(/}\s+/g, '}')
.replace(/:\s+/g, ':')
.replace(/;\s+/g, ';')
.replace(/,\s+/g, ',')
.replace(/;}/g, '}');
}