This repository has been archived by the owner on May 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgulpfile.babel.js
393 lines (326 loc) · 10.8 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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// load plugins
const $ = require('gulp-load-plugins')()
// manually import modules that won't get picked up by gulp-load-plugins
import { src, dest, watch, parallel, series } from 'gulp'
import del from 'del'
import parallelize from 'concurrent-transform'
import browser from 'browser-sync'
import critical from 'critical'
import fs from 'fs'
import yaml from 'js-yaml'
import request from 'request'
// required to get our mix of old and ES6+ js to work with ugify-js 3
import uglifyjs from 'uglify-es'
import composer from 'gulp-uglify/composer'
const minify = composer(uglifyjs, console)
const cp = require('child_process')
// get all the configs: `pkg` and `site`
import pkg from './package.json'
const site = yaml.safeLoad(fs.readFileSync('./_config.yml'))
// handle errors
const onError = (error) => {
console.log($.util.colors.red('\nYou fucked up:', error.message, 'on line' , error.lineNumber, '\n'))
this.emit('end')
}
// 'development' is just default, production overrides are triggered
// by adding the production flag to the gulp command e.g. `gulp build --production`
const isProduction = ($.util.env.production === true ? true : false),
isStaging = ($.util.env.staging === true ? true : false)
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Config
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Port to use for the development server
const PORT = 1337
// paths
const SRC = site.source + '/',
DIST = site.destination + '/'
// deployment
const S3_BUCKET_LIVE = '2017.9984.io',
S3_BUCKET_BETA = 'beta2017.9984.io',
S3_OPTIONS_DEFAULT = '--delete --acl public-read',
S3_OPTIONS_CACHING = '--cache-control max-age=2592000,public'
// SVG sprite
const SPRITECONFIG = {
dest: DIST + 'assets/img/',
mode: {
symbol: {
dest: './',
sprite: 'sprite.svg'
}
}
}
// code banner
const BANNER = [
'/**',
' ** <%= pkg.name %>',
' ** <%= pkg.description %>',
' ** <%= pkg.homepage %>',
' **',
' ** <%= pkg.author.name %> <<%= pkg.author.email %>>',
' **/',
''
].join('\n')
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Tasks
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//
// Delete build artifacts
//
export const clean = () =>
del([
DIST + '**/*',
DIST + '.*' // delete all hidden files
])
//
// Jekyll
//
export const jekyll = (done) => {
browser.notify('Compiling Jekyll')
if (isProduction) {
process.env.JEKYLL_ENV = 'production'
var jekyll_options = 'jekyll build'
} else if (isStaging) {
process.env.JEKYLL_ENV = 'staging'
var jekyll_options = 'jekyll build'
} else {
process.env.JEKYLL_ENV = 'development'
var jekyll_options = 'jekyll build --incremental --drafts --future'
}
const jekyll = cp.execFile('bundle', ['exec', jekyll_options], { stdio: 'inherit' })
jekyll.on('error', (error) => onError() ).on('close', done)
}
//
// HTML
//
export const html = () => src(DIST + '**/*.html')
.pipe($.if(isProduction || isStaging, $.htmlmin({
collapseWhitespace: true,
conservativeCollapse: true,
removeComments: true,
useShortDoctype: true,
collapseBooleanAttributes: true,
removeRedundantAttributes: true,
removeEmptyAttributes: true,
minifyJS: true,
minifyCSS: true
})))
.pipe(dest(DIST))
//
// Styles
//
export const css = () => src([
SRC + '_assets/styles/9984.scss',
SRC + '_assets/styles/page-*.scss'
])
.pipe($.if(!(isProduction || isStaging), $.sourcemaps.init()))
.pipe($.sass({
includePaths: ['node_modules']
}).on('error', $.sass.logError))
.pipe($.autoprefixer())
.pipe($.if(isProduction || isStaging, $.cleanCss()))
.pipe($.if(!(isProduction || isStaging), $.sourcemaps.write()))
.pipe($.if(isProduction || isStaging, $.header(BANNER, { pkg: pkg })))
.pipe($.rename({ suffix: '.min' }))
.pipe(dest(DIST + 'assets/css/'))
.pipe(browser.stream())
// inline critical-path CSS
export const criticalCss = (done) => {
if (isProduction || isStaging) {
critical.generate({
base: DIST,
src: 'index.html',
dest: 'index.html',
inline: true,
minify: true,
dimensions: [{
height: 320,
width: 640
}, {
height: 600,
width: 800
}, {
height: 900,
width: 1360
}]
})
}
done()
}
//
// JavaScript
//
export const js = () =>
src([
SRC + '_assets/scripts/9984.js',
SRC + '_assets/scripts/page-*.js'
])
.pipe($.if(!(isProduction || isStaging), $.sourcemaps.init()))
.pipe($.include({
includePaths: ['node_modules', SRC + '_assets/scripts']
})).on('error', onError)
.pipe($.if(isProduction || isStaging, minify())).on('error', onError)
.pipe($.if(!(isProduction || isStaging), $.sourcemaps.write()))
.pipe($.if(isProduction || isStaging, $.header(BANNER, { pkg: pkg })))
.pipe($.rename({suffix: '.min'}))
.pipe(dest(DIST + 'assets/js/'))
//
// SVG sprite
//
export const svg = () => src(SRC + '_assets/images/*.svg')
.pipe($.if(isProduction || isStaging, $.imagemin({
svgoPlugins: [{ removeRasterImages: true }]
})))
.pipe($.svgSprite(SPRITECONFIG))
.pipe(dest(DIST + 'assets/img/'))
//
// Copy Images
//
export const images = () => src(SRC + '_assets/images/**/*')
.pipe($.if(isProduction || isStaging, $.imagemin([
$.imagemin.gifsicle({ interlaced: true }),
$.imagemin.jpegtran({ progressive: true }),
$.imagemin.optipng({ optimizationLevel: 5 }),
$.imagemin.svgo({plugins: [{ removeViewBox: true }]})
])))
.pipe(dest(DIST + 'assets/img/'))
//
// Copy Fonts
//
export const fonts = () => src(SRC + '_assets/fonts/**/*')
.pipe(dest(DIST + 'assets/fonts/'))
//
// Revision static assets
//
export const rev = (done) => {
// globbing is slow so do everything conditionally for faster dev build
if (isProduction || isStaging) {
return src(DIST + 'assets/**/*.{css,js,png,jpg,jpeg,svg,eot,ttf,woff,woff2}')
.pipe($.rev())
.pipe(dest(DIST + 'assets/'))
// output rev manifest for next replace task
.pipe($.rev.manifest())
.pipe(dest(DIST + 'assets/'))
}
done()
}
//
// Replace all links to assets in files
// from a manifest file
//
export const revReplace = (done) => {
// globbing is slow so do everything conditionally for faster dev build
if (isProduction || isStaging) {
let manifest = src(DIST + 'assets/rev-manifest.json')
return src(DIST + '**/*.{html,css,js}')
.pipe($.revReplace({ manifest: manifest }))
.pipe(dest(DIST))
}
done()
}
//
// Dev Server
//
export const server = (done) => {
browser.init({
server: DIST,
port: PORT,
reloadDebounce: 2000
})
done()
}
//
// Watch for file changes
//
export const watchSrc = () => {
watch(SRC + '_assets/styles/**/*.scss').on('all', series(css))
watch(SRC + '_assets/scripts/**/*.js').on('all', series(js, browser.reload))
watch(SRC + '_assets/images/**/*.{png,jpg,jpeg,gif,webp}').on('all', series(images, browser.reload))
watch(SRC + '_assets/images/**/*.{svg}').on('all', series(svg, browser.reload))
watch([SRC + '**/*.{html,xml,json,txt,md,yml}', './*.yml', SRC + '_includes/svg/*']).on('all', series('build', browser.reload))
}
//
// Build banner
//
const buildBanner = (done) => {
console.log($.util.colors.gray(" ------------------------------------------"))
console.log($.util.colors.green(' Building ' + ($.util.env.production ? 'production' : $.util.env.staging ? 'staging' : 'dev') + ' version...'))
console.log($.util.colors.gray(" ------------------------------------------"))
done()
}
//
// Deploy banner
//
const deployBanner = (done) => {
if (($.util.env.live || $.util.env.beta || $.util.env.gamma) === true ) {
console.log($.util.colors.gray(" ------------------------------------------"))
console.log($.util.colors.green(' Deploying to ' + ($.util.env.live ? 'Live' : $.util.env.beta ? 'Beta' : 'Gamma') + '... '))
console.log($.util.colors.gray(" ------------------------------------------"))
} else {
console.log($.util.colors.red('\nHold your horses! You need to specify a deployment target like so: gulp deploy --beta. Possible targets are: --live, --beta, --gamma\n'))
}
done()
}
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Collection tasks
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//
// Full build
//
// `gulp build` is the development build
// `gulp build --production` is the production build
//
export const build = series(buildBanner, clean, jekyll, parallel(html, css, js, images, svg, fonts), rev, revReplace, criticalCss)
//
// Build site, run server, and watch for file changes
//
// `gulp dev`
//
export const dev = series(build, server, watchSrc)
// Set `gulp dev` as default: `gulp`
export default dev
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Deployment
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//
// gulp deploy --live
// gulp deploy --beta
// gulp deploy --gamma
//
export const s3 = (cb) => {
let S3_BUCKET_TARGET
if ($.util.env.live === true) {
S3_BUCKET_TARGET = S3_BUCKET_LIVE
} else if ($.util.env.beta === true) {
S3_BUCKET_TARGET = S3_BUCKET_BETA
} else {
return
}
cp.exec(`aws s3 sync ${DIST} s3://${S3_BUCKET_TARGET} --exclude "assets/*" ${S3_OPTIONS_DEFAULT}`, (err) => cb(err))
cp.exec(`aws s3 sync ${DIST} s3://${S3_BUCKET_TARGET} --exclude "*" --include "assets/*" ${S3_OPTIONS_DEFAULT} ${S3_OPTIONS_CACHING}`, (err) => cb(err))
}
//
// Ping search engines on live deployment
//
export const seo = (done) => {
const googleUrl = 'http://www.google.com/webmasters/tools/ping?sitemap=',
bingUrl = 'http://www.bing.com/webmaster/ping.aspx?siteMap='
const response = (error, response) => {
if (error) {
$.util.log($.util.colors.red(error))
} else {
$.util.log($.util.colors.gray('Status:', response && response.statusCode))
if (response.statusCode === 200) {
$.util.log($.util.colors.green('Successfully notified'))
}
}
}
if ($.util.env.live === true) {
request(googleUrl + site.url + '/sitemap.xml', response)
request(bingUrl + site.url + '/sitemap.xml', response)
}
done()
}
//
// `gulp deploy`
//
export const deploy = series(deployBanner, s3, seo)