Use EcmaScript 6, w/ transpiling for compatibility

ES6 has some nice new features. Specifically relevant to this piece of
work are:

Arrow functions[1], whose `this` context is bound the value of `this` in the
current scope and can’t be overidden. The code is cleaner as a result, and
doesn’t need the addition of a bind polyfill for older browsers.

Template strings[2], which are similar to triple-quoted multi line strings in
Python. This means less fiddly and error-prone string concatenation.

This commit adds Babel[3] to the Gulp pipeline. This transpiles Javascript
written to the ES6 specification into code which is compatible with older
browsers that don’t understand ES6 syntax.

It also rewrites the gulpfile itself using some ES6 syntax, for the same reasons.

1. https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions
2. https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/template_strings
3. https://babeljs.io
This commit is contained in:
Chris Hill-Scott
2015-12-20 00:00:01 +00:00
parent 3ed415fb75
commit 74da3b1adf
6 changed files with 120 additions and 127 deletions

View File

@@ -1,91 +0,0 @@
// GULPFILE
// - - - - - - - - - - - - - - -
// This file processes all of the assets in the "src" folder
// and outputs the finished files in the "dist" folder.
// 1. LIBRARIES
// - - - - - - - - - - - - - - -
var gulp = require('gulp'),
plugins = require('gulp-load-plugins')(),
// 2. CONFIGURATION
// - - - - - - - - - - - - - - -
paths = {
src: 'app/assets/',
dist: 'app/static/',
templates: 'app/templates/'
};
// 3. TASKS
// - - - - - - - - - - - - - - -
// Move GOV.UK template resources
gulp.task('copy:govuk_template:template', function() {
return gulp.src('bower_components/govuk_template/views/layouts/govuk_template.html')
.pipe(gulp.dest(paths.templates));
});
gulp.task('copy:govuk_template:assets', function() {
return gulp.src('bower_components/govuk_template/assets/**/*')
.pipe(gulp.dest(paths.dist));
});
// Concatenate and minify
gulp.task('jquery', function () {
return plugins.jquery.src({
release: 1,
flags: [
'-ajax', '-ajax/jsonp', '-ajax/load', '-ajax/parseJSON',
'-ajax/parseXML', '-ajax/script', '-ajax/var/nonce',
'-ajax/var/rquery', '-ajax/xhr', '-manipulation/_evalUrl',
'-deprecated', '-effects', '-effects/Tween',
'-effects/animatedSelector', '-effects/support', '-event-alias'
]
})
.pipe(gulp.dest(paths.dist + 'javascripts/'));
});
gulp.task('javascripts', function() {
return gulp.src(paths.src + 'javascripts/main.js')
.pipe(plugins.include())
.pipe(gulp.dest(paths.dist + 'javascripts/'))
.pipe(plugins.uglify())
.on('error', function(e) { console.log("Uglify did not complete."); })
.pipe(gulp.dest(paths.dist + 'javascripts/'));
});
gulp.task('sass', function () {
return gulp.src(paths.src + '/stylesheets/main*.scss')
.pipe(plugins.sass({outputStyle: 'compressed'}))
.pipe(gulp.dest(paths.dist + '/stylesheets'));
});
// Copy images
gulp.task('images', function() {
return gulp.src(paths.src + 'images/**/*')
.pipe(gulp.dest(paths.dist + '/images'));
});
// Watch for changes and re-run tasks
gulp.task('watchForChanges', function() {
gulp.watch(paths.src + 'javascripts/**/*', ['javascripts']);
gulp.watch(paths.src + 'stylesheets/**/*', ['sass']);
gulp.watch(paths.src + 'images/**/*', ['images']);
});
// Default: compile everything
gulp.task('default',
['copy:govuk_template:template', 'copy:govuk_template:assets', 'jquery', 'javascripts', 'sass', 'images']
);
// Optional: recompile on changes
gulp.task('watch',
['default', 'watchForChanges']
);