Skip to content

Instantly share code, notes, and snippets.

@pts-moog16
Last active March 8, 2017 07:39
Show Gist options
  • Save pts-moog16/0816efdb32ca932f79885a4786840d62 to your computer and use it in GitHub Desktop.
Save pts-moog16/0816efdb32ca932f79885a4786840d62 to your computer and use it in GitHub Desktop.
gulpfile tasks for compiling styles
const gulp = require('gulp');
const sass = require('gulp-sass');
const path = require('path');
const gutil = require("gulp-util");
// custom importer for scss files that resolve file names in
// main app.scss manifest
const importer = require('../scssImporter');
const postcss = require('gulp-postcss');
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');
const rev = require('gulp-rev');
const exec = require('child_process').execSync;
// gets an array of all clientnames
const clients = require('./clients')();
/*********************************
**********************************
******** Styles Tasks **********
**********************************
**********************************/
gulp.task('buildStyles', () => {
// Builds minified app.css files for every client and
// delivers them to our static directory for our app
const promises = [];
clients.forEach(client => {
const promise = new Promise((resolve) => {
buildClientStyles(client, resolve);
});
promises.push(promise);
});
return Promise.all(promises);
});
function buildClientStyles(client, resolve = () => {}) {
// some postcss plugins (autoprefixer is highly recommended, even life changing)
const postcssPlugins = [
autoprefixer({ browsers: ['last 2 versions'] }),
cssnano()
];
return gulp.src(path.resolve(__dirname, '../../styles/app.scss'))
.pipe(sass({ importer: importer.bind({client}, Array.prototype.slice.call(arguments)) }))
.pipe(postcss(postcssPlugins)
.pipe(gulp.dest(`../../static/css/${client}`))
.on('end', () => {
gutil.log(`[sass] built ${client}`);
resolve();
});
gulp.task('revisionStyles', ['buildStyles'], () => {
// styles build for production
// will remove the app.css file for each client and
// replace it with a hash revision filename
const promises = [];
clients.forEach(client => {
const promise = new Promise((resolve, reject) => {
const appScssPath = '../../static/css', client, 'app.css';
gulp.src(appScssPath)
.pipe(rev())
.pipe(gulp.dest(`../../static/css/${client}`))
.on('end', () => {
exec(`rm ${appScssPath}`);
gutil.log(`[sass] revisioned ${client}`);
resolve();
});
});
promises.push(promise);
});
return Promise.all(promises);
});
// development build task
gulp.task('buildStylesDev', ['setDevVariables', 'buildStyles']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment