{"id":4110,"library":"minify-html","title":"minify-html Python Library","description":"minify-html is a Python binding for an extremely fast and smart HTML + JS + CSS minifier written in Rust. It offers advanced minification strategies that outperform other minifiers in both speed and effectiveness, handling invalid HTML and templating syntax. The library is actively maintained with frequent minor releases, often multiple times a month, and is currently at version 0.18.1.","status":"active","version":"0.18.1","language":"en","source_language":"en","source_url":"https://github.com/wilsonzlin/minify-html","tags":["html","minification","web","performance","css","javascript","rust-bindings"],"install":[{"cmd":"pip install minify-html","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"The Python binding exposes a direct `minify` function, unlike some other language bindings that might use a class or differently cased function name.","wrong":"from minify_html import MinifyHtml","symbol":"minify","correct":"from minify_html import minify"}],"quickstart":{"code":"from minify_html import minify\n\nhtml_input = \"\"\"\n<!DOCTYPE html>\n<html>\n<head>\n    <title> Test Page </title>\n    <style>\n        body { background-color: #f0f0f0; }\n        h1 { color: blue; }\n    </style>\n</head>\n<body>\n    <!-- Main Content -->\n    <h1> Hello, World! </h1>\n    <p> This is a <span> test </span> paragraph. </p>\n    <script>\n        // Some JavaScript\n        function greet() {\n            console.log(\"Welcome!\");\n        }\n        greet();\n    </script>\n</body>\n</html>\n\"\"\"\n\nminified_html = minify(\n    html_input,\n    keep_comments=False,\n    minify_css=True,\n    minify_js=True,\n    do_not_minify_doctype=True\n)\nprint(minified_html)","lang":"python","description":"Demonstrates how to minify an HTML string, including inline CSS and JavaScript, while configuring options like comment removal and doctype preservation."},"warnings":[{"fix":"Use `snake_case` keyword arguments directly when calling the `minify` function (e.g., `minify(html, keep_comments=False)`).","message":"When configuring minification options, the Python binding uses `snake_case` keyword arguments (e.g., `keep_comments=False`). Users accustomed to other language bindings (like Java's `setKeepComments(true)`) or older documentation might incorrectly try `camelCase` or `set_` prefixed methods, which will result in a `TypeError`. Always refer to the Python API documentation for the exact parameter names.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure the Go compiler is installed in your environment if you encounter issues with JS/CSS minification during a custom build or non-standard installation. For typical usage with pre-built wheels, this is generally not an issue.","message":"Enabling JavaScript (`minify_js=True`) or CSS (`minify_css=True`) minification, particularly when building the package from source or in specific environments, might require the Go compiler to be installed. This is because the underlying Rust library leverages `esbuild` for JS/CSS minification via its `js-esbuild` feature. Pre-built wheels typically include this functionality, but custom installations might fail if Go is not present in the environment.","severity":"gotcha","affected_versions":"All versions (especially for source builds)"},{"fix":"Review the available configuration options such as `keep_spaces_between_attributes` or `keep_html_and_head_opening_tags` to fine-tune whitespace preservation if unexpected rendering issues occur. Test minified output thoroughly in your target browsers.","message":"The `minify-html` library employs an advanced, context-aware whitespace minification strategy. While this is generally highly effective, in specific edge cases where precise whitespace rendering is critical (e.g., between certain inline elements or custom components), it might remove more whitespace than anticipated.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}