Skip to content

Instantly share code, notes, and snippets.

@robkb
Last active August 29, 2015 14:02
Show Gist options
  • Save robkb/3d38572b257752a85320 to your computer and use it in GitHub Desktop.
Save robkb/3d38572b257752a85320 to your computer and use it in GitHub Desktop.
Gruntfile for fb-flo watcher, plus less+autoprefixer with watcher
/*
* gruntfile based on https://gist.github.com/billyvg/2a7321623b2d2a87381c
*
* [grab the associated `package.json` from here](https://gist.github.com/robert-bosweb/6c0303c11839f063b854)
* */
module.exports = function (grunt) {
var pathToMainLessFile = "assets/less/"; // include the trailing forward slash
var mainLessFileName = "central"; //.less is implied; leave it out
var mainLessFile = pathToMainLessFile + mainLessFileName;
// need to use var as key name; can it be done inside grunt.initConfig()?
var lessMainFiles = {};
lessMainFiles[mainLessFile + '.min.css'] = mainLessFile + '.less';
grunt.initConfig({
less: {
main: {
options: {
sourceMap: true,
compress: true,
sourceMapFilename: pathToMainLessFile + mainLessFileName + '.css.map',
sourceMapURL: mainLessFileName + '.css.map',
sourceMapRootpath: '/'
},
files: lessMainFiles
}
},
autoprefixer: {
options: {
map: true
},
main: {
src: mainLessFile + '.min.css',
dest: mainLessFile + '.min.prefixed.css'
}
},
watch:{
lessFiles :{
files: [
'**/*.less',
'!node_modules/**'
],
tasks: ['lessPrefixed'],
options: {}
}
},
concurrent: {
watchers: {
tasks: ['flo', 'watch'],
options: {
logConcurrentOutput: true
}
}
}
});
grunt.loadNpmTasks('grunt-autoprefixer');
grunt.loadNpmTasks('grunt-contrib-less');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-concurrent');
// configure your flo task
grunt.registerTask('flo', 'Runs the fb-flo server for live editing', function () {
var flo = require('fb-flo');
var path = require('path');
var fs = require('fs');
//noinspection JSUnusedLocalSymbols
var done = this.async();
var WEBROOT = process.cwd();
//noinspection JSUnusedLocalSymbols
var server = flo(
WEBROOT,
{
port: 8888,
host: 'localhost',
verbose: false,
persistent: true,
useFilePolling: false,
pollingInterval: 400,
globs: '**/*.css'
},
function (fp, callback) {
var excludefile = !!( fp.match(/___$/) || fp.match(/^\.idea/) || fp.match(/\.git/) );
if (!excludefile) {
console.log('fp: ' + fp);
callback({
resourceURL: '/' + fp,
contents: fs.readFileSync(WEBROOT + '/' + fp).toString()
/*reload : true*/
});
}
/*if (fp.match(/\.js$/)) {
callback({
resourceURL: fp.replace(/^web\/js/, '/js'),
contents : fs.readFileSync(WEB_ROOT + '/' + fp).toString(),
reload: true
});
}
else if (fp.match(/^build.*?\.css$/)) {
callback({
resourceURL: fp.replace(/^build\/web\/css/, '/css'),
contents : fs.readFileSync(WEB_ROOT + '/' + fp).toString(),
reload: true
});
}*/
}
);
});
grunt.registerTask('lessPrefixed', ['less:main', 'autoprefixer:main']);
grunt.registerTask('default', ['lessPrefixed', 'concurrent:watchers']);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment