Build pipeline

gulp-template

A front-end build starter on Gulp 4 written in ESM: nine tasks, a single place where every path is declared, and one --build flag that switches the whole pipeline between development and production. Below: the task graph and the processing chains, read out of the repository's code rather than its README.


What it is and what it solves

Not a site — a build skeleton you copy in at the start of a new markup project.

The starter exists to take the repetitive setup out of every new project: compiling SCSS with autoprefixing and media-query grouping, assembling HTML from include blocks, converting fonts to woff/woff2, generating .webp copies of images and SVG sprites, minification, a live-reload server, zipping the build and uploading it over FTP.

The key architectural decision is that all configuration lives outside the tasks. Paths sit in gulp/configs/path.js, shared plugins in gulp/configs/plugins.js, and both structures are put on global.app together with the mode flags. After that a task file contains nothing but the flow: where to read from, what to apply, where to write. The second decision is one flag instead of two configs: the mode comes from whether --build is in the arguments, and inside the tasks the differences are expressed with gulp-if, so the dev and prod chains never fork into separate files.

Task graph

Read straight off gulpfile.js: two entry points, a shared clean step, a block of six tasks that run in parallel, and different tails for dev and prod.

The diagram is wider than the screen — scroll it horizontally.

npm run dev gulp · app.isBuild = false npm run prod gulp prod --build · isBuild = true cleanBuild del('./dist') dev skips the font conversion step prod fontsConverter · prod only otfToTtf ttfToWoff ttfToWoff2 src/fonts/*.otf → .ttf → .woff + .woff2 — written back into src/fonts FilesProcess · gulp.parallel — six tasks at once fonts series: fontsStyle → fontsCp → dist/fonts scss src/scss/**/*.scss → dist/css images src/img/** (raster) → dist/img + .webp svgSprites src/img/**/*.svg → dist/img/icons html src/html/*.html → dist/*.html js src/js/app.js → dist/js/app.min.js each task reads app.isBuild and uses gulp-if to switch on minification, autoprefixing and webp prod ends here: dist/ is built dev dev only · gulp.parallel watcher gulp.watch: scss · html · img · js CpCerts → server browser-sync, baseDir: dist/
A solid line is the shared path, a dashed line a branch belonging to one mode, and color marks what runs only under --build. The zip and deploy tasks are not on the diagram: they are separate entry points that repeat cleanBuild → FilesProcess and append ZipBuild and FtpUpload.

Style pipeline

The most substantial task in the starter is gulp/tasks/scss.js. Fourteen steps, two taps that write to disk, and four steps that only the flag turns on.

gulp.src('src/scss/**/*.scss') plumber + notify.onError replace('@img/' → '../img/') sourcemaps.init() dart-sass · expanded groupCssMediaQueries() webpcss(.webp/.no-webp) autoprefixer(grid: true) dest('dist/css') cleanCss() rename({extname:'.min.css'}) sourcemaps.write('./') dest('dist/css') browsersync.stream() dev --build --build --build --build dev dist/css/styles.css dist/css/styles.min.css
Unlabelled steps always run. Note the two writes into the same directory: the first dest saves readable CSS, then the stream carries on through cleanCss and rename and is saved a second time — one pass yields both the expanded and the minified file. replace('@img/') sits before the compiler, so the @img/ alias works directly in the SCSS sources, and the same replacement in html.js makes it valid in markup too.

Tasks

Everything the modules under gulp/tasks/ export.

