Skip to content

Instantly share code, notes, and snippets.

@treb0r
Created March 20, 2015 14:29
Show Gist options
  • Save treb0r/c6a89a7289b98c24580a to your computer and use it in GitHub Desktop.
Save treb0r/c6a89a7289b98c24580a to your computer and use it in GitHub Desktop.
http://Roots.io Sage Gulpfile and package.json modified for use with http://Patternlab.io
// ## Globals
/*global $:true*/
var $ = require('gulp-load-plugins')();
var argv = require('yargs').argv;
var browserSync = require('browser-sync');
var gulp = require('gulp');
var lazypipe = require('lazypipe');
var merge = require('merge-stream');
var shell = require('gulp-shell');
var runSequence = require('run-sequence');
// See https://github.com/austinpray/asset-builder
var manifest = require('asset-builder')('./source/assets/manifest.json');
// `path` - Paths to base asset directories. With trailing slashes.
// - `path.source` - Path to the source files. Default: `assets/`
// - `path.dist` - Path to the build directory. Default: `dist/`
var path = manifest.paths;
// `config` - Store arbitrary configuration values here.
var config = manifest.config || {};
// `globs` - These ultimately end up in their respective `gulp.src`.
// - `globs.js` - Array of asset-builder JS dependency objects. Example:
// ```
// {type: 'js', name: 'main.js', globs: []}
// ```
// - `globs.css` - Array of asset-builder CSS dependency objects. Example:
// ```
// {type: 'css', name: 'main.css', globs: []}
// ```
// - `globs.fonts` - Array of font path globs.
// - `globs.images` - Array of image path globs.
// - `globs.bower` - Array of all the main Bower files.
var globs = manifest.globs;
// `project` - paths to first-party assets.
// - `project.js` - Array of first-party JS assets.
// - `project.css` - Array of first-party CSS assets.
var project = manifest.getProjectGlobs();
// CLI options
var enabled = {
// Enable static asset revisioning when `--production`
rev: argv.production,
// Disable source maps when `--production`
maps: !argv.production,
// Fail styles task on error when `--production`
failStyleTask: argv.production
};
// Path to the compiled assets manifest in the dist directory
var revManifest = path.dist + 'assets.json';
// ## Reusable Pipelines
// See https://github.com/OverZealous/lazypipe
// ### CSS processing pipeline
// Example
// ```
// gulp.src(cssFiles)
// .pipe(cssTasks('main.css')
// .pipe(gulp.dest(path.dist + 'styles'))
// ```
var cssTasks = function(filename) {
return lazypipe()
.pipe(function() {
return $.if(!enabled.failStyleTask, $.plumber());
})
.pipe(function() {
return $.if(enabled.maps, $.sourcemaps.init());
})
.pipe(function() {
return $.if('*.less', $.less());
})
.pipe(function() {
return $.if('*.scss', $.sass({
outputStyle: 'nested', // libsass doesn't support expanded yet
precision: 10,
includePaths: ['.'],
errLogToConsole: !enabled.failStyleTask
}));
})
.pipe($.concat, filename)
.pipe($.pleeease, {
autoprefixer: {
browsers: [
'last 2 versions', 'ie 8', 'ie 9', 'android 2.3', 'android 4',
'opera 12'
]
}
})
.pipe(function() {
return $.if(enabled.rev, $.rev());
})
.pipe(function() {
return $.if(enabled.maps, $.sourcemaps.write('.'));
})();
};
// ### JS processing pipeline
// Example
// ```
// gulp.src(jsFiles)
// .pipe(jsTasks('main.js')
// .pipe(gulp.dest(path.dist + 'scripts'))
// ```
var jsTasks = function(filename) {
return lazypipe()
.pipe(function() {
return $.if(enabled.maps, $.sourcemaps.init());
})
.pipe($.concat, filename)
.pipe($.uglify)
.pipe(function() {
return $.if(enabled.rev, $.rev());
})
.pipe(function() {
return $.if(enabled.maps, $.sourcemaps.write('.'));
})();
};
// ### Write to rev manifest
// If there are any revved files then write them to the rev manifest.
// See https://github.com/sindresorhus/gulp-rev
var writeToManifest = function(directory) {
return lazypipe()
.pipe(gulp.dest, path.dist + directory)
.pipe(function() {
return $.if('**/*.{js,css}', browserSync.reload({stream:true}));
})
.pipe($.rev.manifest, revManifest, {
base: path.dist,
merge: true
})
.pipe(gulp.dest, path.dist)();
};
// ## Gulp tasks
// Run `gulp -T` for a task summary
// Build patternlab
gulp.task('patternlab', shell.task('php core/builder.php -g'));
gulp.task('patternlab-css', shell.task('php core/builder.php -g -c'));
// ### Styles
// `gulp styles` - Compiles, combines, and optimizes Bower CSS and project CSS.
// By default this task will only log a warning if a precompiler error is
// raised. If the `--production` flag is set: this task will fail outright.
gulp.task('styles', ['wiredep'], function() {
var merged = merge();
manifest.forEachDependency('css', function(dep) {
var cssTasksInstance = cssTasks(dep.name);
if (!enabled.failStyleTask) {
cssTasksInstance.on('error', function(err) {
console.error(err.message);
this.emit('end');
});
}
merged.add(gulp.src(dep.globs, {base: 'styles'})
.pipe(cssTasksInstance));
});
return merged
.pipe(writeToManifest('styles'));
});
// ### Scripts
// `gulp scripts` - Runs JSHint then compiles, combines, and optimizes Bower JS
// and project JS.
gulp.task('scripts', ['jshint'], function() {
var merged = merge();
manifest.forEachDependency('js', function(dep) {
merged.add(
gulp.src(dep.globs, {base: 'scripts'})
.pipe(jsTasks(dep.name))
);
});
return merged
.pipe(writeToManifest('scripts'));
});
// ### Fonts
// `gulp fonts` - Grabs all the fonts and outputs them in a flattened directory
// structure. See: https://github.com/armed/gulp-flatten
gulp.task('fonts', function() {
return gulp.src(globs.fonts)
.pipe($.flatten())
.pipe(gulp.dest(path.dist + 'fonts'));
});
// ### Images
// `gulp images` - Run lossless compression on all the images.
gulp.task('images', function() {
return gulp.src(globs.images)
.pipe($.imagemin({
progressive: true,
interlaced: true,
svgoPlugins: [{removeUnknownsAndDefaults: false}]
}))
.pipe(gulp.dest(path.dist + 'images'));
});
// ### JSHint
// `gulp jshint` - Lints configuration JSON and project JS.
gulp.task('jshint', function() {
return gulp.src([
'bower.json', 'gulpfile.js'
].concat(project.js))
.pipe($.jshint())
.pipe($.jshint.reporter('jshint-stylish'))
.pipe($.jshint.reporter('fail'));
});
// ### Clean
// `gulp clean` - Deletes the build folder entirely.
gulp.task('clean', require('del').bind(null, [path.dist]));
// ### Watch
// `gulp watch` - Use BrowserSync to proxy your dev server and synchronize code
// changes across devices. Specify the hostname of your dev server at
// `manifest.config.devUrl`. When a modification is made to an asset, run the
// build step for that asset and inject the changes into the page.
// See: http://www.browsersync.io
gulp.task('watch', function() {
browserSync({
proxy: config.devUrl,
snippetOptions: {
whitelist: ['/wp-admin/admin-ajax.php'],
blacklist: ['/wp-admin/**']
}
});
gulp.watch([path.source + '../_data/**/*'], ['patternlab']);
gulp.watch([path.source + '../_patterns/**/*'], ['patternlab']);
gulp.watch([path.source + 'styles/**/*'], ['build-styles']);
gulp.watch([path.source + 'scripts/**/*'], ['build-scripts']);
gulp.watch([path.source + 'fonts/**/*'], ['build-images-fonts']);
gulp.watch([path.source + 'images/**/*'], ['build-images-fonts']);
gulp.watch(['bower.json'], ['wiredep']);
gulp.watch('**/*.php', function() {
browserSync.reload();
});
});
// ### Build
// `gulp build` - Run all the build tasks but don't clean up beforehand.
// Generally you should be running `gulp` instead of `gulp build`.
gulp.task('build', function (callback){
runSequence(['styles', 'scripts', 'fonts', 'images'], 'patternlab-css', callback);
});
// Watch sequential tasks //
gulp.task('build-styles', function (callback){
runSequence('styles', 'patternlab', callback);
});
gulp.task('build-scripts', function (callback){
runSequence(['jshint', 'scripts'], 'patternlab', callback);
});
gulp.task('build-images-fonts', function (callback){
runSequence(['images', 'fonts'], 'patternlab', callback);
});
// ### Wiredep
// `gulp wiredep` - Automatically inject Less and Sass Bower dependencies. See
// https://github.com/taptapship/wiredep
gulp.task('wiredep', function() {
var wiredep = require('wiredep').stream;
return gulp.src(project.css)
.pipe(wiredep())
.pipe($.changed(path.source + 'styles', {
hasChanged: $.changed.compareSha1Digest
}))
.pipe(gulp.dest(path.source + 'styles'));
});
// ### Gulp
// `gulp` - Run a complete build. To compile for production run `gulp --production`.
gulp.task('default', ['clean'], function() {
gulp.start('build');
});
{
"name": "sage",
"version": "8.0.1",
"author": "Ben Word <[email protected]>",
"homepage": "https://roots.io/sage/",
"private": true,
"repository": {
"type": "git",
"url": "git://github.com/roots/sage.git"
},
"bugs": {
"url": "https://github.com/roots/sage/issues"
},
"licenses": [
{
"type": "MIT",
"url": "http://opensource.org/licenses/MIT"
}
],
"scripts": {
"build": "bower install && gulp",
"jshint": "gulp jshint",
"jscs": "jscs gulpfile.js assets/scripts/*.js"
},
"engines": {
"node": ">= 0.12.0",
"npm": ">=2.1.5"
},
"devDependencies": {
"asset-builder": "^1.0.1",
"browser-sync": "^2.0.1",
"del": "^1.1.1",
"gulp": "^3.8.10",
"gulp-changed": "^1.1.0",
"gulp-concat": "^2.3.4",
"gulp-flatten": "0.0.4",
"gulp-if": "^1.2.5",
"gulp-imagemin": "^2.0.0",
"gulp-jshint": "^1.8.4",
"gulp-less": "^3.0.1",
"gulp-load-plugins": "^0.8.0",
"gulp-pleeease": "^1.1.0",
"gulp-plumber": "^0.6.3",
"gulp-rename": "^1.2.0",
"gulp-rev": "^3.0.0",
"gulp-sass": "^1.2.4",
"gulp-shell": "0.3.0",
"gulp-sourcemaps": "^1.1.1",
"gulp-uglify": "^1.0.1",
"imagemin-pngcrush": "^4.0.0",
"jshint-stylish": "^1.0.0",
"lazypipe": "^0.2.2",
"merge-stream": "^0.1.7",
"run-sequence": "^1.0.2",
"traverse": "^0.6.6",
"wiredep": "^2.1.0",
"yargs": "^3.2.1"
}
}
@icandeveloper
Copy link

Or is there a way you can zip and post your whole set up somewhere?

@heyalbert
Copy link

Any idea how to use it with compass and Patternlab.io?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment