Skip to content

Commit b141780

Browse files
committed
Adds minify tool for JS/JSON/CSS/XML/SQL - #1
1 parent 408dfaa commit b141780

File tree

6 files changed

+115
-1
lines changed

6 files changed

+115
-1
lines changed

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"author": "Hirako2000",
2121
"license": "MIT",
2222
"scripts": {
23-
"now-start": "exit 0",
23+
"now-start": "exit 1",
2424
"now-build": "gulp build && cross-env NODE_ENV=production node .",
2525
"start": "cross-env NODE_ENV=dev browser-refresh .",
2626
"start:prod-test": "cross-env NODE_ENV=production node .",
@@ -37,10 +37,12 @@
3737
"babel-polyfill": "^6.7.2",
3838
"basscss": "^8.0.3",
3939
"bluebird": "^3.3.4",
40+
"clean-css": "^4.1.9",
4041
"highlight.js": "^9.12.0",
4142
"isdev": "^1.0.1",
4243
"js-beautify": "^1.6.14",
4344
"jshashes": "^1.0.7",
45+
"jsonminify": "^0.4.1",
4446
"koa": "^2.3.0",
4547
"koa-better-body": "^3.0.4",
4648
"koa-compress": "^2.0.0",
@@ -62,6 +64,7 @@
6264
"pretty-data": "^0.40.0",
6365
"shortid": "^2.2.8",
6466
"source-map-support": "^0.4.0",
67+
"uglify-es": "^3.1.9",
6568
"winston": "^2.3.1"
6669
},
6770
"devDependencies": {

src/routes/header.marko

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ header
77
span
88
a href='/beautify' -- Beautify
99
span -- |
10+
span
11+
a href='/minify' -- Minify
12+
span -- |
1013
span
1114
a href='/paste' -- Paste
1215
span -- |

src/routes/home/template.marko

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ include('../layout.marko')
88
p -- Beautify de-obfuscates/formats pieces of code and syntax-highlight the result making it easier to read and understand...
99
strong.p1 -- JavaScript/JSON - XML - HTML - CSS - SQL
1010

11+
div.col.col-12.p2
12+
div.sm-col.lg-col-4.sm-col-12.md-col-4
13+
a.btn.btn-huge href='/minify' -- Minify
14+
div.sm-col.lg-col-8.sm-col-12.md-col-8.pt1.pb2
15+
p -- Minify pieces of code to save space
16+
strong.p1 -- JavaScript - JSON - XML - CSS - SQL
17+
1118
div.col.col-12.p2
1219
div.sm-col.lg-col-4.sm-col-12.md-col-4
1320
a.btn.btn-huge href='/hash' -- Hash

src/routes/minify/index.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import {app} from 'server';
2+
import {post, get} from 'koa-route';
3+
import UglifyJS from 'uglify-es';
4+
import jsonminify from 'jsonminify';
5+
import CleanCSS from 'clean-css';
6+
import {pd} from 'pretty-data';
7+
import template from './template.marko';
8+
9+
const javascript = 'JAVASCRIPT';
10+
const json = 'JSON';
11+
const xml = 'XML';
12+
const css = 'CSS';
13+
const sql = 'SQL';
14+
15+
const formats = [javascript, json, xml, css, sql];
16+
17+
const title = 'Minify - GisTeam';
18+
19+
app.use(get('/minify', async ctx => {
20+
ctx.body = template.stream({formats, title});
21+
ctx.type = 'text/html';
22+
}));
23+
24+
app.use(post('/minify', async ctx => {
25+
const format = ctx.request.body.format;
26+
const input = ctx.request.body.input;
27+
let output;
28+
29+
switch (format) {
30+
case javascript:
31+
output = minifyJS(input);
32+
break;
33+
case json:
34+
output = minifyJSON(input);
35+
break;
36+
case css:
37+
output = minifyCSS(input);
38+
break;
39+
case sql:
40+
output = pd.sqlmin(input);
41+
break;
42+
case xml:
43+
output = pd.xml(input);
44+
break;
45+
case 'guess':
46+
output = minifyJS(input);
47+
break;
48+
default:
49+
output = minifyJS(input);
50+
}
51+
52+
ctx.body = template.stream({format, formats, input, result: output, title});
53+
ctx.type = 'text/html';
54+
}));
55+
56+
function minifyJS(data) {
57+
const out = UglifyJS.minify(data, {
58+
compress: false, mangle: false
59+
});
60+
if (out.error) return 'Parsing Error, perhaps try JSON minifier...';
61+
return out.code;
62+
}
63+
64+
function minifyJSON(data) {
65+
return jsonminify(data);
66+
}
67+
68+
function minifyCSS(data) {
69+
const output = new CleanCSS({}).minify(data);
70+
if (!output || output.styles.length === 0) return 'No CSS in there...';
71+
return output.styles;
72+
}

src/routes/minify/template.marko

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
include('../layout.marko')
2+
@content
3+
div.pt2.pb4.pl3.pr3
4+
form class='form' action='/minify' method='post'
5+
div
6+
p -- Input
7+
div
8+
p
9+
select class="ui dropdown full-width" name='format'
10+
for(format in data.formats)
11+
if(format === data.format)
12+
option value='${format}' selected -- ${format}
13+
else
14+
option value='${format}' -- ${format}
15+
div
16+
p
17+
textarea class='textarea' name='input' rows="10"
18+
if(data.input)
19+
-- ${data.input}
20+
div
21+
p
22+
button type="submit" class='btn full-width' onClick="" value="button"
23+
-- MINIFY!
24+
if(data.result)
25+
div
26+
p -- Output
27+
pre
28+
code -- ${data.result}

src/server.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ require('./routes/home');
9090
require('./routes/beautify');
9191
require('./routes/paste');
9292
require('./routes/hash');
93+
require('./routes/minify');
9394

9495
/* Connect to MongoDB */
9596
const mongoHost = process.env.DB_HOST || config.mongodb.host;

0 commit comments

Comments
 (0)