记录下自己写的gulp打包脚本
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打包脚本的更多相关文章
- Linux下Maven+SVN自动打包脚本
公司的开发环境每次部署项目都很麻烦,需要手动打包并上传上去.这个太麻烦了,所以就准备搞个自动打包的脚本.脚本自动从svn代码库里面更新最新的代码下来,然后maven打包,最后把war包丢到to ...
- gulp 粗粗学习 记录下
看视频学习 粗粗记录下 以便以后学习 1.初记录 gulp.task //定义一个任务 gulp.src //锁定到做task任务的文件路径 gulp.dest //锁定到任务做完后文件去向的路径 g ...
- xcode8.3 shell 自动打包脚本 记录
题记 xcode升级8.3后发现之前所用的xcode自动打包基本无法使用,因此在网上零碎找到些资料,将之前的脚本简化.此次脚本是基于xcode证书配置进行打包(之前是指定描述文件.相对繁琐).因此代码 ...
- webpack打包和gulp打包工具详细教程
30分钟手把手教你学webpack实战 阅读目录 一:什么是webpack? 他有什么优点? 二:如何安装和配置 三:理解webpack加载器 四:理解less-loader加载器的使用 五:理解ba ...
- gulp打包详解
gulp的作用 删除文件中冗余的内容,压缩文件,减小文件体积 实际项目中运行的都是压缩完成以后的文件 减小加载响应时间 gulp打包压缩对象 html,css,js,sass,webserver 音频 ...
- vmware虚拟机下ubuntu 13.04使用zeranoe脚本交叉编译ffmpeg
2013-07-01今天是建党节,习总书记指出,党的建设要以“照镜子.正衣冠.洗洗澡.治治病”为总要求.希望我们的党越来越纯洁,为人民谋福利.言归正传,每次项目中需要编译相应的ffmpeg,都很费时费 ...
- 使用linux下的crontab定时任务跑定时脚本
使用linux下的crontab定时任务跑定时脚本 tags:定时任务 定时脚本 crontab linux定时脚本 linux 引言:应该有许多人曾经很好奇一些定时脚本是怎么做出来的.我们这次就来说 ...
- 使用gulp打包普通项目
前言: 在使用gulp打包工具之前,我做的H5项目在浏览器中的缓存是很严重的,若改了一点css,加了一句js代码,不手动清除浏览器缓存是看不到效果的.老总也在项目演示当中遇到这些问题,一查找原因却是缓 ...
- assetBundle打包脚本与LUA
AssetBundles与脚本 所有Unity的AssetBundle,无论是从本地获取 还是www,或者打包整个场景.物体上的脚本都不会被编译.所以AssetBundle打包的时候即使物体上有脚本. ...
随机推荐
- C#中ISpostback
响应客户端控件时ispostback为true 代码: using System; using System.Collections.Generic; using System.Linq; using ...
- SSAS系列——【05】多维数据(编程体系结构)
原文:SSAS系列--[05]多维数据(编程体系结构) 1.什么是AMO? 翻译:AMO是SSAS中一个完整的管理类集合,它在Microsoft.AnalysisServices命名空间下,我们可以在 ...
- JS判断Array数组中是否包含指定元素
1.调用方式: var arr=["a","b"]; alert(arr.in_array("a")) 2.JS判断数组是否包含指定元素方法 ...
- VS2013中实现angular代码智能提示
第一步:在项目同添加angular js文件的引用: 这里使用NuGet包管理器来给项目添加angular js install-package angularjs 第二步:添加智能提示js文件 我们 ...
- Pointers to classes (From the note of my firend)
Pointers to classes Objects can also be pointed to by pointers: Once declared, a class becomes a ...
- 为网上流行论点“UIAutomator不能通过中文文本查找控件”正名
1. 问题描述和起因 相信大家学习UIAutomator一开始的时候必然会看过一下这篇文章. Android自动化测试(UiAutomator)简要介绍 因为你在百度输入UIAutomator搜索的时 ...
- HDU 1256 图片8
图片8 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- 快速构建Windows 8风格应用13-SearchContract构建
原文:快速构建Windows 8风格应用13-SearchContract构建 本篇博文主要介绍如何在应用中构建SearchContract,相应的原理已经在博文<快速构建Windows 8风格 ...
- Ubuntu12.10无法安装openssh-server[已解决]
因为要在Ubuntu下搞些东西,家里的台式有Deepin2013,但是发现有很多依赖的问题,实在不想解决,就到win7下用VBox安装了Ubuntu.打算使用SourceCRT连接虚拟机,但是在安装在 ...
- LinQ—扩展方法
概述 本节主要解说扩展方法,涉及LinQ的详细知识不多. 扩展方法的描写叙述 .net framework为编程人员提供了非常多的类,非常多的方法,可是,不论.net framework在类中为我们提 ...