# 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 module for Node.js. ## 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 wanted spaces. ## Performance Speed and effectiveness of Node.js version compared to other JS minifiers. ![Chart showing speed of HTML minifiers](./bench/speed.png) ![Chart showing effectiveness of HTML minifiers](./bench/minification.png) ## Usage ### CLI ##### Get [Windows](https://wilsonl.in/hyperbuild/bin/0.0.11-windows-x86_64.exe) | [macOS](https://wilsonl.in/hyperbuild/bin/0.0.11-macos-x86_64) | [Linux](https://wilsonl.in/hyperbuild/bin/0.0.11-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.11" ``` ##### Use ```rust use hyperbuild::hyperbuild; fn main() { let mut code = b"

Hello, world!

"; match hyperbuild(&mut code) { Ok(minified_len) => {} Err(error_type, error_at_char_no) => {} }; } ```
Node.js hyperbuild is available as a [Node.js native module](https://www.npmjs.com/package/hyperbuild), 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("

Hello, world!

"); ```
## Minification ### Whitespace hyperbuild has advanced context-aware whitespace minification that does things such as: - Leave whitespace untouched in `pre` and `code`, which are whitespace sensitive. - Trim and collapse whitespace in content tags, as whitespace is collapsed anyway when rendered. - Remove whitespace in layout tags, which allows the use of inline layouts while keeping formatted code. #### Methods There are three whitespace minification methods. When processing text content, hyperbuild chooses which ones to use depending on the containing element.
Collapse whitespace > **Applies to:** any element except [whitespace sensitive](./src/spec/tag/wss.rs) elements. Reduce a sequence of whitespace characters in text nodes to a single space (U+0020).
BeforeAfter
```html

↵ ··The·quick·brown·fox↵ ··jumps·over·the·lazy↵ ··dog.↵

```
```html

·The·quick·brown·fox·jumps·over·the·lazy·dog.·

```
Destroy whole whitespace > **Applies to:** any element except [whitespace sensitive](./src/spec/tag/wss.rs), [content](./src/spec/tag/content.rs), [content-first](./src/spec/tag/contentfirst.rs), and [formatting](./src/spec/tag/formatting.rs) elements. Remove any text nodes that only consist of whitespace characters.
BeforeAfter
```html
    ↵ ··
  • A
  • ↵ ··
  • B
  • ↵ ··
  • C
```
```html
    ↵ ··
  • A
  • B
  • C
```
Trim whitespace > **Applies to:** any element except [whitespace sensitive](./src/spec/tag/wss.rs) and [formatting](./src/spec/tag/formatting.rs) elements. Remove any leading/trailing whitespace from any leading/trailing text nodes of a tag.
BeforeAfter
```html

↵ ··Hey,·I·just·found↵ ··out·about·this·cool·website!↵ ··[1]

```
```html

Hey,·I·just·found↵ ··out·about·this·cool·website!↵ ··[1]

```
#### Element types hyperbuild recognises elements based on one of a few ways it assumes they are used. By making these assumptions, it can apply optimal whitespace minification strategies. |Group|Elements|Expected children| |---|---|---| |Formatting|`a`, `strong`, [and others](./src/spec/tag/formatting.rs)|Formatting elements, text.| |Content|`h1`, `p`, [and others](./src/spec/tag/content.rs)|Formatting elements, text.| |Layout|`div`, `ul`, [and others](./src/spec/tag/layout.rs)|Layout elements, content elements.| |Content-first|`label`, `li`, [and others](./src/spec/tag/contentfirst.rs)|Like content but could be layout with only one child.|
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

↵ ··Hey,·I·just·found↵ ··out·about·this·cool·website!↵ ··[1]

``` ###### After ```html

Hey,·I·just·found·out·about·this·cool·website!·[1]

```
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
  • ↵ ··
    ↵ ····
    ↵ ····
    ↵ ··
  • ``` ###### After ```html
  • ```
    ### Tags [Optional closing tags](https://html.spec.whatwg.org/multipage/syntax.html#syntax-tag-omission) are removed. ### Attributes Any entities in attribute values are decoded, and then the shortest representation of the value is calculated and used: - Double quoted, with any `"` encoded. - Single quoted, with any `'` encoded. - Unquoted, with `"`/`'` first character (if applicable), `>` last character (if applicable), and any whitespace encoded. Some attributes have their whitespace (after any decoding) trimmed and collapsed: - `class` [Boolean attributes](./gen/boolean_attrs.json) will have their values removed. `type` attributes on `script` tags with a value equaling a [JavaScript MIME type](https://mimesniff.spec.whatwg.org/#javascript-mime-type) are removed. `type` attributes on `style` tags are removed. If an attribute value is empty after any processing, it is completely removed (i.e. no `=`), as an empty attribute is implicitly [the same](https://html.spec.whatwg.org/multipage/syntax.html#attributes-2) as an attribute with an empty string value. Spaces are removed between attributes if possible. ### Other - Comments are removed. - Entities are decoded if valid (see relevant parsing section). - Whitespace is trimmed and collapsed inside `` and `` respectively (case sensitive). Note that the closing tag must not contain any whitespace (e.g. ``). [hyperbuild can handle text script content.](./notes/Text%20script%20content.md) ## Development ### More minification options - Removal of boolean attribute values. - Removal of redundant attributes (empty or default value). - Handling of conditional or special comments.