Version your RequireJS enabled script
npm install grunt-static-versioning-requirejsGSVR is a Grunt plugin that allows you to version all your static content so that you can set far out content expiration on Javascript, CSS, HTML Templates and images and ensure that current builds of software are always serving the right versions.
GSRV is meant to work with James Burke's awesome RequireJS library for scripts and optionally with LESS for stylesheets.
If you don't use a module loader with your Javascript - YOU SHOULD!
Using a CommonJS or AMD architecture is almost a must in this day and for such script heavy web applications. And, Require forces you into a non-blocking, modular architecuture which subsequently allows for out of the box script packaging and minification.
This plugin is meant to be used during the continuous integration process since it effectively 'productionalizes' your content. That doesn't have to be the case if you have a custom build process outside of any CI tool.
1. RequireJS Optimizer Integration to version just created production level Require AMD packages
2. Optional Content Delivery Network integration to FTP your content to your favorite CDN
3. Regex replacement within source code so that you can tweak your static paths to point to the new production content (locally or on CDN - ex: /js-build instead of /js or [CDNPATH]/js-build)
4. Client side template support to serve your Angular, Knockout, etc. templates from your CDN instead of your webserver (requires CORS support from your CDN)
5. Coming Soon LESS/CSS/StyleSheet Images Support so that you can version all your CSS and your stylesheet images and have them sent to your desired location
~0.4.1If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install these plugins with these commands:
npm install grunt-contrib-requirejs --save-dev
npm install grunt-static-versioning-requirejs --save-dev
Once the plugins have been installed, enable them inside your Gruntfile with these line of JavaScript:
grunt.loadNpmTasks('grunt-contrib-requirejs');
grunt.loadNpmTasks('grunt-static-versioning-requirejs');
requirejs to the data object passed into grunt.initConfig().Refer to the requirejs grunt plugin sample configuration for all the configuration options.
The configuration below assumes that the require configuration file will be in /static/js and be named site.js.
The output for the plugin goes in /static/js-build.
#### RequireJS Configuration
requirejs: {
compile: {
options: {
dir: 'static/js-build',
baseUrl: 'static/js', // Directory to look for the require configuration file
mainConfigFile: 'static/js/site.js', // This is relative to the grunt file
paths: {
jquery: 'empty:', // exclude from build because we use the google cdn
ko: 'empty:', // exclude from build because we use the microsoft cdn
angular: 'empty:' // exclude from build because we use the google cdn
},
modules: [
{ name: 'app/global' }, // Create a global bundle
{
name: 'app/account/account', // Creaete an account bundle and exclude global
exclude: ['app/global']
}
],
preserveLicenseComments: false, // remove all comments
removeCombined: true, // remove files which aren't in bundles
optimize: 'uglify2' // minify bundles with uglify 2
}
}
}
##### Options
For a complete list of options see the requirejs grunt plugin sample configuration.
#### Setup Require in your web application
Require JS should be served in your layout page, master page, etc just before the closing body tag. Serve require from the cloud fare CDN if you don't have a CDN of your own: