Skip to content

Instantly share code, notes, and snippets.

@drsm79
Created August 6, 2012 14:36
Show Gist options
  • Save drsm79/3274880 to your computer and use it in GitHub Desktop.
Save drsm79/3274880 to your computer and use it in GitHub Desktop.
Bundle task
bundle: {
// Files to bundle up that aren't css/js managed by release. Copied into
// same directory structure
files: ['*.html'],
// Folders to bundle up that aren't managed by release. Copy files and
// folders from src (key) to destination (value), relative to the
// destination parameter.
folders: {'assets/img':'assets/img', 'dist/release':''},
destination: 'bundle'
}
// Copy files and folders into a new directory structure
grunt.registerTask("bundle", "Bundle files and directories.", function(){
// bail if release hasn't run
grunt.task.requires('release');
// TODO: ditch this when grunt v0.4 is released
grunt.util = grunt.util || grunt.utils;
var _ = grunt.util._;
var options = _.defaults(grunt.config("bundle") || {}, {
destination: 'bundle',
files: ['*.html'],
folders: {'dist': ''}
});
grunt.file.mkdir(options.destination);
function recurse_expand(source, dest){
var files = {};
grunt.file.recurse(source, function(abspath, r, s, f){
files[abspath] = abspath.replace(source, dest);
});
return files;
}
// Copy over (wild carded) files
_.each(options.files, function(f){
_.each(grunt.file.expand(f), function(source){
var dest = options.destination + '/' + source;
grunt.log.writeln('copy ' + source + ' to ' + dest);
grunt.file.copy(source, dest);
});
});
// Copy over (wild carded) directory contents
_.each(options.folders, function(dest, origin, list){
_.each(recurse_expand(origin, dest), function(newpath, path, l){
var destination = options.destination + '/' + newpath;
destination = destination.replace('//', '/');
grunt.log.writeln('copy ' + path + ' to ' + destination);
grunt.file.copy(path, destination);
});
});
});
// Build a release (lint, test, minify css/js) then copy resulting
// files and all other assets into a bundle directory
grunt.registerTask("package", "release bundle");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment