hyperbuild 0.0.27 → 0.0.28

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +438 -0
  3. data/lib/linux +0 -0
  4. data/lib/macos +0 -0
  5. metadata +2 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e2ecb04d69e6820cb386fea2c8a3bf2bd282418b26c7e1044429118797956ce5
4
- data.tar.gz: fd2c1cba2d79d3efdf0ff6e4dea0d0d6491b3fe240f2a4604632ebac6271290a
3
+ metadata.gz: 97f5a53d0edfa1c0d48de35b710be8a28720d531d5d103ac444c61a966d54945
4
+ data.tar.gz: 34843de3acf3029b8ca4627b88a3cfcc7c16bc5db8505f909c8f22c9b24a1a4a
5
5
  SHA512:
6
- metadata.gz: 193453d84c4e68f7b1c9ee24bccf159918b6c97add863a0a296c10531c57419c61755e94284a0cca2c1c8bffae12eb3575f2fbeeb7ddc3b95109d44d61543200
7
- data.tar.gz: 45f6654248824035e3ee196240bb273d8252c95fe046a1b1c0bb2c754e9ad1c189c148f63a07bc5c69f5d0fc8199bec2a78bc705e8ffe3ac9bf5e462470af1ef
6
+ metadata.gz: d6f894c0c4db260af124633733eabf6462f7034ee2306148dba7037b7d6b8a90454e1fb4777e0b98333207490ff060c756fe7c390fdf0f289b028ced43975ce5
7
+ data.tar.gz: 6ec47700614cb897f1bab5bf23d5f7fa86cc4f4b56bd41d7e4d45fdd8adcf4542c1bd4a72d0ec8d7d56ce0253485dead9d60c617b19aacb62d93ce8138f9cfae
@@ -0,0 +1,438 @@
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.28-windows-x86_64.exe) |
29
+ [macOS](https://wilsonl.in/hyperbuild/bin/0.0.28-macos-x86_64) |
30
+ [Linux](https://wilsonl.in/hyperbuild/bin/0.0.28-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.28"
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
+ Download the [JAR](https://wilsonl.in/hyperbuild/bin/0.0.28.jar) file.
103
+
104
+ ##### Use
105
+
106
+ Make sure to add the JAR file to the class path.
107
+
108
+ ```java
109
+ import in.wilsonl.hyperbuild.Hyperbuild;
110
+
111
+ class Main {
112
+ public static void main(String[] args) {
113
+ String minified = Hyperbuild.minify("<p> Hello, world! </p>");
114
+ }
115
+ }
116
+ ```
117
+
118
+ </details>
119
+
120
+ <details>
121
+ <summary><strong>Python</strong></summary>
122
+
123
+ hyperbuild is available as a [native module](https://github.com/PyO3/pyo3), and supports CPython (the default Python interpreter) versions 3.5 and higher.
124
+
125
+ ##### Get
126
+
127
+ Download the native module for [Windows](https://wilsonl.in/hyperbuild/bin/0.0.28-windows-x86_64-python.pyd), [macOS](https://wilsonl.in/hyperbuild/bin/0.0.28-macos-x86_64-python.so), or [Linux](https://wilsonl.in/hyperbuild/bin/0.0.28-linux-x86_64-python.so).
128
+
129
+ Rename the file to `hyperbuild.pyd` on Windows or `hyperbuild.so` on macOS/Linux.
130
+
131
+ ##### Use
132
+
133
+ 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.
134
+
135
+ ```python
136
+ import hyperbuild
137
+
138
+ minified = hyperbuild.minify("<p> Hello, world! </p>")
139
+ ```
140
+
141
+ </details>
142
+
143
+ <details>
144
+ <summary><strong>Ruby</strong></summary>
145
+
146
+ hyperbuild is available as a [native module](https://github.com/danielpclark/rutie) for macOS and Linux, and supports Ruby versions 2.5 and higher.
147
+
148
+ ##### Get
149
+
150
+ Download and install the [gem](https://wilsonl.in/hyperbuild/bin/0.0.28.gem).
151
+
152
+ ##### Use
153
+
154
+ ```ruby
155
+ require 'hyperbuild'
156
+
157
+ print hyperbuild.minify "<p> Hello, world! </p>"
158
+ ```
159
+
160
+ </details>
161
+
162
+ ## Minification
163
+
164
+ ### Whitespace
165
+
166
+ hyperbuild has advanced context-aware whitespace minification that does things such as:
167
+
168
+ - Leave whitespace untouched in `pre` and `code`, which are whitespace sensitive.
169
+ - Trim and collapse whitespace in content tags, as whitespace is collapsed anyway when rendered.
170
+ - Remove whitespace in layout tags, which allows the use of inline layouts while keeping formatted code.
171
+
172
+ #### Methods
173
+
174
+ There are three whitespace minification methods. When processing text content, hyperbuild chooses which ones to use depending on the containing element.
175
+
176
+ <details>
177
+ <summary><strong>Collapse whitespace</strong></summary>
178
+
179
+ > **Applies to:** any element except [whitespace sensitive](./src/spec/tag/whitespace.rs) elements.
180
+
181
+ Reduce a sequence of whitespace characters in text nodes to a single space (U+0020).
182
+
183
+ <table><thead><tr><th>Before<th>After<tbody><tr><td>
184
+
185
+ ```html
186
+ <p>↵
187
+ ··The·quick·brown·fox↵
188
+ ··jumps·over·the·lazy↵
189
+ ··dog.↵
190
+ </p>
191
+ ```
192
+
193
+ <td>
194
+
195
+ ```html
196
+ <p>·The·quick·brown·fox·jumps·over·the·lazy·dog.·</p>
197
+ ```
198
+
199
+ </table>
200
+ </details>
201
+
202
+ <details>
203
+ <summary><strong>Destroy whole whitespace</strong></summary>
204
+
205
+ > **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.
206
+
207
+ Remove any text nodes that only consist of whitespace characters.
208
+
209
+ <table><thead><tr><th>Before<th>After<tbody><tr><td>
210
+
211
+ ```html
212
+ <ul>↵
213
+ ··<li>A</li>↵
214
+ ··<li>B</li>↵
215
+ ··<li>C</li>↵
216
+ </ul>
217
+ ```
218
+
219
+ <td>
220
+
221
+ ```html
222
+ <ul>↵
223
+ ··<li>A</li><li>B</li><li>C</li>↵
224
+ </ul>
225
+ ```
226
+
227
+ </table>
228
+ </details>
229
+
230
+ <details>
231
+ <summary><strong>Trim whitespace</strong></summary>
232
+
233
+ > **Applies to:** any element except [whitespace sensitive](./src/spec/tag/whitespace.rs) and [formatting](./src/spec/tag/whitespace.rs) elements.
234
+
235
+ Remove any leading/trailing whitespace from any leading/trailing text nodes of a tag.
236
+
237
+ <table><thead><tr><th>Before<th>After<tbody><tr><td>
238
+
239
+ ```html
240
+ <p>↵
241
+ ··Hey,·I·<em>just</em>·found↵
242
+ ··out·about·this·<strong>cool</strong>·website!↵
243
+ ··<sup>[1]</sup>↵
244
+ </p>
245
+ ```
246
+
247
+ <td>
248
+
249
+ ```html
250
+ <p>Hey,·I·<em>just</em>·found↵
251
+ ··out·about·this·<strong>cool</strong>·website!↵
252
+ ··<sup>[1]</sup></p>
253
+ ```
254
+
255
+ </table>
256
+ </details>
257
+
258
+ #### Element types
259
+
260
+ 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.
261
+
262
+ |Group|Elements|Expected children|
263
+ |---|---|---|
264
+ |Formatting|`a`, `strong`, [and others](./src/spec/tag/whitespace.rs)|Formatting elements, text.|
265
+ |Content|`h1`, `p`, [and others](src/spec/tag/whitespace.rs)|Formatting elements, text.|
266
+ |Layout|`div`, `ul`, [and others](./src/spec/tag/whitespace.rs)|Layout elements, content elements.|
267
+ |Content-first|`label`, `li`, [and others](./src/spec/tag/whitespace.rs)|Like content but could be layout with only one child.|
268
+
269
+ <details>
270
+ <summary><strong>Formatting elements</strong></summary>
271
+
272
+ > Whitespace is collapsed.
273
+
274
+ 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.
275
+
276
+ </details>
277
+
278
+ <details>
279
+ <summary><strong>Content elements</strong></summary>
280
+
281
+ > Whitespace is trimmed and collapsed.
282
+
283
+ 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.
284
+
285
+ ###### Before
286
+
287
+ ```html
288
+ <p>↵
289
+ ··Hey,·I·<em>just</em>·found↵
290
+ ··out·about·this·<strong>cool</strong>·website!↵
291
+ ··<sup>[1]</sup>↵
292
+ </p>
293
+ ```
294
+
295
+ ###### After
296
+
297
+ ```html
298
+ <p>Hey,·I·<em>just</em>·found·out·about·this·<strong>cool</strong>·website!·<sup>[1]</sup></p>
299
+ ```
300
+
301
+ </details>
302
+
303
+ <details>
304
+ <summary><strong>Layout elements</strong></summary>
305
+
306
+ > Whitespace is trimmed and collapsed. Whole whitespace is removed.
307
+
308
+ 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.
309
+
310
+ ###### Before
311
+
312
+ ```html
313
+ <ul>↵
314
+ ··<li>A</li>↵
315
+ ··<li>B</li>↵
316
+ ··<li>C</li>↵
317
+ </ul>
318
+ ```
319
+
320
+ ###### After
321
+
322
+ ```html
323
+ <ul><li>A</li><li>B</li><li>C</li></ul>
324
+ ```
325
+
326
+ </details>
327
+
328
+ <details>
329
+ <summary><strong>Content-first elements</strong></summary>
330
+
331
+ > Whitespace is trimmed and collapsed.
332
+
333
+ 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.
334
+
335
+ ###### Before
336
+
337
+ ```html
338
+ <li>↵
339
+ ··<article>↵
340
+ ····<section></section>↵
341
+ ····<section></section>↵
342
+ ··</article>↵
343
+ </li>
344
+ ```
345
+
346
+ ###### After
347
+
348
+ ```html
349
+ <li><article><section></section><section></section></article></li>
350
+ ```
351
+
352
+ </details>
353
+
354
+ ### Tags
355
+
356
+ [Optional closing tags](https://html.spec.whatwg.org/multipage/syntax.html#syntax-tag-omission) are removed.
357
+
358
+ ### Attributes
359
+
360
+ Any entities in attribute values are decoded, and then the shortest representation of the value is calculated and used:
361
+
362
+ - Double quoted, with any `"` encoded.
363
+ - Single quoted, with any `'` encoded.
364
+ - Unquoted, with `"`/`'` first character (if applicable), `>` last character (if applicable), and any whitespace encoded.
365
+
366
+ `class` attributes have their whitespace (after any decoding) trimmed and collapsed.
367
+
368
+ [Boolean attribute](./gen/attrs.json) values are removed.
369
+ [Some attributes](./gen/attrs.json) are completely removed if their value is empty or the default value after any processing.
370
+
371
+ `type` attributes on `script` tags with a value equaling a [JavaScript MIME type](https://mimesniff.spec.whatwg.org/#javascript-mime-type) are removed.
372
+
373
+ 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.
374
+
375
+ Spaces are removed between attributes if possible.
376
+
377
+ ### Entities
378
+
379
+ Entities are decoded if valid (see relevant parsing section) and their decoded characters as UTF-8 is shorter or equal in length.
380
+
381
+ Numeric entities that do not refer to a valid Unicode Scalar Value are decoded to U+FFFD REPLACEMENT CHARACTER.
382
+
383
+ 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`.
384
+
385
+ ### Comments
386
+
387
+ Comments are removed.
388
+
389
+ ### Ignored
390
+
391
+ 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.
392
+
393
+ ## Parsing
394
+
395
+ Only UTF-8/ASCII-encoded HTML code is supported.
396
+
397
+ hyperbuild simply does HTML minification, and almost does no syntax checking or standards enforcement for performance and code complexity reasons.
398
+
399
+ 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>`
400
+
401
+ However, there are some syntax requirements for speed and sanity reasons.
402
+
403
+ ### Tags
404
+
405
+ 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.
406
+
407
+ 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>`.
408
+
409
+ ### Entities
410
+
411
+ Well-formed entities are decoded, including in attribute values.
412
+
413
+ They are interpreted as characters representing their decoded value. This means that `&#9;` is considered a whitespace character and could be minified.
414
+
415
+ Malformed entities are interpreted literally as a sequence of characters.
416
+
417
+ 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.
418
+
419
+ Numeric character references that do not reference a valid [Unicode Scalar Value](https://www.unicode.org/glossary/#unicode_scalar_value) are considered malformed.
420
+
421
+ ### Attributes
422
+
423
+ Backticks (`` ` ``) are not valid quote marks and not interpreted as such.
424
+ However, backticks are valid attribute value quotes in Internet Explorer.
425
+
426
+ 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.
427
+
428
+ ### Script and style
429
+
430
+ `script` and `style` tags must be closed with `</script>` and `</style>` respectively (case sensitive).
431
+
432
+ hyperbuild does **not** handle [escaped and double-escaped](./notes/Script%20data.md) script content.
433
+
434
+ ## Issues and contributions
435
+
436
+ Contributions welcome!
437
+
438
+ 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.
data/lib/linux CHANGED
Binary file
data/lib/macos CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hyperbuild
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.27
4
+ version: 0.0.28
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wilson Lin
@@ -31,6 +31,7 @@ executables: []
31
31
  extensions: []
32
32
  extra_rdoc_files: []
33
33
  files:
34
+ - README.md
34
35
  - lib/hyperbuild.rb
35
36
  - lib/linux
36
37
  - lib/macos