[英]How do i minify all js files within a directory structure using javascript::minify module?
I wanted to minify all js files in a directory tree using the minify module from the javascript::minify package but am unable to do the same.I was successful in doing it for individual files but when i run it for the entire directory , i get a warning message at line 345 in the minify.pm file. 我想使用javascript :: minify包中的minify模块来缩小目录树中的所有js文件,但无法做到这一点。我成功地对单个文件进行了处理,但是当我为整个目录运行它时,我在minify.pm文件的第345行获得警告消息。
Here is the code that i came up with: 这是我想出的代码:
use File::Find;
use JavaScript::Minifier qw(minify);
find(sub {
if (-f and /\.js$/) {
$t = 'temp.js';
open(INFILE, $_) or die;
open(OUTFILE, ">$t") or die;
minify(input => *INFILE, outfile => *OUTFILE);
close(INFILE);
close(OUTFILE);
rename($t,$_);
}
}, '.');
Following is the code that works for individual files: 以下是适用于单个文件的代码:
use JavaScript::Minifier qw(minify);
$file = 'ex.js';
$temp = 'temp.js';
open(INFILE, $file) or die;
open(OUTFILE, ">$temp") or die;
minify(input => *INFILE, outfile => *OUTFILE);
close(INFILE);
close(OUTFILE);
rename($temp,$file);
Please help with possible solutions.. 请提供可能的解决方案。
JavaScript::Minifier
complains with empty files. JavaScript::Minifier
抱怨空文件。
The following demonstrates: 以下说明:
use strict;
use warnings;
use JavaScript::Minifier qw(minify);
print minify(input => '');
Outputs: 输出:
Use of uninitialized value in pattern match (m//) at C:/strawberry/perl/site/lib/JavaScript/Minifier.pm line 345.
Therefore, you have a .js
file somewhere that is completely empty. 因此,您的.js
文件完全空白。 The easy fix for this is just to verify that the file has data before you process it: 简单的解决方法是在处理文件之前先验证文件是否包含数据:
if (-f && -s && /\.js$/) {
However, I'd recommend some cleaning up of your script to the following: 但是,我建议对脚本进行以下清理:
use strict;
use warnings;
use autodie;
use File::Find;
use JavaScript::Minifier qw(minify);
find(sub {
return unless -f && -s && /\.js$/;
local @ARGV = my $file = $_;
local $^I = '.bak';
local $/;
while (<>) {
print minify(input => $_);
}
unlink "$file$^I"; # Optionally delete backup
}, '.');
Notes: 笔记:
You probably would've gotten an answer a lot faster if you included the exact warning message in your post. 如果您在帖子中包含确切的警告消息,则可能会更快地得到答案。
I've reported this bug to the module author: minify produces warning for empty files (and it's fixed ) 我已将此错误报告给模块作者: minify会为空文件生成警告 (并且已修复 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.