This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
executable file
·139 lines (123 loc) · 3.13 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
// Core dependencies
const gulp = require('gulp');
// External dependencies
const browserSync = require('browser-sync');
const clean = require('gulp-clean');
const sass = require('gulp-sass');
const nodemon = require('gulp-nodemon');
const webpack = require('webpack-stream');
// Local dependencies
const config = require('./app/config');
// Set configuration variables
const port = process.env.PORT || config.port;
// Delete all the files in /public build directory
function cleanPublic() {
return gulp.src('public', { allowEmpty: true})
.pipe(clean());
}
// Compile SASS to CSS
function compileStyles() {
return gulp.src([
'app/assets/sass/**/*.scss',
'docs/assets/sass/**/*.scss'
])
.pipe(sass())
.pipe(gulp.dest('public/css'))
.on('error', (err) => {
console.log(err)
process.exit(1)
});
}
// Compile JavaScript (with ES6 support)
function compileScripts() {
return gulp.src([
'app/assets/javascript/**/*.js',
'docs/assets/javascript/**/*.js'
])
.pipe(webpack({
mode: 'production',
module: {
rules: [
{
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
},
},
},
],
},
output: {
filename: 'main.min.js',
},
target: 'web',
}))
.pipe(gulp.dest('public/js'));
}
// Compile assets
function compileAssets() {
return gulp.src([
'app/assets/**/**/*.*',
'docs/assets/**/**/*.*',
'!**/assets/**/**/*.js', // Don't copy JS files
'!**/assets/**/**/*.scss', // Don't copy SCSS files
])
.pipe(gulp.dest('public'));
}
// Start nodemon
function startNodemon(done) {
const server = nodemon({
script: 'app.js',
stdout: false,
ext: 'scss js html',
quiet: true,
});
let starting = false;
const onReady = () => {
starting = false;
done();
};
server.on('start', () => {
starting = true;
setTimeout(onReady);
});
server.on('stdout', (stdout) => {
process.stdout.write(stdout);
if (starting) {
onReady();
}
});
}
function reload() {
browserSync.reload();
}
// Start browsersync
function startBrowserSync(done){
browserSync.init({
proxy: 'localhost:' + port,
port: port + 1000,
ui: false,
files: ['app/views/**/*.*', 'docs/views/**/*.*'],
ghostmode: false,
open: false,
notify: true,
watch: true,
}, done);
gulp.watch("public/**/*.*").on("change", reload);
}
// Watch for changes within assets/
function watch() {
gulp.watch('app/assets/sass/**/*.scss', compileStyles);
gulp.watch('app/assets/javascript/**/*.js', compileScripts);
gulp.watch('app/assets/**/**/*.*', compileAssets);
gulp.watch('docs/assets/sass/**/*.scss', compileStyles);
gulp.watch('docs/assets/javascript/**/*.js', compileScripts);
gulp.watch('docs/assets/**/**/*.*', compileAssets);
}
exports.watch = watch;
exports.compileStyles = compileStyles;
exports.compileScripts = compileScripts;
exports.cleanPublic = cleanPublic;
gulp.task('build', gulp.series(cleanPublic, compileStyles, compileScripts, compileAssets));
gulp.task('default', gulp.series(startNodemon, startBrowserSync, watch));