-
Notifications
You must be signed in to change notification settings - Fork 6
/
gulpfile.babel.js
239 lines (218 loc) · 5.86 KB
/
gulpfile.babel.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
/* eslint-env node, process */
"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 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
}
// SASS
gulp.task( "sass", () => {
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", () => {
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", done => {
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" ]
} );
done();
} );
// Minify HTML
gulp.task( "html", done => {
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/./" ) );
done();
} );
// Service Worker
gulp.task( "sw", () => {
const rootDir = "./";
const distDir = "./_site";
return sw.write( `${rootDir}/sw.js`, {
staticFileGlobs: [ distDir + "/**/*.{js,html,css,png,jpg,svg}" ],
stripPrefix: distDir
} );
} );
// Images
gulp.task( "img", () => {
return gulp.src( "_img/posts/*.{png,jpg}" )
.pipe( responsive( {
"*": [ // For all the images in the posts 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" }
},
{
width: 1999,
rename: { suffix: "_lg" }
},
{ // max-width hero
width: 1920
}
]
},
{
quality: 70,
progressive: true,
withMetadata: false,
errorOnEnlargement: false,
errorOnUnusedConfig: false,
silent: true
} ) )
.pipe( imagemin() )
.pipe( gulp.dest( "assets/img/posts/" ) );
} );
// Build the Jekyll Site
gulp.task( "jekyll-build", done => {
browserSync.notify( messages.jekyllBuild );
return cp.spawn( jekyll, [ "build" ], { stdio: "inherit" } )
.on( "close", done );
} );
// Rebuild Jekyll & do page reload
gulp.task( "rebuild",
gulp.series( [ "jekyll-build" ], done => {
browserSync.reload();
done();
} )
);
gulp.task( "clean", () => {
return gulp.src( "_site", { read: false, allowEmpty: true } )
.pipe( clean() );
} );
gulp.task( "serve", function() {
return browserSync( {
server: {
baseDir: "_site"
}
} );
} );
gulp.task( "styles", gulp.series( [ "sass", "critical" ] ) );
gulp.task( "watch", () => {
gulp.watch( "_sass/**/*.scss", gulp.series( "styles" ) );
gulp.watch( [
"*.html",
"_layouts/*.html",
"_includes/*.html",
"_posts/*.md",
"pages_/*.md",
"_include/*html"
], gulp.series( "rebuild" ) );
gulp.watch( "_js/**/*.js", gulp.series( "js" ) );
} );
gulp.task( "build", gulp.series( [
"clean",
gulp.parallel( [ "sass", "js", "img" ] ),
"jekyll-build",
"critical",
"sw"
] ) );
gulp.task( "default", gulp.series( [
"build",
"serve",
"watch"
] ) );