forked from kostajh/savaslabs.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
282 lines (238 loc) · 8.86 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Define variables.
var appendPrepend = require('gulp-append-prepend');
var autoprefixer = require('autoprefixer');
var browserSync = require('browser-sync').create();
var cleancss = require('gulp-clean-css');
var concat = require('gulp-concat');
var del = require('del');
var gulp = require('gulp');
var gutil = require('gulp-util');
var imagemin = require('gulp-imagemin');
var jpegRecompress = require('imagemin-jpeg-recompress');
var notify = require('gulp-notify');
var postcss = require('gulp-postcss');
var rename = require('gulp-rename');
var run = require('gulp-run');
var runSequence = require('run-sequence');
var sass = require('gulp-ruby-sass');
var uglify = require('gulp-uglify');
var paths = require('./_assets/gulp_config/paths');
// Uses Sass compiler to process styles, adds vendor prefixes, minifies, then
// outputs file to the appropriate location.
gulp.task('build:styles:main', function() {
return sass(paths.sassFiles + '/main.scss', {
style: 'compressed',
trace: true,
loadPath: [paths.sassFiles]
}).pipe(postcss([ autoprefixer({ browsers: ['last 2 versions'] }) ]))
.pipe(cleancss())
.pipe(gulp.dest(paths.jekyllCssFiles))
.pipe(gulp.dest(paths.siteCssFiles))
.pipe(browserSync.stream())
.on('error', gutil.log);
});
// Processes critical CSS, to be included in head.html.
gulp.task('build:styles:critical', function() {
return sass(paths.sassFiles + '/critical.scss', {
style: 'compressed',
trace: true,
loadPath: [paths.sassFiles]
}).pipe(postcss([ autoprefixer({ browsers: ['last 2 versions'] }) ]))
.pipe(cleancss())
.pipe(gulp.dest('_includes'))
.on('error', gutil.log);
});
// Copies any other CSS files to the assets directory, to be used by pages/posts
// that specify custom CSS files.
gulp.task('build:styles:css', function() {
return gulp.src([paths.sassFiles + '/*.css'])
.pipe(postcss([ autoprefixer({ browsers: ['last 2 versions'] }) ]))
.pipe(cleancss())
.pipe(gulp.dest(paths.jekyllCssFiles))
.pipe(gulp.dest(paths.siteCssFiles))
.on('error', gutil.log);
});
// Builds all styles.
gulp.task('build:styles', [
'build:styles:main',
'build:styles:critical',
'build:styles:css'
]);
gulp.task('clean:styles', function(callback) {
del([paths.jekyllCssFiles, paths.siteCssFiles, '_includes/critical.css']);
callback();
});
// Concatenates and uglifies global JS files and outputs result to the
// appropriate location.
gulp.task('build:scripts:global', function() {
return gulp.src([
paths.jsFiles + '/global/lib' + paths.jsPattern,
paths.jsFiles + '/global/*.js'
])
.pipe(concat('main.js'))
.pipe(uglify())
// We need to add front matter so Jekyll will process variables.
.pipe(appendPrepend.prependFile('./_assets/gulp_config/front-matter.txt'))
// Only place in `assets` because Jekyll needs to process the file.
.pipe(gulp.dest(paths.jekyllJsFiles))
.on('error', gutil.log);
});
// Concatenates and uglifies leaflet JS files and outputs result to the
// appropriate location.
gulp.task('build:scripts:leaflet', function() {
return gulp.src([
paths.jsFiles + '/leaflet/leaflet.js',
paths.jsFiles + '/leaflet/leaflet-providers.js'
])
.pipe(concat('leaflet.js'))
.pipe(uglify())
.pipe(gulp.dest(paths.jekyllJsFiles))
.pipe(gulp.dest(paths.siteJsFiles))
.on('error', gutil.log);
});
// Builds all scripts.
gulp.task('build:scripts', ['build:scripts:global', 'build:scripts:leaflet']);
gulp.task('clean:scripts', function(callback) {
del([paths.jekyllJsFiles, paths.siteJsFiles]);
callback();
});
// Optimizes and copies image files.
// We're including imagemin options because we're overriding the default
// JPEG optimization plugin.
gulp.task('build:images', function() {
return gulp.src(paths.imageFilesGlob)
.pipe(imagemin([
imagemin.gifsicle(),
jpegRecompress(),
imagemin.optipng(),
imagemin.svgo()
]))
.pipe(gulp.dest(paths.jekyllImageFiles))
.pipe(gulp.dest(paths.siteImageFiles))
.pipe(browserSync.stream());
});
gulp.task('clean:images', function(callback) {
del([paths.jekyllImageFiles, paths.siteImageFiles]);
callback();
});
// Copies fonts.
gulp.task('build:fonts', ['fontawesome']);
// Places Font Awesome fonts in proper location.
gulp.task('fontawesome', function() {
return gulp.src(paths.fontFiles + '/font-awesome/**.*')
.pipe(rename(function(path) {path.dirname = '';}))
.pipe(gulp.dest(paths.jekyllFontFiles))
.pipe(gulp.dest(paths.siteFontFiles))
.pipe(browserSync.stream())
.on('error', gutil.log);
});
gulp.task('clean:fonts', function(callback) {
del([paths.jekyllFontFiles, paths.siteFontFiles]);
callback();
});
// Runs jekyll build command.
gulp.task('build:jekyll', function() {
var shellCommand = 'bundle exec jekyll build --config _config.yml';
return gulp.src('')
.pipe(run(shellCommand))
.on('error', gutil.log);
});
// Runs jekyll build command using test config.
gulp.task('build:jekyll:test', function() {
var shellCommand = 'bundle exec jekyll build --future --config _config.yml,_config.test.yml';
return gulp.src('')
.pipe(run(shellCommand))
.on('error', gutil.log);
});
// Runs jekyll build command using local config.
gulp.task('build:jekyll:local', function() {
var shellCommand = 'bundle exec jekyll build --future --config _config.yml,_config.test.yml,_config.dev.yml';
return gulp.src('')
.pipe(run(shellCommand))
.on('error', gutil.log);
});
// Deletes the entire _site directory.
gulp.task('clean:jekyll', function(callback) {
del(['_site']);
callback();
});
gulp.task('clean', ['clean:jekyll',
'clean:fonts',
'clean:images',
'clean:scripts',
'clean:styles']);
// Builds site anew.
gulp.task('build', function(callback) {
runSequence('clean',
['build:scripts', 'build:images', 'build:styles', 'build:fonts'],
'build:jekyll',
callback);
});
// Builds site anew using test config.
gulp.task('build:test', function(callback) {
runSequence('clean',
['build:scripts', 'build:images', 'build:styles', 'build:fonts'],
'build:jekyll:test',
callback);
});
// Builds site anew using local config.
gulp.task('build:local', function(callback) {
runSequence('clean',
['build:scripts', 'build:images', 'build:styles', 'build:fonts'],
'build:jekyll:local',
callback);
});
// Default Task: builds site.
gulp.task('default', ['build']);
// Special tasks for building and then reloading BrowserSync.
gulp.task('build:jekyll:watch', ['build:jekyll:local'], function(callback) {
browserSync.reload();
callback();
});
gulp.task('build:scripts:watch', ['build:scripts'], function(callback) {
runSequence('build:jekyll:local');
browserSync.reload();
callback();
});
// Static Server + watching files.
// Note: passing anything besides hard-coded literal paths with globs doesn't
// seem to work with gulp.watch().
gulp.task('serve', ['build:local'], function() {
browserSync.init({
server: paths.siteDir,
ghostMode: false, // Toggle to mirror clicks, reloads etc. (performance)
logFileChanges: true,
logLevel: 'debug',
open: true // Toggle to automatically open page when starting.
});
// Watch site settings.
gulp.watch(['_config*.yml'], ['build:jekyll:watch']);
// Watch .scss files; changes are piped to browserSync.
gulp.watch('_assets/styles/**/*.scss', ['build:styles']);
// Watch .js files.
gulp.watch('_assets/js/**/*.js', ['build:scripts:watch']);
// Watch image files; changes are piped to browserSync.
gulp.watch('_assets/img/**/*', ['build:images']);
// Watch posts.
gulp.watch('_posts/**/*.+(md|markdown|MD)', ['build:jekyll:watch']);
// Watch drafts if --drafts flag was passed.
if (module.exports.drafts) {
gulp.watch('_drafts/*.+(md|markdown|MD)', ['build:jekyll:watch']);
}
// Watch html and markdown files.
gulp.watch(['**/*.+(html|md|markdown|MD)', '!_site/**/*.*'], ['build:jekyll:watch']);
// Watch RSS feed XML files.
gulp.watch('**.xml', ['build:jekyll:watch']);
// Watch data files.
gulp.watch('_data/**.*+(yml|yaml|csv|json)', ['build:jekyll:watch']);
// Watch favicon.png.
gulp.watch('favicon.png', ['build:jekyll:watch']);
});
// Updates Ruby gems
gulp.task('update:bundle', function() {
return gulp.src('')
.pipe(run('bundle install'))
.pipe(run('bundle update'))
.pipe(notify({ message: 'Bundle Update Complete' }))
.on('error', gutil.log);
});