- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
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 “压缩”传递给 html-minifier
的 HTML 文件中嵌入的 JavaScript。 UglifyJS 可以通过启用 Can uglify-js remove the console.log statements? 从 JavaScript 中删除 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#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
在作为 var result = UglifyJS.minify(code, minifyJS);
在 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' ],
...
关于json - 如何在 Windows 命令行上通过 html-minifier 将选项传递给 UglifyJS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49763248/
我的 js 文件中有这样的行 console.log('FunctionName()'); 默认的 Ajax Minifier 设置不会从 .min.js 输出中删除这些行。 我在这个讨论中注意到一个
我使用 uglifyJS 缩小了 Javascript 和 CSS 文件,磁盘上的文件大小已大大减小。但在使用 Chrome 开发者工具检查网络选项卡中加载资源的总时间时,我发现没有区别。那么缩小真的
我正在探索将单个流文件传输到自定义处理器中的两个或多个关系的选项,我在文档中没有找到任何帮助,NIFI 支持此功能吗? 示例代码: session.transfer(flowFile, REL_SUC
我有一个 JS 文件,我正试图缩小它,但它遇到了一些问题。这是文件: $(document).ready(function () { function DisplayMessage(message,
我能找到的几乎所有 CSS 压缩器基本上都是去除空格的正则表达式。有没有更聪明的 CSS 压缩器?比如合并相似的选择器/属性等? 最佳答案 您可以在 Google 上搜索“CSS 优化器”或“CSS
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 8 年前。 Improv
我正在使用这个压缩器: #!/usr/bin/perl use Getopt::Std; use strict; my %opts; getopts('f:t:h', \%opts) || usage
我正在尝试编写一个可用于根据文件类型缩小 html、css 和 js 的 minify 函数。我想将现有的 gulp 插件用于这 3 个缩小过程来进行实际的缩小。我遇到的问题是我不知道如何在单个黑胶唱
我正在努力成为一个好男孩,并且用口香糖最小化我的css。我目前正在使用gulp-sass(不是gulp-ruby-sass),它似乎没有内置的缩小功能。所以现在我正在使用gulp-minify-css
我想使用 Laravel Elixir 来缩小我的 css/files 文件。但我不想使用混合方法并将它们合并。我想要的只是从我原来的“custom.js”生成一个“custom.min.js”文件。
有没有办法告诉 UglifyJS 跳过特定的代码部分,也许使用这样的注释: // uglifyjs:skipStart filter = function(item){ /* some crazy f
我是 jquery 新手,我可以用它进行简单的编码。我想更多地了解这些文件“缩小”“未压缩”以及何时应该使用每个文件? 最佳答案 每个文件的缩小版本和压缩版本只是在具有相同功能的情况下缩小尺寸的版本。
可以在不添加 -min 后缀的情况下缩小 js 文件吗? 我想保留原来的名字。 gulp.task("js-external-release", function() { gulp.src(c
在 page1.html 上说我需要 mootools.js 和 main.js ......我猜这些工具应该生成一个缩小的 js 文件(比如 min1.js)。 然后在 page2.html 我需要
我最近从 require.js 迁移到 webpack,并且一些 node_module 内置了 .min 文件。当我使用 node_module 中的文件时,我只做 require('my-modu
我正在使用 libman.json 将客户端库从 cdnjs 拉到我项目的一个文件夹中。然后我想将这些库捆绑并缩小到一个 js 文件中,该文件将在 Web 应用程序中部署和引用。为此,我使用了一个名为
我下载了 cordova-minify 插件,它可以压缩我的 Cordova 应用程序 javascript 代码。我尝试运行命令: cordova build browser 我明白了 cordov
我正在构建一个 angular2 应用程序,并开始进行我的构建/缩小过程。我可以使用未缩小的单个 JS 文件在开发模式下成功构建和运行我的应用程序。但是,当我将它们构建为单个串联文件时,我的应用程序将
Minify对我来说工作正常,但在更改其中一个源文件后,我收到一个空白页面。刷新页面不会改变任何东西。没有缓存的刷新(Strg + F5)解决了这个问题。在没有缓存的情况下刷新一次后,我可以返回使用缓
我经常看到“缩小”这个词,但我似乎从未选择过“缩小”的脚本(即来自 jQuery 插件),但我想我应该这样做。如果我是对的,缩小意味着在不改变其功能的情况下从源代码中删除所有不必要的东西,那么为什么我
我是一名优秀的程序员,十分优秀!