CSS HTML JS Minifier
CSS-HTML-JS-Minify is a standalone, asynchronous, cross-platform, and Unicode-ready Python 2 and 3 library for minifying web assets (CSS, HTML, JavaScript). It can process single files or entire folders, offering features like GZIP compression, SHA1 hashing for filenames, and optional alphabetical sorting of CSS properties. The current version is 2.5.5, last updated in 2018, indicating a maintenance-oriented release cadence.
Common errors
-
AttributeError: 'module' object has no attribute 'hash'
cause Attempting to use `hash=True` as an argument in the Python API for minification functions.fixThe correct argument name for adding a SHA1 hash to filenames in the Python API is `add_hash`. Change `hash=True` to `add_hash=True`. -
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position ...
cause Using standard input/output streams with files containing non-ASCII (Unicode) characters, which is a deprecated usage pattern for this library.fixAvoid piping input/output using `sys.stdin`/`sys.stdout`. Instead, read file content directly into a string and pass it to `html_minify`, `css_minify`, or `js_minify`, or use the `process_single_..._file` functions with file paths. -
TypeError: process_single_html_file() got an unexpected keyword argument 'hash'
cause Passing the `hash` argument to file processing functions like `process_single_html_file` directly, or other minification functions, when `add_hash` is the expected parameter name.fixEnsure you are using `add_hash=True` (or `add_hash=False`) for the hash functionality in the Python API, not `hash`.
Warnings
- deprecated The use of Std-In to Std-Out for minification is deprecated due to potential issues with Unicode characters. It is recommended to process files directly or pass strings to the API functions.
- gotcha The CLI flags `--comments` and `--overwrite` are explicitly marked as 'Not Recommended' in the documentation. Keeping comments can reduce minification effectiveness, and overwriting files without backup can lead to data loss.
- gotcha There is an inconsistency between the CLI argument `--hash` for adding a SHA1 hash to filenames and its Python API equivalent, which is `add_hash`. Using `hash` in the Python API will result in an error.
- gotcha The `--watch` feature, which re-compresses files upon changes, operates with a minimum delay of approximately 60 seconds between runs. This means file changes might not be reflected immediately.
Install
-
pip install css-html-js-minify
Imports
- process_single_html_file
from css_html_js_minify import process_single_html_file
- process_single_js_file
from css_html_js_minify import process_single_js_file
- process_single_css_file
from css_html_js_minify import process_single_css_file
- html_minify
from css_html_js_minify import html_minify
- js_minify
from css_html_js_minify import js_minify
- css_minify
from css_html_js_minify import css_minify
Quickstart
from css_html_js_minify import (html_minify, js_minify, css_minify,
process_single_html_file, process_single_js_file)
# Minify strings
minified_html = html_minify(' <p>yolo<a href="/" >o </a > <!-- hello --></p>')
print(f"Minified HTML string: {minified_html}")
minified_js = js_minify('var i = 1; i += 2 ;\n alert( "hello " ); //hi')
print(f"Minified JS string: {minified_js}")
minified_css = css_minify('body {width: 50px;}\np {margin-top: 1em;/* hi */ }', comments=False)
print(f"Minified CSS string: {minified_css}")
# Example for file processing (creates dummy files first)
with open('test.htm', 'w') as f:
f.write('<html><body><!-- comment --><h1>Hello</h1></body></html>')
with open('test.js', 'w') as f:
f.write('function greet() { console.log("Hello"); // Comment }')
process_single_html_file('test.htm', overwrite=True) # Overwrites test.htm with minified content
process_single_js_file('test.js', overwrite=False, output_path='test.min.js') # Creates test.min.js
print("Minification of test.htm and test.js completed. Check generated files.")