Skip to content

Instantly share code, notes, and snippets.

@dkarchmer
Created December 8, 2015 17:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dkarchmer/bfe9c49b28315f537cfb to your computer and use it in GitHub Desktop.
Save dkarchmer/bfe9c49b28315f537cfb to your computer and use it in GitHub Desktop.
Example of a Gulp file for processing a Django base template and releasing statics to cloudfront
/* Assumes the following directory structure:
* ./images # With original images
* ./base # With the index.html, css, js to be used to create a base template
* # This directory containts its own gulpfile (maybe coming from a yeoman or similar)
* ./server # Actual Django project, with a templates subdirectory
*/
var gulp = require('gulp');
var chug = require( 'gulp-chug' );
var awspublish = require('gulp-awspublish');
var cloudfront = require("gulp-cloudfront");
var del = require('del');
var responsive = require('gulp-responsive');
var replace = require('gulp-replace');
var rename = require('gulp-rename');
var es = require('event-stream');
var debug = require('gulp-debug');
gulp.paths = {
src: {
base: './DjangoBase',
images: './images'
},
dist: 'dist',
templates: 'server/templates/dist'
};
var aws = {
params: {
Bucket: "myBucket",
Region: "us-east-1"
},
"distributionId": "myCloudFrontDistributionID"
};
var genStream = function(app) {
return gulp.src(gulp.paths.dist + '/' + app + '/index.html')
.pipe(replace(/href="([^h/]\S*)"/g, 'href="{% static \'dist/' + app + '/$1\' %}"'))
.pipe(replace(/src="([^h/]\S*)"/g, 'src="{% static \'dist/' + app + '/$1\' %}"'))
.pipe(gulp.dest(gulp.paths.templates + '/' + app));
};
var publisher = awspublish.create(aws);
// One week = 604,800
var headers = {'Cache-Control': 'max-age=604800, no-transform, public'};
var index_header = {'Cache-Control': 'public, must-revalidate, proxy-revalidate, max-age=0'};
gulp.task('clean', function(cb) {
return del([gulp.paths.dist + '/*'], cb)
});
gulp.task('images', [], function () {
var images = [{
// Logo with name (Horizontal)
name: 'logo-horizontal.png',
width: 200
},{
// Fine Uploader image
name: 'waiting-generic.png',
width: 100,
rename: 'fineuploader/waiting-generic.png'
}];
var options = {
errorOnUnusedImage: false
};
return gulp.src([gulp.paths.images + '/**.png',gulp.paths.images + '/**.jpg'])
.pipe(responsive(images, options))
.pipe(gulp.dest(gulp.paths.dist + '/images'));
});
gulp.task( 'base', ['images'], function () {
return gulp.src( gulp.paths.src.base +'/gulpfile.js', { read: false } )
.pipe( chug({
tasks: [ 'build' ]
}) )
});
// Use 'gulp build_local_base' for local debugging
gulp.task( 'build_local_base', ['images', 'base'], function () {
return genStream('base');
});
gulp.task('deploy', ['build_local_base'], function () {
return gulp.src([gulp.paths.dist + '/**', '!'+ gulp.paths.dist + '/**/index.html'])
.pipe(rename(function (path) {
path.dirname = 'static/dist/' + path.dirname;
}))
.pipe(awspublish.gzip())
.pipe(publisher.publish(headers))
.pipe(publisher.cache())
.pipe(awspublish.reporter())
.pipe(cloudfront(aws));
});
gulp.task('default', ['deploy']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment