• Stars
    star
    712
  • Rank 63,595 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 12 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Grunt build task to concatenate & pre-load your AngularJS templates

grunt-angular-templates

Build Status Dependencies devDependencies

Speed up your AngularJS app by automatically minifying, combining, and automatically caching your HTML templates with $templateCache.

Here's an example of the output created by this task from multiple .html files:

angular.module('app').run(["$templateCache", function($templateCache) {
  $templateCache.put("home.html",
    // contents for home.html ...
  );
  ...
  $templateCache.put("src/app/templates/button.html",
    // contents for button.html
  );
}]);

Then, when you use ng-include or templateUrl with $routeProvider, the template is already loaded without an extra AJAX request!

Table of Contents

Installation

This plugin requires Grunt ~0.4.0

Usemin integration requires grunt-usemin ~2.0.0

Install the plugin:

$ npm install grunt-angular-templates --save-dev

Enable the plugin within your Gruntfile:

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

Options

angular

Global namespace for Angular.

If you use angular.noConflict(), then set this value to whatever you re-assign angular to. Otherwise, it defaults to angular.

bootstrap

Callback to modify the bootstraper that registers the templates with $templateCache.

By default, the bootstrap script wraps function($templateCache) { ... } with:

angular.module('app').run(['$templateCache', ... ]);

If you want to create your own wrapper so you register the templates as an AMD or CommonJS module, set the bootstrap option to something like:

bootstrap: function(module, script) {
  return 'module.exports[module] = ' + script + ';';
}

concat

Name of concat target to append the compiled template path to.

This is especially handy if you combine your scripts using grunt-contrib-concat or grunt-usemin.

htmlmin

Object containing htmlmin options that will significantly reduce the filesize of the compiled templates.

Without this, the HTML (whitespace and all) will be faithfully compiled down into the final .js file. Minifying that file will only cut down on the Javascript code, not the HTML within the strings.

Note - this does incur a performance cost. Simply leave out this option to prevent minificaiton.

I recommend using the following settings for production:

htmlmin: {
  collapseBooleanAttributes:      true,
  collapseWhitespace:             true,
  keepClosingSlash:               true, // Only if you are using SVG in HTML
  removeAttributeQuotes:          true,
  removeComments:                 true, // Only if you don't use comment directives!
  removeEmptyAttributes:          true,
  removeRedundantAttributes:      true,
  removeScriptTypeAttributes:     true,
  removeStyleLinkTypeAttributes:  true
}

module

String of the angular.module to register templates with.

If not specified, it will automatically be the name of the ngtemplates subtask (e.g. app, based on the examples below).

prefix

String to prefix template URLs with. Defaults to ''

If you need to use absolute urls:

ngtemplates: {
  app: {
    options: {
      prefix: '/'
    }
  }
}

If you serve static assets from another directory, you specify that as well.

source

Callback to modify the template's source code.

If you would like to prepend a comment, strip whitespace, or do post-processing on the HTML that ngtemplates doesn't otherwise do, use this function.

append

Boolean to indicate the templates should be appended to dest instead of replacing it. Normally grunt-angular-templates creates a new file at dest. This option makes it append the compiled templates to the dest file rather than replace its contents. This is just a useful alternative to creating a temporary dest file and concatting it to your application.

standalone

Boolean indicated if the templates are part of an existing module or a standalone. Defaults to false.

  • If the value is false, the module will look like angular.module('app'), meaning app module is retrieved.
  • If the value is true, the module will look like angular.module('app', []), meaning app module is created.

url

Callback to modify the template's $templateCache URL.

Normally, this isn't needed as specifying your files with cwd ensures that URLs load via both AJAX and $templateCache.

usemin

Path to <!-- build:js [path/to/output.js] --> usemin target

This should be the output path of the compiled JS indicated in your HTML, such as path/to/output.js shown here.

quotes

Use single or double quotes to wrap the template strings

Defaults to 'double', other option is 'single'

Usage

Compiling HTML Templates

After configuring your ngtemplates task, you can either run the task directly:

$ grunt ngtemplates

Or, bake it into an existing task:

grunt.registerTask('default', [ 'jshint', 'ngtemplates', 'concat' ]);

Including Compiled Templates

Finally, you have to load the compiled templates' .js file into your application.

Using HTML

<script src="templates.js"></script>

Using Grunt's concat task:

This is my personal preference, since you don't have to worry about what the destination file is actually called.

concat:   {
  app:    {
    src:  [ '**.js', '<%= ngtemplates.app.dest %>' ],
    dest: [ 'app.js' ]
  }
}

Using grunt-usemin

Using the following HTML as an example:

<!-- build:js dist/vendors.js -->
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-resource/angular-resource.js"></script>
<!-- endbuild -->

Do not use the concat option, even though grunt-usemin generates a concat.generated object behind the scenes. Instead, use the usemin option to indicate the anticipated output filepath from grunt-usemin.

ngtemplates:  {
  app:        {
    src:      '**.html',
    dest:     'template.js',
    options:  {
      usemin: 'dist/vendors.js' // <~~ This came from the <!-- build:js --> block
    }
  }
}

Note: Earlier versions of grunt-usemin (correctly, in my opinion) would have generated a concat['dist/vendors.js'] object for each build section in the HTML. Now, because there's a single concat.generated object with all JS/CSS files within it, I'm back-tracking the proper concat target for you.

Examples

Register HTML Templates in app Module

ngtemplates:  {
  app:        {
    src:      '**.html',
    dest:     'templates.js'
  }
}

Register Relative Template URLs

Normally, your app, templates, & server are in separate folders, which means that the template URL is different from the file path.

ngtemplates:  {
  app:        {
    cwd:      'src/app',
    src:      'templates/**.html',
    dest:     'build/app.templates.js'
  }
}

This will store the template URL as templates/home.html instead of src/app/templates/home.html, which would cause a 404.

Minify Template HTML

Simply pass the same options as the htmlmin task:

ngtemplates:    {
  app:          {
    src:        '**.html',
    dest:       'templates.js',
    options:    {
      htmlmin:  { collapseWhitespace: true, collapseBooleanAttributes: true }
    }
  }
}

Or, if you already have an existing htmlmin task, you can reference it:

ngtemplates:    {
  app:          {
    src:        '**.html',
    dest:       'templates.js',
    options:    {
      htmlmin:  '<%= htmlmin.app %>'
    }
  }
}

Customize Template URL

Suppose you only use ngtemplates when on production, but locally you serve templates via Node, sans the .html extension.

You can specify a url callback to further customize the registered URL:

ngtemplates:  {
  app:        {
    src:      '**.html',
    dest:     'templates.js',
    options:  {
      url:    function(url) { return url.replace('.html', ''); }
    }
  }
}

Customize Output

