# hyperbuild
A fast one-pass in-place HTML minifier written in Rust with context-aware whitespace handling.
Available as:
- CLI for Windows, macOS, and Linux.
- Rust library.
- Native library for Node.js, Python, Java, and Ruby.
## Features
- Minification is done in one pass with no backtracking or DOM/AST building.
- No extra heap memory is allocated during processing, which increases performance.
- Context-aware whitespace handling allows maximum minification while retaining desired spaces.
## Performance
Speed and effectiveness of Node.js version compared to [html-minfier](https://github.com/kangax/html-minifier) and [minimize](https://github.com/Swaagie/minimize). See [bench](./bench) folder for more details.
Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! ↵
··The·quick·brown·fox↵
··jumps·over·the·lazy↵
··dog.↵
·The·quick·brown·fox·jumps·over·the·lazy·dog.· ↵
··Hey,·I·just·found↵
··out·about·this·cool·website!↵
··[1]↵
Hey,·I·just·found↵
··out·about·this·cool·website!↵
··[1] ↵
··Hey,·I·just·found↵
··out·about·this·cool·website!↵
··[1]↵
Hey,·I·just·found·out·about·this·cool·website!·[1]
## Usage
### CLI
##### Get
[Windows](https://wilsonl.in/hyperbuild/bin/0.0.21-windows-x86_64.exe) |
[macOS](https://wilsonl.in/hyperbuild/bin/0.0.21-macos-x86_64) |
[Linux](https://wilsonl.in/hyperbuild/bin/0.0.21-linux-x86_64)
##### Use
```bash
hyperbuild --src /path/to/src.html --out /path/to/output.min.html
```
### API
Rust
##### Get
```toml
[dependencies]
hyperbuild = "0.0.21"
```
##### Use
```rust
use hyperbuild::hyperbuild;
fn main() {
let mut code = b"Node.js
hyperbuild is [on npm](https://www.npmjs.com/package/hyperbuild), available as a [Node.js native module](https://neon-bindings.com/), and supports Node.js versions 8 and higher.
##### Get
Using npm:
```bash
npm i hyperbuild
```
Using Yarn:
```bash
yarn add hyperbuild
```
##### Use
```js
const hyperbuild = require("hyperbuild");
const minified = hyperbuild.minify("Java
hyperbuild is available via [JNI](https://github.com/jni-rs/jni-rs), and supports Java versions 7 and higher.
##### Get
Download the [JAR](https://wilsonl.in/hyperbuild/bin/0.0.21.jar) file.
##### Use
Make sure to add the JAR file to the class path.
```java
import in.wilsonl.hyperbuild.Hyperbuild;
class Main {
public static void main(String[] args) {
String minified = Hyperbuild.minify("Python
hyperbuild is available as a [native module](https://github.com/PyO3/pyo3), and supports CPython (default Python interpreter) versions 3.5 and higher.
##### Get
Download the native module for [Windows](https://wilsonl.in/hyperbuild/bin/0.0.21-windows-x86_64-python.pyd), [macOS](https://wilsonl.in/hyperbuild/bin/0.0.21-macos-x86_64-python.so), or [Linux](https://wilsonl.in/hyperbuild/bin/0.0.21-linux-x86_64-python.so).
Rename the file to `hyperbuild.pyd` on Windows or `hyperbuild.so` on macOS/Linux.
##### Use
Make sure the native module can be [found by Python](https://docs.python.org/3/tutorial/modules.html#the-module-search-path). This is usually done by placing it into a folder declared in the `PYTHONPATH` environment variable or `sys.path` value, or the same folder as the script that will import it.
```python
import hyperbuild
minified = hyperbuild.minify("Ruby
hyperbuild is available as a [native module](https://github.com/danielpclark/rutie), and supports Ruby versions 2.5 and higher.
##### Get
Download and install the [gem](https://wilsonl.in/hyperbuild/bin/0.0.21.gem).
##### Use
```ruby
require 'hyperbuild'
print hyperbuild.minify "Collapse whitespace
> **Applies to:** any element except [whitespace sensitive](./src/spec/tag/whitespace.rs) elements.
Reduce a sequence of whitespace characters in text nodes to a single space (U+0020).
Before After
```html
```html
Destroy whole whitespace
> **Applies to:** any element except [whitespace sensitive](./src/spec/tag/whitespace.rs), [content](src/spec/tag/whitespace.rs), [content-first](./src/spec/tag/whitespace.rs), and [formatting](./src/spec/tag/whitespace.rs) elements.
Remove any text nodes that only consist of whitespace characters.
Before After
```html
↵
··
```
```html
↵
··
```
Trim whitespace
> **Applies to:** any element except [whitespace sensitive](./src/spec/tag/whitespace.rs) and [formatting](./src/spec/tag/whitespace.rs) elements.
Remove any leading/trailing whitespace from any leading/trailing text nodes of a tag.
Before After
```html
```html
Formatting elements
> Whitespace is collapsed.
Formatting elements are usually inline elements that wrap around part of some text in a content element, so its whitespace isn't trimmed as they're probably part of the content.
Content elements
> Whitespace is trimmed and collapsed.
Content elements usually represent a contiguous and complete unit of content such as a paragraph. As such, whitespace is significant but sequences of them are most likely due to formatting.
###### Before
```html
Layout elements
> Whitespace is trimmed and collapsed. Whole whitespace is removed.
These elements should only contain other elements and no text. This makes it possible to remove whole whitespace, which is useful when using `display: inline-block` so that whitespace between elements (e.g. indentation) does not alter layout and styling.
###### Before
```html
↵
··
```
###### After
```html
```
Content-first elements
> Whitespace is trimmed and collapsed.
These elements are usually like content elements but are occasionally used like a layout element with one child. Whole whitespace is not removed as it might contain content, but this is OK for using as layout as there is only one child and whitespace is trimmed.
###### Before
```html