var formPost = require('./tools/submit.js');
var gulp = require('gulp'),
zip = require('gulp-zip'),
htmlmin = require('gulp-htmlmin'),
cssmin = require('gulp-minify-css'),
imagemin = require('gulp-imagemin'),
pngquant = require('imagemin-pngquant'),
uglify = require('gulp-uglify'),
clean = require('gulp-clean');
cache = require('gulp-cache'),
replace = require('gulp-replace');
minimist = require('minimist'),
gulpif = require('gulp-if'),
stripDebug = require('gulp-strip-debug'),
gulpSequence = require('gulp-sequence'),
rev = require('gulp-rev'),
del = require('del'),
vinylPaths = require('vinyl-paths'),
rename = require('gulp-rename'),
//browserSync = require('browser-sync'),
revCollector = require('gulp-rev-collector'); var configInfo = require("./tools/config_develop.json");
var platformType = {
DEVELOP:1,
TEST:2,
PUBLISH:3
}; var knownOptions = {
number: ['ver',"env"],
default: {
ver: '0.2.0',
env:platformType.DEVELOP
}
};
var processoptions = minimist(process.argv.slice(2), knownOptions);
var zipFileName = function(){
return 'html5_'+configInfo.name +"_"+ processoptions.ver+'.zip';
};
gulp.task("clean", function(){
return gulp.src([configInfo.targetdirectory])
.pipe(clean());
}); gulp.task('minify:html', function () {
var options = {
removeComments: true,//清除HTML注释
collapseWhitespace: true,//压缩HTML
collapseBooleanAttributes: true,//省略布尔属性的值 <input checked="true"/> ==> <input />
removeEmptyAttributes: true,//删除所有空格作属性值 <input id="" /> ==> <input />
removeScriptTypeAttributes: true,//删除<script>的type="text/javascript"
removeStyleLinkTypeAttributes: true,//删除<style>和<link>的type="text/css"
minifyJS: true,//压缩页面JS
minifyCSS: true//压缩页面CSS
};
return gulp.src(['html/*.html', 'index.html', 'error404.html'],{ base: "." })
// .pipe(stripDebug())
.pipe(htmlmin(options))
.pipe(gulp.dest(configInfo.targetdirectorywww));
}); gulp.task('minify:css', function () {
return gulp.src(['css/*.css','!css/mui.css'])
.pipe(cssmin())
.pipe(gulp.dest(configInfo.targetdirectorywww + 'css'));
}); gulp.task('minify:image', function () {
return gulp.src(['img/*.{png,jpg,gif}', "img/**/*.{png,jpg,gif}"])
.pipe(imagemin({
// use: [pngquant({quality: '65-80'})],
optimizationLevel: 5, //类型:Number 默认:3 取值范围:0-7(优化等级)
progressive: true, //类型:Boolean 默认:false 无损压缩jpg图片
interlaced: true, //类型:Boolean 默认:false 隔行扫描gif进行渲染
multipass: true ,//类型:Boolean 默认:false 多次优化svg直到完全优化
use: [pngquant()],
}))
.pipe(gulp.dest(configInfo.targetdirectorywww + 'img'));
}); gulp.task('minify:js', function () {
return gulp.src(['js/*.js', 'js/**/*.js', '!js/config.js', '!js/libs/mui.js'])
.pipe(stripDebug())
.pipe(uglify({
// mangle: true,//类型:Boolean 默认:true 是否修改变量名
// mangle: {except: ['require' ,'exports' ,'module' ,'$']}//排除混淆关键字
// compress: true,//类型:Boolean 默认:true 是否完全压缩
// preserveComments: 'all' //保留所有注释
}))
.pipe(gulp.dest(configInfo.targetdirectorywww + 'js'));
}); gulp.task('copy:files', function () {
return gulp.src(['manifest.json', "fonts/**", "!fonts/iconfont.ttf"], {base: "."})
.pipe(gulp.dest(configInfo.targetdirectorywww));
});
gulp.task('copy:filesall', function(){
return gulp.src(['js/**/*','!js/config.js','html/*.html','css/*','img/**/*','index.html',"error404.html",'manifest.json', "fonts/**"], {base: "."})
.pipe(gulp.dest(configInfo.targetdirectorywww));
});
gulp.task('replace:dev', function () {
return gulp.src(["js/config.js"])
.pipe(gulp.dest(configInfo.targetdirectorywww + "js"));
});
gulp.task('replace:test', function () {
return gulp.src(["js/config.js"])
.pipe(uglify())
.pipe(replace(new RegExp(/IP:"(http|https):\/\/([\w-]+\.)+[\w-]+(\/[\w-./?%&=]*)?"/), 'IP:"http://172.18.0.6/"'))
.pipe(replace(new RegExp(/IP_CHAT:"(http|https):\/\/([\w-]+\.)+[\w-]+(\/[\w-./?%&=]*)?"/), 'IP_CHAT:"http://172.18.0.6/"'))
.pipe(replace(new RegExp(/IP_FILE:"(http|https):\/\/([\w-]+\.)+[\w-]+(\/[\w-./?%&=]*)?"/), 'IP_FILE:"http://172.18.0.6/"'))
.pipe(replace(new RegExp(/,CHAT:"shengqi-api\/chat\/"/), ',CHAT:"chat/"'))
.pipe(gulp.dest(configInfo.targetdirectorywww + "js"));
}); gulp.task('replace:publish', function () {
return gulp.src(["js/config.js"])
.pipe(uglify())
.pipe(replace(new RegExp(/IP:"(http|https):\/\/([\w-]+\.)+[\w-]+(\/[\w-./?%&=]*)?"/), 'IP:"http://sqapp.miligames.com/"'))
.pipe(replace(new RegExp(/IP_CHAT:"(http|https):\/\/([\w-]+\.)+[\w-]+(\/[\w-./?%&=]*)?"/), 'IP_CHAT:"http://sqapp.miligames.com/"'))
.pipe(replace(new RegExp(/IP_FILE:"(http|https):\/\/([\w-]+\.)+[\w-]+(\/[\w-./?%&=]*)?"/), 'IP_FILE:"http://sqapp.miligames.com/"'))
.pipe(replace(new RegExp(/,CHAT:"shengqi-api\/chat\/"/), ',CHAT:"chat/"'))
.pipe(gulp.dest(configInfo.targetdirectorywww + "js"));
}); gulp.task('build:version', function(){
return gulp.src(["version.txt"])
.pipe(replace(new RegExp(/.*/), processoptions.ver))
.pipe(gulp.dest(configInfo.targetdirectorywww));
}); gulp.task('upload', function(){
formPost.postFormInfo(configInfo.uploadip, processoptions.ver,configInfo.targetzip + "/"+zipFileName(), configInfo);
}); gulp.task('archive', function () {
return gulp.src(configInfo.targetdirectory + '**')
.pipe(zip(zipFileName()))
.pipe(gulp.dest(configInfo.targetzip));
}); gulp.task('build:dev', gulpSequence(["clean"], ['copy:filesall', "build:version", "replace:dev"], ["archive"], ["upload"]));
//gulp.task('build:test', gulpSequence(["clean"], ["minify:html", 'minify:css','minify:js','minify:image','copy:files', 'replace:test',"build:version"], ["archive"], ["upload"]));
gulp.task('build:test', gulpSequence(["clean"], ['copy:filesall', 'replace:test',"build:version"], ["archive"], ["upload"]));
gulp.task('build:publish', gulpSequence(["clean"], ["minify:html", 'minify:css','minify:js','minify:image','copy:files', 'replace:publish',"build:version"], ["archive"], ["upload"])); gulp.task("app-build", function(){
switch (processoptions.env) {
case platformType.DEVELOP:
configInfo = require("./tools/config_develop.json");
//gulp.start("build:dev");
gulp.start("b:dev");
break;
case platformType.TEST:
configInfo = require("./tools/config_test.json");
//gulp.start("build:test");
gulp.start("b:test");
break;
case platformType.PUBLISH:
configInfo = require("./tools/config_publish.json");
//gulp.start("build:publish");
gulp.start("b:publish");
break;
}
});
//=========================================================================================================
var cssSrc = 'css/**/app*.css'; gulp.task('mfy:image', function () {
return gulp.src(['img/*.{png,jpg,gif}', "img/**/*.{png,jpg,gif}"])
.pipe(imagemin({
// use: [pngquant({quality: '65-80'})],
optimizationLevel: 5, //类型:Number 默认:3 取值范围:0-7(优化等级)
progressive: true, //类型:Boolean 默认:false 无损压缩jpg图片
interlaced: true, //类型:Boolean 默认:false 隔行扫描gif进行渲染
multipass: true ,//类型:Boolean 默认:false 多次优化svg直到完全优化
use: [pngquant()],
}))
.pipe(gulp.dest(configInfo.targetdirectorywww + 'img'));
}); gulp.task('css_rev', function() {
return gulp.src([cssSrc, 'css/im_chat.css', '!css/mui*.css'])
.pipe(rename(function (path) {
//path.basename += ".min";
path.extname = ".css"
}))
.pipe(rev())
.pipe(gulpif(processoptions.env == platformType.PUBLISH, cssmin()))
.pipe(gulp.dest("css2"))
.pipe(rev.manifest())
.pipe(gulp.dest("rev/css"));
}); gulp.task('js_rev', function() {
return gulp.src(['js/*.js', 'js/**/*.js', '!js/cursormanager.js', '!js/config.js', '!js/libs/*.js'], {base: "."})
.pipe(rename(function (path) {
//path.basename += ".min";
path.extname = ".js"
}))
.pipe(rev())
.pipe(gulpif(processoptions.env == platformType.PUBLISH, uglify({})))
.pipe(gulp.dest("js2"))
.pipe(rev.manifest())
.pipe(gulp.dest("rev/js"));
}); gulp.task('replace_js_rev', ['replace_css_rev', 'js_rev'], function() {
gulp.src(["rev/js/**/*.json", 'html2/*.html'])
.pipe(revCollector({
replaceReved: true
}))
.pipe(gulp.dest("html3")); return gulp.src(["rev/js/**/*.json", 'html2/html/*.html'])
.pipe(revCollector({
replaceReved: true
}))
.pipe(gulp.dest("html3/html"));
}); gulp.task('replace_css_rev', ['css_rev'], function() {
gulp.src(["rev/css/**/*.json", '*.html'])
.pipe(revCollector({
replaceReved: true
}))
.pipe(gulp.dest("html2")); return gulp.src(["rev/css/**/*.json", 'html/*.html'])
.pipe(revCollector({
replaceReved: true
}))
.pipe(gulp.dest("html2/html"));
}); gulp.task('cp:html', ['replace_js_rev'], function() {
var options = {
removeComments: true,//清除HTML注释
collapseWhitespace: true,//压缩HTML
collapseBooleanAttributes: true,//省略布尔属性的值 <input checked="true"/> ==> <input />
removeEmptyAttributes: true,//删除所有空格作属性值 <input id="" /> ==> <input />
removeScriptTypeAttributes: true,//删除<script>的type="text/javascript"
removeStyleLinkTypeAttributes: true,//删除<style>和<link>的type="text/css"
minifyJS: true,//压缩页面JS
minifyCSS: true//压缩页面CSS
}; gulp.src(['html3/html/*.html'], {base: "."})
.pipe(rename({dirname: ''})).pipe(gulpif(processoptions.env == platformType.PUBLISH, htmlmin(options)))
.pipe(gulp.dest(configInfo.targetdirectorywww + "html")); return gulp.src(['html3/*.html'])
.pipe(rename({dirname: ''})).pipe(gulpif(processoptions.env == platformType.PUBLISH, htmlmin(options)))
.pipe(gulp.dest(configInfo.targetdirectorywww));
}); gulp.task('cp:files', ['cp:reved_css','replace_css_rev'], function () {
return gulp.src(['manifest.json', 'css/mui*.css', "fonts/**", "!fonts/iconfont.ttf"], {base: "."})
.pipe(gulp.dest(configInfo.targetdirectorywww));
}); gulp.task('cp:filesall', ['cp:reved_css','replace_css_rev'], function(){
return gulp.src(['!js/config.js','css/mui*.css','img/**/*',"error404.html",'manifest.json', "fonts/**"], {base: "."})
.pipe(gulp.dest(configInfo.targetdirectorywww));
}); gulp.task('cp:unreved_js', ['js_rev'], function () {
gulp.src(['js/libs/*.js'])
.pipe(stripDebug())
.pipe(gulpif(processoptions.env == platformType.PUBLISH, uglify({})))
.pipe(gulp.dest(configInfo.targetdirectorywww + 'js/libs')); return gulp.src(['js/cursormanager.js'])
.pipe(stripDebug())
.pipe(gulpif(processoptions.env == platformType.PUBLISH, uglify({})))
.pipe(gulp.dest(configInfo.targetdirectorywww + 'js'));
}); gulp.task('cp:reved_js', ['js_rev'], function() {
return gulp.src(['js2/**/*.js'])
.pipe(gulp.dest(configInfo.targetdirectorywww));
}); gulp.task('cp:reved_css', ['css_rev'], function() {
return gulp.src(['css2/*'])
.pipe(rename({dirname: ''}))
.pipe(gulp.dest(configInfo.targetdirectorywww + "css/"));
}); gulp.task("cl:tmp", function(){
//gulp.src([configInfo.targetdirectorywww + "html/*.html"]).pipe(vinylPaths(del));
return gulp.src(['js2','css2','html2', 'html3', 'rev'])
.pipe(clean());
}); gulp.task('b:dev', gulpSequence(["clean"], ['cp:filesall', 'cp:unreved_js','cp:html', 'cp:reved_js', "build:version", "replace:dev"], ["archive"], ["upload"],['cl:tmp']));
gulp.task('b:publish', gulpSequence(["clean"], ['cp:unreved_js','cp:html','cp:reved_js','mfy:image','cp:files', 'replace:publish',"build:version"], ["archive"],['cl:tmp']));
gulp.task('b:test', gulpSequence(["clean"], ['cp:filesall', 'cp:unreved_js','cp:html', 'cp:reved_js', 'replace:test',"build:version"], ["archive"],["upload"],['cl:tmp'])); //gulp.task('browser-sync', function () {
// var files = [
// './**/*.html',
// './css/**/*.css',
// './imgs/**/*.png',
// './js/**/*.js'
// ]; // browserSync.init(files, {
// server: {
// baseDir: './'
// }
// });
//});

