1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-11-25 11:18:35 +01:00
blog/gulpfile.babel.js

454 lines
12 KiB
JavaScript
Raw Normal View History

2016-10-05 17:13:28 +02:00
'use strict'
2017-04-03 20:33:40 +02:00
import { src, dest, parallel, series, watch } from 'gulp'
import plugins from 'gulp-load-plugins'
import del from 'del'
import parallelize from 'concurrent-transform'
import browser from 'browser-sync'
import autoprefixer from 'autoprefixer'
import cssnano from 'cssnano'
import critical from 'critical'
import request from 'request'
import fs from 'fs'
import yaml from 'js-yaml'
2017-05-28 05:09:32 +02:00
import chalk from 'chalk'
import yargs from 'yargs'
// get all the configs: `pkg` and `site`
import pkg from './package.json'
const site = yaml.safeLoad(fs.readFileSync('./_config.yml'))
2017-04-03 20:33:40 +02:00
// load plugins
const $ = plugins()
2015-08-09 18:11:26 +02:00
// handle errors
2017-03-25 20:44:13 +01:00
const onError = (error) => {
2017-05-28 05:09:32 +02:00
console.log('')
console.log(chalk.red('You fucked up:', error.message, 'on line' , error.lineNumber))
console.log('')
2016-10-05 17:13:28 +02:00
this.emit('end')
2015-08-09 18:11:26 +02:00
}
2015-08-30 14:02:02 +02:00
// 'development' is just default, production overrides are triggered
// by adding the production flag to the gulp command e.g. `gulp build --production`
2017-05-28 05:09:32 +02:00
const argv = yargs.parse(process.argv.slice(1))
const isProduction = argv.production
2015-08-09 18:11:26 +02:00
2015-06-07 20:53:49 +02:00
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Terminal Banner
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2016-10-05 17:13:28 +02:00
console.log("")
2017-05-28 05:09:32 +02:00
console.log(chalk.dim(" <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>"))
2016-10-05 17:13:28 +02:00
console.log("")
2017-05-28 05:09:32 +02:00
console.log(chalk.cyan(" (o) Just what do you think you're doing,", process.env.USER, "? "))
2016-10-05 17:13:28 +02:00
console.log("")
2017-05-28 05:09:32 +02:00
console.log(chalk.dim(" <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>"))
2016-10-05 17:13:28 +02:00
console.log("")
2015-06-07 20:53:49 +02:00
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Config
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2015-11-20 15:53:49 +01:00
// Port to use for the development server.
2017-03-25 20:44:13 +01:00
const PORT = 1337
2015-11-20 15:53:49 +01:00
// paths
const SRC = site.source,
DIST = site.destination,
2017-03-25 20:44:13 +01:00
S3BUCKET = 'kremalicious.com',
S3PATH = '/',
S3REGION = 'eu-central-1'
// SVG sprite
2017-05-28 04:06:20 +02:00
const SPRITECONFIG = {
2015-11-20 15:53:49 +01:00
dest: DIST + '/assets/img/',
mode: {
symbol: {
dest: './',
2015-06-08 21:22:09 +02:00
sprite: 'sprite.svg'
}
}
}
// code banner
2017-03-25 20:44:13 +01:00
const BANNER = [
'/**',
2015-06-07 03:11:46 +02:00
' ** <%= pkg.name %> v<%= pkg.version %>',
' ** <%= pkg.description %>',
2015-06-08 10:53:12 +02:00
' ** <%= pkg.homepage %>',
' **',
2015-06-07 03:11:46 +02:00
' ** <%= pkg.author.name %> <<%= pkg.author.email %>>',
2015-06-08 10:53:12 +02:00
' **',
' ** YOU EARNED THE GEEK ACHIEVEMENT ',
' ** FOR LOOKING AT MY SOURCE ',
' **',
' ** But because of all the minimizing and caching ',
' ** going on you better check out the code on ',
' ** github ',
' ** ',
' ** <%= pkg.repository.url %> ',
' **/',
''
2016-10-05 17:13:28 +02:00
].join('\n')
2015-11-21 00:10:20 +01:00
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2017-03-25 23:39:42 +01:00
// Tasks
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//
// Delete build artifacts
//
2017-03-25 23:39:42 +01:00
export const clean = () =>
del([
2017-03-25 22:05:01 +01:00
DIST + '**/*',
DIST + '.*', // delete all hidden files
2017-04-03 20:33:40 +02:00
'!' + DIST + '/media'
2017-03-25 22:05:01 +01:00
])
//
// Jekyll
//
2017-03-25 23:39:42 +01:00
export const jekyll = (done) => {
2015-11-20 23:02:31 +01:00
2017-03-25 22:05:01 +01:00
browser.notify('Compiling Jekyll')
2015-08-30 14:02:02 +02:00
if (isProduction) {
2016-10-05 17:13:28 +02:00
process.env.JEKYLL_ENV = 'production'
2017-03-25 22:05:01 +01:00
var jekyll_options = 'jekyll build --lsi'
2015-08-30 14:02:02 +02:00
} else {
2017-03-25 22:05:01 +01:00
var jekyll_options = 'jekyll build --config _config.yml,_config.dev.yml --incremental --drafts --future'
2015-08-30 14:02:02 +02:00
}
2015-06-07 03:11:46 +02:00
2017-03-25 23:39:42 +01:00
let spawn = require('child_process').spawn,
2017-03-25 22:05:01 +01:00
jekyll = spawn('bundle', ['exec', jekyll_options], { stdio: 'inherit' })
2017-03-25 23:39:42 +01:00
jekyll.on('error', (error) => onError() ).on('close', done)
2017-03-25 22:05:01 +01:00
}
2015-06-07 03:11:46 +02:00
2017-04-03 20:33:40 +02:00
2015-08-30 14:02:02 +02:00
//
// HTML
//
2017-03-25 23:39:42 +01:00
export const html = () => src(DIST + '/**/*.html')
.pipe($.if(isProduction, $.htmlmin({
collapseWhitespace: true,
conservativeCollapse: true,
removeComments: true,
useShortDoctype: true,
collapseBooleanAttributes: true,
removeRedundantAttributes: true,
removeEmptyAttributes: true,
minifyJS: true,
minifyCSS: true
})))
.pipe(dest(DIST))
//
// Styles
//
2017-03-25 23:39:42 +01:00
const processors = [
2017-05-28 04:24:33 +02:00
autoprefixer(),
2017-03-25 23:39:42 +01:00
cssnano()
]
2017-03-26 01:17:11 +01:00
2017-03-25 23:39:42 +01:00
export const css = () =>
src([
SRC + '/_assets/styl/kremalicious3.styl',
SRC + '/_assets/styl/post-*.styl'
])
.pipe($.if(!isProduction, $.sourcemaps.init()))
.pipe($.stylus({ 'include css': true })).on('error', onError)
.pipe($.postcss(processors)).on('error', onError)
.pipe($.if(!isProduction, $.sourcemaps.write()))
.pipe($.if(isProduction, $.header(BANNER, { pkg: pkg })))
.pipe($.rename({ suffix: '.min' }))
.pipe(dest(DIST + '/assets/css/'))
.pipe(browser.stream())
2017-04-03 20:33:40 +02:00
// inline critical-path CSS
export const criticalCss = (done) => {
if (isProduction) {
critical.generate({
base: DIST,
src: 'index.html',
dest: 'index.html',
inline: true,
minify: true,
dimensions: [{
height: 320,
width: 480
}, {
height: 600,
width: 650
}, {
height: 700,
width: 960
}, {
height: 900,
width: 1400
}]
})
}
done()
}
//
// Scripts
//
2017-05-28 03:42:02 +02:00
const js = () =>
2017-03-26 01:17:11 +01:00
src([
SRC + '/_assets/js/kremalicious3.js',
'node_modules/picturefill/dist/picturefill.js'
])
2017-03-25 23:39:42 +01:00
.pipe($.sourcemaps.init())
2017-05-28 03:32:47 +02:00
.pipe($.include({
includePaths: ['node_modules', SRC + '/_assets/js']
})).on('error', onError)
2017-03-25 23:39:42 +01:00
.pipe($.if(isProduction, $.uglify())).on('error', onError)
.pipe($.if(!isProduction, $.sourcemaps.write()))
.pipe($.if(isProduction, $.header(BANNER, { pkg: pkg })))
.pipe($.rename({suffix: '.min'}))
.pipe(dest(DIST + '/assets/js/'))
//
2017-04-03 20:33:40 +02:00
// Images
//
2017-04-03 22:57:43 +02:00
const imageminPlugins = [
$.imagemin.gifsicle({ interlaced: true }),
$.imagemin.jpegtran(),
2017-04-03 22:57:43 +02:00
$.imagemin.optipng({ optimizationLevel: 5 }),
$.imagemin.svgo({ plugins: [{removeViewBox: false }]})
]
2017-05-28 04:06:20 +02:00
// Copy all images
export const images = () => src(SRC + '/_assets/img/**/*')
.pipe($.if(isProduction, $.imagemin(imageminPlugins)))
.pipe(dest(DIST + '/assets/img/'))
// SVG sprite
export const svg = () => src(SRC + '/_assets/img/**/*.svg')
2017-04-03 22:57:43 +02:00
.pipe($.if(isProduction, $.imagemin(imageminPlugins)))
2017-05-28 04:06:20 +02:00
.pipe($.svgSprite(SPRITECONFIG))
2017-03-25 23:39:42 +01:00
.pipe(dest(DIST + '/assets/img/'))
//
// Copy fonts
//
2017-03-25 23:39:42 +01:00
export const fonts = () => src(SRC + '/_assets/fonts/**/*')
.pipe(dest(DIST + '/assets/fonts/'))
//
// Copy media
//
2017-03-25 23:39:42 +01:00
export const media = () => src(SRC + '/_media/**/*')
2017-04-03 20:33:40 +02:00
.pipe(dest(DIST + '/media/'))
2015-06-07 20:53:49 +02:00
2017-03-26 01:17:11 +01:00
//
// Revision static assets
//
export const rev = (done) => {
// globbing is slow so do everything conditionally for faster dev build
if (isProduction) {
2017-04-03 20:33:40 +02:00
return src(DIST + '/assets/**/*.{css,js,png,jpg,jpeg,svg,eot,ttf,woff,woff2}')
2017-03-26 01:17:11 +01:00
.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
//
2017-03-25 23:39:42 +01:00
export const revReplace = (done) => {
2015-11-20 15:53:49 +01:00
// globbing is slow so do everything conditionally for faster dev build
if (isProduction) {
2017-04-03 20:33:40 +02:00
let manifest = src(DIST + '/assets/rev-manifest.json')
2017-03-25 23:39:42 +01:00
return src(DIST + '/**/*.{html,css,js}')
.pipe($.revReplace({ manifest: manifest }))
.pipe(dest(DIST))
2015-11-20 15:53:49 +01:00
}
2017-03-25 22:05:01 +01:00
done()
}
2015-06-07 03:11:46 +02:00
//
// Ping search engines
//
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) {
2017-05-28 05:09:32 +02:00
console.log(chalk.red(error))
} else {
2017-05-28 05:09:32 +02:00
console.log(chalk.dim('Status:', response && response.statusCode))
if (response.statusCode === 200) {
2017-05-28 05:09:32 +02:00
console.log(chalk.green('Successfully notified'))
}
}
}
request(googleUrl + site.url + '/sitemap.xml', response)
request(bingUrl + site.url + '/sitemap.xml', response)
done()
}
//
// Dev Server
//
2017-03-25 23:39:42 +01:00
export const server = (done) => {
2015-11-20 15:53:49 +01:00
browser.init({
server: DIST,
2016-05-25 22:46:46 +02:00
port: PORT,
reloadDebounce: 2000
2016-10-05 17:13:28 +02:00
})
2017-03-25 22:05:01 +01:00
done()
}
2015-08-30 14:02:02 +02:00
//
2017-03-25 22:05:01 +01:00
// Watch for file changes
//
2017-03-25 23:39:42 +01:00
export const watchSrc = () => {
2017-04-03 20:33:40 +02:00
watch(SRC + '/_assets/styl/**/*.styl').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))
2017-05-28 04:06:20 +02:00
watch(SRC + '/_assets/img/**/*.{svg}').on('all', series(svg, browser.reload))
2017-04-03 20:33:40 +02:00
watch(SRC + '/_media/**/*').on('all', series(media, browser.reload))
2017-03-25 23:39:42 +01:00
watch([SRC + '/**/*.{html,xml,json,txt,md,yml}', './*.yml', SRC + '_includes/svg/*']).on('all', series('build', browser.reload))
2017-03-25 22:05:01 +01:00
}
2015-08-30 14:02:02 +02:00
2017-03-25 23:39:42 +01:00
//
// Build banner
//
export const buildBanner = (done) => {
2017-05-28 05:09:32 +02:00
console.log(chalk.dim(" ------------------------------------------"))
console.log(chalk.green(' Building ' + (isProduction ? 'production' : 'dev') + ' version...'))
console.log(chalk.dim(" ------------------------------------------"))
2017-03-25 23:39:42 +01:00
done()
}
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Collection tasks
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//
// Full build
//
// `gulp build` is the development build
// `gulp build --production` is the production build
//
2017-07-23 20:40:33 +02:00
export const build = series(
buildBanner,
clean,
jekyll,
parallel(html, css, js, svg, images, fonts, media),
rev,
revReplace,
criticalCss
)
2017-03-25 23:39:42 +01:00
//
// 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
2017-03-25 22:05:01 +01:00
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Deployment
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2017-03-25 23:39:42 +01:00
// create publisher, define config
const publisher = $.awspublish.create({
params: {
'Bucket': S3BUCKET
},
'accessKeyId': process.env.AWS_ACCESS_KEY,
'secretAccessKey': process.env.AWS_SECRET_KEY,
'region': S3REGION
})
2017-03-25 23:39:42 +01:00
export const s3 = () => src(DIST + '/**/*')
.pipe($.awspublishRouter({
cache: {
// cache for 5 minutes by default
cacheTime: 300
},
2017-03-25 23:39:42 +01:00
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
},
2017-03-25 23:39:42 +01:00
// every other asset, cached
'^assets/.+$': {
cacheTime: 2592000 // cache for 1 month
},
2017-03-25 23:39:42 +01:00
// all html files, not cached & gzipped
'^.+\\.html': {
cacheTime: 0,
gzip: true
},
// font mime types
'\.ttf$': {
key: '$&',
headers: { 'Content-Type': 'application/x-font-ttf' }
},
'\.woff$': {
key: '$&',
headers: { 'Content-Type': 'application/x-font-woff' }
},
'\.woff2$': {
key: '$&',
headers: { 'Content-Type': 'application/x-font-woff2' }
},
// pass-through for anything that wasn't matched by routes above, to be uploaded with default options
"^.+$": "$&"
}
}))
// make sure everything goes to the root '/'
.pipe($.rename(function (path) {
path.dirname = S3PATH + path.dirname
}))
.pipe(parallelize(publisher.publish(), 100))
.pipe(publisher.sync()) // delete files in bucket that are not in local folder
.pipe($.awspublish.reporter({
states: ['create', 'update', 'delete']
}))
// `gulp deploy`
export const deploy = series(s3)