-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
125 lines (97 loc) · 2.7 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
// Imports
const gulp = require('gulp'),
del = require('del'),
newer = require('gulp-newer'),
// CSS processing
sass = require('gulp-sass'),
cleanCSS = require('gulp-clean-css'),
// Templating
nunjucks = require('gulp-nunjucks-render'),
// JavaScript handling
concat = require('gulp-concat'),
terser = require('gulp-terser'),
// Development browser
browserSync = require('browser-sync'),
reload = browserSync.reload;
// TASKS
// Clean previous build
gulp.task('clean', function(done){
// Deletes all files from dist/
del.sync('dist/', {force: true});
done()
});
// Nunjucks
gulp.task('nunjucks', function() {
// Gets all .html files in pages
return gulp.src('app/**/*.html')
// Renders template with nunjucks
.pipe(nunjucks({
path: ['app/templates/']
}))
// Outputs files in dist folder
.pipe(gulp.dest('dist'))
});
// Sass preprocessing and CSS minifying
gulp.task('sass', function(){
return gulp.src('scss/style.scss')
.pipe(sass()) // Compiles styles.scss to css
.pipe(cleanCSS({compatibility: 'ie9'})) // Minifies CSS
.pipe(gulp.dest('app/static/css'))
.pipe(reload({
stream: true
}))
});
// Concat and minify JavaScript
gulp.task('js', function() {
const files = [
// Uncomment 2 lines below to add Bootstrap JS support
// 'node_modules/jquery/dist/jquery.slim.min.js',
// 'node_modules/bootstrap/dist/js/bootstrap.bundle.min.js',
'js/*.js'
];
return gulp.src(files)
.pipe(concat('scripts.js'))
// Minify JS
.pipe(terser())
.pipe(gulp.dest('dist/static/js/'));
});
// Copy all static files
gulp.task('copy-static', function(){
return gulp.src('app/static/**/*.*', {base: './app/static/'})
.pipe(gulp.dest('dist/static/'));
});
gulp.task('reload', function(done){
reload();
done();
});
// Watch for changes
gulp.task('watch', function(done){
// Watch HTML pages
gulp.watch('app/**/*.html',
gulp.series('nunjucks', 'copy-static', 'reload'));
// Watch SCSS files
gulp.watch('scss/**/*.scss', gulp.series('sass', 'copy-static'));
// Watch JS files
gulp.watch('js/*.js', gulp.series('js', 'reload'));
done();
});
// Starts browserSync
gulp.task('serve', function(done){
browserSync({
server: {
baseDir: './dist',
index: "index.html",
serveStaticOptions: {
extensions: ['html']
}
}
});
done();
});
// Series
// Default task
gulp.task('default', gulp.series('clean', 'sass', 'nunjucks', 'js',
'copy-static', 'serve', 'watch'));
// Deployment task
gulp.task('build', gulp.series('clean', 'sass', 'nunjucks', 'js',
'copy-static'));