$ show maltsev/htmlnano
htmlnano is a modular HTML minifier built on PostHTML, providing a library API, CLI, PostHTML plugin, and webpack integration. It processes HTML through a configurable pipeline of modules (e.g., whitespace collapse, comment removal, CSS/JS/SVG minification) organized into presets (safe, ampSafe, max). The core architecture uses PostHTML's tree walking and a plugin-like module interface where each module can transform attributes, content, or nodes. Optional peer dependencies (cssnano, terser, svgo, etc.) are loaded lazily, and configuration is handled via cosmiconfig. The codebase demonstrates strong engineering with comprehensive test coverage, property-based fuzz testing, and DOM equivalence validation. While primarily a library, it also includes a CLI and webpack adapter, making it a versatile tool for HTML optimization in build pipelines.
The LLM identified 5 files worth highlighting for reuse:
Implements sophisticated whitespace collapsing with three modes (all, aggressive, conservative). Respects CSS white-space property, pre/textarea elements, inline vs block boundaries, and handles edge cases like leading/trailing whitespace in mixed content. The logic is self-contained and could be extracted as a standalone HTML whitespace normalizer for other projects.
Decodes HTML named and numeric character references with context-aware safety, handling forbidden characters, ambiguous ampersands, and protected raw-text elements. Includes shared attribute quoting logic from helpers/attributeQuoting.ts. This module is a robust, reusable implementation of HTML entity handling that could be used in any HTML processing pipeline.
Implements HTML5 optional tag omission rules (start and end tags) with quirks-mode awareness, whitespace/comment blocking, and workarounds for htmlparser2 behavior differences from the spec. Demonstrates deep understanding of the HTML parsing algorithm and is a self-contained module that could be reused in other HTML minifiers or validators.
Provides shared utilities including optionalImport() for graceful lazy loading of peer dependencies, tag name detection, event handler identification, and CSS CDATA handling. The optionalImport pattern is a clean, reusable approach for handling optional dependencies in Node.js libraries, and the helper functions are generic enough to be extracted into a separate package.
A DOM equivalence testing harness that uses parse5 to parse both source and minified HTML, then compares the resulting DOM trees for structural and content equivalence. This is a reusable pattern for validating HTML transformations beyond simple string comparison, and could be extracted as a standalone testing utility for any HTML processing library.