gulp生成发布包脚本的更多相关文章

  1. 给ASP.NET Core Web发布包做减法

    1.引言 紧接上篇:ASP.NET Core Web App应用第三方Bootstrap模板.这一节我们来讲讲如何优化ASP.NET Core Web发布包繁重的问题. 在ASP.NET Core W ...

  2. 给控件做数字签名之一:将控件打包为Web发布包 [转]

    微软代码签名证书使用指南 http://www.wotrust.com/support/signcode_guide.htm 签名重要性:http://www.wotrust.com/FAQ/whyS ...

  3. NET Core Web发布包

    给ASP.NET Core Web发布包做减法 https://www.cnblogs.com/sheng-jie/p/9122582.html 1.引言 紧接上篇:ASP.NET Core Web ...

  4. Flutter:发布包

    [package] 生成包含模块化Dart代码的可共享Flutter项目 [plugin] 生成一个可共享的Flutter项目, 在Dart代码中包含带有API的API, 针对Android的平台特定 ...

  5. npm ERR publish 403,nodejs发布包流程

    nodejs学习体验之发布包,发布环境如下:1:win10系统,2:已安装nodejs. 具体操作步骤如下: *编写模块 1)新建文件夹,比如:somepackage 2) 该文件夹下新建js文件,比 ...

  6. Gulp解决发布线上文件(CSS和JS)缓存问题

    Gulp解决发布线上文件(CSS和JS)缓存问题 本文的缘由:目前经常线上发布文件后要不断的刷新页面及过很长时间,页面上的CSS和JS文件才能生效,特别对于目前做微信商城的时候,微信内置的浏览器缓存非 ...

  7. npm发布包--所遇到的问题

    npm发布包: 解决方案--npm adduser的坑:http://www.tuicool.com/articles/FZbYve npm ERR publish 403,nodejs发布包流程 : ...

  8. python发布包到pypi的踩坑记录

    前言 突然想玩玩python了^_^ 这篇博文记录了我打算发布包到pypi的踩坑经历.python更新太快了,甚至连这种发布上传机制都在不断的更新,这导致网上的一些关于python发布上传到pypi的 ...

  9. npm学习(七)之如何发布包、更新发布包、删除发布包

    前言 我们经常使用npm来下载别人的模块或者说包,那么我们如何将自己写的模块上传到npm呢? 了解npm政策 在开始之前,最好回顾一下npm的政策,以防您对站点礼仪.命名.许可或其他指导原则有疑问. ...

