Skip to content

Instantly share code, notes, and snippets.

@dryan
Last active December 25, 2015 17:19
Show Gist options
  • Save dryan/7012554 to your computer and use it in GitHub Desktop.
Save dryan/7012554 to your computer and use it in GitHub Desktop.
gruntfile for watching directory and compiling scss files on save
'use strict';
var
SCSS_ROOT = 'media',
CSS_ROOT = 'media',
SASS_FRAMEWORKS = 'bower_components',
fs = require('fs'),
path = require('path'),
scssFileMap = {},
scssFiles = fs.readdirSync(SCSS_ROOT),
components = [],
i = scssFiles.length;
while(i--) {
var
filename = scssFiles[i];
if(path.extname(filename) === '.scss') {
scssFileMap[path.join(CSS_ROOT, filename.replace(/\.scss$/, '.css'))] = path.join(SCSS_ROOT, filename);
}
}
function componentExists(dir) {
var
x = components.length;
while(x--) {
var
component = components[x];
if(dir.indexOf(component) === 0) {
return true;
}
}
return false;
}
function findSassPaths(dir) {
if(componentExists(dir) || path.basename(dir) === 'src') {
return;
}
var
items = fs.readdirSync(dir),
x = items.length;
items.reverse();
while(x--) {
var
item = items[x],
itemPath = path.join(dir, item);
if(fs.lstatSync(itemPath).isDirectory()) {
findSassPaths(itemPath);
} else if(['.scss', '.sass'].indexOf(path.extname(itemPath)) > -1 && !componentExists(itemPath)) {
components.push(['.', dir].join(path.sep));
return;
}
}
}
findSassPaths(SASS_FRAMEWORKS);
module.exports = function (grunt) {
grunt.initConfig({
pkg: {
name: 'grunt-contrib-sass'
},
sass: {
options: {
style: 'compressed',
sourcemap: true,
quiet: true,
loadPath: components
},
compile: {
files: scssFileMap
}
},
watch: {
files: [".", SCSS_ROOT, "*"].join(path.sep),
tasks: ["default"]
}
});
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.registerTask('default', ['sass']);
};
{
"name": "deploy",
"version": "0.0.1",
"devDependencies": {
"grunt": "~0.4.1",
"grunt-contrib-uglify": "latest",
"grunt-contrib-jshint": "latest",
"grunt-contrib-watch": "latest",
"grunt-contrib-concat": "latest",
"grunt-contrib-copy": "latest",
"grunt-contrib-sass": "latest",
"grunt-contrib-clean": "latest",
"grunt-contrib-nodeunit": "latest"
}
}
@oldwestaction
Copy link

ohmyglob

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