Taking Apart gulpfile.js: Studying the Ghost Theme Build Tool
A step-by-step look at the gulpfile.js used in Ghost themes — what Gulp does and how it manages building and minifying CSS, JS and templates.
What Exactly Does Gulp Do?
Web development involves more repetitive work than you'd think.
You have to merge several CSS files into one, add prefixes for browser compatibility, minify JS, and package theme files as a ZIP.
Doing it by hand every time is tedious and leads to mistakes.
What automates this is Gulp.js.
It's a Node.js-based build tool: define the order and method of tasks as code in a gulpfile.js, and one command does many things automatically.
Ghost themes use Gulp too.
At first I just let it go, since running yarn dev took care of everything, but I got curious about what it actually does and opened the gulpfile myself.
Looking at the Structure of gulpfile.js
The structure turned out to be simpler than expected. (Or, as the old joke goes, the black was the background and the white was the text.) Just the needed tasks, neatly organized.
1. Main Plugins
gulp-livereload: detects changes and refreshes the browser automaticallygulp-postcss: a tool that bundles several CSS-related tasksgulp-concat: merges JS files into onegulp-uglify: minifies JS codegulp-zip: packages the theme into a zip filepostcss-easy-import,autoprefixer,cssnano: extra features for CSS
const {series, parallel, watch, src, dest} = require('gulp');
const pump = require('pump');
const fs = require('fs');
const order = require('ordered-read-streams');
// gulp plugins and utils
const livereload = require('gulp-livereload');
const postcss = require('gulp-postcss');
const concat = require('gulp-concat');
const uglify = require('gulp-uglify');
const beeper = require('beeper');
const zip = require('gulp-zip');There was a lot I didn't know at first (and still don't), but I could roughly guess each function from its name.
What Happens in Each Task?
📁 1. The css() Function: Building CSS
- Reads
assets/css/screen.css, - actually resolves the
@importstatements, - adds vendor prefixes (autoprefixer),
- minifies with cssnano,
- and saves the result to the
assets/built/folder.
function css(done) {
pump([
src('assets/css/screen.css', {sourcemaps: true}),
postcss([
easyimport,
autoprefixer(),
cssnano()
]),
dest('assets/built/', {sourcemaps: '.'}),
livereload()
], handleError(done));
}Doing this by hand would have been quite a hassle.
🧩 2. The js() Function: Minified JS Build
- Reads, in order, the common JS files from Ghost's
shared-theme-assets - and the JS files written for the current theme,
- merges and minifies them into
main.min.js. - It's likewise saved to the
assets/built/folder.
function js(done) {
pump([
order(getJsFiles('v1'), {sourcemaps: true}),
concat('main.min.js'),
uglify(),
dest('assets/built/', {sourcemaps: '.'}),
livereload()
], handleError(done));
}What struck me here was the use of a somewhat unfamiliar module, ordered-read-streams. It reads multiple streams in order.
💡 3. The hbs() Function: Watching Templates
- When a
.hbsfile changes, livereload refreshes the browser automatically. - This auto-refresh was quite useful while actually changing the theme structure.
function hbs(done) {
pump([
src(['*.hbs', 'partials/**/*.hbs']),
livereload()
], handleError(done));
}📦 4. The zipper() Function: Packaging the Theme
- This function finally packages the theme as a
.zipfile. - The zip file is created in the
dist/folder. - It's neatly set up to exclude unnecessary files like
node_modules,distandyarn-error.log.
function zipper(done) {
const filename = require('./package.json').name + '.zip';
pump([
src([
'**',
'!node_modules', '!node_modules/**',
'!dist', '!dist/**',
'!yarn-error.log'
]),
zip(filename),
dest('dist/')
], handleError(done));
}This is very handy when uploading the theme to the live blog.
Summary of the Basic Commands
The basic commands defined in this gulpfile come down to:
yarn dev:gulp default→ build (css, js) + livereload server + file watchingyarn build: builds only CSS/JS and stopsyarn zip: builds, then packages the theme into a ZIP file
The Key Benefits of Gulp, as I Understand Them
Here's what I took away from taking apart a gulpfile for the first time:
- Automating work you used to do by hand
→ Repetitive building, minifying and refreshing finish with a single command. - Predictable results
→ CSS/JS minification always produces the same output, and when something goes wrong it's easier to trace where it got tangled. - Preventing mistakes
→ No more merging files in the wrong order or leaving files out.
Closing Thoughts
While modifying a Ghost theme, getting to look at the build tool too — not just tweaking a few lines of CSS — was a fun experience.
Gulp is an older tool than you might think, but its structure is simple enough to understand quickly, and it provides just the right amount of automation for something the size of a personal blog.
Where I used to run yarn dev or yarn zip without thinking, I now feel I understand a little of what steps those commands go through and what they produce.
And I realized that even a single file like this carries a bit of logic and philosophy.