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%^) doesnt 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/
关于作者:专注于基础平台的项目开发 。如有问题或建议 ,请多多赐教!
版权声明:本文版权归作者和博客园共有 ,欢迎转载,但未经作者同意必须保留此段声明 ,且在文章页面明显位置给出原文链接 。
特此声明:所有评论和私信都会在第一时间回复 。也欢迎园子的大大们指正错误 ,共同进步 。或者直接私信我
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角
【推荐】
一下。您的鼓励是作者坚持原创和持续写作的最大动力!
声明:本站所有文章 ,如无特殊说明或标注 ,均为本站原创发布 。任何个人或组织,在未征得本站同意时 ,禁止复制 、盗用 、采集 、发布本站内容到任何网站 、书籍等各类媒体平台 。如若本站内容侵犯了原著者的合法权益 ,可联系我们进行处理。