Grunt Require-Grep Task
Searchs for AMD modules required on your source code (eg. template files) and generates an AMD module from it.
Example:
<!-- a/path/to/page.html --><!-- require("jquery, widgets, twitter") --><!-- another/path/to/anotherpage.html --><!-- require("jquery, widgets, fx") -->
Running grunt-requiregrep can generate:
;
Now you can include this new module in your requirejs main config file. This way if you use a bundler like r.js (check grunt-contrib-requirejs) it will include all modules used on your source files automatically.
Installation
Install this grunt plugin with: npm install grunt-requiregrep
Then add this line to your grunt.js gruntfile:
grunt;
Documentation
Supports the following options:
- src/files: source files to grep for AMD dependencies (default: **/*.*html).
- dest: output filename (eg
all.js
) - options:
- requirePattern: regex to detect dependencies on source files, first capture group should be a comma-separated list of module names. default:
/require\(\s*[\'\"]([^\'\"]*)[\'\"]/gi
- moduleName: output module name. default: null, ie. anonymous module
- onLoad: code to include on module load (eg.
console.log('all modules loaded!');
). - forEachFile: function that will be called for each parsed file with these arguments: file, contents, registerDependency (use this function add a dependency programmatically)
- requirePattern: regex to detect dependencies on source files, first capture group should be a comma-separated list of module names. default:
Example Usage
/*jslint node:true*/ module { 'use strict'; grunt; // load the task grunt; grunt;};