-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
97 lines (79 loc) · 2.38 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
var gulp = require('gulp');
// 压缩html
// gulp中插件应用 下载插件=>引入插件=>应用插件
var htmlClean = require("gulp-htmlclean");
// 压缩图片
var imageMin = require("gulp-imagemin");
// 压缩js
var uglify = require("gulp-uglify");
// 去掉js中的调试语句
var stripDebug = require("gulp-strip-debug");
// 将less转换成css
var less = require("gulp-less");
// 压缩css
var cleanCss = require("gulp-clean-css");
// postcss autoprofixer css自动补全
var postcss = require("gulp-postcss");
var autoprofixer = require("autoprefixer");
// 开启服务器
var connect = require("gulp-connect");
var folder = {
src: "src/",
dist: "dist/"
};
// 判断当前环境变量
var devMod = process.env.NODE_ENV == "development";
// export NODE_ENV=production 设置环境变量
console.log(devMod)
gulp.task("html", function () {
var page = gulp.src(folder.src + "html/*")
.pipe(connect.reload())
if(!devMod) {
page.pipe(htmlClean())
}
page.pipe(gulp.dest(folder.dist + "html/"));
});
gulp.task("css", function () {
var page = gulp.src(folder.src + "css/*")
.pipe(connect.reload())
.pipe(less())
.pipe(postcss([autoprofixer()]))
if (!devMod) {
page.pipe(cleanCss())
}
page.pipe(gulp.dest(folder.dist + "css/"));
});
gulp.task("js", function () {
var page = gulp.src(folder.src + "js/*")
.pipe(connect.reload())
if (!devMod) {
page.pipe(stripDebug())
.pipe(uglify())
}
page.pipe(gulp.dest(folder.dist + "js/"));
});
gulp.task("image", function () {
gulp.src(folder.src + "images/*")
.pipe(imageMin())
.pipe(gulp.dest(folder.dist + "images/"));
});
// 开启服务器
gulp.task("server", function () {
connect.server({
port: "8888",
livereload: true
});
});
// 监听文件变化,然后利用connect.reload()自动刷新页面
gulp.task("watch", function () {
gulp.watch(folder.src + "html/*", ["html"]);
gulp.watch(folder.src + "css/*", ["css"]);
gulp.watch(folder.src + "js/*", ["js"]);
});
gulp.task("default", ["html", "css", "js", "image", "server", "watch"]);
// gulp.src()
// gulp.dest()
// gulp.task()
// gulp.watch()
// gulp 可以看成run task对文件流操作
// webpack module bandle模块化操作,将一切文件看成模块来操作