var c = {
rootPath: 'src',//项目文件夹
outputPath: 'output',//文件编译后的输出目录
revPath: 'manifest',//rev映射文件目录
appjs: 'app.js',// run JS
homePage: 'index.html',
cssFolderPaths: ['css'],//需要压缩的CSS目录
jsFolderPaths: ['model'],//需要压缩的JS目录
ngjsFolderPaths: ['app', 'common', 'directives', 'filters'],//需要压缩的ngJS目录
htmlFolderPaths: ['app', 'layout'],
filterFolderPaths: ['frameworks', 'images']
}; var root = function (path) {
if (path)
return c.rootPath + '/' + path
return c.rootPath;
}; var output = function (path) {
if (path)
return c.outputPath + '/' + path
return c.outputPath;
} var fs = require('fs');
var path = require('path');
var gulp = require("gulp"),
clean = require('gulp-clean'),
ngAnnotate = require('gulp-ng-annotate'),//压缩ngjs
util = require('gulp-util'),
filter = require('gulp-filter'),
ngmin = require('gulp-ngmin'),
stripDebug = require('gulp-strip-debug'),
minifyhtml = require("gulp-minify-html"),//压缩html
minify = require("gulp-minify-css"),//压缩css
uglify = require("gulp-uglify"),//压缩代码
rename = require("gulp-rename"),//重命名
concat = require("gulp-concat"),//合并代码
sourcemaps = require("gulp-sourcemaps"),
rev = require("gulp-rev"),//对文件名加MD5后缀
revReplace = require("gulp-rev-replace"),//路径替换
merge = require("merge-stream"); //入口命令
gulp.task('run', ['clear'],
function () {
setTimeout(function () {
gulp.run([
'filter',
'css',
'html',
'global',
'model',
'ngjs'], function () {
var manifest = gulp.src("./" + c.revPath + '/*.json');
return gulp.src([root(c.homePage)])
.pipe(revReplace({
manifest: manifest
}))
.pipe(gulp.dest(c.outputPath));
});
}, 2000);
}); //所有不需要压缩及合并的目录
gulp.task('filter', function () {
return c.filterFolderPaths.map(function (cpath) {
return gulp.src([root(cpath + '/**')])
.pipe(gulp.dest(output(cpath)));
});
}); //压缩css
gulp.task('css', function () {
var tasks = c.cssFolderPaths.map(function (cpath) {
return gulp.src([root(cpath + '/**/*.css')])
.pipe(minify())
.pipe(rev())
.pipe(gulp.dest(output(cpath)))
.pipe(rev.manifest(cpath + '.json'))
.pipe(gulp.dest('./' + c.revPath));
});
return merge(tasks);
}); //压缩html
gulp.task('html', function () {
var tasks = c.htmlFolderPaths.map(function (cpath) {
return gulp.src([root(cpath + '/**/*.html')])
.pipe(minifyhtml())
.pipe(gulp.dest(output(cpath)));
});
return merge(tasks);
}); //压缩global js文件,并替换index.html中的引用
gulp.task('global', function () {
return gulp.src([root('/*.js')])
.pipe(ngAnnotate())
.pipe(ngmin({ dynamic: false }))
.pipe(stripDebug())
.pipe(uglify({ outSourceMap: false }))//压缩ngjs
// .pipe(concat(c.appjs))
.pipe(rev()) //文件名加MD5后缀
.pipe(gulp.dest(c.outputPath))
.pipe(rev.manifest('global.json'))//生成manifest.json
.pipe(gulp.dest('./' + c.revPath));
}); //压缩model
gulp.task('model', function () {
var tasks = c.jsFolderPaths.map(function (cpath) {
return gulp.src([root(cpath + '/**/*.js')])
.pipe(uglify())
.pipe(rev())
.pipe(gulp.dest(output(cpath)))
.pipe(rev.manifest(cpath + '.json'))
.pipe(gulp.dest('./' + c.revPath));
});
return merge(tasks);
}); //压缩ngjs
gulp.task('ngjs', function () {
var tasks = c.ngjsFolderPaths.map(function (cpath) {
return gulp.src([root(cpath + '/**/*.js')])
.pipe(uglify())
.pipe(rename(function (path) {
if (path.basename.toLowerCase().indexOf('login') > -1) {
path.basename = path.basename.toLowerCase();
}
return path;
}))
.pipe(rev())
.pipe(gulp.dest(output(cpath)))
.pipe(rev.manifest(cpath + '.json'))
.pipe(gulp.dest('./' + c.revPath));
});
return merge(tasks);
}); //清理目录
gulp.task('clear', function () {
gulp.src(c.outputPath, { read: false })
.pipe(clean());
gulp.src(c.revPath, { read: false })
.pipe(clean());
gulp.src(['dist', 'rev'], { read: false })
.pipe(clean());
//gulp.src(root('/**/*.*scc'), { read: false })
// .pipe(clean());
});

  

