I've recently decided to update some really old gulp files to gulp 4. I pretty much started from scratch as my workflow is totally different and therefore I wanted a bespoke solution to fit how I now work.
So far I have a file that will process my files and watch for changes and reload the browser. My question is - at the moment I pipe files to a tmp folder for development but when I'm ready to produce my production files, is there a neat way to incorporate this logic into my existing functions or is it better to make new function specifically to handle final build processes?
I mainly ask because I could make some buildStylesProd function but is there a neater more DRY way to handle this all within a single function?
Current progress:
// Initialize modules
// Importing specific gulp API functions lets us write them below as series() instead of gulp.series()
const { src, dest, watch, series, lastRun, parallel } = require('gulp');
// Importing all the Gulp-related packages we want to use
const sourcemaps = require('gulp-sourcemaps');
const sass = require('gulp-sass');
const concat = require('gulp-concat');
const uglify = require('gulp-uglify');
const postcss = require('gulp-postcss');
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');
const del = require('del');
const browserSync = require('browser-sync').create();
var replace = require('gulp-replace');
var settings = {
reload: true,
styles: true,
scripts: true,
clean: true
};
// File paths
const files = {
input: 'src/',
tmp: 'tmp/',
scssPath: 'src/scss/**/*.scss',
jsPath: 'src/js/**/*.js',
cssTmp: 'tmp/css',
jsTmp: 'tmp/js',
htmlPath: 'perch/templates/**/*.html',
pageFiles: 'perch/templates/**/*.php',
}
// Watch for changes to the tmp directory
var startServer = function (done) {
// Make sure this feature is activated before running
if (!settings.reload) return done();
// Initialize BrowserSync
browserSync.init({
injectChanges: true,
});
// Signal completion
done();
};
// Reload the browser when files change
var reloadBrowser = function (done) {
if (!settings.reload) return done();
browserSync.reload();
done();
};
function buildStyles(done){
// Make sure this feature is activated before running
if (!settings.styles) return done();
return src(files.scssPath)
.pipe(sourcemaps.init()) // initialize sourcemaps first
.pipe(sass()) // compile SCSS to CSS
.on("error", sass.logError)
.pipe(postcss([ autoprefixer(), cssnano() ])) // PostCSS plugins
.pipe(sourcemaps.write('.')) // write sourcemaps file in current directory
.pipe(dest(files.cssTmp)
); // put final CSS in dist folder
}
// JS task: concatenates and uglifies JS files to script.js
function buildScripts(done){
// Make sure this feature is activated before running
if (!settings.scripts) return done();
return src([
files.jsPath
//,'!' + 'includes/js/jquery.min.js', // to exclude any specific files
])
//.pipe(concat('all.js'))
.pipe(uglify())
.pipe(dest(files.jsTmp)
);
}
// Remove pre-existing content from output folders
var cleanTmp = function (done) {
// Make sure this feature is activated before running
if (!settings.clean) return done();
// Clean the dist folder
del.sync([
files.tmp
]);
// Signal completion
return done();
};
// Watch for changes
var watchSource = function (done) {
watch([files.input, files.pageFiles], series(exports.default, reloadBrowser));
done();
};
// Export the default Gulp task so it can be run
// Runs the scss and js tasks simultaneously
// then runs cacheBust, then watch task
exports.default = series(
cleanTmp,
parallel(
buildStyles,
buildScripts
)
);
// Watch and reload
// gulp watch
exports.watch = series(
exports.default,
startServer,
watchSource
);