Skip to main content

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 [input file]

If no input file is provided STDIN is used by default.
Minified JavaScript code is printed to STDOUT.

$ cat test.js
var a = function( obj ) {
        for ( var name in obj ) {
                return false;
        }
        return true;
};
$
$ slimit < test.js
var a=function(obj){for(var name in obj)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)
var a=function(obj){for(var name in obj)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

Roadmap

  • More minifications

  • Speed improvements

Change History

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

slimit-0.4.tar.gz (51.2 kB view details)

Uploaded Source

File details

Details for the file slimit-0.4.tar.gz.

File metadata

  • Download URL: slimit-0.4.tar.gz
  • Upload date:
  • Size: 51.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for slimit-0.4.tar.gz
Algorithm Hash digest
SHA256 117ed1384d65f7b768f0204114d2b4518b231c6fdcfdf6289a6becc7b5de4c21
MD5 a13c1e89d2ecb1597b02e26fd2031f53
BLAKE2b-256 83babbf7d2160e8cb4ec5469b71de754f15603013cf9dfaa85f72b29ebeb4b93

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page