diff --git a/package.json b/package.json index 7275688..269e46c 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,8 @@ "box2dweb": "^2.1.0-b", "colors": "^1.1.2", "express": "^4.13.4", - "socket.io": "^1.4.5" + "recursive-readdir": "^1.3.0", + "socket.io": "^1.4.5", + "uglify-js": "^2.6.2" } } diff --git a/public/index.html b/public/index.html index a224b0e..ade171d 100644 --- a/public/index.html +++ b/public/index.html @@ -7,7 +7,7 @@ Starbugs - + diff --git a/public/js/main.js b/public/js/main.js deleted file mode 100644 index e69de29..0000000 diff --git a/public/js/starbugs/main.js b/public/js/starbugs/main.js new file mode 100644 index 0000000..2f9b9c2 --- /dev/null +++ b/public/js/starbugs/main.js @@ -0,0 +1,7 @@ +'use strict'; + +window.addEventListener('load', init); + +function init() { + +} diff --git a/server/web/index.js b/server/web/index.js index 0069ed5..1143d4e 100644 --- a/server/web/index.js +++ b/server/web/index.js @@ -2,6 +2,8 @@ const express = require('express'); +const minify = require('./minify.js'); + class WebServer { constructor() { @@ -11,6 +13,13 @@ class WebServer { this.app = express(); let app = this.app; + app.get('/starbugs.min.js', (req, res) => { + minify(result => { + res.contentType('starbugs.min.js'); + res.end(result); + }); + }); + app.use(express.static('public')); app.listen(8080); diff --git a/server/web/minify.js b/server/web/minify.js new file mode 100644 index 0000000..73c0799 --- /dev/null +++ b/server/web/minify.js @@ -0,0 +1,39 @@ +const colors = require('colors'); +const fs = require('fs'); +const path = require('path'); +const recursive = require('recursive-readdir'); +const uglify = require('uglify-js'); + +function minifyJs(callback) { + callback = callback || function() {}; + + var dir = path.join(__dirname, '../../public/js/starbugs'); + var cache = ''; + + recursive(dir, function(err, files) { + for(var i in files) { + cache += fs.readFileSync(files[i], 'utf8').toString(); + } + + var comment = ''; + + try { + cache = uglify.minify(cache, { fromString: true }).code; + + comment = '/* This is a minified file. '; + comment += 'If you would like to view the source in a more readable format, '; + comment += 'please contact the developer.*/\n' + } catch(err) { + console.log('Error parsing kelvin.min.js file'.red); + + + comment = '/* This file could not be minified because of JS syntax errors.'; + comment += ' If you encounter errors when running Starbugs, please contact'; + comment += ' a developer.*/\n' + } + + callback(comment + cache); + }); +} + +module.exports = minifyJs;