Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass options to UglifyJS through html-minifier on Windows command line?

Tags:

HTMLMinifier (html-minifier) (3.5.14) for Node.js (v8.11.1), installed with npm install html-minifier -g, can be run via command line (Windows CMD), e.g. html-minifier --help produces the usage info (excerpts):

  Usage: html-minifier [options] [files...]

  Options:

    -V, --version                        output the version number

...

    --minify-js [value]                  Minify Javascript in script elements and on* attributes (uses uglify-js)

...

    -c --config-file <file>              Use config file
    --input-dir <dir>                    Specify an input directory
    --output-dir <dir>                   Specify an output directory
    --file-ext <text>                    Specify an extension to be read, ex: html
    -h, --help                           output usage information

The option --minify-js [value] relies on UglifyJS to "compress" the JavaScript embedded inside the HTML file(s) passed to html-minifier. UglifyJS can remove console.log() function calls (Can uglify-js remove the console.log statements?) from the JavaScript, by enabling the drop_console option (also see pure_funcs).

But --minify-js drop_console=true does not have an effect, nor does something like "uglify:{options:{compress:{drop_console:true}}}" or "compress:{pure_funcs:['console.log']}".

How can such an option be set, ideally via the html-minifier command line (alternatively by config-file, though it just sets "minifyJS": true)?

like image 416
handle Avatar asked Apr 10 '18 21:04

handle


People also ask

What is Uglify and minify?

Minifying is just removing unnecessary white-space and redundant like comments and semicolons. And it can be reversed back when needed. Uglifying is transforming the code into an "unreadable" form by changing variable names, function names, etc, to hide the original content.

What is Uglify in JavaScript?

Uglify JS is a JavaScript library for minifying JavaScript files. To 'uglify' a JavaScript file is to minify it using Uglify. Uglification improves performance while reducing readability. Encryption: This is the process of translating data, called plain data, into encoded data.


1 Answers

I was very close.

I started digging through the code (installed in %appdata%\npm\node_modules\html-minifier) to see what happens with the options provided, i.e. adding debug output with console.log(xyz); (using an actual debugger probably would be a better idea).

So, here's my "trace":

  • option: https://github.com/kangax/html-minifier/blob/gh-pages/cli.js#L118
  • option handling: https://github.com/kangax/html-minifier/blob/gh-pages/cli.js#L144
  • argument parsing using [commander][2]
  • createOptions() https://github.com/kangax/html-minifier/blob/gh-pages/cli.js#L197
    • options then contains e.g. minifyJS: 'compress:{pure_funcs:[\'console.log\']}',
  • passed on to minify() https://github.com/kangax/html-minifier/blob/gh-pages/src/htmlminifier.js#L806 which immediately runs
  • processOptions() https://github.com/kangax/html-minifier/blob/gh-pages/src/htmlminifier.js#L616

where finally in line https://github.com/kangax/html-minifier/blob/gh-pages/src/htmlminifier.js#L667 options.minifyJS is handled, before it's run as var result = UglifyJS.minify(code, minifyJS); in https://github.com/kangax/html-minifier/blob/gh-pages/src/htmlminifier.js#L680.

But there our option string compress:{pure_funcs:['console.log']} gets cleaned because it's not yet an object, resulting in {}.

Or, in a different trial with a different string you may encounter the error Could not parse JSON value '{compress:{pure_funcs:'console.log']}}'

At least it gets that far! But why doesn't it work?

First, it's a good time to revisit the JSON spec: https://www.json.org/index.html

Second, see if the string could be parsed as valid JSON, e.g. with the JSON.parse() demo at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

Third, figure out how to get that string through the CMD as argument (escaping the double quotes).

Finally, make sure the data structure to configure UgliFyJS is correct. That's quite easy, since it's documented: https://github.com/mishoo/UglifyJS2#minify-options-structure

And behold, simply escaping the double quotes with a backslash works for me:

html-minfier ... --minify-js {\"compress\":{\"pure_funcs\":[\"console.log\"]}} ...

and it properly shows up in the options as

...
{ compress:
   { pure_funcs: [ 'console.log' ],
...
like image 175
handle Avatar answered Oct 01 '22 01:10

handle