Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callback Support (alternative implementation) #908

Open
wants to merge 68 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
1da64f8
Added tests.
Apr 12, 2018
736a572
Added callback parameter to minify.
Apr 12, 2018
b773ab4
Added callback parameter to minifyCSS and minifyJS.
Apr 12, 2018
fdf4ca3
Create a placeholder class for async text.
Apr 12, 2018
b71cc2f
When processing entries in the buffer, make sure they are a string.
Apr 12, 2018
d9104ac
The result is only finalize and returned if there are no async tasks …
Apr 12, 2018
79e5f8d
Set up minifyJS and minifyCSS to be able to complete asynchronously.
Apr 12, 2018
7b2864f
Added error handling.
Apr 12, 2018
99b55ed
Removed extra empty lines.
Apr 12, 2018
a7c0584
"along side" --> "alongside"
Apr 12, 2018
ee14967
Added extra check to 'minify error with callback' test.
Apr 12, 2018
3a6846d
Added a test to ensure the minifyCSS and miniftJS callbacks can only …
Apr 12, 2018
390eacb
Fixed bug with AsyncTextPlaceholder not being able to have the emptyS…
Apr 12, 2018
e74fcf1
Fixed bug with error not being given to callback after all async task…
Apr 12, 2018
d77176a
Added callback information to the readme.
Apr 12, 2018
cdc653b
Refactored.
RebeccaStevens Apr 12, 2018
28c1ac3
Refactored - Async tasks now do not start until after all sync code h…
Apr 13, 2018
80f5ad9
Added inline callback tests.
Apr 13, 2018
c64327a
All test now test both synchronously and asynchronously.
Apr 18, 2018
9247ece
Merge branch 'gh-pages' into callback-support-alt
Apr 18, 2018
ee3054f
Fix potential issue with placeholder not being at the same index it w…
Apr 18, 2018
aa56a66
Refactored.
Apr 18, 2018
f82ca4a
Asynchronously tests now give more helpful feedback when an error occ…
Apr 18, 2018
314c4b8
Created a test_minify_error function to test for expected errors.
Apr 18, 2018
5bcb767
Fixed issue with code still executing after callback.
Apr 18, 2018
3f2bc24
Initial step to get normalizeAttr working with a callback.
Apr 18, 2018
0a5e9b8
Fixed conversion from sync to async minifyJS and minifyCSS.
Apr 18, 2018
fe5da2d
Refactored.
Apr 18, 2018
377e049
Merge branch 'gh-pages' into callback-support-alt
Apr 19, 2018
f23648e
Async start tasks now modifies the buffer inplace.
Apr 19, 2018
37b4ea7
removeStartTag and removeEndTag now take an indexBound which prevents…
Apr 19, 2018
cebb717
Added callbacks to more functions and wrapped them in AsyncTasks.
Apr 19, 2018
7067027
squashTrailingWhitespace now uses indexBound.
Apr 19, 2018
690f85e
Fixed issue with bad error reporting when the minify function never c…
Apr 20, 2018
bf46922
Removed placeholders as they are not necessary when everything will b…
Apr 20, 2018
a1a5a66
Removed unneeded data object wraps.
Apr 20, 2018
35d3d26
Tasks no longer have data associated with them.
Apr 20, 2018
d8613ad
Moved 'isLast' into function loop to mimic original for loop scoping.
Apr 20, 2018
bb191cf
Slight code style improvements.
Apr 20, 2018
29c71ea
Added missing callbacks.
Apr 20, 2018
d6b2bf3
Fixed bad implementation of cleanConditionalComment callback support.
Apr 20, 2018
0316fb9
Removed already fixed fixme.
Apr 20, 2018
818f20a
Fixed issue with minifyCSS and minifyJS functions taking an error in …
Apr 20, 2018
58d6d42
Added async tests.
Apr 20, 2018
58ca98e
Fixed up tests.
Apr 20, 2018
8e4351d
Added 'callback called multiple times' tests.
Apr 20, 2018
5961a66
Fixed bad test 'callback called multiple times' and got it passing.
RebeccaStevens Apr 23, 2018
adad665
Fixed bug in tests causing them to throw errors.
RebeccaStevens Apr 23, 2018
f57abf3
Fixed up tests.
RebeccaStevens Apr 23, 2018
587da38
Fixed up jsdoc.
RebeccaStevens Apr 23, 2018
9addaf6
Increased timout.
RebeccaStevens Apr 23, 2018
99d8546
Made the test_minify_async_error consistent with test_minify_sync_error
RebeccaStevens Apr 23, 2018
3552c4e
Fixed up jsdoc types.
RebeccaStevens Apr 23, 2018
31edf01
Merged async and sync test functions togetehr.
RebeccaStevens Apr 24, 2018
1a1edc5
Refactored.
RebeccaStevens Apr 24, 2018
61e5cd9
Added safety checks and fix bad test.
RebeccaStevens Apr 24, 2018
7ef92b4
Switch out assert.throws with test_minify_error
RebeccaStevens Apr 24, 2018
b17b970
Removed async only option from test_minify.
Apr 26, 2018
fcd3c59
Refactored Task and TaskGroup.
Apr 26, 2018
a2968a0
Fix issue with not all callbacks being in the form of (error, result).
Apr 26, 2018
f9dee3c
Moved typedefs below imports.
Apr 26, 2018
675d086
Fixed 'Maximum call stack size exceeded' in the sync case.
Apr 26, 2018
aef9258
Removed sync callback tests. Callbacks are never sync now.
Apr 26, 2018
e8c9048
Fixed 'Maximum call stack size exceeded' in the async case.
Apr 26, 2018
c39b07a
Improved comment.
Apr 26, 2018
38a147d
Recovered from 'Maximum call stack size exceeded' error.
Apr 26, 2018
8767828
Refactored
Apr 27, 2018
955882a
The buffer can now recover from 'Maximum call stack size exceeded' er…
Apr 27, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 60 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ Most of the options are disabled by default.
| `includeAutoGeneratedTags` | Insert tags generated by HTML parser | `true` |
| `keepClosingSlash` | Keep the trailing slash on singleton elements | `false` |
| `maxLineLength` | Specify a maximum line length. Compressed output will be split by newlines at valid HTML split-points |
| `minifyCSS` | Minify CSS in style elements and style attributes (uses [clean-css](https://github.com/jakubpawlowicz/clean-css)) | `false` (could be `true`, `Object`, `Function(text, type)`) |
| `minifyJS` | Minify JavaScript in script elements and event attributes (uses [UglifyJS](https://github.com/mishoo/UglifyJS2)) | `false` (could be `true`, `Object`, `Function(text, inline)`) |
| `minifyCSS` | Minify CSS in style elements and style attributes (uses [clean-css](https://github.com/jakubpawlowicz/clean-css)) | `false` (could be `true`, `Object`, `Function(text, type[, cb])`) |
| `minifyJS` | Minify JavaScript in script elements and event attributes (uses [UglifyJS](https://github.com/mishoo/UglifyJS2)) | `false` (could be `true`, `Object`, `Function(text, inline[, cb])`) |
| `minifyURLs` | Minify URLs in various attributes (uses [relateurl](https://github.com/stevenvachon/relateurl)) | `false` (could be `String`, `Object`, `Function(text)`) |
| `preserveLineBreaks` | Always collapse to 1 line break (never remove it entirely) when whitespace between tags include a line break. Must be used in conjunction with `collapseWhitespace=true` | `false` |
| `preventAttributesEscaping` | Prevents the escaping of the values of attributes | `false` |
Expand Down Expand Up @@ -142,6 +142,8 @@ For command line usage please see `html-minifier --help`

### Node.js

#### Synchronous Usage

```js
var minify = require('html-minifier').minify;
var result = minify('<p title="blah" id="moo">foo</p>', {
Expand All @@ -150,6 +152,62 @@ var result = minify('<p title="blah" id="moo">foo</p>', {
result; // '<p title=blah id=moo>foo</p>'
```

#### Asynchronous Usage

`minify` can take an optional callback function that will be invoked once the
minification is complete (or an error occurs).

This allows for the following functions to complete asynchronously:

* minifyCSS
* minifyJS

```js
var minify = require('html-minifier').minify;

minify(
'<style>body { margin: 0; display: flex; }</style>',
{
minifyCSS: function(css, cb) {
doSomethingAsync(css, function(updatedCss) {
cb(updatedCss);
});
},
},
function(error, result) {
if (error) {
// Handle error.
} else {
// Do something with the result.
}
}
);
```

##### Promisify

`minify` is compatable with NodeJS's [`util.promisify`](https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_util_promisify_original)
function added in v8.0.0

```js
const util = require('util');
const minify = util.promisify(require('html-minifier').minify);

minify('<style>body { margin: 0; display: flex; }</style>', {
minifyCSS: (css, cb) => {
doSomethingAsync(css, updatedCss => {
cb(updatedCss);
});
}
})
.then(result => {
// Do something with the result.
})
.catch(error => {
// Handle error.
});
```

## Running benchmarks

Benchmarks for minified HTML:
Expand Down