-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
250 lines (227 loc) · 6.27 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
let gulp = require('gulp'),
concat = require('gulp-concat'),
imagemin = require('gulp-imagemin'),
include = require('gulp-include'),
plumber = require('gulp-plumber'),
rename = require('gulp-rename'),
sourcemaps = require('gulp-sourcemaps'),
stylus = require('gulp-stylus'),
uglify = require('gulp-uglify'),
yaml = require('gulp-yaml'),
prefixer = require('autoprefixer-stylus'),
browserSync = require('browser-sync'),
cp = require('child_process'),
del = require('del'),
jeet = require('jeet'),
koutoSwiss = require('kouto-swiss'),
rupture = require('rupture');
/**
* 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...');
let bundle = process.platform === "win32" ? "bundle.bat" : "bundle";
return cp
.spawn(bundle, ['exec', 'jekyll build'], { stdio: 'inherit' })
.on('close', done);
}
/**
* 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();
}
/**
* Theme Task
*
* Create the JSON theme file.
*/
function theme() {
return gulp.src('src/yml/theme.yml')
.pipe(yaml({ schema: 'DEFAULT_SAFE_SCHEMA' }))
.pipe(gulp.dest('src/styl/'));
}
/**
* Main CSS Task
*
* The regular Stylus files are run through kouto-swiss/prefixer/jeet/rupture
* and placed into one single main styles.min.css file (and sourcemap)
*/
function mainCss() {
notify('Compiling styles...');
return gulp.src('src/styl/main.styl')
.pipe(sourcemaps.init())
.pipe(stylus({
use: [koutoSwiss(), prefixer(), jeet(), rupture()],
compress: true
}))
.pipe(rename('styles.min.css'))
.pipe(plumber())
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('_site/assets/css/'))
.pipe(browserSync.reload({ stream: true }))
.pipe(gulp.dest('assets/css'));
}
/**
* Preview CSS Task
*
* The preview Stylus file is run through kouto-swiss/prefixer/jeet/rupture
* and placed into one single preview.min.css file (and sourcemap)
*/
function previewCss() {
notify('Compiling styles...');
return gulp.src('src/styl/preview.styl')
.pipe(sourcemaps.init())
.pipe(stylus({
use: [koutoSwiss(), prefixer(), jeet(), rupture()],
compress: true
}))
.pipe(rename('preview.min.css'))
.pipe(plumber())
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('_site/assets/css/'))
.pipe(browserSync.reload({ stream: true }))
.pipe(gulp.dest('assets/css'));
}
/**
* CSS Task
*
* Run all the CSS related tasks.
*/
const css = gulp.parallel(mainCss, previewCss);
/**
* 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', '!src/yml/theme.yml'], gulp.series(config, jekyll, reload));
// Watch theme file for changes, rebuild styles & recompile
gulp.watch(['src/yml/theme.yml'], gulp.series(theme, css, config, jekyll, reload));
// Watch stylus files for changes & rebuild styles
gulp.watch(['src/styl/**/*.styl', '!src/styl/preview.styl'], gulp.series(theme, mainCss));
// Watch preview style file for changes, rebuild styles & reload
gulp.watch('src/styl/preview.styl', gulp.series(theme, previewCss, 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 theme, Stylus 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, gulp.series(theme, css), images), config, jekyll, gulp.parallel(server, watch));
/**
* Build Task
*
* Running just `gulp build` will:
* - Compile the theme, Stylus 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, gulp.series(theme, css), images), config, jekyll);