1- # Herb Minifier < Badge type = " info " text = " coming soon " />
1+ # Herb Minifier
22
33** Package:** [ ` @herb-tools/minifier ` ] ( https://www.npmjs.com/package/@herb-tools/minifier )
44
55---
66
7- HTML+ERB template minification.
7+ HTML+ERB template minification. Removes non-significant whitespace while preserving whitespace in ` <pre> ` and ` <code> ` tags.
88
99## Installation
1010
@@ -26,14 +26,139 @@ bun add @herb-tools/minifier
2626```
2727:::
2828
29- <!-- ### Usage -->
29+ ## Usage
3030
31- <!-- TODO -->
31+ ### Basic Usage
3232
33- <!-- #### Configuration Options -->
33+ ``` typescript
34+ import { Herb } from ' @herb-tools/node-wasm'
35+ import { Minifier } from ' @herb-tools/minifier'
3436
35- <!-- TODO -->
37+ // Create and initialize minifier
38+ const minifier = new Minifier (Herb )
39+ await minifier .initialize ()
3640
37- <!-- #### CLI Usage -->
41+ const template = `
42+ <div class="container">
43+ <h1>Hello World</h1>
44+ <p>This is a test</p>
45+ </div>
46+ `
3847
39- <!-- TODO -->
48+ const minified = minifier .minifyString (template )
49+ // Result: '<div class="container"><h1>Hello World</h1><p>This is a test</p></div>'
50+ ```
51+
52+ ### Minifying AST Nodes
53+
54+ You can also minify AST nodes directly:
55+
56+ ``` typescript
57+ import { Herb } from ' @herb-tools/node-wasm'
58+ import { Minifier } from ' @herb-tools/minifier'
59+
60+ const minifier = new Minifier (Herb )
61+ await minifier .initialize ()
62+
63+ const parseResult = Herb .parse (template , { track_whitespace: true })
64+ const minifiedNode = minifier .minify (parseResult .value )
65+ ```
66+
67+ ### Whitespace Preservation
68+
69+ The minifier preserves whitespace in ` <pre> ` and ` <code> ` tags:
70+
71+ ``` typescript
72+ const template = `
73+ <div>
74+ <pre>
75+ Line 1
76+ Line 2
77+ Line 3
78+ </pre>
79+ </div>
80+ `
81+
82+ const minified = minifier .minifyString (template )
83+ // Result: '<div><pre>\n Line 1\n Line 2\n Line 3\n </pre></div>'
84+ ```
85+
86+ ### ERB Support
87+
88+ The minifier works seamlessly with ERB templates:
89+
90+ ``` typescript
91+ const template = `
92+ <div>
93+ <% if admin? %>
94+ <span>Admin</span>
95+ <% else %>
96+ <span>User</span>
97+ <% end %>
98+ </div>
99+ `
100+
101+ const minified = minifier .minifyString (template )
102+ // Result: '<div><%if admin?%><span>Admin</span><%else%><span>User</span><%end%></div>'
103+ ```
104+
105+ ## API Reference
106+
107+ ### ` Minifier `
108+
109+ The main minifier class.
110+
111+ #### Constructor
112+
113+ ``` typescript
114+ new Minifier (herb : HerbBackend )
115+ ```
116+
117+ ** Parameters:**
118+ - ` herb ` : The Herb backend instance
119+
120+ #### Methods
121+
122+ ##### ` initialize() `
123+
124+ ``` typescript
125+ async initialize (): Promise < void >
126+ ```
127+
128+ Initializes the minifier by loading the Herb backend. Must be called before using minification methods.
129+
130+ ##### ` minifyString() `
131+
132+ ``` typescript
133+ minifyString (template : string ): string
134+ ```
135+
136+ Minifies an HTML+ERB template string.
137+
138+ ** Parameters:**
139+ - ` template ` : The template string to minify
140+
141+ ** Returns:** The minified template string
142+
143+ ##### ` minify() `
144+
145+ ``` typescript
146+ minify <T extends Node >(node : T ): T
147+ ` ` `
148+
149+ Minifies an HTML+ERB AST node.
150+
151+ **Parameters:**
152+ - ` node ` : The AST node to minify
153+
154+ **Returns:** The minified AST node
155+
156+ ## Features
157+
158+ - ✅ Removes non-significant whitespace
159+ - ✅ Preserves whitespace in ` <pre >` and ` <code >` tags
160+ - ✅ Supports nested preserve-whitespace tags
161+ - ✅ Works with ERB templates
162+ - ✅ Preserves HTML attributes
163+ - ✅ Handles self-closing tags
164+ - ✅ Gracefully handles parse errors (returns original template)
0 commit comments