1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-09-24 01:58:50 +02:00
blog/gulpfile.js

392 lines
11 KiB
JavaScript
Raw Normal View History

// load plugins
var $ = require('gulp-load-plugins')();
// manually require modules that won"t get picked up by gulp-load-plugins
var gulp = require('gulp'),
del = require('del'),
2015-08-09 18:11:26 +02:00
chalk = require('chalk'),
merge = require('merge-stream'),
2015-08-23 09:45:57 +02:00
pkg = require('./package.json'),
2015-09-13 23:55:00 +02:00
parallelize = require('concurrent-transform'),
2015-11-20 15:53:49 +01:00
browser = require('browser-sync');
// Temporary solution until gulp 4
// https://github.com/gulpjs/gulp/issues/355
var runSequence = require('run-sequence');
2015-08-09 18:11:26 +02:00
// handle errors
var onError = function(error) {
console.log(chalk.red('You fucked up:', error.message, 'on line' , error.lineNumber));
2015-08-29 21:57:46 +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`
var isProduction = ($.util.env.production === true ? true : false);
2015-08-09 18:11:26 +02:00
2015-06-07 20:53:49 +02:00
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Terminal Banner
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
console.log("");
2015-08-29 21:57:46 +02:00
console.log(chalk.gray(" <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>"));
2015-06-07 20:53:49 +02:00
console.log("");
2015-09-26 22:22:40 +02:00
console.log(chalk.cyan(" (o) Just what do you think you're doing,", process.env.USER, "? "));
2015-06-07 20:53:49 +02:00
console.log("");
2015-08-29 21:57:46 +02:00
console.log(chalk.gray(" <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>"));
2015-06-07 20:53:49 +02:00
console.log("");
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Config
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2015-11-20 15:53:49 +01:00
// Port to use for the development server.
var PORT = 1337;
// Browsers to target when prefixing CSS.
var COMPATIBILITY = ['last 2 versions', 'ie >= 9'];
// paths
2015-11-20 15:53:49 +01:00
var SRC = '_src',
DIST = '_site',
CDN = 'https://cdn.kremalicious.com',
S3BUCKET = 'kremalicious.com',
S3PATH = '/',
S3REGION = 'eu-central-1';
// icons
2015-11-20 15:53:49 +01:00
var ICONS = {
entypo: {
2015-11-20 15:53:49 +01:00
src: SRC + '/_assets/icons/entypo/',
dist: DIST + '/assets/img/',
prefix: 'entypo-',
2015-06-08 19:40:53 +02:00
icons: [
2015-06-22 21:28:31 +02:00
'twitter', 'facebook', 'google+', 'magnifying-glass', 'menu', 'rss', 'link', 'arrow-with-circle-down', 'forward', 'heart', 'info-with-circle', 'infinity', 'github', 'chevron-right', 'chevron-left', 'eye'
2015-06-08 19:40:53 +02:00
]
}
}
2015-11-20 15:53:49 +01:00
var iconset = ICONS.entypo;
// Iterate through the icon set array
iconset.icons.forEach(function(icon, i, icons) {
icons[i] = iconset.src + icon + '.svg';
});
// SVG sprite
2015-11-20 15:53:49 +01:00
var SPRITE = {
dest: DIST + '/assets/img/',
mode: {
symbol: {
dest: './',
2015-06-08 21:22:09 +02:00
sprite: 'sprite.svg'
}
}
}
// code banner
2015-11-20 15:53:49 +01:00
var 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 %> ',
' **/',
''
].join('\n');
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Tasks
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//
// Delete build artifacts
//
2015-11-20 15:53:49 +01:00
gulp.task('clean', function(done) {
return del([
2015-11-20 15:53:49 +01:00
DIST + '/**/*',
DIST + '/.*', // delete all hidden files
'!' + DIST + '/media/**'
], done)
});
//
// Jekyll
//
gulp.task('jekyll', function(cb) {
var spawn = require('child_process').spawn;
2015-08-30 14:02:02 +02:00
if (isProduction) {
2015-11-20 15:53:49 +01:00
var jekyll = spawn('bundle', ['exec', 'jekyll', 'build', '--lsi', 'JEKYLL_ENV=production'], { stdio: 'inherit' });
2015-08-30 14:02:02 +02:00
} else {
2015-11-20 15:53:49 +01:00
var jekyll = spawn('bundle', ['exec', 'jekyll', 'build', '--config', '_config.yml,_config.dev.yml', '--drafts', '--future', '--incremental'], { stdio: 'inherit' });
2015-08-30 14:02:02 +02:00
}
2015-06-07 03:11:46 +02:00
jekyll.on('exit', function(code) {
cb(code === 0 ? null : 'ERROR: Jekyll process exited with code: ' + code);
2015-06-07 03:11:46 +02:00
});
});
2015-08-30 14:02:02 +02:00
//
// HTML
//
gulp.task('html', function() {
2015-11-20 15:53:49 +01:00
return gulp.src(DIST + '/**/*.html')
2015-08-30 14:02:02 +02:00
.pipe($.if(isProduction, $.htmlmin({
collapseWhitespace: true,
conservativeCollapse: true,
removeComments: true,
useShortDoctype: true,
collapseBooleanAttributes: true,
removeRedundantAttributes: true,
removeEmptyAttributes: true
})))
2015-11-20 15:53:49 +01:00
.pipe(gulp.dest(DIST))
});
//
// Styles
//
gulp.task('css', function() {
return gulp.src([
2015-11-20 15:53:49 +01:00
SRC + '/_assets/styl/kremalicious3.styl',
SRC + '/_assets/styl/post-*.styl'
])
2015-11-20 15:53:49 +01:00
.pipe($.sourcemaps.init())
2015-08-20 20:06:14 +02:00
.pipe($.stylus({ 'include css': true })).on('error', onError)
2015-11-20 15:53:49 +01:00
.pipe($.autoprefixer({ browsers: COMPATIBILITY }))
2015-08-30 14:02:02 +02:00
.pipe($.if(isProduction, $.cssmin()))
2015-11-20 15:53:49 +01:00
.pipe($.if(!isProduction, $.sourcemaps.write()))
.pipe($.if(isProduction, $.header(BANNER, { pkg: pkg })))
2015-06-22 21:28:31 +02:00
.pipe($.rename({ suffix: '.min' }))
2015-11-20 15:53:49 +01:00
.pipe(gulp.dest(DIST + '/assets/css/'))
});
//
// Scripts
//
// Libraries
gulp.task('js:libraries', function() {
return gulp.src([
2015-11-20 15:53:49 +01:00
'node_modules/picturefill/DIST/picturefill.js'
2015-08-30 15:05:08 +02:00
])
.pipe($.if(isProduction, $.uglify())).on('error', onError)
.pipe($.rename({ suffix: '.min'}))
2015-11-20 15:53:49 +01:00
.pipe(gulp.dest(DIST + '/assets/js/'))
});
// Project js
gulp.task('js:project', function() {
2015-11-20 15:53:49 +01:00
return gulp.src(SRC + '/_assets/js/app.js')
.pipe($.include()).on('error', onError)
2015-11-20 15:53:49 +01:00
.pipe($.sourcemaps.init())
.pipe($.concat('kremalicious3.min.js'))
2015-08-30 14:02:02 +02:00
.pipe($.if(isProduction, $.uglify())).on('error', onError)
2015-11-20 15:53:49 +01:00
.pipe($.if(!isProduction, $.sourcemaps.write()))
.pipe($.if(isProduction, $.header(BANNER, { pkg: pkg })))
.pipe(gulp.dest(DIST + '/assets/js/'))
});
// Collect all script tasks
gulp.task('js', ['js:libraries', 'js:project'])
//
// Icons
//
2015-06-08 21:22:09 +02:00
gulp.task('icons', function() {
return gulp.src(iconset.icons)
2015-06-08 21:22:09 +02:00
.pipe($.rename({ prefix: iconset.prefix }))
.pipe(gulp.dest(iconset.dist))
.pipe($.filter('**/*.svg'))
2015-08-30 14:02:02 +02:00
.pipe($.if(isProduction, $.imagemin({ svgoPlugins: [{ removeViewBox: false }] })))
2015-11-20 15:53:49 +01:00
.pipe($.svgSprite(SPRITE))
.pipe(gulp.dest(iconset.dist))
});
//
// Copy images
//
gulp.task('images', function() {
return gulp.src([
2015-11-20 15:53:49 +01:00
SRC + '/_assets/img/**/*',
'!' + SRC + '/_assets/img/entypo/**/*'
])
2015-08-30 14:02:02 +02:00
.pipe($.if(isProduction, $.imagemin({
optimizationLevel: 5, // png
progressive: true, // jpg
interlaced: true, // gif
multipass: true, // svg
svgoPlugins: [{ removeViewBox: false }]
})))
2015-11-20 15:53:49 +01:00
.pipe(gulp.dest(DIST + '/assets/img/'))
});
//
// Copy fonts
//
gulp.task('fonts', function() {
2015-11-20 15:53:49 +01:00
return gulp.src(SRC + '/_assets/fonts/**/*')
.pipe(gulp.dest(DIST + '/assets/fonts/'))
});
//
// Copy media
//
gulp.task('media', function() {
2015-11-20 15:53:49 +01:00
return gulp.src(SRC + '/_media/**/*')
.pipe(gulp.dest(DIST + '/media/'))
});
//
// Revision static assets
//
2015-08-30 14:02:02 +02:00
gulp.task('rev', function() {
2015-11-20 15:53:49 +01:00
// globbing is slow so do everything conditionally for faster dev build
if (isProduction) {
return gulp.src(DIST + '/assets/**/*.{css,js,png,jpg,jpeg,svg,eot,ttf,woff}')
.pipe($.if(isProduction, $.rev()))
.pipe(gulp.dest(DIST + '/assets/'))
// output rev manifest for next replace task
.pipe($.if(isProduction, $.rev.manifest()))
.pipe(gulp.dest(DIST + '/assets/'))
}
});
2015-06-07 20:53:49 +02:00
//
// Replace all links to assets in files
// from a manifest file
//
2015-08-30 14:02:02 +02:00
gulp.task('rev:replace', function() {
2015-11-20 15:53:49 +01:00
// globbing is slow so do everything conditionally for faster dev build
if (isProduction) {
var manifest = gulp.src(DIST + '/assets/rev-manifest.json');
return gulp.src(DIST + '/**/*.{html,xml,txt,json,css,js,png,jpg,jpeg,svg,eot,ttf,woff}')
.pipe($.if(isProduction, $.revReplace({ manifest: manifest })))
.pipe(gulp.dest(DIST))
}
});
2015-06-07 03:11:46 +02:00
//
// CDN url injection
//
gulp.task('cdn', function() {
2015-08-23 20:34:08 +02:00
return gulp.src([
2015-11-20 15:53:49 +01:00
DIST + '/**/*.html',
DIST + '/assets/css/*.css'
], { base: DIST })
.pipe($.replace('/assets/css/', CDN + '/assets/css/'))
.pipe($.replace('/assets/js/', CDN + '/assets/js/'))
//.pipe($.replace('/assets/img/', CDN + '/assets/img/'))
.pipe($.replace('/media/', CDN + '/media/'))
.pipe($.replace('https://kremalicious.com/media/', CDN + '/media/'))
.pipe($.replace('https://kremalicious.com' + CDN + '/media/', CDN + '/media/'))
.pipe($.replace('../', CDN + '/assets/'))
.pipe(gulp.dest(DIST))
2015-06-07 03:11:46 +02:00
});
2015-08-23 09:45:57 +02:00
//
// Assets uploading to S3
//
2015-08-23 20:34:08 +02:00
gulp.task('s3:assets', function() {
2015-08-23 09:45:57 +02:00
var publisher = $.awspublish.create({
params: {
2015-11-20 15:53:49 +01:00
'Bucket': S3BUCKET
2015-08-23 09:45:57 +02:00
},
'accessKeyId': process.env.AWS_ACCESS_KEY_ID,
'secretAccessKey': process.env.AWS_SECRET_ACCESS_KEY,
2015-11-20 15:53:49 +01:00
'region': S3REGION
2015-08-23 09:45:57 +02:00
});
// define custom headers
var headers = {
'Cache-Control': 'max-age=315360000, no-transform, public',
'x-amz-acl': 'public-read'
};
2015-11-20 15:53:49 +01:00
var assets = gulp.src(DIST + '/assets/**/*', { base: DIST + '/' }),
media = gulp.src(DIST + '/media/**/*', { base: DIST + '/' });
2015-08-23 09:45:57 +02:00
return merge(assets, media)
.pipe($.rename(function (path) {
// This is weird, but is needed to make the file use the relative path...
}))
.pipe($.awspublish.gzip({ ext: '' })) // gzip all the things
.pipe(parallelize(publisher.publish(headers), 10))
//.pipe(publisher.sync()) // delete files in bucket that are not in local folder
.pipe($.awspublish.reporter({
states: ['create', 'update', 'delete']
}));
});
//
// Dev Server
//
2015-11-20 18:13:34 +01:00
gulp.task('server', ['build'], function() {
2015-11-20 15:53:49 +01:00
browser.init({
server: DIST,
port: PORT
});
});
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Task sequences
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2015-06-08 10:53:12 +02:00
//
2015-11-20 15:53:49 +01:00
// Build site, run server, and watch for file changes
//
2015-11-20 15:53:49 +01:00
gulp.task('default', ['build', 'server'], function() {
gulp.watch([SRC + '/_assets/styl/**/*.styl'], ['css', browser.reload]);
gulp.watch([SRC + '/_assets/js/*.js'], ['js', browser.reload]);
gulp.watch([SRC + '/_assets/img/**/*.{png,jpg,jpeg,gif}'], ['images', browser.reload]);
gulp.watch([SRC + '/_assets/img/**/*.{svg}'], ['icons', browser.reload]);
gulp.watch([SRC + '/_media/**/*'], ['media', browser.reload]);
gulp.watch([SRC + '/**/*.{html,xml,json,txt,md}', './*.yml'], ['build', browser.reload]);
});
2015-08-30 14:02:02 +02:00
//
2015-08-30 14:02:02 +02:00
// Full build
//
2015-11-20 15:53:49 +01:00
gulp.task('build', function(done) {
2015-08-30 14:02:02 +02:00
2015-09-26 22:22:40 +02:00
console.log(chalk.gray(" ------------------------------------------"));
console.log(chalk.green(' Building ' + ($.util.env.production ? 'production' : 'dev') + ' version...'));
console.log(chalk.gray(" ------------------------------------------"));
2015-08-30 14:02:02 +02:00
runSequence(
'clean',
2015-08-30 14:02:02 +02:00
'jekyll',
2015-11-20 15:53:49 +01:00
['html', 'css', 'js', 'images', 'icons', 'fonts', 'media'],
2015-08-30 15:05:08 +02:00
'rev',
'rev:replace',
2015-11-20 15:53:49 +01:00
done
);
});