Skip to main content

Python package and CLI for html-minifier-terser plus Tailwind minification

Project description

minify-tw-html

This is a convenient CLI and Python lib wrapper for html-minifier-terser (the highly configurable, well-tested, JavaScript-based HTML minifier) and the Tailwind v4 CLI.

  • It lets you use the Play CDN for rapid development, then lets you minify your HTML/CSS/JavaScript and Tailwind CSS with a single command from the CLI (no npm project setup).

  • If you're using Python, it can be added as a PyPI dependency to a project and used as a minification library from Python.

It checks for an npm installation and uses that, raising an error if not available. If it finds npm it does its own npm install so it's self-contained. The required npm packages are cached locally.

Why? It seems like Tailwind v4 compilation should be a simple operation should be a single CLI command and (optionally) be easily combined with a modern full-featured minifier like html-minifier-terser but I didn't find an existing tool for this.

Previously I had been using the minify-html (which has a convenient Python package). It is great and fast, but ran into some unfixed bugs and wanted proper Tailwind v4 compilation, so switched to this approach.

CLI Use

Save a file like this for example as page.html. Note we are using the Play CDN for simple zero-build development:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Test HTML</title>
    <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
    <style>
        /* Custom CSS that will be minified alongside Tailwind */
        .custom-shadow { 
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); 
        }
    </style>
</head>
<body class="m-0 p-5 bg-gray-50">

<!-- This comment should be removed -->

<div class="custom-shadow bg-gray-100 p-4 m-2 rounded-lg">
  <h1 class="text-2xl font-bold text-blue-600 mb-3">Test Header</h1>
  <p class="text-gray-700 mb-4">This is a test paragraph with some content.</p>
  <button 
      onclick="testFunction()" 
      class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded transition-colors duration-200">
      Click Me
  </button>
</div>
<script>
  // This JavaScript should get minified
  function testFunction() {
      console.log('Hello from test function!');
      alert('Button was clicked!');
      return 'Some return value';
  }
</script>
</body>
</html>

If you want to minify it and compile all Tailwind CSS.

Get help:

$ minify-tw-html --help
usage: minify-tw-html [-h] [--no-minify] [--verbose] src_html dest_html

CLI for HTML minification with Tailwind CSS v4 compilation.

This script can be used for general HTML minification (including inline CSS/JS) and/or
Tailwind CSS v4 compilation and inlining (replacing CDN script with compiled CSS).

Minification includes:
- HTML structure: whitespace removal, comment removal
- Inline CSS: all <style> tags and style attributes are minified
- Inline JavaScript: all <script> tags are minified (not external JS files)

positional arguments:
  src_html       Input HTML file.
  dest_html      Output HTML file.

options:
  -h, --help     show this help message and exit
  --no-minify    Skip HTML minification (only compile Tailwind if present).
  --verbose, -v  Enable verbose logging.

Run it:

$ minify-tw-html page.html page.min.html --verbose
Tailwind v4 CDN script detected - will compile and inline Tailwind CSS
Installing npm dependencies...
Running: npm install
Running: npx @tailwindcss/cli -i [...]/input.css -o [...]/tailwind.min.css --minify
Tailwind stderr:  tailwindcss v4.1.8

Done in 21ms

Tailwind CSS v4 compiled and inlined successfully
Minifying HTML (including inline CSS and JS)...
Running: npx html-minifier-terser --collapse-whitespace --remove-comments --minify-css true --minify-js true -o [...]/page.min.html [...]/tmpf8bfzeic.html
HTML minified and written to page.min.html
Tailwind CSS compiled, HTML minified: 1223 bytes  4680 bytes (+282.7%)

$ cat page.min.html 
<!DOCTYPE html><html>
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>Test HTML</title>
<style>/*! tailwindcss v4.1.8 | MIT License | https://tailwindcss.com */@layer theme{:host,:root{--font-sans:ui-sans-serif,system-ui,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";--font-mono:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;--default-font-family:var(--font-sans);--default-mono-font-family:var(--font-mono)}}@layer base{*,::backdrop,:after,:before{box-sizing:border-box;border:0 solid;margin:0;padding:0}::file-selector-button[...]
</style>
</head>
<body class="m-0 p-5 bg-gray-50">
<div class="custom-shadow bg-gray-100 p-4 m-2 rounded-lg"><h1 class="text-2xl font-bold text-blue-600 mb-3">Test Header</h1><p class="text-gray-700 mb-4">This is a test paragraph with some content.</p><button onclick="testFunction()" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded transition-colors duration-200">Click Me</button></div>
<script>function testFunction(){return console.log("Hello from test function!"),alert("Button was clicked!"),"Some return value"}</script>
</body></html>

(Last output truncated for clarity.)

Note because of the Tailwind compilation this page actually grew because we've compiled in the CSS for instant loading. But for large pages it of course shrinks.

Python Use

As a library: uv add minify-tw-html (or pip install minify-tw-html etc.). Then:

from pathlib import Path
from minify_tw_html import minify_tw_html

minify_tw_html(Path("page.html"), Path("page.min.html"))

Project Docs

For how to install uv and Python, see installation.md.

For development workflows, see development.md.

For instructions on publishing to PyPI, see publishing.md.


This project was built from simple-modern-uv.

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

minify_tw_html-0.1.2.tar.gz (25.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

minify_tw_html-0.1.2-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file minify_tw_html-0.1.2.tar.gz.

File metadata

  • Download URL: minify_tw_html-0.1.2.tar.gz
  • Upload date:
  • Size: 25.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for minify_tw_html-0.1.2.tar.gz
Algorithm Hash digest
SHA256 35e8084b2a4600fafb8ebd2b405010420804c2c93df28c3c2c3224396dd90b3b
MD5 5d7ad77d9e0a4fc94c0e2a6d4f77094a
BLAKE2b-256 43a2ca5ad30aa8f7b99f5237f7efe5f860bb01fccc59d4ff1e5f184386600d88

See more details on using hashes here.

Provenance

The following attestation bundles were made for minify_tw_html-0.1.2.tar.gz:

Publisher: publish.yml on jlevy/minify-tw-html

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file minify_tw_html-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: minify_tw_html-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for minify_tw_html-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9efb46fd7172962b3d61f6e2f2dffe072eb62fb9631ba02d6f1c6a0d8c6afcaa
MD5 f077f6049cf455f6dc23b38022665033
BLAKE2b-256 a8c550f797efc6794117635581626a2f56e767d22cc87c6c139120e996f7692c

See more details on using hashes here.

Provenance

The following attestation bundles were made for minify_tw_html-0.1.2-py3-none-any.whl:

Publisher: publish.yml on jlevy/minify-tw-html

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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