-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
168 lines (151 loc) · 3.89 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const gulp = require("gulp");
const sass = require("gulp-sass")(require("sass"));
const uglify = require("gulp-uglify");
const clean = require("gulp-clean");
const rename = require("gulp-rename");
const open = require("gulp-open");
const connect = require("gulp-connect");
const connectRewrite = require("connect-modrewrite");
const inject = require("gulp-inject");
const cleanCSS = require("gulp-clean-css");
const htmlmin = require("gulp-htmlmin");
// ----- gulp dev start ----- //
gulp.task("clean", function () {
return gulp
.src("./dist/", { read: false, allowEmpty: true })
.pipe(clean({ force: true }));
});
gulp.task("sass", function () {
return gulp
.src("src/sass/**/*.scss")
.pipe(sass().on("error", sass.logError))
.pipe(gulp.dest("./dist/css"))
.pipe(connect.reload());
});
gulp.task("js", function () {
return gulp
.src("src/js/**/*.js")
.pipe(uglify())
.pipe(
rename(function (path) {
path.basename += ".min";
path.extname = ".js";
})
)
.pipe(gulp.dest("./dist/js"))
.pipe(connect.reload());
});
gulp.task("html", function () {
return gulp
.src("src/**/*.html")
.pipe(
inject(gulp.src(["./src/partials/*.html"]), {
starttag: "<!-- inject:{{path}} -->",
relative: true,
removeTags: true,
transform: function (filePath, file) {
// return file contents as string
return file.contents.toString("utf8");
},
})
)
.pipe(gulp.dest("./dist"))
.pipe(connect.reload());
});
gulp.task("webserver", function () {
const server = connect.server({
root: ["dist", "static"],
host: "0.0.0.0", //host設定'0.0.0.0',就可以用內網檢視
port: 3000, //設定一個沒在使用的port
livereload: true, //auto refresh
middleware: function () {
return [connectRewrite(["^.([^\\.]+)$ /$1.html [L]"])];
},
});
return gulp.src("./").pipe(
open({
uri: "http://localhost:" + server.port,
})
);
});
gulp.task("watch", function () {
gulp.watch("src/sass/**/*.scss", gulp.series("sass"));
gulp.watch("src/js/**/*.js", gulp.series("js"));
gulp.watch("src/**/*.html", gulp.series("html"));
});
gulp.task(
"default",
gulp.series(
"clean",
"sass",
"js",
"html",
gulp.parallel("webserver", "watch"),
function (done) {
done();
}
)
);
// ----- gulp dev end ----- //
// ----- gulp deploy start ----- //
gulp.task("deploy-clean", function () {
return gulp
.src("./deploy/", { read: false, allowEmpty: true })
.pipe(clean({ force: true }));
});
gulp.task("deploy-sass", function () {
return gulp
.src(["src/sass/**/*.scss", "!src/sass/vars.scss"]) // exclude var.scss fail
.pipe(sass().on("error", sass.logError))
.pipe(cleanCSS({}))
.pipe(gulp.dest("./deploy/css"));
});
gulp.task("deploy-js", function () {
return gulp
.src("src/js/**/*.js")
.pipe(
uglify({
compress: {
drop_console: true,
},
})
)
.pipe(
rename(function (path) {
path.basename += ".min";
path.extname = ".js";
})
)
.pipe(gulp.dest("./deploy/js"));
});
gulp.task("deploy-html", function () {
return gulp
.src("src/**/*.html")
.pipe(
inject(gulp.src(["./src/partials/*.html"]), {
starttag: "<!-- inject:{{path}} -->",
relative: true,
removeTags: true,
transform: function (filePath, file) {
// return file contents as string
return file.contents.toString("utf8");
},
})
)
.pipe(htmlmin({ collapseWhitespace: true }))
.pipe(gulp.dest("./deploy"));
});
gulp.task("deploy-static", function () {
return gulp.src("./static/**/*").pipe(gulp.dest("./deploy/"));
});
gulp.task(
"build",
gulp.series(
"deploy-clean",
"deploy-sass",
"deploy-js",
"deploy-html",
"deploy-static"
)
);
// ----- gulp deploy end ----- //