-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
executable file
·89 lines (78 loc) · 2.24 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
/**
*
* gulpfile.js
*
* 采用Gulp构建项目
*
* 作者: 魏国兴
* 日期: 20160412
*
* 组件安装
* npm install gulp gulp-jshint gulp-minify-css gulp-uglify gulp-rename gulp-concat gulp-clean gulp-html-replace --save-dev
*
* 运行
* gulp
*
*/
// 引入 gulp及组件
var gulp = require('gulp'), //基础库
minifycss = require('gulp-minify-css'), //css压缩
jshint = require('gulp-jshint'), //js检查(这里并没有使用校验模块,有待优化)
uglify = require('gulp-uglify'), //js压缩
rename = require('gulp-rename'), //重命名
concat = require('gulp-concat'), //合并文件
clean = require('gulp-clean'), //清空文件夹
htmlmin = require('gulp-htmlmin'), //压缩HTML
htmlreplace = require('gulp-html-replace');//替换HTML中的路径
// 样式处理
gulp.task('css', function () {
var cssSrc = ['./css/reset.css', './css/calendar.css'],
cssDst = './dist/css';
gulp.src(cssSrc)
.pipe(concat('calendar.min.css'))
.pipe(minifycss())
.pipe(gulp.dest(cssDst));
});
// 图片处理
gulp.task('img', function(){
var imgSrc = './img/*',
imgDst = './dist/img';
gulp.src(imgSrc)
.pipe(gulp.dest(imgDst));
})
// js处理
gulp.task('js', function () {
var jsSrc = ['./data/*.js','./js/*.js'],
jsDst ='./dist/js';
gulp.src(jsSrc)
.pipe(concat('calendar.min.js'))
.pipe(uglify())
.pipe(gulp.dest(jsDst));
});
// 替换html的路径
gulp.task("html", function(){
var htmlSrc = './*.html',
htmlDst ='./dist';
return gulp.src(htmlSrc)
.pipe(htmlreplace({
'css':'css/calendar.min.css',
'js': 'js/calendar.min.js'
}))
.pipe(htmlmin({
removeComments: true,
collapseWhitespace: true,
}))
.pipe(gulp.dest(htmlDst));
});
// 清空图片、样式、js
gulp.task('clean', function() {
gulp.src(['./dist/*.html','./dist/css/*', './dist/data/*','./dist/js/*','./dist/img/*'], {read: false})
.pipe(clean());
});
// 默认任务 清空图片、样式、js并重建 运行语句 gulp
gulp.task('default', ['clean'], function(){
// 延时500ms,清除目录有一定延时
setTimeout(function(){
gulp.start('html','css','img','js');
},500);
});