Created
March 26, 2013 00:25
-
-
Save jpillora/5242135 to your computer and use it in GitHub Desktop.
Grunt task to include globs of files into one file using templates
This file contains hidden or 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
glob = require "glob-manifest" | |
async = require "async" | |
_ = require "lodash" | |
class IncludesRun | |
#types of buildable files | |
templates: | |
js: _.template '<script src="<%= src %>"></script>' | |
css: _.template '<link rel="stylesheet" href="<%= src %>"/>' | |
constructor: (@grunt, @name, @data, @done) -> | |
#opts | |
_.bindAll @ | |
@opts = @data.options or {} | |
targets = _.pick(@data, 'js', 'css') | |
builds = _.map targets, (file, type) -> | |
{ type, src: file.src, dest: file.dest } | |
async.each builds, @build, (err) => | |
if err | |
@grunt.fail.warn err | |
return done false | |
done true | |
#build files | |
build: (obj, callback) -> | |
glob obj.src, (error, files) => | |
if error isnt null | |
return callback(error) | |
contents = files.map (f) => | |
@template(f, obj.type) | |
@grunt.file.write obj.dest, contents.join("\n") | |
callback() | |
#build one file | |
template: (filepath, type) -> | |
filepath = filepath.replace(@opts.root, '') if @opts.root | |
# console.log "template: #{filepath}" | |
if not @templates[type] | |
return grunt.fail.warn "Unkown file type: #{ext}" | |
return @templates[type]({ src: filepath}) | |
module.exports = (grunt) -> | |
# define new task | |
grunt.registerMultiTask "includes", "Include Scripts via Jade", -> | |
new IncludesRun(grunt, @target, @data, @async()) | |
#and finally, config | |
includes: | |
dev: | |
options: | |
root: './build/dev/' | |
#scripts | |
js: | |
dest: './temp/includes/app-scripts.html' | |
src: [ | |
'./build/dev/scripts/init.js' | |
'./build/dev/scripts/services/*.js' | |
'./build/dev/scripts/filters/*.js' | |
'./build/dev/scripts/directives/*.js' | |
'./build/dev/scripts/controllers/*.js' | |
'./build/dev/scripts/run.js' | |
] | |
#styles | |
css: | |
dest: './temp/includes/app-styles.html' | |
src: [ | |
'./build/dev/**/*.css' | |
] | |
uat: | |
options: | |
root: './build/uat/' | |
js: | |
#scripts | |
dest:'./temp/includes/app-scripts.html' | |
src: './build/uat/scripts/app.js' | |
css: | |
#styles | |
dest: './temp/includes/app-styles.html' | |
src: './build/uat/styles/app.css' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment