如何通过 Windows 命令行上的 html-minifier 将选项传递给 UglifyJS?
How to pass options to UglifyJS through html-minifier on Windows command line?
HTMLMinifier (html-minifier) (3.5.14) for Node.js (v8.11.1),与 npm install html-minifier -g
一起安装,可以通过命令行 运行 (Windows CMD), 例如html-minifier --help
生成使用信息(摘录):
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
选项 --minify-js [value]
依赖于 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
选项(另见 pure_funcs
)。
但是 --minify-js drop_console=true
没有效果,"uglify:{options:{compress:{drop_console:true}}}"
或 "compress:{pure_funcs:['console.log']}"
.
之类的东西也没有效果
如何设置这样的选项,最好是通过 html-minifier 命令行(或者通过 config-file,尽管它只是设置 "minifyJS": true
)?
我很亲近。
我开始研究代码(安装在 %appdata%\npm\node_modules\html-minifier
中)以查看提供的选项会发生什么,即使用 console.log(xyz);
添加调试输出(使用实际的调试器可能是更好的主意).
所以,这是我的 "trace":
- 选项:https://github.com/kangax/html-minifier/blob/gh-pages/cli.js#L118
- 选项处理:https://github.com/kangax/html-minifier/blob/gh-pages/cli.js#L144
- 使用 [
commander
][2] 解析参数
createOptions()
https://github.com/kangax/html-minifier/blob/gh-pages/cli.js#L197
options
然后包含例如minifyJS: 'compress:{pure_funcs:[\'console.log\']}',
- 传递给
minify()
https://github.com/kangax/html-minifier/blob/gh-pages/src/htmlminifier.js#L806 立即运行
processOptions()
https://github.com/kangax/html-minifier/blob/gh-pages/src/htmlminifier.js#L616
最后在 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.
行
但是我们的选项字符串 compress:{pure_funcs:['console.log']}
被清理了,因为它还不是一个对象,导致 {}
.
或者,在使用不同字符串的不同试验中,您可能会遇到错误 Could not parse JSON value '{compress:{pure_funcs:'console.log']}}'
至少它走到了这一步!但为什么它不起作用?
首先,现在是重新审视 JSON 规范的好时机:https://www.json.org/index.html
其次,查看字符串是否可以被解析为有效 JSON,例如JSON.parse()
演示位于 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
第三,弄清楚如何通过 CMD 获取该字符串作为参数(转义双引号)。
最后,确保配置UgliFyJS的数据结构是正确的。这很简单,因为它被记录在案:https://github.com/mishoo/UglifyJS2#minify-options-structure
看,简单地用反斜杠转义双引号对我有用:
html-minfier ... --minify-js {\"compress\":{\"pure_funcs\":[\"console.log\"]}} ...
它在选项中正确显示为
...
{ compress:
{ pure_funcs: [ 'console.log' ],
...
例如。 curl 可以从文件中读取配置,如代理等...
许多程序都是这样做的。 git, maven, gradle..... 无论你如何调用它们,在哪里调用它们,它们都会查找你或系统提供的配置:首先从当前目录,然后从用户主目录,然后系统 /etc/...
如果这些节点包中没有电池,它们只能用于单独的 html 和 js 文件。
HTMLMinifier (html-minifier) (3.5.14) for Node.js (v8.11.1),与 npm install html-minifier -g
一起安装,可以通过命令行 运行 (Windows CMD), 例如html-minifier --help
生成使用信息(摘录):
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
选项 --minify-js [value]
依赖于 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
选项(另见 pure_funcs
)。
但是 --minify-js drop_console=true
没有效果,"uglify:{options:{compress:{drop_console:true}}}"
或 "compress:{pure_funcs:['console.log']}"
.
如何设置这样的选项,最好是通过 html-minifier 命令行(或者通过 config-file,尽管它只是设置 "minifyJS": true
)?
我很亲近。
我开始研究代码(安装在 %appdata%\npm\node_modules\html-minifier
中)以查看提供的选项会发生什么,即使用 console.log(xyz);
添加调试输出(使用实际的调试器可能是更好的主意).
所以,这是我的 "trace":
- 选项:https://github.com/kangax/html-minifier/blob/gh-pages/cli.js#L118
- 选项处理:https://github.com/kangax/html-minifier/blob/gh-pages/cli.js#L144
- 使用 [
commander
][2] 解析参数
createOptions()
https://github.com/kangax/html-minifier/blob/gh-pages/cli.js#L197options
然后包含例如minifyJS: 'compress:{pure_funcs:[\'console.log\']}',
- 传递给
minify()
https://github.com/kangax/html-minifier/blob/gh-pages/src/htmlminifier.js#L806 立即运行 processOptions()
https://github.com/kangax/html-minifier/blob/gh-pages/src/htmlminifier.js#L616
最后在 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.
但是我们的选项字符串 compress:{pure_funcs:['console.log']}
被清理了,因为它还不是一个对象,导致 {}
.
或者,在使用不同字符串的不同试验中,您可能会遇到错误 Could not parse JSON value '{compress:{pure_funcs:'console.log']}}'
至少它走到了这一步!但为什么它不起作用?
首先,现在是重新审视 JSON 规范的好时机:https://www.json.org/index.html
其次,查看字符串是否可以被解析为有效 JSON,例如JSON.parse()
演示位于 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
第三,弄清楚如何通过 CMD 获取该字符串作为参数(转义双引号)。
最后,确保配置UgliFyJS的数据结构是正确的。这很简单,因为它被记录在案:https://github.com/mishoo/UglifyJS2#minify-options-structure
看,简单地用反斜杠转义双引号对我有用:
html-minfier ... --minify-js {\"compress\":{\"pure_funcs\":[\"console.log\"]}} ...
它在选项中正确显示为
...
{ compress:
{ pure_funcs: [ 'console.log' ],
...
例如。 curl 可以从文件中读取配置,如代理等... 许多程序都是这样做的。 git, maven, gradle..... 无论你如何调用它们,在哪里调用它们,它们都会查找你或系统提供的配置:首先从当前目录,然后从用户主目录,然后系统 /etc/...
如果这些节点包中没有电池,它们只能用于单独的 html 和 js 文件。