hyperbuild 0.0.29

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +444 -0
  3. data/lib/hyperbuild.rb +18 -0
  4. data/lib/linux +0 -0
  5. data/lib/macos +0 -0
  6. metadata +63 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3faa7b58ff53374cb3ef9cdbebb655aaee1c477e40c847a33319a26ba7cdf5db
4
+ data.tar.gz: 79fc28156da9e719ea2a2cf4fe2fe8d513a71b226e45f80d1ed4a2dddb3c6931
5
+ SHA512:
6
+ metadata.gz: 93b67a3e99a3b2593d619e78f588e0a876818fdf24abe12ddca7d4797f78eacb320cc25f91a47099328c7102c7b0c8c1bd74736fc145a9d535307c057eb2b7b4
7
+ data.tar.gz: 0330a7c4927f31aadad7a5dca460dbcaeb656a6804c3f594013ebae31bbf7547f2d1dc765a34b1c87948fe9e201d9adacc27871a66eecdddfa5b8781052112c8
@@ -0,0 +1,444 @@
1
+ # hyperbuild
2
+
3
+ A fast one-pass in-place HTML minifier written in Rust with context-aware whitespace handling.
4
+
5
+ Available as:
6
+ - CLI for Windows, macOS, and Linux.
7
+ - Rust library.
8
+ - Native library for Node.js, Python, Java, and Ruby.
9
+
10
+ ## Features
11
+
12
+ - Minification is done in one pass with no backtracking or DOM/AST building.
13
+ - No extra heap memory is allocated during processing, which increases performance.
14
+ - Context-aware whitespace handling allows maximum minification while retaining desired spaces.
15
+
16
+ ## Performance
17
+
18
+ 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.
19
+
20
+ <img width="435" alt="Chart showing speed of HTML minifiers" src="./bench/results/average-speeds.png"> <img width="435" alt="Chart showing effectiveness of HTML minifiers" src="./bench/results/average-sizes.png">
21
+
22
+ ## Usage
23
+
24
+ ### CLI
25
+
26
+ ##### Get
27
+
28
+ [Windows](https://wilsonl.in/hyperbuild/bin/0.0.29-windows-x86_64.exe) |
29
+ [macOS](https://wilsonl.in/hyperbuild/bin/0.0.29-macos-x86_64) |
30
+ [Linux](https://wilsonl.in/hyperbuild/bin/0.0.29-linux-x86_64)
31
+
32
+ ##### Use
33
+
34
+ ```bash
35
+ hyperbuild --src /path/to/src.html --out /path/to/output.min.html
36
+ ```
37
+
38
+ ### API
39
+
40
+ <details>
41
+ <summary><strong>Rust</strong></summary>
42
+
43
+ ##### Get
44
+
45
+ ```toml
46
+ [dependencies]
47
+ hyperbuild = "0.0.29"
48
+ ```
49
+
50
+ ##### Use
51
+
52
+ ```rust
53
+ use hyperbuild::hyperbuild;
54
+
55
+ fn main() {
56
+ let mut code = b"<p> Hello, world! </p>".to_vec();
57
+ match hyperbuild(&mut code) {
58
+ Ok(minified_len) => {}
59
+ Err((error_type, error_at_char_no)) => {}
60
+ };
61
+ }
62
+ ```
63
+
64
+ </details>
65
+
66
+ <details>
67
+ <summary><strong>Node.js</strong></summary>
68
+
69
+ 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.
70
+
71
+ ##### Get
72
+
73
+ Using npm:
74
+
75
+ ```bash
76
+ npm i hyperbuild
77
+ ```
78
+
79
+ Using Yarn:
80
+
81
+ ```bash
82
+ yarn add hyperbuild
83
+ ```
84
+
85
+ ##### Use
86
+
87
+ ```js
88
+ const hyperbuild = require("hyperbuild");
89
+
90
+ const minified = hyperbuild.minify("<p> Hello, world! </p>");
91
+ ```
92
+
93
+ </details>
94
+
95
+ <details>
96
+ <summary><strong>Java</strong></summary>
97
+
98
+ hyperbuild is available via [JNI](https://github.com/jni-rs/jni-rs), and supports Java versions 7 and higher.
99
+
100
+ ##### Get
101
+
102
+ Add as a Maven dependency:
103
+
104
+ ```xml
105
+ <dependency>
106
+ <groupId>in.wilsonl.hyperbuild</groupId>
107
+ <artifactId>hyperbuild</artifactId>
108
+ <version>0.0.29</version>
109
+ </dependency>
110
+ ```
111
+
112
+ ##### Use
113
+
114
+ ```java
115
+ import in.wilsonl.hyperbuild.Hyperbuild;
116
+
117
+ class Main {
118
+ public static void main(String[] args) {
119
+ String minified = Hyperbuild.minify("<p> Hello, world! </p>");
120
+ }
121
+ }
122
+ ```
123
+
124
+ </details>
125
+
126
+ <details>
127
+ <summary><strong>Python</strong></summary>
128
+
129
+ hyperbuild is available as a [native module](https://github.com/PyO3/pyo3), and supports CPython (the default Python interpreter) versions 3.5 and higher.
130
+
131
+ ##### Get
132
+
133
+ Download the native module for [Windows](https://wilsonl.in/hyperbuild/bin/0.0.29-windows-x86_64-python.pyd), [macOS](https://wilsonl.in/hyperbuild/bin/0.0.29-macos-x86_64-python.so), or [Linux](https://wilsonl.in/hyperbuild/bin/0.0.29-linux-x86_64-python.so).
134
+
135
+ Rename the file to `hyperbuild.pyd` on Windows or `hyperbuild.so` on macOS/Linux.
136
+
137
+ ##### Use
138
+
139
+ 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.
140
+
141
+ ```python
142
+ import hyperbuild
143
+
144
+ minified = hyperbuild.minify("<p> Hello, world! </p>")
145
+ ```
146
+
147
+ </details>
148
+
149
+ <details>
150
+ <summary><strong>Ruby</strong></summary>
151
+
152
+ hyperbuild is published on [RubyGems](https://rubygems.org/gems/hyperbuild), available as a [native module](https://github.com/danielpclark/rutie) for macOS and Linux, and supports Ruby versions 2.5 and higher.
153
+
154
+ ##### Get
155
+
156
+ Add the library as a dependency to `Gemfile` or `*.gemspec`.
157
+
158
+ ##### Use
159
+
160
+ ```ruby
161
+ require 'hyperbuild'
162
+
163
+ print hyperbuild.minify "<p> Hello, world! </p>"
164
+ ```
165
+
166
+ </details>
167
+
168
+ ## Minification
169
+
170
+ ### Whitespace
171
+
172
+ hyperbuild has advanced context-aware whitespace minification that does things such as:
173
+
174
+ - Leave whitespace untouched in `pre` and `code`, which are whitespace sensitive.
175
+ - Trim and collapse whitespace in content tags, as whitespace is collapsed anyway when rendered.
176
+ - Remove whitespace in layout tags, which allows the use of inline layouts while keeping formatted code.
177
+
178
+ #### Methods
179
+
180
+ There are three whitespace minification methods. When processing text content, hyperbuild chooses which ones to use depending on the containing element.
181
+
182
+ <details>
183
+ <summary><strong>Collapse whitespace</strong></summary>
184
+
185
+ > **Applies to:** any element except [whitespace sensitive](./src/spec/tag/whitespace.rs) elements.
186
+
187
+ Reduce a sequence of whitespace characters in text nodes to a single space (U+0020).
188
+
189
+ <table><thead><tr><th>Before<th>After<tbody><tr><td>
190
+
191
+ ```html
192
+ <p>↵
193
+ ··The·quick·brown·fox↵
194
+ ··jumps·over·the·lazy↵
195
+ ··dog.↵
196
+ </p>
197
+ ```
198
+
199
+ <td>
200
+
201
+ ```html
202
+ <p>·The·quick·brown·fox·jumps·over·the·lazy·dog.·</p>
203
+ ```
204
+
205
+ </table>
206
+ </details>
207
+
208
+ <details>
209
+ <summary><strong>Destroy whole whitespace</strong></summary>
210
+
211
+ > **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.
212
+
213
+ Remove any text nodes that only consist of whitespace characters.
214
+
215
+ <table><thead><tr><th>Before<th>After<tbody><tr><td>
216
+
217
+ ```html
218
+ <ul>↵
219
+ ··<li>A</li>↵
220
+ ··<li>B</li>↵
221
+ ··<li>C</li>↵
222
+ </ul>
223
+ ```
224
+
225
+ <td>
226
+
227
+ ```html
228
+ <ul>↵
229
+ ··<li>A</li><li>B</li><li>C</li>↵
230
+ </ul>
231
+ ```
232
+
233
+ </table>
234
+ </details>
235
+
236
+ <details>
237
+ <summary><strong>Trim whitespace</strong></summary>
238
+
239
+ > **Applies to:** any element except [whitespace sensitive](./src/spec/tag/whitespace.rs) and [formatting](./src/spec/tag/whitespace.rs) elements.
240
+
241
+ Remove any leading/trailing whitespace from any leading/trailing text nodes of a tag.
242
+
243
+ <table><thead><tr><th>Before<th>After<tbody><tr><td>
244
+
245
+ ```html
246
+ <p>↵
247
+ ··Hey,·I·<em>just</em>·found↵
248
+ ··out·about·this·<strong>cool</strong>·website!↵
249
+ ··<sup>[1]</sup>↵
250
+ </p>
251
+ ```
252
+
253
+ <td>
254
+
255
+ ```html
256
+ <p>Hey,·I·<em>just</em>·found↵
257
+ ··out·about·this·<strong>cool</strong>·website!↵
258
+ ··<sup>[1]</sup></p>
259
+ ```
260
+
261
+ </table>
262
+ </details>
263
+
264
+ #### Element types
265
+
266
+ 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.
267
+
268
+ |Group|Elements|Expected children|
269
+ |---|---|---|
270
+ |Formatting|`a`, `strong`, [and others](./src/spec/tag/whitespace.rs)|Formatting elements, text.|
271
+ |Content|`h1`, `p`, [and others](src/spec/tag/whitespace.rs)|Formatting elements, text.|
272
+ |Layout|`div`, `ul`, [and others](./src/spec/tag/whitespace.rs)|Layout elements, content elements.|
273
+ |Content-first|`label`, `li`, [and others](./src/spec/tag/whitespace.rs)|Like content but could be layout with only one child.|
274
+
275
+ <details>
276
+ <summary><strong>Formatting elements</strong></summary>
277
+
278
+ > Whitespace is collapsed.
279
+
280
+ 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.
281
+
282
+ </details>
283
+
284
+ <details>
285
+ <summary><strong>Content elements</strong></summary>
286
+
287
+ > Whitespace is trimmed and collapsed.
288
+
289
+ 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.
290
+
291
+ ###### Before
292
+
293
+ ```html
294
+ <p>↵
295
+ ··Hey,·I·<em>just</em>·found↵
296
+ ··out·about·this·<strong>cool</strong>·website!↵
297
+ ··<sup>[1]</sup>↵
298
+ </p>
299
+ ```
300
+
301
+ ###### After
302
+
303
+ ```html
304
+ <p>Hey,·I·<em>just</em>·found·out·about·this·<strong>cool</strong>·website!·<sup>[1]</sup></p>
305
+ ```
306
+
307
+ </details>
308
+
309
+ <details>
310
+ <summary><strong>Layout elements</strong></summary>
311
+
312
+ > Whitespace is trimmed and collapsed. Whole whitespace is removed.
313
+
314
+ 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.
315
+
316
+ ###### Before
317
+
318
+ ```html
319
+ <ul>↵
320
+ ··<li>A</li>↵
321
+ ··<li>B</li>↵
322
+ ··<li>C</li>↵
323
+ </ul>
324
+ ```
325
+
326
+ ###### After
327
+
328
+ ```html
329
+ <ul><li>A</li><li>B</li><li>C</li></ul>
330
+ ```
331
+
332
+ </details>
333
+
334
+ <details>
335
+ <summary><strong>Content-first elements</strong></summary>
336
+
337
+ > Whitespace is trimmed and collapsed.
338
+
339
+ 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.
340
+
341
+ ###### Before
342
+
343
+ ```html
344
+ <li>↵
345
+ ··<article>↵
346
+ ····<section></section>↵
347
+ ····<section></section>↵
348
+ ··</article>↵
349
+ </li>
350
+ ```
351
+
352
+ ###### After
353
+
354
+ ```html
355
+ <li><article><section></section><section></section></article></li>
356
+ ```
357
+
358
+ </details>
359
+
360
+ ### Tags
361
+
362
+ [Optional closing tags](https://html.spec.whatwg.org/multipage/syntax.html#syntax-tag-omission) are removed.
363
+
364
+ ### Attributes
365
+
366
+ Any entities in attribute values are decoded, and then the shortest representation of the value is calculated and used:
367
+
368
+ - Double quoted, with any `"` encoded.
369
+ - Single quoted, with any `'` encoded.
370
+ - Unquoted, with `"`/`'` first character (if applicable), `>` last character (if applicable), and any whitespace encoded.
371
+
372
+ `class` attributes have their whitespace (after any decoding) trimmed and collapsed.
373
+
374
+ [Boolean attribute](./gen/attrs.json) values are removed.
375
+ [Some attributes](./gen/attrs.json) are completely removed if their value is empty or the default value after any processing.
376
+
377
+ `type` attributes on `script` tags with a value equaling a [JavaScript MIME type](https://mimesniff.spec.whatwg.org/#javascript-mime-type) are removed.
378
+
379
+ If an attribute value is empty after any processing, everything but the name 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.
380
+
381
+ Spaces are removed between attributes if possible.
382
+
383
+ ### Entities
384
+
385
+ Entities are decoded if valid (see relevant parsing section) and their decoded characters as UTF-8 is shorter or equal in length.
386
+
387
+ Numeric entities that do not refer to a valid Unicode Scalar Value are decoded to U+FFFD REPLACEMENT CHARACTER.
388
+
389
+ If an entity is unintentionally formed after decoding, the leading ampersand is encoded, e.g. `&&#97;&#109;&#112;;` becomes `&ampamp;`. This is done as `&amp` is equal to or shorter than all other entity representations of characters part of an entity (`[&#a-zA-Z0-9;]`), and there is no other conflicting entity name that starts with `amp`.
390
+
391
+ ### Comments
392
+
393
+ Comments are removed.
394
+
395
+ ### Ignored
396
+
397
+ Bangs, [processing instructions](https://en.wikipedia.org/wiki/Processing_Instruction), and empty elements are not removed as it is assumed there is a special reason for their declaration.
398
+
399
+ ## Parsing
400
+
401
+ Only UTF-8/ASCII-encoded HTML code is supported.
402
+
403
+ hyperbuild simply does HTML minification, and almost does no syntax checking or standards enforcement for performance and code complexity reasons.
404
+
405
+ For example, this means that it's not an error to have self-closing tags, declare multiple `<body>` elements, use incorrect attribute names and values, or write something like `<br>alert('');</br>`
406
+
407
+ However, there are some syntax requirements for speed and sanity reasons.
408
+
409
+ ### Tags
410
+
411
+ Tag names are case sensitive. For example, this means that `P` won't be recognised as a content element, `bR` won't be considered as a void tag, and the contents of `Script` won't be parsed as JavaScript.
412
+
413
+ Tags must not be [omitted](https://html.spec.whatwg.org/multipage/syntax.html#syntax-tag-omission). Void tags must not have a separate closing tag e.g. `</input>`.
414
+
415
+ ### Entities
416
+
417
+ Well-formed entities are decoded, including in attribute values.
418
+
419
+ They are interpreted as characters representing their decoded value. This means that `&#9;` is considered a whitespace character and could be minified.
420
+
421
+ Malformed entities are interpreted literally as a sequence of characters.
422
+
423
+ If a named entity is an invalid reference as per the [specification](https://html.spec.whatwg.org/multipage/named-characters.html#named-character-references), it is considered malformed.
424
+
425
+ Numeric character references that do not reference a valid [Unicode Scalar Value](https://www.unicode.org/glossary/#unicode_scalar_value) are considered malformed.
426
+
427
+ ### Attributes
428
+
429
+ Backticks (`` ` ``) are not valid quote marks and not interpreted as such.
430
+ However, backticks are valid attribute value quotes in Internet Explorer.
431
+
432
+ Special handling of some attributes require case sensitive names and values. For example, `CLASS` won't be recognised as an attribute to minify, and `type="Text/JavaScript"` on a `<script>` will cause the element to be parsed as a [data block](https://html.spec.whatwg.org/dev/scripting.html#data-block) instead of JavaScript code.
433
+
434
+ ### Script and style
435
+
436
+ `script` and `style` tags must be closed with `</script>` and `</style>` respectively (case sensitive).
437
+
438
+ hyperbuild does **not** handle [escaped and double-escaped](./notes/Script%20data.md) script content.
439
+
440
+ ## Issues and contributions
441
+
442
+ Contributions welcome!
443
+
444
+ If hyperbuild did something unexpected, such as misunderstood some syntax, or incorrectly did/didn't do some minification, [raise an issue](https://github.com/wilsonzlin/hyperbuild/issues) with some relevant code that causes the issue.
@@ -0,0 +1,18 @@
1
+ require 'fiddle'
2
+
3
+ class HyperbuildLoader
4
+ def self.operating_system
5
+ case RbConfig::CONFIG['host_os'].downcase
6
+ when /linux|bsd|solaris/ then 'linux'
7
+ when /darwin/ then 'macos'
8
+ when /mingw|mswin/ then 'windows'
9
+ else 'unknown'
10
+ end
11
+ end
12
+
13
+ def self.lib_path
14
+ File.join(__dir__, operating_system)
15
+ end
16
+ end
17
+
18
+ Fiddle::Function.new(Fiddle.dlopen(HyperbuildLoader.lib_path)['Init_hyperbuild'], [], Fiddle::TYPE_VOIDP).call
Binary file
Binary file
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hyperbuild
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.29
5
+ platform: ruby
6
+ authors:
7
+ - Wilson Lin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-01-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: fiddle
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ description:
28
+ email:
29
+ - code@wilsonl.in
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - README.md
35
+ - lib/hyperbuild.rb
36
+ - lib/linux
37
+ - lib/macos
38
+ homepage: https://github.com/wilsonzlin/hyperbuild
39
+ licenses:
40
+ - MIT
41
+ metadata: {}
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 2.7.6.2
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: Fast one-pass in-place HTML minifier written in Rust with context-aware whitespace
62
+ handling
63
+ test_files: []