-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgulpfile.js
More file actions
executable file
·135 lines (117 loc) · 2.95 KB
/
gulpfile.js
File metadata and controls
executable file
·135 lines (117 loc) · 2.95 KB
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
// Core dependencies
const gulp = require('gulp');
// External dependencies
const babel = require('gulp-babel');
const browserSync = require('browser-sync');
const clean = require('gulp-clean');
const sass = require('gulp-sass')(require('sass'));
const nodemon = require('gulp-nodemon');
// Local dependencies
const config = require('./app/config');
// Set configuration variables
const port = parseInt(process.env.PORT) || config.port;
// Delete all the files in /public build directory
function cleanPublic() {
return gulp.src('public', { allowEmpty: true }).pipe(clean());
}
sass.compiler = require('sass');
// Compile SASS to CSS
function compileStyles() {
return gulp
.src(['app/assets/sass/**/*.scss'])
.pipe(sass({
silenceDeprecations: [
'legacy-js-api',
'color-functions',
'mixed-decls',
'global-builtin',
'import'
],
}))
.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'])
.pipe(babel())
.pipe(gulp.dest('public/js'));
}
// Compile assets
function compileAssets() {
return gulp
.src([
'app/assets/**/**/*.*',
'!**/assets/**/**/*.js', // Don't copy JS files
'!**/assets/**/**/*.scss', // Don't copy SCSS files
], { encoding: false })
.pipe(gulp.dest('public'));
}
// Start nodemon
function startNodemon(done) {
const server = nodemon({
script: 'app.js',
stdout: true,
ext: 'js',
quiet: false,
});
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/**/*.*', 'lib/example-templates/**/*.*'],
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);
}
function setWatchEnv(done) {
process.env.WATCH = 'true';
done();
}
exports.watch = watch;
exports.compileStyles = compileStyles;
exports.compileScripts = compileScripts;
exports.cleanPublic = cleanPublic;
exports.setWatchEnv = setWatchEnv;
gulp.task(
'build',
gulp.series(cleanPublic, compileStyles, compileScripts, compileAssets)
);
gulp.task('default', gulp.series(setWatchEnv, startNodemon, startBrowserSync, watch));