Minifying a file, the simple way

71 views
Skip to first unread message

Yves Amsellem

unread,
Jun 4, 2015, 3:00:50 PM6/4/15
to esp...@googlegroups.com
Looking at this demo (http://esprima.org/demo/minify.html), it seems straightforward to minify a file with esprima. But there is not a single line of code explaining how anywhere.

Is this a game where only brave developers who find it in the page source are allowed to used it?
Have I missed something?

I've seen the quote:
> Minification is carried out using escodegen and esmangle projects.

But a brunch of code would be a good help ;-)

Yves Amsellem

unread,
Jun 8, 2015, 5:38:38 AM6/8/15
to esp...@googlegroups.com
var fs = require('fs');
var esprima = require('esprima');
var esmangle = require('esmangle');
var escodegen = require('escodegen');

var before = fs.readFileSync('file.tpl', {encoding: 'utf-8'});

var syntax = esprima.parse(before, {raw: true, loc: true});
syntax
= esmangle.optimize(syntax);
var after = escodegen.generate(syntax, {
        format
: { indent: { style: '' },
        quotes
: 'auto',
        compact
: true
   
}
});

Here is the solution I come up with.
Doing some benchmark with benchmark.js on node 0.12 make it closer to uglify.js, but not better (and because uglify is 3 times slower on 0.12, I was expecting a difference).

If you know a way to optimize this, go ahead ;-).
Reply all
Reply to author
Forward
0 new messages