记录下自己写的gulp打包脚本的更多相关文章

  1. Linux下Maven+SVN自动打包脚本

        公司的开发环境每次部署项目都很麻烦,需要手动打包并上传上去.这个太麻烦了,所以就准备搞个自动打包的脚本.脚本自动从svn代码库里面更新最新的代码下来,然后maven打包,最后把war包丢到to ...

  2. gulp 粗粗学习 记录下

    看视频学习 粗粗记录下 以便以后学习 1.初记录 gulp.task //定义一个任务 gulp.src //锁定到做task任务的文件路径 gulp.dest //锁定到任务做完后文件去向的路径 g ...

  3. xcode8.3 shell 自动打包脚本 记录

    题记 xcode升级8.3后发现之前所用的xcode自动打包基本无法使用,因此在网上零碎找到些资料,将之前的脚本简化.此次脚本是基于xcode证书配置进行打包(之前是指定描述文件.相对繁琐).因此代码 ...

  4. webpack打包和gulp打包工具详细教程

    30分钟手把手教你学webpack实战 阅读目录 一:什么是webpack? 他有什么优点? 二:如何安装和配置 三:理解webpack加载器 四:理解less-loader加载器的使用 五:理解ba ...

  5. gulp打包详解

    gulp的作用 删除文件中冗余的内容,压缩文件,减小文件体积 实际项目中运行的都是压缩完成以后的文件 减小加载响应时间 gulp打包压缩对象 html,css,js,sass,webserver 音频 ...

  6. vmware虚拟机下ubuntu 13.04使用zeranoe脚本交叉编译ffmpeg

    2013-07-01今天是建党节,习总书记指出,党的建设要以“照镜子.正衣冠.洗洗澡.治治病”为总要求.希望我们的党越来越纯洁,为人民谋福利.言归正传,每次项目中需要编译相应的ffmpeg,都很费时费 ...

  7. 使用linux下的crontab定时任务跑定时脚本

    使用linux下的crontab定时任务跑定时脚本 tags:定时任务 定时脚本 crontab linux定时脚本 linux 引言:应该有许多人曾经很好奇一些定时脚本是怎么做出来的.我们这次就来说 ...

  8. 使用gulp打包普通项目

    前言: 在使用gulp打包工具之前,我做的H5项目在浏览器中的缓存是很严重的,若改了一点css,加了一句js代码,不手动清除浏览器缓存是看不到效果的.老总也在项目演示当中遇到这些问题,一查找原因却是缓 ...

  9. assetBundle打包脚本与LUA

    AssetBundles与脚本 所有Unity的AssetBundle,无论是从本地获取 还是www,或者打包整个场景.物体上的脚本都不会被编译.所以AssetBundle打包的时候即使物体上有脚本. ...

随机推荐

  1. ZOJ 2724 Windows 消息队列 (优先队列)

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2724 Message queue is the basic fund ...

  2. Cocos2d-x数据持久-变更数据

    当数据变化,参与SQL报表insert.update和delete声明.这项3个月SQL语句可以带参数. 详细过程的数据,例如,下面的变化看出.(1) 采用sqlite3_open开放式数据库功能.( ...

  3. 深入struts2(三)---工作机制和运行流程图

    1     工作原理 1.1     体系架构 图2.1 struts2.0体系架构图 1.2     工作机制 针对上节体系架构图,以下分步说明运行流程 Ø  client初始化一个指向Servle ...

  4. .net mvc ajax list post

    http://stackoverflow.com/questions/13242414/passing-a-list-of-objects-into-an-mvc-controller-method- ...

  5. jQuery UI dialog 參数说明

    前段时间碰到个问题 jquery UI dialog弹出层 弹出多个层是 比方弹出两个层A和B  B层假设显示的数据表格太大,伸到了A层的外面,那伸到A层之外的部分就看不到了,由于B层是在A层上弹出的 ...

  6. 【视频】零基础学Android开发:蓝牙聊天室APP(二)

    零基础学Android开发:蓝牙聊天室APP第二讲 2.1 课程内容应用场景 2.2 Android UI设计 2.3 组件布局:LinearLayout和RelativeLayout 2.4 Tex ...

  7. centos安装zabbix集群监控(亲测无坑版)

    一. 安装lemp环境 下载安装包:wget bbs.linuxtone.org/docs/autoinstall/lemp_auto_v1.0.6.tar.gz 包解压:tar zxvf lemp_ ...

  8. 安卓MonkeyRunner源码分析之与Android设备通讯方式

    如前文<谁动了我的截图?--Monkeyrunner takeSnapshot方法源码跟踪分析>所述,本文主要会尝试描述android的自动化测试框架MonkeyRunner究竟是如何和目 ...

  9. monkey源码分析之事件注入方法变化

    在上一篇文章<Monkey源码分析之事件注入>中,我们看到了monkey在注入事件的时候用到了<Monkey源码分析番外篇之Android注入事件的三种方法比较>中的第一种方法 ...

  10. yii中登录后跳转回登录前请求的页面

    当我们请求一个经过权限控制的请求不通过时,会跳转到一个地方请求权限,请求结束后需要跳转回之前的页面.比如我们请求一个需要登录的action,会被跳转到login页面,我们希望登录成功后跳转到我们之前希 ...