Web_Designer_UK__May_2018
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Developer tutorials<br />
Automate your workflow with Gulp<br />
The Gulp<br />
ecosystem<br />
At the time of writing, there are<br />
3,399 plugins listed at gulpjs.<br />
com/plugins. This means<br />
there’s an incredible amount<br />
of potential automation we can<br />
apply to our workflows. Here’s<br />
abriefoverviewofsomeofour<br />
favourite plugins not featured<br />
in this tutorial:<br />
‘gulp-notify’ enables you<br />
to send OS notifications from<br />
Gulp tasks. Possible uses<br />
include notifying when a build is<br />
complete,orifthere’sanerror.<br />
‘gulp-plumber’ changes<br />
Gulp error handling so that<br />
when things break (such as<br />
SASS not compiling) the task<br />
won’t quit prematurely.<br />
‘gulp-stylelint’ and ‘gulpeslint’<br />
bring these popular<br />
frameworks to Gulp, enabling<br />
you to automatically lint your<br />
SASS and JavaScript on build.<br />
‘gulp-git’ enables you to<br />
run Git commands from a Gulp<br />
task, so for example, when you<br />
complete a build, you could<br />
choose to commit it also.<br />
npm install browserify vinyl-source-stream<br />
glob gulp-streamify gulp-uglify babelpreset-es2015<br />
babelify babel-core -D<br />
Then add these to our list of dependency imports.<br />
const browserify = require(‘browserify’);<br />
const source = require(‘vinyl-sourcestream’);<br />
const glob = require(‘glob’);<br />
const streamify = require(‘gulp-streamify’);<br />
const uglify = require(‘gulp-uglify’);<br />
13. Bundling JavaScript – part 2<br />
Next, create a new Gulp task called ‘js’ and within it, use<br />
the glob plugin to grab all the JavaScript files in our root<br />
‘jsDev’ directory and return them in an array. We’ll then<br />
loop through this using a map function, enabling us to<br />
process each JavaScript file individually.<br />
gulp.task(‘js’, function() {<br />
console.log(‘Bundling JavaScript ’);<br />
var files = glob.sync(`${jsDev}/*.js`);<br />
files.map(function(file) {<br />
});<br />
});<br />
14. Bundling JavaScript – part 3<br />
Within the map function, the ‘file’ variable references a<br />
string containing the full path of the current file (For<br />
example ‘/dev/js/main.js’). If we want to rename a minified<br />
‘main.js’ in a later step to ‘main.min.js’ like within the SASS<br />
task, we will need its base filename ‘main’. Let’s filter out<br />
the path information which isn’t required, by adding the<br />
snippet below to the map function.<br />
const filename = file.replace(`${jsDev}/`,<br />
‘’)<br />
.replace(‘.js’, ‘’);<br />
15. BundlingJavaScript–part4<br />
Next,let’ssendthefiletoBrowserifythroughthe<br />
transformfunctionsoitcanbebundledanditsES6<br />
transpiled. The vinyl-source-stream dependency is<br />
thenusedtosendthefilebackusingGulpsoitcanbe<br />
renamed if minify is ‘true’. Finally, if minify is true, we run<br />
thebundledfilethroughuglifytominifyitandthensend<br />
it to the ‘jsDist’ directory.<br />
return browserify({entries: file})<br />
.transform(‘babelify’, {presets:<br />
[‘es2015’]})<br />
.bundle()<br />
.pipe(source(file))<br />
.pipe(gulpif(minify, rename({<br />
dirname: ‘’,<br />
basename: filename,<br />
suffix: ‘.min’,<br />
extname: ‘.js’<br />
}), rename({<br />
dirname: ‘’,<br />
basename: filename,<br />
extname: ‘.js’<br />
})))<br />
.pipe(gulpif(minify, streamify(uglify())))<br />
.pipe(gulp.dest(jsDist));<br />
16. Setting up Nunjucks<br />
One thing possible to automate with Gulp is static site<br />
generation. In this project, a templating language called<br />
Nunjucks will be used to generate HTML files from<br />
templates using car data in a JSON file. We can see these<br />
Nunjucks templates for ourselves at ‘dev/html’. Install<br />
‘gulp-nunjucks’ in the usual way and then include it as a<br />
dependency called ‘nunjucks’ in gulpfile. Next, create a<br />
new task called ‘makes-list’.<br />
gulp.task(‘makes-list’, function() {<br />
console.log(‘Generating homepage ’);<br />
});<br />
17. Import JSON data<br />
In the task, the first thing we’ll be doing is importing the<br />
car data, which will feed into the static site generation.<br />
However, before we do even that, we first need to delete<br />
any leftover cache from previous builds to make sure the<br />
car data we’re using is always up to date. If we don’t, when<br />
we make changes to the car data in future, the out-of-date<br />
cached version may be used by the task instead.<br />
delete require.cache[require.<br />
resolve(`./${dataDev}/cars.json`)];<br />
let config = require(`./${dataDev}/cars.<br />
json`);<br />
18. Inject data into a single page<br />
Using Nunjucks, we’ll make the car data available to the<br />
‘index.html’ template, enabling it to display a list of car<br />
makes once compiled. With the ‘src()’ method, grab the<br />
‘index.html’ template and then pipe it to Nunjucks. Next,<br />
we’ll use Nunjucks’ ‘compile()’ method to inject car data<br />
from the JSON we’ve imported to the template and then<br />
render it. Finally we’ll send the compiled HTML to the<br />
‘htmlDist’ directory.<br />
return gulp.src(`${htmlDev}/index.html`)<br />
.pipe(nunjucks.compile({<br />
date: config[‘date’],<br />
makes: config[‘makes’]<br />
}))<br />
.pipe(gulp.dest(htmlDist));<br />
19. Render new pages – part 1<br />
It’s also possible in Gulp to generate lots of HTML files<br />
tutorial _________________________________________________83