Some people like AMD & RequireJS and would like wrap the output in AMD or something else (don't ask me why!):

ngtemplates:      {
  app:            {
    src:          '**.html',
    dest:         'templates.js',
    options:      {
      bootstrap:  function(module, script) {
        return 'define(' + module + ', [], function() { return { init: ' + script + ' }; });';
      }
    }
  }
}

You will be able to custom everything surrounding $templateCache.put(...).

Changelog

  • v1.1.0 - Added the merge option to allow templates to maintain directory structure if set to false (#114)
  • v1.0.4 - Updated html-minifier to 2.1.2 (#162)
  • v1.0.3 - Fixes issue with using usemin without uglify (#153)
  • v1.0.2 - Fixes issue with escaping carriage returns (#147), Fixes issue with escaping backslashes (#146)
  • v1.0.1 - Log error instead of warning when minify fails (#139)
  • v1.0.0 - Updated unit tests for performance and bumps dependency versions (#143)
  • v0.6.0 - Adds quotes options to allow wrapping in single instead of double quotes (#142)
  • v0.5.9 - Fixes over-matching on cwd when expand:true
  • v0.5.8 - Fixes cwd being part of the $templateCache string when expand:true (#134), Added verbose logging for minify (#136)
  • v0.5.7 – Improve error messages (#100)
  • v0.5.6 – Updated html-minifier to correct whitespace issues. (96)
  • v0.5.5 – Add append option to concat, not overwrite the dest. (#89)
  • v0.5.4 – Specifying an invalid usemin option still creates file (#84)
  • v0.5.3 – Fix bug with Underscore templates (#79)
  • v0.5.2 – Fix usemin matching issue on Windows (#80)
  • v0.5.1 – Add usemin option form v0.4.10
  • v0.5.0 – Works with grunt-usemin (#44)
  • v0.4.10 – Add usemin option
  • v0.4.9 – Improve prefix and support for URLs (#57)
  • v0.4.8 – Compiled assets are JSHint-able (#58)
  • v0.4.7 – Fix bug for when htmlmin is not an Object (#56)
  • v0.4.6 – Add prefix option for easier URL prefixes (#53)
  • v0.4.5 – Attempt to better normalize templates based on current OS (#52)
  • v0.4.4 – Fixed regression caused by htmlmin (#54)
  • v0.4.3 - options.concat targets on Windows convert / to \\. #48
  • v0.4.2 - Fix for using grunt-env to change environments. Thanks to @FredrikAppelros (#20)
  • v0.4.1 – Fix bug with empty files.
  • v0.4.0 – Complete rewrite.
  • v0.3.12 – Whoops, forgot to make htmlmin a regular dependency. Thanks @rubenv (#37)
  • v0.3.11 – Add htmlmin option that supports both an { ... } and <%= htmlmin.options %> for existing tasks.
  • v0.3.10 – Fix unknown concat target bug on windows, thanks to @trask (#31)
  • v0.3.9 – Allow the creation of a new module via module.define, thanks to @sidwood (#28)
  • v0.3.8 – Fix error that occurs when adding 0-length files, thanks to @robertklep (#27)
  • v0.3.7 – Add noConflict option to work with angular.noConflict, thanks to @mbrevoort (#26)
  • v0.3.6 – Fix issue with dading to concat task when it's an array, thanks to @codefather (#23)
  • v0.3.5 – Preserver line endings in templates, thanks to @groner (#21)
  • v0.3.4 – Attempt to fix a bug with Path, thanks to @cgross (#19)
  • v0.3.3 – Add concat option for automatically adding compiled template file to existing concat (or usemin-created) task, thanks to @cgross (#17)
  • v0.3.2 – Add module option for setting which module the templates will be added to, thanks to @sidwood (#20)
  • v0.3.1 – Add prepend option for modifying final $templateCache IDs, thanks to @mbarchein. (#16)
  • v0.3.0 – BC break - Templates are added to an existing module (e.g. myapp) rather than being their own myapp.templates module to be manually included, thanks to @geddesign. (#10)
  • v0.2.2 – Escape backslashes, thanks to @dallonf. (#9)
  • v0.2.1 – Remove ./bin/grunt-angular-templates. No need for it!
  • v0.2.0 – Update to Grunt 0.4, thanks to @jgrund. (#5)
  • v0.1.3 – Convert \\ to / in template IDs (for on win32 systems) (#3)
  • v0.1.2 – Added NPM keywords
  • v0.1.1 – Fails to combine multiple templates. Added directions to README on how to integrate with AngularJS app. Integrated with TravisCI
  • v0.1.0 – Released to NPM

License

Copyright (c) 2013 Eric Clemmons Licensed under the MIT license.

Bitdeli Badge

More Repositories

1

click-to-component

Option+Click React components in your browser to instantly open the source in VS Code
TypeScript
1,710
star
2

react-resolver

Async rendering & data-fetching for universal React applications.
JavaScript
1,646
star
3

webpack-hot-server-example

Webpack Hot Module Replacement (HMR) Example with Express
JavaScript
367
star
4

polydev

Faster, route-centric development for Node.js apps with built-in Hot Module Replacement.
JavaScript
284
star
5

unique-selector

Given a DOM node, return a unique CSS selector matching only that element
JavaScript
260
star
6

grunt-express-server

Grunt task for running an Express Server that works great with LiveReload + Watch/Regarde
JavaScript
248
star
7

terse-webpack

Simplified, fluent Webpack API with presets. Describe *what* your app needs, not *how*.
JavaScript
215
star
8

start-server-webpack-plugin

Automatically start your server once Webpack's build completes.
JavaScript
158
star
9

grunt-react

[DEPRECATED] Grunt task for compiling Facebook React's .jsx templates into .js
JavaScript
151
star
10

per-env

Clean up your package.json with per-NODE_ENV npm scripts
JavaScript
133
star
11

genesis-skeleton

Modern, opinionated, full-stack starter kit for rapid, streamlined application development.
CoffeeScript
106
star
12

if-env

Simplify npm scripts with `if-env ... && npm run this || npm run that`
JavaScript
97
star
13

node-recorder

Simple recording & replaying of HTTP requests for predictable development & testing.
TypeScript
97
star
14

jinja.js

Twig/Jinja/JinJS Client-Side Templating + jQuery Plugin
JavaScript
76
star
15

mdx-site

Static site generator powered by TypeScript, MDX, & React.
TypeScript
51
star
16

next.js-conf-2020

From Front-end to Full Stack with Amplify Framework
JavaScript
38
star
17

reload-server-webpack-plugin

Webpack plugin that automatically (re)starts your server between builds.
JavaScript
38
star
18

bookshelf-manager

Easily wire up models to APIs with supported for complex, nested saving.
JavaScript
34
star
19

universal-webpack

Library to greatly simplify running, watching, testing, & deploying universal apps with webpack.
JavaScript
34
star
20

medium-to-markdown

Convert Medium URLs to Markdown
JavaScript
30
star
21

mvc-to-react

Eric Clemmons' Space City JS 2015 talk – "Moving from MVC to React"
JavaScript
30
star
22

angular-github-demo

AngularJS demo application for HoustonJS
CSS
26
star
23

tech-2016-lightning-talks

10-minute lightning talks about Storybook, Styled Components, MobX, & Jest.
JavaScript
26
star
24

react.lazy-example

Testing out React.lazy (https://github.com/facebook/react/pull/13398)
JavaScript
24
star
25

grunt-angular-modularize

Write AngularJS Modules Once. Build with RequireJS (AMD), Browserify (CommonJS), or simply concat.
JavaScript
23
star
26

hot-module

Reload changed files in Node *without* restarting the server (like nodemon/piping/etc.)
JavaScript
23
star
27

github-semantic-version

Automated semantic version releases powered by Github Issues.
JavaScript
21
star
28

ECVagrantBundle

Symfony2 bundle to generate a working PHP5.3+ Vagrant environment
PHP
21
star
29

codelift

Visual Development for React, Next.js, & Tailwind CSS
17
star
30

de-pagify

Bookmarklet to enable "endless scroll" on popular sites
PHP
16
star
31

performant-3rd-party-widgets

Houston.js demo + slides for babel/webpack/react bundling & optimizations.
JavaScript
15
star
32

diez

Incredibly simple, Dependency Injection for isomorphic Javascript applications.
JavaScript
14
star
33

jest-storybook

Generate Jest snapshots from existing storybooks (similar to storyshots).
JavaScript
13
star
34

react-tooling-challenge

Competition to improve the tooling landscape for all React users.
13
star
35

eslint-config-future

ESLint Shareable Config for ES5/ES6/ES7 Javascript
JavaScript
12
star
36

wordpress-skeleton

[DEPRECATED] Dude, use https://github.com/genesis/wordpress/ !!
PHP
12
star
37

react-router-server-location

A React Router Location for universal apps.
JavaScript
12
star
38

start-cluster

Start a Node cluster by simply running `start-cluster`
JavaScript
11
star
39

graphql-context-services

GraphQL example using Context & Services
JavaScript
11
star
40

graphql-demo

Introduction to GraphQL (http://www.meetup.com/NodejsHouston/events/229815892/)
JavaScript
11
star
41

create-preact-pwa

"yarn create"-friendly version of preact-cli
JavaScript
9
star
42

grunt-verbosity

Adjust verbosity for individual grunt tasks
JavaScript
8
star
43

mootools-namespace

Allows Classes to specify namespace ("My.Fx.Accordion") and extend "Moo.Fx.Accordion", similar to Dojo.provide/require
JavaScript
6
star
44

tech-screen-template

Opinionated (but customizable!) template for tech screen interviews
TypeScript
6
star
45

express-hot-middleware

Simple way to enable hot-reloading in your Express applications.
JavaScript
6
star
46

babel-plugin-react-pure-to-class

https://github.com/gaearon/babel-plugin-react-transform/issues/57#issuecomment-167652102
JavaScript
5
star
47

graphql-mock-object

Easily prototype UIs with dynamic GraphQL objects
JavaScript
5
star
48

amplify-visor

Amplify Visor is a prototype GUI that customers run locally that streamlines the Amplify app-building experience – configuring & deploying Amplify visually, automatically installing dependencies, and reducing developer friction.
JavaScript
5
star
49

sync-github-pivotal

Bi-directional syncing between Pivotal Tracker & GitHub Issues
TypeScript
4
star
50

generator-genesis

Yeoman Generator for Genesis Skeleton
JavaScript
4
star
51

redux-reconnect

Redux hooks for React Resolver & Server-Side Rendering via "reconnect(...)"
3
star
52

create-graphql-app

Add an Amplify Function to `create-react-app`.
JavaScript
3
star
53

begin-react-app

Begin app
JavaScript
2
star
54

react-zones-example

Can Zones magically simplify SSR? http://blog.kwintenp.com/how-the-hell-do-zones-really-work/
2
star
55

vagrant-php

Instantly setup & launch PHP5.x Vagrant machine
PHP
2
star
56

vagrant-shim

Transparently run commands through your Vagrant VM.
PHP
2
star
57

sublime-typescript

DEPRECATED - Use Microsoft's official plugin instead!
2
star
58

ericclemmons

MDX
2
star
59

ericclemmons.com

TypeScript
2
star
60

amplify-flow

Native app for GitHub issue management for Amplify JS
TypeScript
2
star
61

ericclemmons.github.io

Official (Dynamic) Website of Eric Clemmons
CSS
2
star
62

zend-memcached-namespaced

Zend_Cache_Backend_Memcached with Namspace support for clearing specific keys
1
star
63

react-shared-state

Simple <SharedState> component to wrap your app & auto-inject Redux & MobX stores.
1
star
64

angular-lazy-load

Experimenting with lazy-loading angular.modules
JavaScript
1
star
65

microbundle-monorepo

Having an issue with CSS not being extracted when part of a monorepo
CSS
1
star
66

flummox-parent-promise

JavaScript
1
star
67

css-system

JavaScript
1
star
68

genesis-skeleton.com

Official website of Genesis Skeleton
CoffeeScript
1
star
69

mobx-resolver

Prefect MobX data dependencies before rendering React components.
1
star
70

redux-devtools-visual

Easily scrub through a visual history of your application, branch, and repeat!
1
star
71

amplify-js-canary

Canary releases for https://github.com/aws-amplify/amplify-js
1
star
72

amplify-js-issues-4506

https://github.com/aws-amplify/amplify-js/issues/4506
HTML
1
star
73

capistrano-svn-deployment

Example deployment script using Python & Capistrano for PHP apps in SVN
Python
1
star
74

ng-gitboard

Github dashboard powered by Angular.
JavaScript
1
star
75

redux-server

Redux-powered server & middleware for predictably handling requests with immutable responses.
1
star
76

ericclemmons.github.com

Office (Static) Website of Eric Clemmons
1
star