rjsmin: Fast Javascript Minifier for Python
rJSmin is a javascript minifier written in Python, based on the semantics of jsmin.c by Douglas Crockford. It is a re-implementation aiming for speed, designed for runtime use rather than a preprocessing step, and usually produces the same results as the original `jsmin.c`. The current version is 1.2.5 and it supports Python 2.7 and 3.6+.
Warnings
- gotcha rJSmin performs no error detection. Unterminated string, regex, and comment literals are treated as regular JavaScript code and minified as such, which can lead to invalid output without explicit error messages.
- gotcha rJSmin does not handle streams and requires the complete JavaScript code as a single string input. It cannot process code incrementally from a file stream.
- gotcha While generally compatible with jsmin.c, rJSmin has subtle behavioral differences. For example, control characters in string/regex literals are untouched, newlines are generally disallowed in string/regex literals (except for ECMA-5 line continuations), and sequences like '+ +' or '- -' are not collapsed to '++' or '--'.
Install
-
pip install rjsmin
Imports
- jsmin
from rjsmin import jsmin
Quickstart
from rjsmin import jsmin
js_code = """\
function hello( name ) {
/* a comment */
return 'Hello, ' + name + '!';
}
"""
minified_code = jsmin(js_code)
print(minified_code)
# Expected output: function hello(name){return'Hello,'+name+'!'}