-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
81 lines (72 loc) · 2.44 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
require("dotenv").config();
const gulp = require('gulp');
const sassModule = require('gulp-sass')(require('sass'));
const sourcemaps = require('gulp-sourcemaps');
const browserSyncModule = require('browser-sync').create();
const nodemonModule = require('gulp-nodemon');
const host = process.env.HOST || "localhost";
const port = parseInt(process.env.PORT || 80);
function sass() {
return gulp.src("app/assets/scss/**/*.scss")
.pipe(sourcemaps.init())
.pipe(sassModule().on("error", sassModule.logError))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest("app/assets/css/"));
};
function browsersync(cb) {
return browserSyncModule.init({
injectChanges: true,
proxy: "http://" + host + "/",
open: false,
port: port + 1,
snippetOptions: {
rule: {
match: /<\/head>/i,
fn: function (snippet, match) {
return snippet.replace('id=', `nonce="browsersync" id=`) + match;
}
}
}
}, cb);
};
function nodemon(cb) {
var started = false;
nodemonModule({
script: 'server.js',
exec: "node --trace-warnings --inspect=9229",
env: {
"NODE_ENV": 'development',
"HOST": host,
"PORT": port
},
watch: ["*.js", "app/routes/"],
ignore: ["app/assets/js/", "gulpfile.js"]
}).on('start', function () {
// to avoid nodemon being started multiple times
// thanks @matthisk
if(!started) {
started = true;
console.log("Nodemon started.");
setTimeout(cb, 3000);
}
}).on('restart', function (...args) {
setTimeout(() => browserSyncModule.reload({}), 3000);
}).on("error", (e) => cb("Server failed to start. " + e.message));
};
const watch = gulp.series(sass, function (cb) {
gulp.watch("app/assets/scss/**/*.scss", sass);
// Catch and stream changes
gulp.watch(["app/assets/**/*.*", "!**/*.map", "!app/assets/scss/**"]).on("all", streamFileChanges);
gulp.watch(["app/views/**/*.*"]).on("all", browserSyncModule.reload);
cb();
});
function streamFileChanges(event, path) {
gulp.src(path).pipe(browserSyncModule.stream());
}
module.exports = {
sass,
browsersync,
nodemon,
watchTask: watch,
default: gulp.series(nodemon, browsersync, watch)
}