gulp生成发布包脚本

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: './'
// }
// });
//});
原文地址:https://www.cnblogs.com/yuanxiaoping_21cn_com/p/5796375.html