-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
275 lines (268 loc) · 7.63 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
* Created by wangjunkai on 2016/4/15.
*/
/*引入gulp*/
// 基础库
var gulp = require('gulp');
//脚本压缩
var uglify = require('gulp-uglify');
//css压缩
var minify = require('gulp-minify-css');
//css雪碧图
var imageResize = require('gulp-image-resize');
var sprite = require('gulp.spritesmith');
var gm = require('gulp-gm');
var parallel = require("concurrent-transform");
var os = require("os");
//图片压缩&&深度压缩
var imagemin = require('gulp-imagemin'),
pngquant = require('imagemin-pngquant');
//js检查
var jshint = require('gulp-jshint'),
stylish = require('jshint-stylish');
// 网页自动刷新
var livereload = require('gulp-livereload');
//文件重命名
var rename = require('gulp-rename');
var rev = require('gulp-rev');
//来源地图
var sourcemaps = require('gulp-sourcemaps');
//只操作修改过的文件
var changed = require('gulp-changed');
//合并文件
var concat = require('gulp-concat');
//清理文件
var clean = require('gulp-clean');
//处理css3前缀
var autoprefixer = require('gulp-autoprefixer');
//文件的路径(./代表根目录)
var paths = {
all: '*',
img_sprite: 'img/sprite/*.png',
img: 'img/*',
js: 'js/*.js',
css: 'www/css/*.css',
dist: 'dist',
dist_js: 'dist/js',
dist_css: 'www/css/dist/*.min.css',
dist_img: 'dist/img',
dist_maps: ''
};
var js_path = {
js_lib: 'www/lib/',
js_map: 'map/',
js_lib_dist: 'www/dist/js/lib/',
js_lib_dist_concat: 'www/dist/js/lib/concat/'
};
function getDate() {
var a = new Date();
var m = a.getMonth() + 1;
var month = (m < 10 ? '0' + m : m),
date = a.getDate() < 10 ? '0' + a.getDate() : a.getDate();
return a.getFullYear() + month.toString() + date.toString();
}
var css_path = {
css_map: 'map/',
css_modules: 'www/css/',
css_modules_dist: 'www/dist/css/modules/' + getDate() + '/',
css_modules_dist_concat: 'www/dist/css/modules/' + getDate() + '/concat/',
css_activities: 'www/activities/css/',
css_activities_dist: 'www/dist/css/activities/' + getDate() + '/',
css_activities_dist_concat: 'www/dist/css/activities/' + getDate() + '/concat/'
};
/*注册任务*/
//移动文件
gulp.task('move', function () {
return gulp.src('./*.html')
.pipe('dist/html');
});
//js检查
gulp.task('jshint', function () {
return gulp.src('www/lib/ionic/ionic.bundle.js')
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'))
.pipe(jshint.reporter('fail'));
});
//脚本压缩
function each_ary(rootPath, pathAry) {
var a = [];
if (rootPath) {
for (var i = 0; i < pathAry.length; i++) {
var str = rootPath + pathAry[i];
if (pathAry[i].substring(0, 1) === '!') {
str = '!' + rootPath + pathAry[i].substring(1);
}
a.push(str);
}
} else {
a = pathAry;
}
return a;
}
var requirejs = [
'requirejs/require.js'
];
var js_lib_ary = [
'ionic/js/ionic.bundle.js',
'jquery/dist/jquery.js',
'lodash/lodash.js',
'angular-local-storage/dist/angular-local-storage.js',
'ionic/js/angular/angular-sanitize.js',
'restangular/dist/restangular.js',
'oclazyload/dist/ocLazyLoad.js',
'ionic-image-lazy-load/ionic-image-lazy-load.js',
'qrcode/jquery.qrcode.min.js',
'base64/base64.js'
];
var css_modules_ary = [
'freexf.css', 'home.css', 'user.css', 'course.css', 'member.css', 'set.css', 'pay.css'
];
var css_activities_ary = [
'!*.bundle.css',
'!french.css',
'!Germany.css',
'!japanese.css',
'!korean.css',
'!russian.css',
'!spanish.css',
'*.css'
];
gulp.task('uglify', function () {
return gulp.src(each_ary(js_path.js_lib, requirejs))
.pipe(changed(js_path.js_lib_dist))
.pipe(sourcemaps.init())//来源地图
//.pipe(rename({suffix: '.min'}))//重命名
.pipe(rev())
.pipe(uglify())
.pipe(sourcemaps.write(js_path.js_map))
.pipe(gulp.dest(js_path.js_lib_dist));
});
//css压缩
gulp.task('minify-activities', function () {
//each_ary(css_path.css_activities, css_activities_ary)
//[css_path.css_activities + '*.css', '!' + css_path.css_activities + '*.bundle.css']
return gulp.src(each_ary(css_path.css_activities, css_activities_ary))
.pipe(changed(css_path.css_activities_dist))
.pipe(sourcemaps.init())
.pipe(autoprefixer({
browsers: ['> 1%'],
cascade: true
}))
//.pipe(rename({suffix: '.min'}))
.pipe(rev())
.pipe(minify())
.pipe(sourcemaps.write(css_path.css_map))
.pipe(gulp.dest(css_path.css_activities_dist))
});
//css压缩
gulp.task('minify-modules', function () {
//each_ary(css_path.css_modules, css_modules_ary)
return gulp.src([css_path.css_modules + '*.css', '!' + css_path.css_modules + '*.bundle.css'])
.pipe(changed(css_path.css_modules_dist))
.pipe(sourcemaps.init())
.pipe(autoprefixer({
browsers: ['> 1%'],
cascade: true
}))
//.pipe(rename({suffix: '.min'}))
.pipe(rev())
.pipe(minify())
.pipe(sourcemaps.write(css_path.css_map))
.pipe(gulp.dest(css_path.css_modules_dist))
});
//图片压缩
gulp.task('imagemin', function () {
return gulp.src('www_ipad/activities/img/invitefriends/*.png')
//.pipe(changed(paths.dist_img))
.pipe(imagemin({
progressive: true,
svgoPlugins: [{removeViewBox: false}],
use: [pngquant()]
}))
.pipe(gulp.dest('www_ipad/activities/img/invitefriends/dist'));
});
//网页自动刷新
gulp.task('livereload', function () {
return gulp.src(paths.all)
.pipe(livereload());
});
//合并文件
var js_lib_ary_dist = [
'ionic-*.bundle.js',
'jquery-b19a18fce8.js',
'lodash-53a7121b59.js',
'angular-local-storage-*.js',
'angular-sanitize-*.js',
'restangular-*.js',
'ocLazyLoad-*.js',
'ionic-image-lazy-load-*.js',
'jquery-3ddbe55bb7.qrcode.min.js',
'base64-*.js'
];
var css_activities_ary_dist = ['*.css'];
var css_modules_ary_dist = ['*.css'];
gulp.task('concat-activities', function () {
return gulp.src(each_ary(css_path.css_activities_dist, css_activities_ary_dist))
.pipe(concat('activities.bundle.css'))
//.pipe(rename({suffix: '.min'}))
.pipe(rev())
.pipe(gulp.dest(css_path.css_activities_dist_concat))
});
gulp.task('concat-modules', function () {
return gulp.src(each_ary(css_path.css_modules_dist, css_modules_ary_dist))
.pipe(concat('freexf.bundle.css'))
//.pipe(rename({suffix: '.min'}))
.pipe(rev())
.pipe(gulp.dest(css_path.css_modules_dist_concat))
});
gulp.task('concat', function () {
return gulp.src(each_ary(js_path.js_lib_dist, js_lib_ary_dist))
.pipe(concat('freexf.bundle.js'))
//.pipe(rename({suffix: '.min'}))
.pipe(rev())
.pipe(gulp.dest(js_path.js_lib_dist_concat))
});
//清理文件
gulp.task('clean', function () {
return gulp.src([paths.dist_css], {
read: false
})
.pipe(clean())
});
//css3前缀
gulp.task('prefixer', function () {
return gulp.src('./index_three.css')
.pipe(autoprefixer({
browsers: ['> 1%'],
cascade: true
}))
.pipe(gulp.dest('./dist'));
});
//css雪碧图
/*gulp.task('sprite-resize', function () {
return gulp.src('www/img/home/icon-yinyu.png')
.pipe(parallel(
imageResize({width: '50%', height: '50%'}),
os.cpus().length
))
.pipe(gulp.dest('www/img/home/dest'))
});*/
gulp.task('sprite', function () {
return gulp.src('www_ipad/img/home/tubiao/icon-*.png')
.pipe(sprite({
imgName: 'sprite.png',
cssName: 'sprite.css',
padding: 23,
/*top-down ,left-right ,diagonal ,alt-diagonal ,binary-tree*/
algorithm: 'left-right',
algorithmOpts: {sort: false}
}))
.pipe(gulp.dest('www_ipad/img/home/dest'))
});
// 监听任务
gulp.task('watch', function () {
livereload.listen();
gulp.watch(paths.all, ['livereload']);
});
// 默认任务
gulp.task('default', ['watch', 'livereload', 'uglify', 'minify', 'imagemin']);