added JS merger

This commit is contained in:
Nathan DECHER 2020-03-24 10:46:01 +01:00
parent 7362b4dc5c
commit bd6d9f3399
5 changed files with 45 additions and 4 deletions

1
.gitignore vendored
View file

@ -2,5 +2,6 @@ node_modules/
public/assets/*.png
public/assets/*.json
public/css/*.css
public/js/*.js
public/favicon.ico

View file

@ -8,9 +8,11 @@ SNAKE = public/assets/snake.json
CSS = public/css/snek.css
OUTPUT = $(ICON) $(APPLE) $(WALL) $(SNAKE) $(CSS)
JS = public/js/snek.js
all: icon css apple wall snake
OUTPUT = $(ICON) $(APPLE) $(WALL) $(SNAKE) $(CSS) $(JS)
all: icon apple wall snake css js
icon: $(ICON)
apple: $(APPLE)
wall: $(WALL)
@ -19,6 +21,8 @@ snake: $(SNAKE)
css: $(CSS)
js: $(JS)
public/assets/icon32.png: assets/icon.jpg
convert $^ -resize 32x $@
public/assets/icon256.png: assets/icon.jpg
@ -35,8 +39,11 @@ public/assets/wall32.png: assets/wall.png
public/assets/snake.json: assets/snake.json
cp $^ $@
public/css/snek.css: src/less/snek.less
lessc $^ $@
public/css/snek.css: src/less/snek.less $(wildcard src/less/*.less)
lessc $< $@
public/js/snek.js: $(wildcard src/js/*.js)
node mergejs.js $^ > $@
clean:
rm -f $(OUTPUT)

29
mergejs.js Normal file
View file

@ -0,0 +1,29 @@
const fs=require('fs');
const requireFn=`
const require=function require(name) {
if(require.cache[name]) return require.cache[name];
if(!require.source[name]) throw new Error("Cannot require "+name+": not found");
require.cache[name]=require.source[name]() || true;
return require.cache[name];
};
require.cache=Object.create(null);
require.source=Object.create(null);
window.require=require;
`;
let outputCode=[requireFn];
process.argv
.slice(2)
.map(a => [a, a.match(/([a-zA-Z_][a-zA-Z0-9_-]*).js$/)[1]])
.forEach(([modFile, modName]) => {
const modSource=fs.readFileSync(modFile, 'utf8');
outputCode.push(`
require.source['${modName}']=(a => a.bind(a)) (function ${modName}() {
'use strict';
${modSource}
});
`);
});
fs.writeSync(1, outputCode.join('\n'));

0
public/js/.gitkeep Normal file
View file

View file

@ -1,3 +1,5 @@
const Assets=require('assets');
const [EMPTY, FOOD, WALL, SNAKE]=Array(4).keys();
const ifNaN=(v, r) => isNaN(v)?r:v;
@ -191,3 +193,5 @@ class SnekGame {
requestAnimationFrame(() => this.tick());
}
}
return SnekGame;