-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
170 lines (153 loc) · 3.86 KB
/
gulpfile.js
File metadata and controls
170 lines (153 loc) · 3.86 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
let gulp = require('gulp'),
concat = require('gulp-concat'),
imagemin = require('gulp-imagemin'),
include = require('gulp-include'),
plumber = require('gulp-plumber'),
sourcemaps = require('gulp-sourcemaps'),
uglify = require('gulp-uglify'),
browserSync = require('browser-sync');
/**
* Notify
*
* Show a notification in the browser's corner.
*
* @param {*} message
*/
function notify(message) {
browserSync.notify(message);
}
/**
* Config Task
*
* Build the main YAML config file.
*/
function config() {
return gulp.src('src/yml/_config.yml')
.pipe(include())
.on('error', console.error)
.pipe(gulp.dest('./'));
}
/**
* Jekyll Task
*
* Build the Jekyll Site.
*
* @param {*} done
*/
function jekyll(done) {
notify('Building Jekyll...');
return require('child_process').exec('bundle exec jekyll build');
}
/**
* Server Task
*
* Launch server using BrowserSync.
*
* @param {*} done
*/
function server(done) {
browserSync({
server: {
baseDir: '_site'
}
});
done();
}
/**
* Reload Task
*
* Reload page with BrowserSync.
*
* @param {*} done
*/
function reload(done) {
notify('Reloading...');
browserSync.reload();
done();
}
/**
* Main JS Task
*
* All regular .js files are collected, minified and concatonated into one
* single scripts.min.js file (and sourcemap)
*/
function mainJs() {
notify('Building JS files...');
return gulp.src('src/js/main/**/*.js')
.pipe(sourcemaps.init())
.pipe(uglify())
.pipe(concat('scripts.min.js'))
.pipe(plumber())
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('_site/assets/js/'))
.pipe(browserSync.reload({ stream: true }))
.pipe(gulp.dest('assets/js'));
}
/**
* Preview JS Task
*
* Copy preview JS files to the assets folder.
*/
function previewJs() {
notify('Copying preview files...');
return gulp.src('src/js/preview/**/*.*')
.pipe(gulp.dest('assets/js/'));
}
/**
* JavaScript Task
*
* Run all the JS related tasks.
*/
const js = gulp.parallel(mainJs, previewJs);
/**
* Images Task
*
* All images are optimized and copied to assets folder.
*/
function images() {
notify('Copying image files...');
return gulp.src('src/img/**/*.{jpg,png,gif,svg}')
.pipe(plumber())
.pipe(imagemin({ optimizationLevel: 5, progressive: true, interlaced: true }))
.pipe(gulp.dest('assets/img/'));
}
/**
* Watch Task
*
* Watch files to run proper tasks.
*/
function watch() {
// Watch YAML files for changes & recompile
gulp.watch(['src/yml/*.yml'], gulp.series(config, jekyll, reload));
// Watch SASS files for changes & rebuild styles
gulp.watch(['_sass/**/*.scss'], gulp.series(jekyll, reload));
// Watch JS files for changes & recompile
gulp.watch('src/js/main/**/*.js', mainJs);
// Watch preview JS files for changes, copy files & reload
gulp.watch('src/js/preview/**/*.js', gulp.series(previewJs, reload));
// Watch images for changes, optimize & recompile
gulp.watch('src/img/**/*', gulp.series(images, config, jekyll, reload));
// Watch html/md files, rebuild config, run Jekyll & reload BrowserSync
gulp.watch(['*.html', '_includes/*.html', '_layouts/*.html', '_posts/*', '_authors/*', 'pages/*', 'category/*'], gulp.series(config, jekyll, reload));
}
/**
* Default Task
*
* Running just `gulp` will:
* - Compile the SASS and JavaScript files
* - Optimize and copy images to its folder
* - Build the config file
* - Compile the Jekyll site
* - Launch BrowserSync & watch files
*/
exports.default = gulp.series(gulp.parallel(js, images), config, jekyll, gulp.parallel(server, watch));
/**
* Build Task
*
* Running just `gulp build` will:
* - Compile the SASS and JavaScript files
* - Optimize and copy images to its folder
* - Build the config file
* - Compile the Jekyll site
*/
exports.build = gulp.series(gulp.parallel(js, images), config, jekyll);