首页 > 其他分享 >本地JS文件批量压缩

本地JS文件批量压缩

时间:2022-11-27 22:01:10浏览次数:71  
标签:target JS %% 压缩 js source file path 文件批量

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

标签:target,JS,%%,压缩,js,source,file,path,文件批量
From: https://www.cnblogs.com/toutou/p/js_min.html

相关文章

  • Microsoft & Node.js All In One
    Microsoft&Node.jsAllInOneNode.jsreadnestedfolderspathconstfs=require("fs").promises;constpath=require("path");constitems=awaitfs.readd......
  • ApiJSON简单使用示例
    1{2"[]":{3"query":2,4"User":{5"@column":"id,name"6},7"count":5,8"@order":......
  • JS的深浅拷贝
    基本数据类型:number、string、boolean、null、undefined引用数据类型:object、function、array存储位置的不同基本数据类型:将值存储在栈中,栈中存放的是对应的值......
  • js原型和原型链
    目录原型和原型链原型原型链原型和原型链原型在理解之前,明白一个前提,js也是有类的,也可以定义方法和构造函数隐式原型:对象的_proto_属性(里面有类定义的方法,且这个属性每......
  • jquery009-js执行的先后顺序
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title></head><bodystyle="width:980px;margin:0auto"><h1>#一,当页面框架......
  • 文本压缩归档及文本编辑器vi
    一、文件压缩与归档1.文件压缩gzip、bzip2命令用于文件压缩,压缩完成后源文件会消失,不能用于文件夹压缩常见命令选项:-q 压缩等级,0-9,值越大,压缩等级越高    ......
  • leetcode 56. 合并区间 js实现
    以数组 intervals 表示若干个区间的集合,其中单个区间为 intervals[i]=[starti,endi] 。请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入......
  • vulnhub靶场压缩文件解密
    fcrackzip爆破fcrackzip-D-p/usr/share/wordlists/rockyou.txt-usecr3tSteg.zipjohn爆破zip2johnsecr3tSteg.zip|teehash#转换为可识别的hashjohnhashdi......
  • vs2008 调试js
    面对一大段的JavaScript脚本,以前总是会很头疼,找不到调试这些代码的方法。如果出现什么错误或异常,总是要从头分析,然后插入很多Alert(),调试起来很麻烦。VisualStudio2008中J......
  • 解决fastjson内存对象相互应用导入json字符串出现错误问题
        日常在使用FastJson的时候可能很少会遇到这种问题。比如:我们在一个对象中存在一个集合属性对象这个集合属性对象元素属性也同样存在集合属性那么在使用toJSONSt......