随机推荐

  1. git fetch , git pull

    要讲清楚git fetch,git pull,必须要附加讲清楚git remote,git merge .远程repo, branch . commit-id 以及 FETCH_HEAD. 1. [g ...

  2. linux 系统安装mysql (rpm)

    其实按照本文安装成功,但是启动依然有问题:最好参考链接配置. http://blog.csdn.net/xiaoxiaoxuewen/article/details/7550107 我用的是ubunt ...

  3. python unittest 1

    今天开始研究python的unittest模块,为我们当前做的项目定制测试方案,包括unittest和自动化流程测试. python的unittest的基本使用方法如下:   1.import uni ...

  4. Python 中遍历序列中元素和下标

    enumerate 函数用于遍历序列中的元素以及它们的下标 for i,v in enumerate(['tic','tac','toe']): print i,v #0 tic #1 tac #2 ...

  5. webGL 光照

    1.着色(shading) 在三维图形学术语“着色”的真正含义就是,根据光照条件重建“物体各表面明暗不一的效果”的过程.明白着色过程,需要考虑两件事:    1.发出光线的光源类型.    2.物体表 ...

  6. 求字符串长度StringLength();

    // StringLength2.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" int StringLength(char str[]) ...

  7. Ubuntu18.10安装网易云音乐(图文并茂!)

    听音乐,怎么少得了网易云音乐,下面我们在Ubuntu18.10上来安装下: 首先进入网易云音乐的下载页:https://music.163.com/#/download,选择下载Ubuntu版本: 我 ...

  8. 5种实现垂直居中css

    摘要: 在我们制作页面的时候经常会遇到内容垂直居中的需求,今天分享5种垂直居中的方法,每种方法都有自己的优缺点,可以选择自己喜欢的方式.以下代码都经过本人亲自测试. line-height: < ...

  9. SpringMVC由浅入深day02_1课程安排_2包装类型pojo参数绑定_3集合类型绑定

    springmvc第二天 高级知识 复习: springmvc框架: DispatcherServlet前端控制器:接收request,进行response HandlerMapping处理器映射器: ...

  10. Windows最全快捷键

    单独按Windows:显示或隐藏“开始”功能表 Windows+BREAK:显示“系统属性” 对话框 Windows+D:显示桌面 Windows+M:最小化所有窗口 Windows+Shift+M: ...