Last active
December 29, 2015 17:29
-
-
Save lifedraft/7704321 to your computer and use it in GitHub Desktop.
Compile all coffee and compass files recursively within an project with grunt.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function( grunt ) { | |
// Compile all coffee and compass files recursively within an project with grunt. | |
// Usage: | |
// > cd <my-project> | |
// > npm install grunt-contrib-watch | |
// > npm install grunt-contrib-coffee | |
// > npm install grunt-contrib-compass | |
// > grunt assets_config | |
// this generates an assets_config.js file, so when you create new coffee or sass file you have to rerun this task. | |
// > grunt watch | |
if (grunt.file.exists('assets_config.js')) { | |
var config = grunt.file.readJSON('assets_config.js'); | |
grunt.initConfig(config); | |
}; | |
grunt.registerTask('assets_config', 'search all modules', function() { | |
var compass = grunt.config.get('compass') || {}; | |
var coffee = grunt.config.get('coffee') || {}; | |
var watch = grunt.config.get('watch') || {}; | |
grunt.file.recurse('.', function(abspath, rootdir, subdir, filename) { | |
if (!subdir) { | |
return false; | |
}; | |
if (subdir.indexOf('/sass') !== -1) { | |
if (!(subdir in compass)) { | |
var stylesheets_dir = subdir.split("sass").join('') + 'stylesheets'; | |
watch[subdir] = { | |
files: [subdir + '/*.{scss,sass}'], | |
tasks: ['compass:' + subdir] | |
}; | |
compass[subdir] = { | |
options: { | |
sassDir: subdir, | |
cssDir: stylesheets_dir | |
} | |
}; | |
}; | |
}; | |
if (filename.indexOf('.coffee') !== -1) { | |
coffee[subdir] = coffee[subdir] || { | |
files: {}, | |
options: { | |
sourceMap: true | |
}, | |
}; | |
var javascripts_dir = subdir.split("coffeescripts").join('') + 'javascripts'; | |
if (!(subdir in watch)) { | |
watch[subdir] = { | |
// files: '<config.coffee.' + subdir +'.files>', // [subdir + '/*.coffee'], | |
files: [subdir + '/*.coffee'], | |
tasks: ['coffee:' + subdir] | |
}; | |
} | |
coffee[subdir].files[javascripts_dir + '/' + filename + '.js'] = abspath; | |
} | |
}); | |
var asset_config = { | |
compass: compass, | |
coffee: coffee, | |
watch: watch | |
}; | |
grunt.file.delete('assets_config.js'); | |
grunt.file.write('assets_config.js', JSON.stringify(asset_config)); | |
}); | |
grunt.loadNpmTasks('grunt-contrib-watch'); | |
grunt.loadNpmTasks('grunt-contrib-coffee'); | |
grunt.loadNpmTasks('grunt-contrib-compass'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment