|
| 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 | +} |
0 commit comments