-
Notifications
You must be signed in to change notification settings - Fork 3
/
gulpfile.js
318 lines (295 loc) · 10.9 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
var gulp = require('gulp');
var jshint = require('gulp-jshint'); //校验js语法规范
var concat = require('gulp-concat'); //连接文件
var uglify = require('gulp-uglify'); //压缩js
var less = require('gulp-less'); //less 2 css
var sass = require('gulp-sass'); //scss 2 css
var minifyCSS = require('gulp-minify-css'); //压缩css
var minifyHTML = require('gulp-minify-html'); //压缩html
var sourcemaps = require('gulp-sourcemaps'); //添加sourcemap
var imagemin = require('gulp-imagemin'); //压缩图片
var cache = require('gulp-cache'); //缓存图片,防止重复压缩
var plumber = require('gulp-plumber'); //防止less编译报错阻断watch
var inject = require('gulp-inject'); //替换html里的内容 和inject相似
var browserSync = require('browser-sync'); //mobile debug liveload
var replace = require('gulp-replace'); //替换文件内容
var rename = require('gulp-rename'); //重命名文件
var exec = require('child_process').exec; //执行命令行
var rimraf = require('gulp-rimraf');
var path = require('path');
var config = require('./config.js'); //加载我们的配置文件
var version = config.version;
var paths = config.paths;
var dir_release = paths.release.ver ? paths.release.dir + version + '/' : paths.release.dir;
var css_libs = paths.lib.css;
var js_libs = paths.lib.js;
var apiReg = paths.api.reg;
var apiRep = paths.api.rep;
var timestamp = new Date();
var revTemp = (Math.random()*timestamp.getDate()).toString().substr(-3)+(Math.random()*timestamp.getMilliseconds()).toString().substr(-3);
var rev = paths.release.rev ? '-'+version+'.'+ revTemp : '';
var viewReg = /(templateUrl\s*:\s*[\'\"])(views)\//g;
var viewRep = '$1$2'+rev+'/';
/* 源文件 task */
//less编译
gulp.task('src:less', function() {
return gulp.src('src/css/**/*.less')
.pipe(plumber()) //防报错影响watch
.pipe(sourcemaps.init())
.pipe(less())
.pipe(sourcemaps.write('maps/'))
.pipe(gulp.dest('src/css/'));
});
//sass编译
gulp.task('src:sass', function() {
gulp.src('src/css/**/*.scss')
//.pipe(plumber()) //防报错影响watch
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(sourcemaps.write('maps/'))
.pipe(gulp.dest('src/css/'));
});
// js hint
gulp.task('src:jshint', function() {
return gulp.src('src/js/**/*.js')
.pipe(jshint())
.pipe(jshint.reporter('default'));
});
// 监视文件的变化
gulp.task('src:watch', ['src:less', 'src:sass'], function() {
gulp.watch('src/css/**/*.less', ['src:less']);
gulp.watch('src/css/**/*.scss', ['src:sass']);
gulp.watch('src/js/**/*.js', ['src:jshint']);
});
// 注入
gulp.task('src:inject', ['src:less', 'src:sass'], function() {
var target = gulp.src('src/*.html');
// It's not necessary to read the files (will speed up things), we're only after their paths:
var sources_libcss = gulp.src(css_libs, {
read: false
});
var sources_libjs = gulp.src(js_libs, {
read: false
});
var sources_pagecss = gulp.src('src/css/*.css', {
read: false
});
var sources_pagejs = gulp.src('src/js/*.js', {
read: false
});
return target.pipe(inject(sources_libcss, {
relative: true,
starttag: '<!-- inject:lib:css -->'
}))
.pipe(inject(sources_libjs, {
relative: true,
starttag: '<!-- inject:lib:js -->'
}))
.pipe(inject(sources_pagecss, {
relative: true,
starttag: '<!-- inject:page:css -->',
transform: function(filepath, file, i, length, targetFile) {
var srcName = path.basename(filepath, '.css'),
tarName = path.basename(targetFile.path, '.html');
return srcName !== tarName ? null : inject.transform.apply(inject.transform, arguments);
}
}))
.pipe(inject(sources_pagejs, {
relative: true,
starttag: '<!-- inject:page:js -->',
transform: function(filepath, file, i, length, targetFile) {
var srcName = path.basename(filepath, '.js'),
tarName = path.basename(targetFile.path, '.html');
return srcName !== tarName ? null : inject.transform.apply(inject.transform, arguments);
}
}))
.pipe(gulp.dest('src/'));
});
//清除缓存
gulp.task('clear', function (done) {
return cache.clearAll(done);
});
//初始化(总)
gulp.task('src:setup', ['clear','src:inject']);
/* 发布 task */
// 移动 fonts
gulp.task('release:fonts', function() {
//判断有没有引用bootstrap,如果有就复制fonts文件到src相对路径中
var bs_pattern = 'css/bootstrap.css';
css_libs.forEach(function(val, index) {
if (val.indexOf(bs_pattern) > -1) {
var bs_fonts = val.replace(bs_pattern, 'fonts/*');
gulp.src(bs_fonts)
.pipe(gulp.dest('src/fonts/'));
return false;
}
});
return gulp.src('src/fonts/*')
.pipe(gulp.dest(dir_release + 'fonts'));
});
//压缩lib css
gulp.task('release:minLibCss', /*['release:fonts'],*/ function() {
return gulp.src(css_libs)
.pipe(minifyCSS())
.pipe(concat('lib.css'))
.pipe(rename(function (path) {
path.basename += rev;
}))
.pipe(gulp.dest(dir_release + 'css/'));
});
//压缩page css
gulp.task('release:minPageCss', /*['release:fonts'],*/ function() {
return gulp.src('src/css/*.css')
.pipe(minifyCSS())
.pipe(rename(function (path) {
path.basename += rev;
}))
//.pipe(concat('app.css')) 不用连接
.pipe(gulp.dest(dir_release + 'css/'));
});
//压缩 css(总)
gulp.task('release:css', ['release:minLibCss', 'release:minPageCss']);
//压缩 lib js
gulp.task('release:minLibJs', function() {
return gulp.src(js_libs)
.pipe(replace(apiReg, apiRep)) //替换api路径
.pipe(replace(viewReg, viewRep)) //替换view rev
.pipe(sourcemaps.init())
.pipe(uglify())
.pipe(concat('lib.js'))
.pipe(rename(function (path) {
path.basename += rev;
}))
.pipe(sourcemaps.write('maps/'))
.pipe(gulp.dest(dir_release + 'js/'));
});
// 压缩 page js
gulp.task('release:minPageJs', function() {
return gulp.src('src/js/*.js')
.pipe(jshint())
.pipe(jshint.reporter('default'))
.pipe(replace(apiReg, apiRep)) //替换api路径
.pipe(replace(viewReg, viewRep)) //替换view rev
.pipe(sourcemaps.init())
.pipe(uglify())
.pipe(rename(function (path) {
path.basename += rev;
}))
.pipe(sourcemaps.write('maps/'))
.pipe(gulp.dest(dir_release + 'js/'));
});
// 压缩 js(总)
gulp.task('release:js', ['release:minLibJs', 'release:minPageJs']);
//压缩CSS引用图片
gulp.task('release:minHtmlImg', function() {
return gulp.src('src/img/**/*')
.pipe(cache(imagemin({
optimizationLevel: 3,
progressive: true,
interlaced: true
})))
.pipe(gulp.dest(dir_release + 'img/'));
});
//压缩 html中的图片
gulp.task('release:minCssImg', function() {
return gulp.src('src/css/img/**/*')
.pipe(cache(imagemin({
optimizationLevel: 3,
progressive: true,
interlaced: true
})))
.pipe(gulp.dest(dir_release + 'css/img/'));
});
//压缩图片(总)
gulp.task('release:img', ['release:minHtmlImg', 'release:minCssImg']);
// 注入
gulp.task('release:inject', ['release:css', 'release:js'], function() {
var target = gulp.src('src/*.html').pipe(gulp.dest(dir_release + ''));
var sources_libcss = gulp.src(dir_release + 'css/lib'+rev+'.css', {
read: false
});
var sources_libjs = gulp.src(dir_release + 'js/lib'+rev+'.js', {
read: false
});
var sources_pagecss = gulp.src(dir_release + 'css/*.css', {
read: false
});
var sources_pagejs = gulp.src(dir_release + 'js/*.js', {
read: false
});
return target.pipe(inject(sources_libcss, {
relative: true,
starttag: '<!-- inject:lib:css -->'
}))
.pipe(inject(sources_libjs, {
relative: true,
starttag: '<!-- inject:lib:js -->'
}))
.pipe(inject(sources_pagecss, {
relative: true,
starttag: '<!-- inject:page:css -->',
transform: function(filepath, file, i, length, targetFile) {
var srcName = path.basename(filepath, '.css').replace(rev,''),
tarName = path.basename(targetFile.path, '.html');
return srcName !== tarName ? null : inject.transform.apply(inject.transform, arguments);
}
}))
.pipe(inject(sources_pagejs, {
relative: true,
starttag: '<!-- inject:page:js -->',
transform: function(filepath, file, i, length, targetFile) {
var srcName = path.basename(filepath, '.js').replace(rev,''),
tarName = path.basename(targetFile.path, '.html');
return srcName !== tarName ? null : inject.transform.apply(inject.transform, arguments);
}
}))
.pipe(gulp.dest(dir_release));
});
//移动 views
gulp.task('release:views', function() {
return gulp.src('src/views/**/*.html')
.pipe(gulp.dest(dir_release + 'views'+rev+'/'));
});
//压缩 view html
gulp.task('release:minViewHtml', function() {
var opts = {
empty: true,
conditionals: true,
spare: true,
quotes: true
};
return gulp.src('src/views/**/*.html')
.pipe(minifyHTML(opts))
.pipe(gulp.dest(dir_release + 'views'+rev+'/'));
});
//压缩 page html
gulp.task('release:minPageHtml', ['release:inject'], function() {
var opts = {
conditionals: true,
spare: true,
quotes: true
};
return gulp.src(dir_release + '*.html')
.pipe(minifyHTML(opts))
.pipe(gulp.dest(dir_release));
});
//压缩 html
gulp.task('release:html', ['release:minViewHtml', 'release:minPageHtml']);
//清除发布目录
gulp.task('release:clean', function(cb) {
return gulp.src([dir_release + 'js/**/*.js', dir_release + 'css/**/*.css', dir_release + 'js/maps/*.map',dir_release + 'view*/'], { read: false })
.pipe(rimraf({ force: true }));
});
//发布(总)
gulp.task('release', ['release:fonts', 'release:views', 'release:img', 'release:inject']);
//加一个js/css不压缩的dev版本
//带html压缩的发布(总)
gulp.task('release:+minhtml', ['release:fonts', 'release:img', 'release:html']);
// browser-sync Static server
gulp.task('debug', function() {
var debug = config.paths.debug;
var debugOpts = debug.proxy === '' || debug.proxy === undefined ? {server:{baseDir:debug.baseDir}} : {proxy: debug.proxy};
browserSync(debugOpts);
//监听文件变化实时更新
gulp.watch("./src/**/*.*").on('change', browserSync.reload);
});