-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
76 lines (76 loc) · 2.44 KB
/
Copy pathgulpfile.js
File metadata and controls
76 lines (76 loc) · 2.44 KB
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
//1引入gulp
const gulp = require('gulp');
const less = require('gulp-less');
const cssmin = require('gulp-cssmin');
const uglify = require('gulp-uglify');
const connect = require('gulp-connect');
const concat = require('gulp-concat');
const imagemin = require('gulp-imagemin');
const open = require('open');
//2声明地址
const app = {
srcPath:'src/',//源码目录
buildPath:'build/',//搭建目录
distPath:'dist/'//发布目录
};
//定义任务
//10 将bower下载的js插件引入到build和dist中
gulp.task('lib',function(){
gulp.src('bower_components/**/*.js')
.pipe(gulp.dest(app.buildPath+'lib/'))
.pipe(gulp.dest(app.distPath+'lib/'))
.pipe(connect.reload());//自动刷新
});
//3将所有html文件移动到另一个位置
gulp.task('html',function () {
gulp.src(app.srcPath+'**/*.html')
.pipe(gulp.dest(app.buildPath))//目标地址
.pipe(gulp.dest(app.distPath))
.pipe(connect.reload())
});
//4 less
gulp.task('less',function () {
gulp.src(app.srcPath+'style/index.less')
.pipe(less())
.pipe(gulp.dest(app.buildPath+'style/'))
.pipe(cssmin())
.pipe(gulp.dest(app.distPath+'style/'))
.pipe(connect.reload())
});
//5合并js
gulp.task('js',function () {
gulp.src(app.srcPath+'**/*.js')
.pipe(concat('index.js'))//将所有js合并到index.js文件中
.pipe(gulp.dest(app.buildPath+'js/'))
.pipe(uglify())
.pipe(gulp.dest(app.distPath+'js/'))
.pipe(connect.reload())
});
//6 压缩图片
gulp.task('image',function () {
gulp.src(app.srcPath+'images/**/*')
.pipe(gulp.dest(app.buildPath+'images/'))
.pipe(imagemin())
.pipe(gulp.dest(app.distPath+'images/'))
.pipe(connect.reload())
});
//7同时执行多个任务
gulp.task('build',['html','less','js','image','lib']);
//8设置服务器
gulp.task('server',['build'],function () {
//设置服务器
connect.server({
root:[app.buildPath],//服务器启动目录
livereload:true,//自动检测文件的变化
port:8085//端口号
});
//监听文件的变化
gulp.watch('bower_components/**/*', ['lib']);
gulp.watch(app.srcPath+'**/*.html',['html']);
gulp.watch(app.srcPath+'js/**/*.js',['js']);
gulp.watch(app.srcPath+'images/**/*',['image']);
gulp.watch(app.srcPath+'style/**/*.less',['less']);
open('http://localhost:8085')
});
//9定义默认任务
gulp.task('default',['server']);