TaskWhat it doesInOut
cleanBuildDeletes the build directory before every run./dist—
scssdart-sass, media-query grouping, webp classes, autoprefixing, minification; two writes in one passsrc/scss/**/*.scssdist/css/styles.css, styles.min.css
htmlAssembles the page from blocks via @@include, swaps <img> for <picture> with webp, stamps asset versions, formats and minifiessrc/html/*.html + blocks/*dist/index.html, index.min.html
jsRuns the entry point through webpack-stream; the mode comes from the flagsrc/js/app.jsdist/js/app.min.js
imagesMakes .webp copies, then compresses the originals with imagemin + pngquant; svg is copied as issrc/img/**dist/img/** + .webp
svgSpritesBundles every svg into a single sprite in stack modesrc/img/**/*.svgdist/img/icons/icons.svg
fontsStyleWalks the font directory recursively, derives style and weight from the file name, and generates _fonts.scss with calls to the font() mixinsrc/fonts/**src/scss/_fonts.scss
fontsCpCopies into the build only those fonts whose name appears somewhere in the SCSSsrc/fonts/**dist/fonts/**
otfToTtf
ttfToWoff
ttfToWoff2
Converts font formats in place, in the sources; runs in prod only, and woff and woff2 run in parallelsrc/fonts/*.otf, *.ttfsrc/fonts/*.ttf, *.woff, *.woff2
serverbrowser-sync over dist/: style injection without a reload, click and scroll sync across devicesdist/localhost
CpCertsPuts TLS certificates next to the build for the server's https modesrc/certs/**dist/certs/**
ZipBuildPacks the finished build into an archive named after the projectdist/**<project>-dist.zip
FtpUploadPushes the build to FTP through vinyl-ftpdist/**remote host

Key excerpts

Four decisions the whole thing rests on.

global.app = {
    isBuild: process.argv.includes('--build'),
    isDev: !process.argv.includes('--build'),
    gulp: gulp,
    path: path,
    plugins: plugins,
    MainFileType: 'html'
}

The only piece of global build state. Tasks import neither gulp nor the configs — they read app, which keeps a task file a flat description of a stream and lets you move it into another project without touching a single import.

const fonts = gulp.series(fontsStyle, fontsCp)
const fontsConverter = gulp.series(otfToTtf, gulp.parallel(ttfToWoff, ttfToWoff2))
const FilesProcess = gulp.parallel(fonts, scss, images, svgSprites, html, js)

export const dev  = gulp.series(cleanBuild, FilesProcess,
                                gulp.parallel(watcher, gulp.series(CpCerts, server)))
export const prod = gulp.series(cleanBuild, gulp.series(fontsConverter, FilesProcess))

The entire task graph is five lines. series appears where there is a real dependency (generate _fonts.scss first, then copy the fonts; .otf → .ttf first, then two formats out of the ttf), parallel everywhere there is none.

.pipe(app.plugins.if(app.isBuild, groupCssMediaQueries()))
.pipe(app.plugins.if(app.isBuild, webpcss({ webpClass: '.webp', noWebpClass: '.no-webp' })))
.pipe(app.plugins.if(app.isBuild, autoprefixer({ grid: true, cascade: true })))
.pipe(app.gulp.dest(app.path.build.css))
.pipe(app.plugins.if(app.isBuild, cleanCss()))

gulp-if instead of two configs: one chain describes both modes, and the expensive steps (autoprefixing, media-query merging, minification) cost nothing on every file save in dev.

src: {
    html:      `${srcFolder}/${srcHtmlFolder}/*.html`,
    images:    `${srcFolder}/${imgFolder}/**/*.{jpeg,jpg,png,gif,webp}`,
    svgs:      `${srcFolder}/${imgFolder}/**/*.svg`,
    scss:      `${srcFolder}/${scssFolder}/**/*.{scss,map.scss}`,
    fontsScss: `${srcFolder}/${scssFolder}/${fontsScss}`,
    js:        `${srcFolder}/${jsFolder}/app.js`
},
watch: {
    scss: [`${srcFolder}/${scssFolder}/**/*.scss`, `!${srcFolder}/${scssFolder}/**/_fonts.scss`]
}

Every path is built from constants in one file. The second line of the watch section is the one that matters in practice: _fonts.scss is excluded from watching, otherwise fontsStyle — which rewrites that file itself — would put the rebuild into a loop.

Known defects

An honest list. The first four are what had to be fixed to make the starter build at all: out of the box it died on the very first task. The rest are still in the code.

Build output

After the fixes npm run prod runs clean and produces index.html, css/styles.min.css, css/preloader.min.css and js/app.min.js.

Open the built page

The pipeline's output as it is: a four-block skeleton, ?_v= version suffixes stamped by the html task, styles pulled from css/styles.min.css. The page is here as proof that the build works, not as a sample of markup.