Task to build a requirejs AMD module out of a directory of files.
Install this grunt plugin next to your project's grunt.js gruntfile with: npm install grunt-require-dir
Then add this line to your project's grunt.js
gruntfile:
grunt.loadNpmTasks('grunt-require-dir');
- require-dir
- src : Your source files to include. Should all be under one common directory
- baseDir : The local base directory prefix, removed from the source file paths (optional)
- prefixDir : The new directory path to place in front of the source file paths (optional)
- dest : The destination AMD file
- plugin : The requirejs plugin to prefix the files with (optional). Useful for templates.
'require-dir' : {
all : {
plugin : 'tpl',
src : 'test/fixtures/texttree/**/*.tmpl',
baseDir : 'test/fixtures/texttree/',
prefixDir : 'customDir/',
dest : 'test/fixtures/texttree.js'
}
},
Given the config above, and the file tree below
|_ /fixtures
| |_ /texttree
| | |_ /A
| | | |_ one.tmpl
| | | |_ two.tmpl
| | |_ bar.tmpl
| | |_ foo.tmpl
| | |_ unwanted.ext
Running grunt require-dir
(or grunt require-dir:all
), you will
get a file in test/fixtures/texttree.js
with the contents
define(function(require){
return {
'A' : {
'one' : require("tpl!customDir/A/one.tmpl"),
'two' : require("tpl!customDir/A/two.tmpl")
},
'bar' : require("tpl!customDir/bar.tmpl"),
'foo' : require("tpl!customDir/foo.tmpl")
};
});
Which, when required, will allow you to access modules in the same structure as your directory, e.g.
define(['texttree.js'],function(texttree){
texttree.A.one; // A/one.tmpl
texttree.bar; // bar.tmpl
})
0.3.0 First public release
Copyright (c) 2012 Jarrod Overson
Licensed under the MIT license.