/
gulpfile.js
103 lines (89 loc) · 3.22 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
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var open = require('open');
var app = {
srcPath: 'src/',
devPath: 'build/',
prdPath: 'dist/'
};
gulp.task('lib', function () {
gulp.src('bower_components/**/*')
.pipe(gulp.dest(app.devPath + 'vender'))
.pipe(gulp.dest(app.prdPath + 'vender'))
.pipe($.connect.reload());
});
gulp.task('html', function () {
gulp.src(app.srcPath + '**/*.html')
.pipe(gulp.dest(app.devPath))
.pipe($.htmlmin({
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
}))
.pipe(gulp.dest(app.prdPath))
.pipe($.connect.reload());
});
gulp.task('json', function () {
gulp.src(app.srcPath + 'data/*.json')
.pipe(gulp.dest(app.devPath + 'data'))
.pipe(gulp.dest(app.prdPath + 'data'))
.pipe($.connect.reload());
});
gulp.task('less', function () {
gulp.src(app.srcPath + 'less/*.less')
// .pipe($.plumber())
.pipe($.less())
.pipe(gulp.dest(app.devPath + 'css'))
.pipe($.cssmin({
advanced: true,//类型:Boolean 默认:true [是否开启高级优化(合并选择器等)]
compatibility: 'ie7',//保留ie7及以下兼容写法 类型:String 默认:''or'*' [启用兼容模式; 'ie7':IE7兼容模式,'ie8':IE8兼容模式,'*':IE9+兼容模式]
keepBreaks: false,//类型:Boolean 默认:false [是否保留换行]
keepSpecialComments: '*'
//保留所有特殊前缀 当你用autoprefixer生成的浏览器前缀,如果不加这个参数,有可能将会删除你的部分前缀
}))
.pipe(gulp.dest(app.prdPath + 'css'))
.pipe($.connect.reload());
});
gulp.task('js', function () {
gulp.src(app.srcPath + 'scripts/**/*.js')
// .pipe($.plumber())
.pipe($.concat('index.js'))
.pipe(gulp.dest(app.devPath + 'js'))
.pipe($.uglify())
.pipe(gulp.dest(app.prdPath + 'js'))
.pipe($.connect.reload());
});
gulp.task('image', function () {
gulp.src(app.srcPath + 'image/**/*')
// .pipe($.plumber())
.pipe(gulp.dest(app.devPath + 'image'))
.pipe($.imagemin())
.pipe(gulp.dest(app.prdPath + 'image'))
.pipe($.connect.reload());
});
gulp.task('build', ['lib', 'html', 'json', 'less', 'js', 'image']);
gulp.task('clean', function () {
gulp.src([app.devPath, app.prdPath])
.pipe($.clean());
});
gulp.task('serve', function () {
$.connect.server({
root: [app.devPath],
livereload: true, // 自动刷新
port: 1234,
});
open('http://localhost:1234/index.html');
gulp.watch('bower_components/**/*', ['lib']);
gulp.watch(app.srcPath+'**/*.html', ['html']);
gulp.watch(app.srcPath+'data/*.json', ['json']);
gulp.watch(app.srcPath+'less/*.less', ['less']);
gulp.watch(app.srcPath+'less/**/*.less', ['less']);
gulp.watch(app.srcPath+'scripts/**/*.js', ['js']);
gulp.watch(app.srcPath+'image/**/*', ['image']);
});
gulp.task('default', ['serve', 'build']);