-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
219 lines (196 loc) · 5.38 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
'use strict';
// Gulp and node
const gulp = require('gulp');
const cp = require('child_process');
const notify = require('gulp-notify');
const size = require('gulp-size');
// Basic workflow plugins
const browserSync = require('browser-sync');
const browserify = require('browserify');
const source = require('vinyl-source-stream');
const buffer = require('vinyl-buffer');
const clean = require('gulp-clean');
const sass = require('gulp-sass');
const jekyll = process.platform === 'win32' ? 'jekyll.bat' : 'jekyll';
const messages = {
jekyllBuild: '<span style="color: grey">Running:</span> $ jekyll build'
};
// Performance workflow plugins
const htmlmin = require('gulp-htmlmin');
const prefix = require('gulp-autoprefixer');
const sourcemaps = require('gulp-sourcemaps');
const uglify = require('gulp-uglify');
const critical = require('critical');
const sw = require('sw-precache');
// Image Generation
const responsive = require('gulp-responsive');
const $ = require('gulp-load-plugins')();
const rename = require('gulp-rename');
const imagemin = require('gulp-imagemin');
const src = {
css: '_sass/jekyll-sleek.scss',
js: '_js/scripts.js',
};
const dist = {
css: '_site/assets/css',
js: '_site/assets/js',
};
function handleErrors() {
var args = Array.prototype.slice.call(arguments);
notify.onError({
title: 'Compile Error',
message: '<%= error.message %>'
}).apply(this, args);
this.emit('end'); // Keep gulp from hanging on this task
}
// Build the Jekyll Site
gulp.task('jekyll-build', function (done) {
browserSync.notify(messages.jekyllBuild);
return cp.spawn( jekyll , ['build'], {stdio: 'inherit'})
.on('close', done);
});
gulp.task('deploy', ['jekyll-build'], function () {
return gulp.src('./_site/**/*')
.pipe(deploy());
});
// Rebuild Jekyll & do page reload
gulp.task('rebuild', ['jekyll-build'], function (done) {
browserSync.reload();
done();
});
// Serve after jekyll-build
gulp.task('browser-sync', ['sass', 'js', 'sw', 'jekyll-build'], function() {
browserSync({
server: {
baseDir: '_site'
}
});
});
// SASS
gulp.task('sass', function() {
return gulp.src(src.css)
.pipe(sourcemaps.init())
.pipe(sass({
outputStyle: 'compressed',
includePaths: ['scss'],
onError: browserSync.notify
}).on('error', sass.logError))
.pipe(sourcemaps.write({includeContent: false}))
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(prefix())
.pipe(sourcemaps.write('./'))
.pipe(rename({ basename: 'main' }))
.pipe(gulp.dest(dist.css))
.pipe(browserSync.reload({ stream: true }))
.pipe(gulp.dest('assets/css'));
});
// JS
gulp.task('js', function() {
return browserify(src.js, {debug: true, extensions: ['es6']})
.transform('babelify', {presets: ['es2015']})
.bundle()
.on('error', handleErrors)
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(uglify())
.pipe(sourcemaps.write('./maps'))
.pipe(size())
.pipe(gulp.dest(dist.js))
.pipe(browserSync.reload({stream: true}))
.pipe(gulp.dest('assets/js'))
});
gulp.task('critical', function (cb) {
critical.generate({
base: '_site/',
src: 'index.html',
css: ['assets/css/main.css'],
dimensions: [{
width: 320,
height: 480
},{
width: 768,
height: 1024
},{
width: 1280,
height: 960
}],
dest: '../_includes/critical.css',
minify: true,
extract: false,
ignore: ['@font-face']
});
});
gulp.task('watch', function() {
gulp.watch('_sass/**/*.scss', ['sass']);
gulp.watch(['*.html', '_layouts/*.html', '_includes/*.html', '_posts/*.md', 'pages_/*.md', '_include/*html'], ['rebuild']);
gulp.watch(src.js, ['js']);
});
gulp.task('default', ['browser-sync', 'watch']);
// Minify HTML
gulp.task('html', function() {
gulp.src('./_site/index.html')
.pipe(htmlmin({ collapseWhitespace: true }))
.pipe(gulp.dest('./_site'))
gulp.src('./_site/*/*html')
.pipe(htmlmin({ collapseWhitespace: true }))
.pipe(gulp.dest('./_site/./'))
});
gulp.task('sw', function() {
const rootDir ='./';
const distDir = './_site';
sw.write(`${rootDir}/sw.js`, {
staticFileGlobs: [distDir + '/**/*.{js,html,css,png,jpg,svg}'],
stripPrefix: distDir
});
});
// Images
gulp.task('img', function() {
return gulp.src('_img/posts/*.{png,jpg}')
.pipe($.responsive({
// For all the images in the folder
'*': [{
width: 230,
rename: {suffix: '_placehold'},
}, {
// thubmnail
width: 535,
rename: { suffix: '_thumb' },
}, {
// thumbnail @2x
width: 535 * 2,
rename: { suffix: '_thumb@2x' },
}, {
width: 575,
rename: { suffix: '_xs'}
}, {
width: 767,
rename: {suffix: '_sm'}
}, {
width: 991,
rename: { suffix: '_md' }
}, {
// max-width hero
width: 1920,
}],
}, {
quality: 70,
progressive: true,
withMetadata: false,
errorOnEnlargement: false,
}))
.pipe(imagemin())
.pipe(gulp.dest('assets/img/posts/'));
});
gulp.task('clean', function () {
return gulp.src('_site', {read: false})
.pipe(clean());
});
gulp.task('serve', function() {
browserSync({
server: {
baseDir: '_site'
}
});
});
gulp.task('build', ['sass', 'js', 'jekyll-build', 'img', 'sw']);