han*_*dle 5 json cmd escaping minify node.js
用于 Node.js (v8.11.1) 的HTMLMinifier ( html-minifier ) (3.5.14),随 一起安装,可以通过命令行 ( Windows CMDnpm install html-minifier -g )运行,例如生成使用信息(摘录):html-minifier --help
Usage: html-minifier [options] [files...]
Options:
-V, --version output the version number
Run Code Online (Sandbox Code Playgroud)
...
--minify-js [value] Minify Javascript in script elements and on* attributes (uses uglify-js)
Run Code Online (Sandbox Code Playgroud)
...
-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
Run Code Online (Sandbox Code Playgroud)
该选项--minify-js [value]依赖于UglifyJS来“压缩”嵌入到传递给html-minifier. 通过启用该选项,UglifyJS 可以从 JavaScript 中删除console.log()函数调用(uglify-js 可以删除 console.log 语句吗?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);(使用实际的调试器可能是一个更好的主意)。
所以,这是我的“踪迹”:
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#L806processOptions() 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 中进行处理,然后再像https://github.com/kangax/htmloptions.minifyJS那样运行-minifier/blob/gh-pages/src/htmlminifier.js#L680。var result = UglifyJS.minify(code, minifyJS);
但是我们的选项字符串compress:{pure_funcs:['console.log']}被清理了,因为它还不是一个对象,导致{}.
或者,在使用不同字符串的不同试验中,您可能会遇到错误Could not parse JSON value '{compress:{pure_funcs:'console.log']}}'
至少能走到这一步!但为什么不起作用呢?
首先,现在是重新审视 JSON 规范的好时机: https: //www.json.org/index.html
其次,查看字符串是否可以解析为有效的 JSON,例如 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parseJSON.parse()上的演示
第三,弄清楚如何通过 CMD 获取该字符串作为参数(转义双引号)。
最后,确保配置 UgliFyJS 的数据结构正确。这很简单,因为它已记录在案:https ://github.com/mishoo/UglifyJS2#minify-options-struct
看吧,简单地用反斜杠转义双引号对我来说是有效的:
html-minfier ... --minify-js {\"compress\":{\"pure_funcs\":[\"console.log\"]}} ...
它正确地显示在选项中
...
{ compress:
{ pure_funcs: [ 'console.log' ],
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2154 次 |
| 最近记录: |