Skip to content

Instantly share code, notes, and snippets.

@j-greig
Created February 11, 2018 16:39
Show Gist options
  • Save j-greig/6586b9bd51ad1c6e1de2231950f9a593 to your computer and use it in GitHub Desktop.
Save j-greig/6586b9bd51ad1c6e1de2231950f9a593 to your computer and use it in GitHub Desktop.
Gulp setup for Craft based on a simplified version of https://nystudio107.com/blog/a-gulp-workflow-for-frontend-development-automation
// package vars
const pkg = require("./package.json");
// gulp
const gulp = require("gulp");
// load all plugins in "devDependencies" into the variable $
const $ = require("gulp-load-plugins")({
pattern: ["*"],
scope: ["devDependencies"]
});
const onError = (err) => {
console.log(err);
};
// Default task
gulp.task("default", () => {
$.livereload.listen();
gulp.watch([pkg.paths.src.css + "**/*.css"], ["css"]);
gulp.watch([pkg.paths.src.js + "**/*.js"], ["js"]);
gulp.watch([pkg.paths.templates + "**/**/*.{html,twig}"], () => {
gulp.src(pkg.paths.templates)
.pipe($.plumber({errorHandler: onError}))
.pipe($.livereload());
});
});
// Compress CSS
gulp.task('css', () => {
$.fancyLog("🤜 Building css");
return gulp.src([pkg.paths.src.css + "**/*.css"])
.pipe($.cleanCss({debug: true}, (details) => {
console.log(`${details.name}: ${details.stats.originalSize}`);
console.log(`${details.name}: ${details.stats.minifiedSize}`);
}))
.pipe($.concat("style.min.css"))
.pipe(gulp.dest(pkg.paths.dist + "css"));
});
// Compress JS
gulp.task('js', () => {
return gulp.src([pkg.paths.src.js + "**/*.js"])
.pipe($.concat("scripts.min.js"))
.pipe(gulp.dest(pkg.paths.dist + "js"));
});
// Critical CSS
gulp.task('criticalcss', ['css'], (callback) => {
doSynchronousLoop(pkg.globs.critical, processCriticalCSS, () => {
// all done
callback();
});
});
// Inline JS task - minimize the inline Javascript into _inlinejs in the templates path
gulp.task("js-inline", () => {
$.fancyLog("🤜 Copying inline js");
return gulp.src(pkg.globs.inlineJs)
.pipe($.plumber({errorHandler: onError}))
.pipe($.if(["*.js", "!*.min.js"],
$.newer({dest: pkg.paths.templates + "_inlinejs", ext: ".min.js"}),
$.newer({dest: pkg.paths.templates + "_inlinejs"})
))
.pipe($.if(["*.js", "!*.min.js"],
$.uglify()
))
.pipe($.if(["*.js", "!*.min.js"],
$.rename({suffix: ".min"})
))
.pipe($.size({gzip: true, showFiles: true}))
.pipe(gulp.dest(pkg.paths.templates + "_inlinejs"))
.pipe($.filter("**/*.js"))
.pipe($.livereload());
});
// Process data in an array synchronously, moving onto the n+1 item only after the nth item callback
function doSynchronousLoop(data, processData, done) {
if (data.length > 0) {
const loop = (data, i, processData, done) => {
processData(data[i], i, () => {
if (++i < data.length) {
loop(data, i, processData, done);
} else {
done();
}
});
};
loop(data, 0, processData, done);
} else {
done();
}
}
// Process the critical path CSS one at a time
function processCriticalCSS(element, i, callback) {
const criticalSrc = pkg.urls.critical + element.url;
const criticalDest = pkg.paths.templates + element.template + '_critical.min.css';
$.fancyLog("-> Generating critical CSS: " + $.chalk.cyan(criticalSrc) + " -> " + $.chalk.magenta(criticalDest));
$.critical.generate({
src: criticalSrc,
dest: criticalDest,
inline: false,
ignore: [],
css: [
pkg.paths.dist.css + pkg.vars.siteCssName,
],
minify: true,
width: 1200,
height: 1200
}, (err, output) => {
if (err) {
$.fancyLog($.chalk.magenta(err));
}
callback();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment