本地JS文件批量压缩

  • 本地JS文件批量压缩已关闭评论
  • 127 次浏览
  • A+
所属分类:Web前端
摘要

最近在维护一个小后台项目,有段JS需要压缩上传到CDN存储服务器。由于之前压缩的JS文件都比较少,都是手动压缩的。这次需要压缩的文件比较多,所以用了批量压缩。特此记录一下,方便大家和自己以后再用到的时候备忘。

最近在维护一个小后台项目,有段JS需要压缩上传到CDN存储服务器。由于之前压缩的JS文件都比较少,都是手动压缩的。这次需要压缩的文件比较多,所以用了批量压缩。特此记录一下,方便大家和自己以后再用到的时候备忘。

v准备工作

安装nodejs

首先在本地安装node.js和npm,一般npm集成于nodejs,即安装nodejs,同时也安装了npm。node.js下载地址,下载以后直接不停下一步就行,全部使用默认选项即可。下载完成后打开CMD,node -v检测是否安装成功,安装成功则会显示nodejs版本号。

安装uglify插件

在cmd命令行执行:npm install uglify-js -g

v开始压缩

压缩的时候将下面的代码拷贝下来,然后生成bat文件,再运行bat文件(有些电脑可能需要windows管理员身份运行),然后依次输入当前的JS文件目录。再输入生成输出压缩后JS的目录即可。

@ECHO OFF setlocal enabledelayedexpansion set source_path=%1 set target_dir=%2  IF [%1]==[] (        rem echo please input javascript file or directory        set /p  source_path=please input javascript file or directory: )  IF [%2]==[] (        rem echo please input output directory        set /p target_dir=please input output directory: )    rem source path exists? FOR %%i IN (%source_path%) DO SET FileAttrib=%%~ai  if "%FileAttrib%" equ ""   (            rem not found file attribute, source path not exist       echo source path ^(%source_path%^) doesn't exist       exit /b 0 )   ELSE   IF "%FileAttrib:~0,1%" equ "d" (       rem source path is directory and not end with , append  to source path       IF %source_path:~-1% neq  (             set source_path=%source_path%       )    )           echo source path is  %source_path%    rem target path exists? FOR %%i IN (%target_dir%) DO SET fa=%%~ai  IF   [%fa%]==[]   (     rem target path not exist, make it     mkdir %target_dir%        )    IF %target_dir:~-1% neq  (       rem append  to target path       set target_dir=%target_dir% )     echo target path is %target_dir%    IF [%FileAttrib:~0,1%]==[d] (       for /r %source_path% %%I in (*.js) do (         set file_name=%%~nI       set parent=%%~dpI       set target_parent=%target_dir%!parent:%source_path%=!       if not exist !target_parent!  mkdir !target_parent!       cd !target_parent!        if [!file_name:~-4!] neq [.min] (                    set w= uglifyjs %%I -m -c -O ascii_only=true -o   !target_parent!%%~nI.min.js              rem uglify .js file             echo uglifyjs from "%%I" to "!target_parent!%%~nI.min.js"             start cmd /c  "!w!"       )  else (            rem copy min.js file            echo copy file from "%%~dpnI.js" to "!target_parent!%%~nI.js"             start cmd /c "copy  %%~dpnI.js  !target_parent!%%~nI.js"        )             )         ) else (     for %%I in (%source_path%) do (         IF    "%%~xI" EQU ".js"  (                  set file_name=%%~nI               if [!file_name:~-4!] neq [.min] (                      rem uglify .js file                   set val=%target_dir%%%~nI.min.js                          echo uglifyjs from "%%I" to "!val!"                   start cmd /c "uglifyjs  %%I  -m -c  -O ascii_only=true -o   !val!"                                  ) else (                   rem copy min.js file                   echo copy file from "%%I" to "%target_dir%%%~nI.js"                   start cmd /c "copy  %%I %target_dir%%%~nI.js"                 )                  )     )        )    echo done  

v源码地址

https://github.com/toutouge/javademosecond/tree/master/hellolearn

作  者:请叫我头头哥
出  处:http://www.cnblogs.com/toutou/
关于作者:专注于基础平台的项目开发。如有问题或建议,请多多赐教!
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
特此声明:所有评论和私信都会在第一时间回复。也欢迎园子的大大们指正错误,共同进步。或者直接私信
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是作者坚持原创和持续写作的最大动力!