-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
151 lines (134 loc) · 3.1 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// gulp basic config
const Gulp = require("gulp");
const Minifycss = require("gulp-minify-css");
const Uglify = require("gulp-uglify");
const Imagemin = require("gulp-imagemin");
const FileInclude = require("gulp-file-include");
const Watch = require("gulp-watch");
const WebServer = require("gulp-webserver");
const RunSequence = require("gulp4-run-sequence");
const Clean = require("gulp-clean");
// css compile plugin
// const Sass = require("gulp-sass");
const Less = require("gulp-less");
const LessAutoprefix = require("less-plugin-autoprefix");
const autoprefix = new LessAutoprefix({ browsers: ["last 2 versions"] });
// babel used
const Babel = require("gulp-babel");
const Dist = "dist/";
Gulp.task("copy-html", () => {
return Gulp.src("src/view/*.html")
.pipe(
FileInclude({
prefix: "##",
basepath: "@file"
})
)
.on("error", function(err) {
console.error("Task:copy-html,", err.message);
this.end();
})
.pipe(Gulp.dest(Dist));
});
Gulp.task("copy-js", async () => {
return await Gulp.src("src/js/**")
.pipe(Babel())
.pipe(Uglify())
.pipe(Gulp.dest(Dist + "/js"));
});
// Gulp.task("Sass", () => {
// Gulp.src("src/sass/*.scss")
// .pipe(Sass().on("error", Sass.logError))
// .pipe(Gulp.dest(Dist + "/sass"));
// });
Gulp.task("compile-less", done => {
return Gulp.src("src/less/*.less")
.pipe(
Less({
plugins: [autoprefix]
})
)
.pipe(Minifycss())
.pipe(Gulp.dest(Dist + "/css"));
});
Gulp.task("copy-css", done => {
return Gulp.src("src/css/*.css")
.pipe(Minifycss())
.pipe(Gulp.dest(Dist + "/css"));
});
Gulp.task("copy-images", done => {
return Gulp.src("src/images/**")
.pipe(Imagemin())
.pipe(Gulp.dest(Dist + "/images"));
});
Gulp.task("watch", done => {
Gulp.watch(
"src/view/*",
Gulp.series("copy-html", done => {
done();
})
);
Gulp.watch(
"src/js/**",
Gulp.series("copy-js", done => {
done();
})
);
Gulp.watch(
"src/css/*",
Gulp.series("copy-css", done => {
done();
})
);
Gulp.watch(
"src/less/*",
Gulp.series("compile-less", done => {
done();
})
);
Gulp.watch(
"src/images/**",
Gulp.series("copy-images", done => {
done();
})
);
done();
});
Gulp.task(
"copy-sources",
Gulp.series(
"copy-css",
"compile-less",
"copy-js",
"copy-html",
"copy-images",
done => {
console.log("copy sources is finished");
done();
}
),
done => {
done();
}
);
Gulp.task("web-server", done => {
return Gulp.src(Dist).pipe(
WebServer({
port: 3131,
host: "localhost",
livereload: true,
open: "http://localhost:3131/index.html"
})
);
});
Gulp.task("clean", () => {
return Gulp.src(Dist).pipe(Clean());
});
Gulp.task("start", done => {
// RunSequence是用来设置任务串行执行,因为有些任务是有先后顺序依赖,[]内的并行执行,()内的串行执行
RunSequence("clean", ["copy-sources", "watch"], "web-server");
done();
});
Gulp.task("default", Gulp.series("start"), done => {
done();
});