add client js minifier
This commit is contained in:
parent
4c6eb86794
commit
1de440cedb
6 changed files with 59 additions and 2 deletions
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
<title>
|
||||
Starbugs
|
||||
</title>
|
||||
<script src="starbugs-min.js"></script>
|
||||
<script src="starbugs.min.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<canvas id="starbugs_canvas">
|
||||
|
|
7
public/js/starbugs/main.js
Normal file
7
public/js/starbugs/main.js
Normal file
|
@ -0,0 +1,7 @@
|
|||
'use strict';
|
||||
|
||||
window.addEventListener('load', init);
|
||||
|
||||
function init() {
|
||||
|
||||
}
|
|
@ -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);
|
||||
|
|
39
server/web/minify.js
Normal file
39
server/web/minify.js
Normal file
|
@ -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;
|
Loading…
Add table
Add a link
Reference in a new issue