Use @cowboys wonderful js based optimizer grunt together with @jrburkes r.js optimizer, to build your AMD based projects with grunt.
Now with almond goodness.
I removed the 'clearTarget' config option from the plugin, because i want you all to go to grunt-contrib page and use their 'clean' task instead!
Install this grunt plugin next to your project's [grunt.js gruntfile][getting_started] with: npm install grunt-requirejs
Then add this line to your project's grunt.js
gruntfile.
grunt.loadNpmTasks('grunt-requirejs');
Load the grunt-requirejs task as described in 'Getting started' and add your r.js optimizer configuration to your grunt file:
Example require js optimizer grunt file config entry:
// ... grunt file contents
requirejs: {
dir: 'build',
appDir: 'src',
baseUrl: 'js',
paths: {
underscore: '../vendor/underscore',
jquery : '../vendor/jquery',
backbone : '../vendor/backbone'
},
pragmas: {
doExclude: true
},
skipModuleInsertion: false,
optimizeAllPluginResources: true,
findNestedDependencies: true
}
// ... even more grunt file contents
You see, there is no difference in declaring your require config when your using your gruntfile instead of using a separate requirejs config file.
If you like to replace require.js with almond.js during the build process, grunt-requirejs comes with an experimental almond injection mode. It even converts your require script calls in your html files to call the 'almondyfied' module, instead of calling require.js that then calls (e.g.) loads the module.
The only constraint for using the auto almond insertion is, that you at least define one module (mostly named 'main').
// ... grunt file contents
requirejs: {
// almond specific contents
// *insert almond in all your modules
almond: true,
// *replace require script calls, with the almond modules
// in the following files
replaceRequireScript: [{
files: ['build/index.html'],
module: 'main'
}],
// "normal" require config
// *create at least a 'main' module
// thats necessary for using the almond auto insertion
modules: [{name: 'main'}],
dir: 'build',
appDir: 'src',
baseUrl: 'js',
paths: {
underscore: '../vendor/underscore',
jquery : '../vendor/jquery',
backbone : '../vendor/backbone'
},
pragmas: {
doExclude: true
},
skipModuleInsertion: false,
optimizeAllPluginResources: true,
findNestedDependencies: true
}
// ... even more grunt file contents
If you define a special output name for your generated module file, you have to specify a "modulePath" property inside your "replaceRequireScript" configuration
requirejs: {
almond: true,
replaceRequireScript: [{
files: ['index.html'],
module: 'main',
modulePath: '/js/main-build'
}],
baseUrl: "js",
paths: {
'Handlebars': 'libs/Handlebars',
'Backbone': 'libs/backbone',
'underscore': 'libs/underscore',
'json2': 'libs/json2',
},
modules: [{name: 'main'}],
out: 'js/main-build.js'
}
First occured in issue #3. You probably have to set
requirejs: {
wrap: true
}
like described here: https://github.com/jrburke/almond#usage
By default it is assumed that your are using the optimizer for only JS or CSS not both. However should you wish to use require.js to optimize your CSS in addition to your JS, this is possible using a dual config. This will allow you to maintain your config options for both your CSS and JS under the requirejs key in your grunt.js.
// ... grunt file contents
requirejs: {
js: {
// config for js
},
css: {
// config for css
}
}
// ... even more grunt file contents
Then when calling your task you can pass as an argument the mode your wish to run the task in.
grunt.registerTask('release', 'requirejs:css requirejs:js');
or
> grunt requirejs:js
> grunt requirejs:css
If no argument is specified, then the task will look for the approprate config in the following order, JS, CSS and then finally it will use whatever config has been defined if neither JS or CSS is found.
- Removed jQuery dependency and replaced it with cheerio
- Updated versions of 3rd party libs
- RequireJS Version bump to 2.0
- Removed npm dependency for tracing the almond file
- Added some informations in the readme about the almond 'wrap=true'
- Added 'modulePath' configuration option for specifying your modules path
- Added 'modulePath' documentation
- Added dual config
- Optimized almond integration (removed npm dependency)
- Readme updates
- requirejs isnt a multi task anymore
- Removed clearTarget (use grunt-contrib clean instead)
- Added almond integration
- Added automatic almond js module script tag replacement for html files
- Improved documentation
- Initial Release
Copyright (c) 2012 asciidisco
Licensed under the MIT license.