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 #907

Open
wants to merge 16 commits into
base: gh-pages
Choose a base branch
from
Open

Callback Support #907

wants to merge 16 commits into from

Conversation

RebeccaStevens
Copy link

Adds callback support to minify, minifyCSS and minifyJS functions.
Does not break any existing functionality.

Example Use:

var minify = require('html-minifier').minify;
var html = '<html><head><style>body { margin: 0; display: flex; }</style></head></html>';

var config = {
  minifyCSS: function(css, cb) {
    doSomethingAsync(css, function(updatedCss) {
      cb(updatedCss);
    });
  },
  minifyJS: function(js, inline, cb) {
    doSomethingElseAsync(js, function(updatedJs) {
      cb(updatedJs);
    });
  }
};

minify(html, config, function(error, result) {
  if (error) {
    // Handle error.
  } else {
    // Do something with the results.
  }
});

Fixes #906

@RebeccaStevens
Copy link
Author

I plan on adding documentation updates soon.

@alexlamsl
Copy link
Collaborator

As stated in #906 (comment), you need to preserve the signature of minify(), i.e. it needs to return a string that is the minified input.

Any changes to that interface would be a breaking change and cannot be accepted.

@RebeccaStevens
Copy link
Author

RebeccaStevens commented Apr 11, 2018

The signature of minify() is preserve for synchronous calls. It only differs for asynchronous calls.

@alexlamsl
Copy link
Collaborator

I see.

Before I can perform a code review, please exclude any auto-generated files from the changeset, i.e. dist/*.js

@RebeccaStevens
Copy link
Author

Changes to dist files have now been undone.

@RebeccaStevens
Copy link
Author

Just did a rebase.

@alexlamsl
Copy link
Collaborator

Thanks - looking at the changes, one question jumps out - is HTMLParser still functional synchronously?

Apologies for not mentioning this earlier - there are users would rely on that to perform pre/post-processing, outside the scope of minification.

One way I can think of is similar to ignoreCustomFragments, i.e. assign temporary (unique) strings as placeholders for asynchronous minify{CS,J}S. That way the changes would also be confined within src/htmlminifier.js


// If the result is defined then minifyCSS completed synchronously.
// eslint-disable-next-line no-undefined
if (minifyCSSResult !== undefined) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use typeof minifyCSSResult !== 'undefined' instead of overriding eslint

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.

@RebeccaStevens
Copy link
Author

HTMLParser is synchronous when minifyCSS and minifyJS are synchronous.

Does HTMLParser also need to be made synchronous for when either minifyCSS or minifyJS are asynchronous?

@alexlamsl
Copy link
Collaborator

HTMLParser is synchronous when minifyCSS and minifyJS are synchronous

I seem to have trouble validating that - the following test file is used:

// test.js
var HTMLParser = require('./src/htmlparser').HTMLParser;
var input = require('fs').readFileSync('./index.html', 'utf8');

function log(prefix) {
  return function() {
    var args = [].slice.call(arguments);
    args.unshift(prefix);
    console.log.apply(console, args);
  };
}

new HTMLParser(input, {
  start: log('start'),
  end: log('end'),
  chars: log('chars'), 
  comment: log('comment')
});

On current HEAD of gh-pages:

$ cat test.js | node
chars
  html
start html [ { name: 'lang',
    value: 'en',
    customAssign: '=',
    customOpen: '',
    customClose: '',
    quote: '"' } ] false
chars
   html head
start head [] false
... snip many lines in between ...
end body [] false
chars
 /body /html
end html [ { name: 'lang',
    value: 'en',
    customAssign: '=',
    customOpen: '',
    customClose: '',
    quote: '"' } ] false
chars
 /html

... whereas with this PR:

$ cat test.js | node
chars
  html function (error) {
            if (error) {
              return handleError(error);
            }

            prevTag = '';
            checkForParseError();
          }

@RebeccaStevens
Copy link
Author

RebeccaStevens commented Apr 11, 2018

I'll look into that tomorrow and make the necessary change to get it working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants