SlimIt - JavaScript minifier
Project description
Welcome to SlimIt
SlimIt is a JavaScript minifier written in Python. It compiles JavaScript into more compact code so that it downloads and runs faster.
SlimIt also provides a library that includes a JavaScript parser, lexer, pretty printer and a tree visitor.
Let’s minify some code
From the command line:
$ slimit -h Usage: slimit [options] [input file] If no input file is provided STDIN is used by default. Minified JavaScript code is printed to STDOUT. Options: -h, --help show this help message and exit -m, --mangle mangle names $ cat test.js var a = function( obj ) { for ( var name in obj ) { return false; } return true; }; $ $ slimit --mangle < test.js var a=function(a){for(var b in a)return false;return true;};
Or using library API:
>>> from slimit import minify >>> text = """ ... var a = function( obj ) { ... for ( var name in obj ) { ... return false; ... } ... return true; ... }; ... """ >>> print minify(text, mangle=True) var a=function(a){for(var b in a)return false;return true;};
Iterate over, modify a JavaScript AST and pretty print it
>>> from slimit.parser import Parser >>> from slimit.visitors import nodevisitor >>> from slimit import ast >>> >>> parser = Parser() >>> tree = parser.parse('for(var i=0; i<10; i++) {var x=5+i;}') >>> for node in nodevisitor.visit(tree): ... if isinstance(node, ast.Identifier) and node.value == 'i': ... node.value = 'hello' ... >>> print tree.to_ecma() # print awesome javascript :) for (var hello = 0; hello < 10; hello++) { var x = 5 + hello; } >>>
Using lexer in your project
>>> from slimit.lexer import Lexer >>> lexer = Lexer() >>> lexer.input('a = 1;') >>> for token in lexer: ... print token ... LexToken(ID,'a',1,0) LexToken(EQ,'=',1,2) LexToken(NUMBER,'1',1,4) LexToken(SEMI,';',1,5)
You can get one token at a time using token method:
>>> lexer.input('a = 1;') >>> while True: ... token = lexer.token() ... if not token: ... break ... print token ... LexToken(ID,'a',1,0) LexToken(EQ,'=',1,2) LexToken(NUMBER,'1',1,4) LexToken(SEMI,';',1,5)
LexToken instance has different attributes:
>>> lexer.input('a = 1;') >>> token = lexer.token() >>> token.type, token.value, token.lineno, token.lexpos ('ID', 'a', 1, 0)
Installation
Using pip:
$ sudo pip install slimit
Using easy_install:
$ sudo easy_install slimit
Benchmarks
jQuery 1.6.1 (bytes) |
jsmin |
rJSmin |
SlimIt |
---|---|---|---|
234,995 |
134,819 |
134,215 |
94,290 |
Roadmap
when doing name mangling handle cases with ‘eval’ and ‘with’
foo[“bar”] ==> foo.bar
consecutive declarations: var a = 10; var b = 20; ==> var a=10,b=20;
reduce simple constant expressions if the result takes less space: 1 +2 * 3 ==> 7
IF statement optimizations
if (foo) bar(); else baz(); ==> foo?bar():baz();
if (!foo) bar(); else baz(); ==> foo?baz():bar();
if (foo) bar(); ==> foo&&bar();
if (!foo) bar(); ==> foo||bar();
if (foo) return bar(); else return baz(); ==> return foo?bar():baz();
if (foo) return bar(); else something(); ==> {if(foo)return bar();something()}
remove unreachable code that follows a return, throw, break or continue statement, except function/variable declarations
parsing speed improvements
Acknowledgments
Change History
0.5.3 (2011-06-29)
0.5.2 (2011-06-14)
0.5.1 (2011-06-06)
0.5 (2011-06-06)
Added name mangling
0.4 (2011-05-12)
Minify more by removing block braces { }
More tests
0.3.2 (2011-05-09)
More hacks to use pre-generated lex and yacc tables when called from the command line
0.3.1 (2011-05-09)
Use pre-generated lex and yacc tables when called from the command line
0.3 (2011-05-09)
Added minifier
0.2 (2011-05-07)
Added a JavaScript parser
Added pretty printer
Added node visitor
0.1 (2011-05-02)
Initial public version. It contains only a JavaScript lexer
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file slimit-0.5.3.zip
.
File metadata
- Download URL: slimit-0.5.3.zip
- Upload date:
- Size: 78.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f532187ac01d1d1f5fc20a28ae4fc9363bcffc94a9483209d9504a6526820ac0 |
|
MD5 | 4babe9563839501917e22913479a220f |
|
BLAKE2b-256 | fa537d46655959578d4278f9e6557aafa2c5021f94d0e42f12a4f83e82dddb35 |