website/gulpfile.babel.js

491 lines
13 KiB
JavaScript
Raw Normal View History

2017-08-24 11:39:07 +02:00
/* eslint-disable no-console */
import fs from 'fs'
2018-09-28 12:08:19 +02:00
import cp from 'child_process'
2017-08-09 17:12:21 +02:00
import { src, dest, watch, parallel, series } from 'gulp'
2017-08-24 11:39:07 +02:00
import del from 'del'
import parallelize from 'concurrent-transform'
import browser from 'browser-sync'
import critical from 'critical'
import yaml from 'js-yaml'
import request from 'request'
2018-09-28 13:06:58 +02:00
import chalk from 'chalk'
import minimist from 'minimist'
2017-08-09 17:12:21 +02:00
// required to get our mix of old and ES6+ js to work with ugify-js 3
2017-08-24 11:39:07 +02:00
import uglifyjs from 'uglify-es'
import composer from 'gulp-uglify/composer'
2017-08-09 17:12:21 +02:00
// get all the configs: `pkg` and `site`
2018-09-28 12:08:19 +02:00
import pkg from './package'
2017-08-24 11:39:07 +02:00
// load plugins
const $ = require('gulp-load-plugins')()
const minify = composer(uglifyjs, console)
2017-08-09 17:12:21 +02:00
const site = yaml.safeLoad(fs.readFileSync('./_config.yml'))
// handle errors
const onError = (error) => {
2018-09-28 13:06:58 +02:00
console.log(chalk.red('\nYou fucked up:', error.message, 'on line', error.lineNumber, '\n'))
2017-08-09 17:12:21 +02:00
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`
2018-09-28 13:06:58 +02:00
const options = minimist(process.argv.slice(2))
const isProduction = options.production === true
const isStaging = options.staging === true
2017-08-09 17:12:21 +02:00
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Config
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Port to use for the development server
const PORT = 1337
// paths
2017-08-24 11:39:07 +02:00
const SRC = `${site.source}/`
const DIST = `${site.destination}/`
2017-08-09 17:12:21 +02:00
// deployment
const S3BUCKET = 'ipdb.io'
2017-08-24 11:39:07 +02:00
const S3REGION = 'eu-central-1'
const S3BUCKET_BETA = 'beta.ipdb.io'
const S3REGION_BETA = 'eu-central-1'
2017-08-09 17:12:21 +02:00
// SVG sprite
const SPRITECONFIG = {
2017-08-24 11:39:07 +02:00
dest: `${DIST}assets/img/`,
mode: {
symbol: {
dest: './',
sprite: 'sprite.svg'
}
}
2017-08-09 17:12:21 +02:00
}
// code banner
const BANNER = [
2017-08-24 11:39:07 +02:00
'/**',
' ** <%= pkg.name %>',
' ** <%= pkg.description %>',
' ** <%= pkg.homepage %>',
' **',
' ** <%= pkg.author.name %> <<%= pkg.author.email %>>',
' **/',
''
2017-08-09 17:12:21 +02:00
].join('\n')
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Tasks
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//
// Delete build artifacts
//
export const clean = () =>
del([
2017-08-24 11:39:07 +02:00
`${DIST}**/*`,
`${DIST}.*` // delete all hidden files
2017-08-09 17:12:21 +02:00
])
//
// Jekyll
//
export const jekyll = (done) => {
browser.notify('Compiling Jekyll')
2017-08-24 11:39:07 +02:00
let jekyllOptions
2017-08-09 17:12:21 +02:00
if (isProduction) {
process.env.JEKYLL_ENV = 'production'
2017-08-24 11:39:07 +02:00
jekyllOptions = 'jekyll build'
2017-08-09 17:12:21 +02:00
} else if (isStaging) {
process.env.JEKYLL_ENV = 'staging'
2017-08-24 11:39:07 +02:00
jekyllOptions = 'jekyll build'
2017-08-09 17:12:21 +02:00
} else {
process.env.JEKYLL_ENV = 'development'
2017-08-24 11:39:07 +02:00
jekyllOptions = 'jekyll build --incremental --drafts --future'
2017-08-09 17:12:21 +02:00
}
2018-09-28 12:08:19 +02:00
const jekyllInstance = cp.execFile('bundle', ['exec', jekyllOptions], { stdio: 'inherit' })
const jekyllLogger = (buffer) => {
buffer.toString()
.split(/\n/)
.forEach((message) => console.log(message))
}
2017-08-09 17:12:21 +02:00
2018-09-28 12:08:19 +02:00
jekyllInstance.stdout.on('data', jekyllLogger).on('close', done)
2017-08-09 17:12:21 +02:00
}
//
// HTML
//
2017-08-24 11:39:07 +02:00
export const html = () => src(`${DIST}**/*.html`)
2017-08-09 17:12:21 +02:00
.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([
2017-08-24 11:39:07 +02:00
`${SRC}_assets/scss/ipdb.scss`,
`${SRC}_assets/scss/page-*.scss`
])
2017-08-09 17:12:21 +02:00
.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()))
2017-08-24 11:39:07 +02:00
.pipe($.if(isProduction || isStaging, $.header(BANNER, { pkg })))
2017-08-09 17:12:21 +02:00
.pipe($.rename({ suffix: '.min' }))
2017-08-24 11:39:07 +02:00
.pipe(dest(`${DIST}assets/css/`))
2017-08-09 17:12:21 +02:00
.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([
2017-08-24 11:39:07 +02:00
`${SRC}_assets/js/ipdb.js`,
`${SRC}_assets/js/page-*.js`
2017-08-09 17:12:21 +02:00
])
2017-08-24 11:39:07 +02:00
.pipe($.if(!(isProduction || isStaging), $.sourcemaps.init()))
.pipe($.include({
includePaths: ['node_modules', `${SRC}_assets/js`]
})).on('error', onError)
.pipe($.if(isProduction || isStaging, minify()))
.on('error', onError)
.pipe($.if(!(isProduction || isStaging), $.sourcemaps.write()))
.pipe($.if(isProduction || isStaging, $.header(BANNER, { pkg })))
.pipe($.rename({ suffix: '.min' }))
.pipe(dest(`${DIST}assets/js/`))
2017-08-09 17:12:21 +02:00
//
// SVG sprite
//
2017-08-24 11:39:07 +02:00
export const svg = () => src(`${SRC}_assets/img/*.svg`)
2017-08-09 17:12:21 +02:00
.pipe($.if(isProduction || isStaging, $.imagemin({
svgoPlugins: [{ removeRasterImages: true }]
})))
.pipe($.svgSprite(SPRITECONFIG))
2017-08-24 11:39:07 +02:00
.pipe(dest(`${DIST}assets/img/`))
2017-08-09 17:12:21 +02:00
//
// Copy Images
//
2017-08-24 11:39:07 +02:00
export const images = () => src(`${SRC}_assets/img/**/*`)
2017-08-09 17:12:21 +02:00
.pipe($.if(isProduction || isStaging, $.imagemin([
2017-08-24 11:39:07 +02:00
$.imagemin.gifsicle({ interlaced: true }),
$.imagemin.jpegtran({ progressive: true }),
$.imagemin.optipng({ optimizationLevel: 5 }),
$.imagemin.svgo({ plugins: [{ removeViewBox: true }] })
2017-08-09 17:12:21 +02:00
])))
2017-08-24 11:39:07 +02:00
.pipe(dest(`${DIST}assets/img/`))
2017-08-09 17:12:21 +02:00
//
// Revision static assets
//
export const rev = (done) => {
// globbing is slow so do everything conditionally for faster dev build
if (isProduction || isStaging) {
2017-08-24 11:39:07 +02:00
return src(`${DIST}assets/**/*.{css,js,png,jpg,jpeg,svg,eot,ttf,woff,woff2}`)
2017-08-09 17:12:21 +02:00
.pipe($.rev())
2017-08-24 11:39:07 +02:00
.pipe(dest(`${DIST}assets/`))
2017-08-09 17:12:21 +02:00
// output rev manifest for next replace task
.pipe($.rev.manifest())
2017-08-24 11:39:07 +02:00
.pipe(dest(`${DIST}assets/`))
2017-08-09 17:12:21 +02:00
}
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) {
2017-08-24 11:39:07 +02:00
const manifest = src(`${DIST}assets/rev-manifest.json`)
2017-08-09 17:12:21 +02:00
2017-08-24 11:39:07 +02:00
return src(`${DIST}**/*.{html,css,js}`)
.pipe($.revReplace({ manifest }))
2017-08-09 17:12:21 +02:00
.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 = () => {
2017-08-24 11:39:07 +02:00
watch(`${SRC}_assets/scss/**/*.scss`).on('all', series(css))
watch(`${SRC}_assets/js/**/*.js`).on('all', series(js, browser.reload))
watch(`${SRC}_assets/img/**/*.{png,jpg,jpeg,gif,webp}`).on('all', series(images, browser.reload))
watch(`${SRC}_assets/img/**/*.{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))
2017-08-09 17:12:21 +02:00
}
//
// Build banner
//
2017-08-24 11:39:07 +02:00
/* eslint-disable max-len */
2017-08-09 17:12:21 +02:00
const buildBanner = (done) => {
2017-08-24 11:39:07 +02:00
let buildEnvironment
2018-09-28 13:06:58 +02:00
if (isProduction) {
2017-08-24 11:39:07 +02:00
buildEnvironment = 'production'
2018-09-28 13:06:58 +02:00
} else if (isStaging) {
2017-08-24 11:39:07 +02:00
buildEnvironment = 'staging'
} else {
buildEnvironment = 'dev'
}
2018-09-28 13:06:58 +02:00
console.log(chalk.gray(' ------------------------------------------'))
console.log(chalk.green(` Building ${buildEnvironment} version...`))
console.log(chalk.gray(' ------------------------------------------'))
2017-08-09 17:12:21 +02:00
done()
}
//
// Deploy banner
//
const deployBanner = (done) => {
2017-08-24 11:39:07 +02:00
let deployTarget
2018-09-28 13:06:58 +02:00
if (options.live) {
2017-08-24 11:39:07 +02:00
deployTarget = 'Live'
} else {
deployTarget = 'Beta'
2017-08-24 11:39:07 +02:00
}
2018-09-28 13:06:58 +02:00
if ((options.live || options.beta || options.gamma) === true) {
console.log(chalk.gray(' ------------------------------------------'))
console.log(chalk.green(` Deploying to ${deployTarget}... `))
console.log(chalk.gray(' ------------------------------------------'))
2017-08-09 17:12:21 +02:00
} else {
2018-09-28 13:06:58 +02:00
console.log(chalk.red('\nHold your horses! You need to specify a deployment target like so: gulp deploy --beta. Possible targets are: --live, --beta, --gamma\n'))
2017-08-09 17:12:21 +02:00
}
done()
}
2017-08-24 11:39:07 +02:00
/* eslint-enable max-len */
2017-08-09 17:12:21 +02:00
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Collection tasks
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//
// Full build
//
// `gulp build` is the development build
// `gulp build --production` is the production build
//
2017-08-24 11:39:07 +02:00
export const build = series(
buildBanner, clean, jekyll,
parallel(html, css, js, images, svg),
rev, revReplace, criticalCss
)
2017-08-09 17:12:21 +02:00
//
// Build site, run server, and watch for file changes
//
// `gulp dev`
//
2018-09-28 12:08:19 +02:00
export const dev = series(build, server, watchSrc)
2017-08-09 17:12:21 +02:00
// Set `gulp dev` as default: `gulp`
export default dev
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Deployment
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//
// gulp deploy --live
// gulp deploy --beta
// gulp deploy --gamma
//
2018-09-28 13:22:26 +02:00
export const s3 = (done) => {
2017-08-09 17:12:21 +02:00
// create publisher, define config
2017-08-24 11:39:07 +02:00
let publisher
2018-09-28 13:06:58 +02:00
if (options.live === true) {
2017-08-24 11:39:07 +02:00
publisher = $.awspublish.create({
'params': { 'Bucket': S3BUCKET },
2017-08-09 17:12:21 +02:00
'accessKeyId': process.env.AWS_ACCESS_KEY,
'secretAccessKey': process.env.AWS_SECRET_KEY,
'region': S3REGION
})
2018-09-28 13:06:58 +02:00
} else if (options.beta === true) {
2017-08-24 11:39:07 +02:00
publisher = $.awspublish.create({
'params': { 'Bucket': S3BUCKET_BETA },
2017-08-09 17:12:21 +02:00
'accessKeyId': process.env.AWS_BETA_ACCESS_KEY,
'secretAccessKey': process.env.AWS_BETA_SECRET_KEY,
'region': S3REGION_BETA
})
} else {
return
}
2017-08-24 11:39:07 +02:00
src(`${DIST}**/*`)
2017-08-09 17:12:21 +02:00
.pipe($.awspublishRouter({
cache: {
// cache for 5 minutes by default
cacheTime: 300
},
routes: {
// all static assets, cached & gzipped
'^assets/(?:.+)\\.(?:js|css|png|jpg|jpeg|gif|ico|svg|ttf|eot|woff|woff2)$': {
cacheTime: 2592000, // cache for 1 month
gzip: true
},
// every other asset, cached
'^assets/.+$': {
2017-08-24 11:39:07 +02:00
cacheTime: 2592000 // cache for 1 month
2017-08-09 17:12:21 +02:00
},
// all html files, not cached & gzipped
'^.+\\.html': {
cacheTime: 0,
gzip: true
},
// all pdf files, not cached
'^.+\\.pdf': {
cacheTime: 0
},
// font mime types
2017-08-24 11:39:07 +02:00
'.ttf$': {
2017-08-09 17:12:21 +02:00
key: '$&',
headers: { 'Content-Type': 'application/x-font-ttf' }
},
2017-08-24 11:39:07 +02:00
'.woff$': {
2017-08-09 17:12:21 +02:00
key: '$&',
headers: { 'Content-Type': 'application/x-font-woff' }
},
2017-08-24 11:39:07 +02:00
'.woff2$': {
2017-08-09 17:12:21 +02:00
key: '$&',
headers: { 'Content-Type': 'application/x-font-woff2' }
},
2017-08-24 11:39:07 +02:00
// pass-through for anything that wasn't matched by routes above,
// to be uploaded with default options
'^.+$': '$&'
2017-08-09 17:12:21 +02:00
}
}))
.pipe(parallelize(publisher.publish(), 100)).on('error', onError)
.pipe(publisher.sync()) // delete files in bucket that are not in local folder
.pipe($.awspublish.reporter({
states: ['create', 'update', 'delete']
}))
2018-09-28 13:22:26 +02:00
done()
2017-08-09 17:12:21 +02:00
}
//
// Ping search engines on live deployment
//
export const seo = (done) => {
2017-08-24 11:39:07 +02:00
const googleUrl = 'http://www.google.com/webmasters/tools/ping?sitemap='
const bingUrl = 'http://www.bing.com/webmaster/ping.aspx?siteMap='
2017-08-09 17:12:21 +02:00
2017-08-24 11:39:07 +02:00
const showResponse = (error, response) => {
2017-08-09 17:12:21 +02:00
if (error) {
2018-09-28 13:06:58 +02:00
console.log(chalk.red(error))
2017-08-09 17:12:21 +02:00
} else {
2018-09-28 13:06:58 +02:00
console.log(chalk.gray('Status:', response && response.statusCode))
2017-08-09 17:12:21 +02:00
if (response.statusCode === 200) {
2018-09-28 13:06:58 +02:00
console.log(chalk.green('Successfully notified'))
2017-08-09 17:12:21 +02:00
}
}
}
2018-09-28 13:06:58 +02:00
if (options.live === true) {
2017-08-24 11:39:07 +02:00
request(`${googleUrl + site.url}/sitemap.xml`, showResponse)
request(`${bingUrl + site.url}/sitemap.xml`, showResponse)
}
2017-08-09 17:12:21 +02:00
done()
}
//
// `gulp deploy`
//
export const deploy = series(deployBanner, s3, seo)