forked from ss-zz/ionic_1x_seed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
129 lines (125 loc) · 3.37 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// 前端构建文件
var gulp = require('gulp');
// 文件合并
var concat = require('gulp-concat');
// 重命名
var rename = require('gulp-rename');
// js混淆
var uglify = require('gulp-uglify');
// 清除目录
var clean = require('gulp-clean');
// angularjs模板缓存
var templateCache = require('gulp-angular-templatecache');
// 页面引用文件合并
var useref = require('gulp-useref');
// if判断
var gulpIf = require('gulp-if');
// css压缩
var cleanCss = require('gulp-clean-css');
// 顺序执行任务
var runSequence = require('run-sequence');
// 图片压缩
var imagemin = require('gulp-imagemin');
// 缓存
var cache = require('gulp-cache');
// 自动添加angularjs依赖注入
var ngAnnotate = require('gulp-ng-annotate');
// 清除调试信息
var stripDebug = require('gulp-strip-debug');
// 打压缩包
var zip = require('gulp-zip');
// 配置项
var paths = {
build: 'www',
pub: 'pub',
src_js: ['src/js/**/*.js'],
src_css: ['src/css/**/*.css'],
src_img: ['src/img/**/*'],
src_pub: ['src/pub/**/*', 'js_lib/ngCordova/dist/ng-cordova.min.js'],
src_font: ['js_lib/ionic/fonts/*.*'],
src_template: ['src/js/**/*.html'],
src_index_html: 'src/index.html',
www_temp: ['www/app.min.js', 'www/templates.js']
};
// 默认任务
gulp.task('default', ['build']);
// 压缩所有相关文件
gulp.task('build', function (){
return runSequence(
"minifyJs", "minifyImg", "copyFont", 'copyPub',
"templatecache", "useref", 'cleanTemp');
});
// js 合并压缩
gulp.task('minifyJs', function (done){
return gulp.src(paths.src_js)
.pipe(ngAnnotate())
//.pipe(stripDebug())
.pipe(uglify({outSourceMap: false}))
.pipe(concat('app.min.js'))
.pipe(gulp.dest(paths.build))
;
});
// 图片压缩
gulp.task('minifyImg', function (done){
return gulp.src(paths.src_img)
.pipe(cache(imagemin({optimizationLevel: 3, progressive: true, interlaced: true })))
.pipe(gulp.dest(paths.build + "/img"))
;
});
// 拷贝字体文件
gulp.task('copyFont', function (done){
return gulp.src(paths.src_font)
.pipe(gulp.dest(paths.build + "/fonts"))
;
});
// 拷贝pub目录(其它资源文件)
gulp.task('copyPub', function (done){
return gulp.src(paths.src_pub)
.pipe(gulp.dest(paths.build + "/pub"));
;
});
// 模板文件压缩
gulp.task('templatecache', function (done) {
return gulp.src(paths.src_template)
.pipe(templateCache({standalone:true}))
.pipe(gulp.dest(paths.build))
;
});
// 将页面中引用的文件合并并压缩
gulp.task('useref', function (done) {
return gulp.src(paths.src_index_html)
.pipe(useref())
.pipe(gulpIf('*.css', cleanCss()))
.pipe(gulp.dest(paths.build))
;
});
// 监控
gulp.task('watch', function() {
gulp.watch(paths.src_js, ['build']);
gulp.watch(paths.src_template, ['build']);
gulp.watch(paths.src_img, ['build']);
gulp.watch(paths.src_index_html, ['build']);
});
// 清空发布
gulp.task('clean', function () {
return gulp.src(paths.build, {read: false})
.pipe(clean());
});
// 删除临时文件-自定义
gulp.task('cleanTemp', function () {
return gulp.src(paths.www_temp, {read: false})
.pipe(clean());
});
// www目录压缩
gulp.task('zip', function () {
return gulp.src(paths.build + "/**/*")
.pipe(zip('www.zip'))
.pipe(gulp.dest(paths.pub))
;
});
// web方式发布
gulp.task('pub', function () {
return runSequence(
"minifyJs", "minifyImg", "copyFont", 'copyPub',
"templatecache", "useref", 'cleanTemp', 'zip');
});