-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
79 lines (66 loc) · 1.67 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
'use strict';
/**
* 1、LESS编译 压缩 合并
* 2、JS合并 压缩 混淆
* 3、img复制
* 4、html压缩
*/
// 在gulpfile中先载入gulp包,因为这个包提供了一些API
var gulp=require('gulp');
var less=require('gulp-less');
var cssnano=require('gulp-cssnano');// 压缩
var concat=require('gulp-concat'); // js合并
var uglify=require('gulp-uglify'); // js压缩 混淆
var htmlmin=require('gulp-htmlmin');
//1、LESS编译 压缩 合并
gulp.task('style',function(){
// 这里是在执行style任务时自动执行的
gulp.src(['src/styles/*.less','!src/styles/_*.less'])
.pipe(less())
.pipe(cssnano())
.pipe(gulp.dest('dist/styles'))
.pipe(browserSync.reload({
stream:true
}));
});
// 2、JS合并 压缩 混淆,没有办法混淆和压缩,为什么呢?
gulp.task('script',function(){
gulp.src('src/scripts/*.js')
.pipe(concat('all.js'))
// .pipe(uglify())
.pipe(gulp.dest('dist/scripts'))
.pipe(browserSync.reload({
stream:true
}));
});
// 3、img复制
gulp.task('image',function(){
gulp.src('src/images/*.*')
.pipe(gulp.dest('dist/images'))
.pipe(browserSync.reload({
stream:true
}));
})
// 4、HTML
gulp.task('html',function(){
gulp.src('src/*.html')
.pipe(htmlmin({collapseWhitespace: true}))
.pipe(gulp.dest('dist'))
.pipe(browserSync.reload({
stream:true
}));
});
var browserSync=require('browser-sync');
gulp.task('serve',function(){
browserSync(
{
server:{baseDir:['dist']}
},
function(err, bs) {
console.log(bs.options.getIn(["urls", "local"]));
});
gulp.watch('src/styles/*.less',['style']);
gulp.watch('src/scripts/*.js',['script']);
gulp.watch('src/images/*.*',['image']);
gulp.watch('src/*.html',['html']);
});