jsmin
jsmin is a Python library and command-line tool for minifying JavaScript code, based on Douglas Crockford's original jsmin.c. It removes unnecessary whitespace and comments to reduce file size. The current version is 3.0.1. The project is actively maintained, though the maintainer has stated they do not intend to add ECMAScript 6 (ES6) compatibility, indicating a focused maintenance cadence for its existing feature set.
Warnings
- breaking Version 3.0.0 removed support for Python 2. If you require Python 2 compatibility, you must use version 2.2.2.
- gotcha `jsmin` does not attempt to be compatible with ECMAScript 6 (ES6 / ES.next / Harmony) syntax. Using it on modern JavaScript code may result in incorrect minification or errors.
- gotcha `jsmin` expects syntactically correct JavaScript as input. Providing invalid JavaScript may lead to minification failures or corrupted output.
- gotcha Comments starting with `/*!` (often called 'loud comments' or 'licence comments') are preserved during the minification process since version 2.2.0. Other comments are stripped.
Install
-
pip install jsmin
Imports
- jsmin
from jsmin import jsmin
Quickstart
from jsmin import jsmin
js_code = '''
// This is a comment
function greet(name) {
console.log("Hello, " + name + "!");
}
greet("World");
'''
minified_js = jsmin(js_code)
print(minified_js)
# Example with a file (hypothetical, requires myfile.js to exist)
# try:
# with open('myfile.js', 'r') as js_file:
# minified_from_file = jsmin(js_file.read())
# print(minified_from_file)
# except FileNotFoundError:
# print("myfile.js not found for file example.")