-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
282 lines (265 loc) · 5.56 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
"use strict";
const gulp = require("gulp");
const wait = require("gulp-wait");
const plumber = require("gulp-plumber");
const notify = require("gulp-notify");
const gutil = require("gulp-util");
const del = require("del");
const pugInheritance = require("gulp-pug-inheritance");
const pug = require("gulp-pug");
const changed = require("gulp-changed");
const cached = require("gulp-cached");
const gulpif = require("gulp-if");
const filter = require("gulp-filter");
const sass = require("gulp-sass");
const autoprefixer = require("gulp-autoprefixer");
const babelify = require("babelify");
const browserify = require("browserify");
const watchify = require("watchify");
const watch = require("gulp-watch");
const source = require("vinyl-source-stream");
const buffer = require("vinyl-buffer");
const sourcemaps = require("gulp-sourcemaps");
const minify = require("gulp-minify");
const concat = require("gulp-concat");
const browserSync = require("browser-sync").create();
const reload = browserSync.reload;
// Clean www, do not remove public dir.
gulp.task("clean:www", () => {
gutil.log(gutil.colors.yellow.bold.underline("Cleaning www..."));
del(["www/**/*", "!www/public/", "!www/public/**"], {
dot: true,
});
});
// Compile sass files to css (only scss base dir.)
gulp.task("sass", () => {
return gulp
.src("dev/scss/*.scss")
.pipe(wait(1000))
.pipe(
plumber({
errorHandler: function (err) {
notify.onError({
title: "SCSS error in " + err.plugin,
message: err.message,
})(err);
gutil.beep();
this.emit("end");
},
})
)
.pipe(
sass({
outputStyle: "expanded",
})
)
.pipe(
autoprefixer({
browsers: ["last 4 versions"],
cascade: false,
})
)
.pipe(gulp.dest("www/css/"))
.pipe(
reload({
stream: true,
})
);
});
// Css and Fonts, move to working dir.
gulp.task("css", () => {
return gulp
.src("dev/css/**/*.{css,ttf,eot,otf,svg,woff,woff2}")
.pipe(wait(1000))
.pipe(
plumber({
errorHandler: function (err) {
notify.onError({
title: "CSS/FONTS error in " + err.plugin,
message: err.message,
})(err);
gutil.beep();
this.emit("end");
},
})
)
.pipe(gulp.dest("www/css/"))
.pipe(
reload({
stream: true,
})
);
});
// Only changed compile for pug files watch fix.
gulp.task("setWatchPugInheritance", function () {
global.isWatching = true;
});
// Compile pug files to html
gulp.task("pug", () => {
return gulp
.src("dev/pug/**/*.pug")
.pipe(
plumber({
errorHandler: function (err) {
notify.onError({
title: "PUG error in " + err.plugin,
message: err.message,
})(err);
gutil.beep();
this.emit("end");
},
})
)
.pipe(
changed("www", {
extension: ".html",
})
)
.pipe(gulpif(global.isWatching, cached("pug")))
.pipe(
pugInheritance({
basedir: "dev/pug",
skip: "node_modules",
})
)
.pipe(
filter(function (file) {
return !/\/_/.test(file.path) && !/^_/.test(file.relative);
})
)
.pipe(
pug({
pretty: true,
})
)
.pipe(gulp.dest("www"))
.pipe(
reload({
stream: true,
})
);
});
// Json, move to working dir.
gulp.task("json", () => {
return gulp
.src("dev/json/**/*.json")
.pipe(gulp.dest("www/json/"))
.pipe(
reload({
stream: true,
})
);
});
// Js, move to working dir.
gulp.task("js", () => {
return gulp
.src(["dev/js/**/*.js", "!dev/js/**/app.js"])
.pipe(
plumber({
errorHandler: function (err) {
notify.onError({
title: "JS error in " + err.plugin,
message: err.message,
})(err);
gutil.beep();
this.emit("end");
},
})
)
.pipe(gulp.dest("www/js/"))
.pipe(
reload({
stream: true,
})
);
});
// ES6 task as function-task.
gulp.task("es6", es6);
// ES6, Transpiling for js files.
function es6(watch) {
let b = browserify({
entries: "dev/js/app.js",
}).transform(
babelify.configure({
presets: ["@babel/preset-env"],
})
);
if (watch) b = watchify(b);
let rebundle = function () {
gutil.log(gutil.colors.magenta.bold.underline("Transpiling [app.js] ..."));
return b
.bundle()
.on("error", function (err) {
gutil.beep();
gutil.log(gutil.colors.bgRed("ERROR (Browserify): " + err.message));
})
.pipe(source("app.js"))
.pipe(buffer())
.pipe(
sourcemaps.init({
loadMaps: true,
})
)
.pipe(sourcemaps.write("./"))
.pipe(gulp.dest("www/js/"))
.pipe(
reload({
stream: true,
})
);
};
b.on("update", function () {
return rebundle();
});
return rebundle();
}
// The working directory.
gulp.task(
"browser-sync",
["sass", "css", "setWatchPugInheritance", "pug", "json", "js", "es6"],
function () {
browserSync.init({
server: {
baseDir: "./www/",
},
});
gutil.log(gutil.colors.green.bold.underline("SERVE HAS BEEN STARTED."));
}
);
// Watch files comiling.
gulp.task("watch", function () {
gulp.watch("dev/scss/**/*", ["sass"]);
gulp.watch("dev/css/**/*", ["css"]);
gulp.watch("dev/pug/**/*", ["pug"]);
gulp.watch("dev/json/**/*", ["json"]);
gulp.watch("dev/js/**/*", ["js"]);
});
// Default task.
gulp.task("default", ["watch", "browser-sync"]);
// Clean & Build task.
gulp.task(
"build",
[
"clean:www",
"sass",
"css",
"setWatchPugInheritance",
"pug",
"json",
"js",
"es6",
],
() => {
gutil.log(gutil.colors.cyan.bold.underline("OK! (BUILD IS DONE.)"));
process.exit(0);
}
);
// Project Setup Message.
gulp.task("setupLog", () => {
gutil.log(
gutil.colors.bgGreen(
'OK! (SETUP COMPLETED.), Type "npm run dev" to start development.'
)
);
process.exit(0);
});