简体   繁体   English

npm脚本:需要缩小文件夹(和子文件夹)中的所有HTML文件

[英]npm scripts: need to minify all HTML files in folder (and subfolders)

I want to minify all .html files in a folder (and any folders within) using npm run script. 我想使用npm run script缩小文件夹(以及其中的所有文件夹)中的所有.html文件。 Ideally, all .html files should be overwritten (if that's not possible, a new folder is acceptable). 理想情况下,所有.html文件都应该被覆盖(如果不可能,则可以使用新文件夹)。 It is assumed that there will be non-HTML files in the input folder. 假设输入文件夹中将存在非HTML文件。

npm library minimize works only on per-file but not on folders. npm库minimize仅适用于每个文件但不适用于文件夹。

Another npm library html-minifier does accept folder as input, but fails if there are any non-HTML files present in the input folder: 另一个npm库html-minifier接受文件夹作为输入,但如果输入文件夹中存在任何非HTML文件,则会失败:

html-minifier --input-dir ./test1 --output-dir ./test2 --html-5 --collapse-whitespace

I need this to minify my static website's HTML files. 我需要这个来缩小静态网站的HTML文件。

Since posting original question here on SO, html-minifier added the feature to ignore the non-HTML files. 自从在SO上发布原始问题以来, html-minifier添加了忽略非HTML文件的功能。 Now you can set directories and html-minifier won't error-out when it encounters non-HTML files. 现在您可以设置目录,并且html-minifier在遇到非HTML文件时不会出错。

Usage example, taken from my working npm task: 用法示例,取自我的工作npm任务:

html-minifier --input-dir ./public --output-dir ./public --collapse-whitespace --file-ext html

Let's minify all our static websites' HTML files now! 让我们现在缩小所有静态网站的HTML文件!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM