-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
133 lines (120 loc) · 2.94 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
// ToDo: Create gulp tasks from functions and add propper termination (2)
'use strict';
// Load plugins
const config = require('./config.js');
const nodemon = require('gulp-nodemon');
const browsersync = require('browser-sync').create();
const gulp = require('gulp');
const plumber = require('gulp-plumber');
const sourcemaps = require('gulp-sourcemaps');
const rename = require('gulp-rename');
const sass = require('gulp-sass');
const postcss = require('gulp-postcss');
const cleanCSS = require('gulp-clean-css');
const autoprefixer = require('autoprefixer');
const concat = require('gulp-concat');
const babel = require('gulp-babel');
const minify = require('gulp-minify');
// Starting the webserver
function server() {
let started = false;
let args = [];
if (process.argv.slice(3)[0] === '-insecure') {
args.push('insecure');
}
return nodemon({
script: 'server.js',
args: args,
watch: ['server.js', 'models', 'routes']
}).on('start', function () {
if (!started) {
started = true;
}
browsersync.stream();
});
}
// BrowserSync
function browserSync(done) {
let options = {
proxy : 'https://localhost:3000',
open: false,
cors: true,
port : 3001,
https: {
key: config.ssl.key,
cert: config.ssl.cert,
}
}
if (process.argv.slice(3)[0] === '-insecure') {
options.proxy = 'http://localhost:2999';
delete options.https;
}
browsersync.init(options);
done();
}
// BrowserSync Reload
function browserSyncReload(done) {
browsersync.reload();
done();
}
// Transpile, concatenate and minify CSS
function css() {
return gulp
.src('./src/scss/*.scss')
.pipe(plumber())
.pipe(sass({
outputStyle: 'expanded',
errLogToConsole : true,
sourceComments: 'map'
}))
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(rename({ suffix: '.min' }))
.pipe(postcss([autoprefixer()]))
.pipe(cleanCSS())
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('./public/css/'))
.pipe(browsersync.stream());
}
// Transpile, concatenate and minify scripts
function js() {
return (
gulp
.src(['./src/js/**/*'])
.pipe(plumber())
.pipe(sourcemaps.init())
.pipe(babel())
.pipe(concat('script.js'))
.pipe(minify({
ext:{
min:'.min.js'
},
noSource: true
}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('./public/js/'))
.pipe(browsersync.stream())
);
}
// Watch files
function watchFiles() {
gulp.watch('./src/scss/**/*', css);
gulp.watch('./src/js/**/*', js);
gulp.watch(
[
'./views/**/*',
'./routes/**/*',
'./models/**/*',
'./helpers/**/*',
],
gulp.series(browserSyncReload)
);
}
// Defining complex tasks
const build = gulp.parallel(css, js);
const watch = gulp.parallel(watchFiles, server, browserSync);
// Exporting tasks
exports.css = css;
exports.js = js;
exports.build = build;
exports.watch = watch;
exports.default = build;