grunt-angular-templates-amd

Grunt build task to concatenate & register your AngularJS templates in the $templateCache wrapped for amd usage

grunt-angular-templates-amd

Grunt build task to concatenate & register your AngularJS templates in the $templateCache wrapped for amd usage

NOTE:

  • Use 0.1.x for Grunt 0.3.x.
  • Use 0.2.x or 0.3.x for Grunt 0.4.x.

Install this grunt plugin next to your project's grunt.js gruntfile with: npm install grunt-angular-templates

Then add this line to your project's grunt.js gruntfile:

grunt.loadNpmTasks('grunt-angular-templates');

This plugin provides the grunt task ngtemplates, which will allow you to compile your HTML templates into a single JS file, which preloads $templateCache to prevent round-trips to the server.

// grunt.js 
grunt.initConfig({
  ngtemplates:    {
    build:        {
      options:    {
        module:   'template',         // AMD module name (should be the filename) 
        base:     'src/views',        // $templateCache ID will be relative to this folder 
        prepend:  '/static/assets/'   // (Optional) Prepend path to $templateCache ID 
      },
      src:        [ 'src/views/**.html' ],
      dest:       'dist/templates.js'
    }
  }
});

This will generate the following at dist/templates.js:

(function() {
  var templatesCacheLoader = ["$templateCache", function($templateCache) {
 
    ...
 
  }];
 
  // CommonJS module is defined 
  if (typeof module !== "" && module.exports) {
      module.exports = templatesCacheLoader;
  }
  /*global ender:false */
  if (typeof ender === '') {
      this['templates'] = templatesCacheLoader;
  }
  /*global define:false */
  if (typeof define === "function" && define.amd) {
      define("templates", [], function () {
          return templatesCacheLoader;
      });
  }
})();
var app = angular.module('MyApp', []);
var templateCacheLoader = require('template');
 
if(templateCacheLoader != null)
  app.run templateCacheLoader;
  • Released to NPM

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using grunt.

Copyright (c) 2013 Andy Damevin Licensed under the MIT license.