Build pipeline
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.
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.
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.
--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.
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.
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.
Everything the modules under gulp/tasks/ export.
| Task | What it does | In | Out |
|---|---|---|---|
| cleanBuild | Deletes the build directory before every run | ./dist | — |
| scss | dart-sass, media-query grouping, webp classes, autoprefixing, minification; two writes in one pass | src/scss/**/*.scss | dist/css/styles.css, styles.min.css |
| html | Assembles the page from blocks via @@include, swaps <img> for <picture> with webp, stamps asset versions, formats and minifies | src/html/*.html + blocks/* | dist/index.html, index.min.html |
| js | Runs the entry point through webpack-stream; the mode comes from the flag | src/js/app.js | dist/js/app.min.js |
| images | Makes .webp copies, then compresses the originals with imagemin + pngquant; svg is copied as is | src/img/** | dist/img/** + .webp |
| svgSprites | Bundles every svg into a single sprite in stack mode | src/img/**/*.svg | dist/img/icons/icons.svg |
| fontsStyle | Walks the font directory recursively, derives style and weight from the file name, and generates _fonts.scss with calls to the font() mixin | src/fonts/** | src/scss/_fonts.scss |
| fontsCp | Copies into the build only those fonts whose name appears somewhere in the SCSS | src/fonts/** | dist/fonts/** |
| otfToTtf ttfToWoff ttfToWoff2 | Converts font formats in place, in the sources; runs in prod only, and woff and woff2 run in parallel | src/fonts/*.otf, *.ttf | src/fonts/*.ttf, *.woff, *.woff2 |
| server | browser-sync over dist/: style injection without a reload, click and scroll sync across devices | dist/ | localhost |
| CpCerts | Puts TLS certificates next to the build for the server's https mode | src/certs/** | dist/certs/** |
| ZipBuild | Packs the finished build into an archive named after the project | dist/** | <project>-dist.zip |
| FtpUpload | Pushes the build to FTP through vinyl-ftp | dist/** | remote host |
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.
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.
fontsStyle unconditionally walks src/fonts and
src/img, neither of which is in the repository:
ENOENT: no such file or directory, scandir './src/fonts'. Directories created.DirWalk()
The line data.file_ext, data.file_name, data.dir, data.name = file_ext, …
was never terminated, so the next line, which starts with [, parsed as an index —
name['eot?#iefix'] → undefined.includes(...). On top of that it was a comma
expression, so only data.name was ever assigned, and with the value of dir.
Split into four ordinary assignments.@import "_preloader.scss" against a file named
preloader.scss with no underscore. Windows sometimes lets this slide; Linux does not.@import '~normalize.css' in SCSS
The tilde is webpack resolution syntax; dart-sass leaves the string alone, the invalid
@import lands in the CSS, and clean-css trips over it —
styles.min.css simply was never produced. normalize.css was not in the
dependencies either. The file is now vendored into src/scss/_normalize.scss.for … in instead of for … of in fontsStyle
for (const [font_name, font_dir] in Object.entries(out)) iterates the
array's indices, not its elements, so font_name gets the string "0".
_fonts.scss generation does not work. The defect is latent: the starter ships no fonts,
so it only surfaces on a real project.?? instead of && in fontsCp
getScssData().includes(name) ?? app.gulp.src(...) — the left side is
always a boolean and never null, so the right side is never evaluated. Fonts are never
copied into dist/fonts under any circumstances.images task needs native binaries
imagemin-pngquant pulls in pngquant-bin, which downloads a
platform binary in postinstall. Install with --ignore-scripts, or on a machine with no
network, and the task fails with spawn … pngquant ENOENT.gulp-util (used in deploy.js) is deprecated and unmaintained
since 2018. swiper sits in optionalDependencies but is imported nowhere — the
only mention of it is commented out in app.js. The zip and deploy
tasks were not verified: FTP needs real credentials._header.scss, _nav.scss,
_main.scss, _article.scss, _aside.scss) is a selector with not one
property in it, and the page markup is four words long. That is a deliberate choice by the starter's
author, but there is no point reading the build output as markup work — the value here is the pipeline.
The repository also carries Gulp preview.rar (362 KB), a binary archive in git.
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.
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.