1 | # grunt-angular-templates [![Build Status](https://travis-ci.org/ericclemmons/grunt-angular-templates.png?branch=master)](https://travis-ci.org/ericclemmons/grunt-angular-templates)
|
2 |
|
3 | > Speed up your AngularJS app by automatically minifying, combining,
|
4 | > and automatically caching your HTML templates with `$templateCache`.
|
5 |
|
6 | Here's an example of the output created by this task from multiple `.html` files:
|
7 |
|
8 | ```js
|
9 | angular.module('app').run(["$templateCache", function($templateCache) {
|
10 | $templateCache.put("home.html",
|
11 | // contents for home.html ...
|
12 | );
|
13 | ...
|
14 | $templateCache.put("src/app/templates/button.html",
|
15 | // contents for button.html
|
16 | );
|
17 | }]);
|
18 | ```
|
19 |
|
20 | Then, when you use `ng-include` or `templateUrl` with `$routeProvider`,
|
21 | the template is already loaded without an extra AJAX request!
|
22 |
|
23 |
|
24 | ## Table of Contents
|
25 |
|
26 | - [Installation](#installation)
|
27 | - [Options](#options)
|
28 | - [Usage](#usage)
|
29 | - [Examples](#examples)
|
30 | - [Changelog](#changelog)
|
31 | - [License](#license)
|
32 |
|
33 |
|
34 | ## Installation
|
35 |
|
36 | *This plugin requires [Grunt][1] `~0.4.0`*
|
37 |
|
38 | Install the plugin:
|
39 |
|
40 | $ npm install grunt-angular-templates --save-dev
|
41 |
|
42 | Enable the plugin within your `Gruntfile`:
|
43 |
|
44 | ```js
|
45 | grunt.loadTasks('grunt-angular-templates');
|
46 | ```
|
47 |
|
48 |
|
49 | ## Options
|
50 |
|
51 | ### angular
|
52 |
|
53 | > Global namespace for Angular.
|
54 |
|
55 | If you use `angular.noConflict()`, then set this value to whatever you
|
56 | re-assign angular to. Otherwise, it defaults to `angular`.
|
57 |
|
58 | ### bootstrap
|
59 |
|
60 | > Callback to modify the bootstraper that registers the templates with `$templateCache`.
|
61 |
|
62 | By default, the bootstrap script wraps `function($templateCache) { ... }` with:
|
63 |
|
64 | ```js
|
65 | angular.module('app').run(['$templateCache', ... ]);
|
66 | ```
|
67 |
|
68 | If you want to create your own wrapper so you register the templates as an
|
69 | AMD or CommonJS module, set the `bootstrap` option to something like:
|
70 |
|
71 | ```js
|
72 | bootstrap: function(module, script) {
|
73 | return 'module.exports[module] = ' + script + ';';
|
74 | }
|
75 | ```
|
76 |
|
77 | ### concat
|
78 |
|
79 | > Name of `concat` target to append the compiled template path to.
|
80 |
|
81 | This is especially handy if you combine your scripts using
|
82 | [grunt-contrib-concat][4] or [grunt-usemin][5].
|
83 |
|
84 | ### htmlmin
|
85 |
|
86 | > Object containing [htmlmin options][2] that will *significantly* reduce
|
87 | the filesize of the compiled templates.
|
88 |
|
89 | Without this, the HTML (whitespace and all) will be faithfully compiled
|
90 | down into the final `.js` file. Minifying that file will only cut down
|
91 | on the *Javascript* code, not the *HTML* within the strings.
|
92 |
|
93 | I recommend using the following settings for production:
|
94 |
|
95 | ```js
|
96 | htmlmin: {
|
97 | collapseBooleanAttributes: true,
|
98 | collapseWhitespace: true,
|
99 | removeAttributeQuotes: true,
|
100 | removeComments: true, // Only if you don't use comment directives!
|
101 | removeEmptyAttributes: true,
|
102 | removeRedundantAttributes: true
|
103 | removeScriptTypeAttributes: true,
|
104 | removeStyleLinkTypeAttributes: true
|
105 | }
|
106 | ```
|
107 |
|
108 | ### module
|
109 |
|
110 | > `String` of the `angular.module` to register templates with.
|
111 |
|
112 | If not specified, it will automatically be the name of the `ngtemplates`
|
113 | subtask (e.g. `app`, based on the examples below).
|
114 |
|
115 | ### source
|
116 |
|
117 | > Callback to modify the template's source code.
|
118 |
|
119 | If you would like to prepend a comment, strip whitespace, or do
|
120 | post-processing on the HTML that `ngtemplates` doesn't otherwise do,
|
121 | use this function.
|
122 |
|
123 | ### standalone
|
124 |
|
125 | > Boolean indicated if the templates are part of an existing module or a standalone.
|
126 | Defaults to `false`.
|
127 |
|
128 | ### url
|
129 |
|
130 | > Callback to modify the template's `$templateCache` URL.
|
131 |
|
132 | Normally, this isn't needed as specifying your files with `cwd`
|
133 | ensures that URLs load via both AJAX and `$templateCache`.
|
134 |
|
135 |
|
136 | ## Usage
|
137 |
|
138 |
|
139 | ### Compiling HTML Templates
|
140 |
|
141 | After configuring your `ngtemplates` task, you can either run the
|
142 | task directly:
|
143 |
|
144 | $ grunt ngtemplates
|
145 |
|
146 | Or, bake it into an existing task:
|
147 |
|
148 | ```js
|
149 | grunt.registerTask('default', [ 'jshint', 'ngtemplates', 'concat' ]);
|
150 | ```
|
151 |
|
152 | ### Including Compiled Templates
|
153 |
|
154 | Finally, you have to load the compiled templates' `.js` file into your
|
155 | application.
|
156 |
|
157 |
|
158 | #### Using HTML
|
159 |
|
160 | ```html
|
161 | <script src="templates.js"></script>
|
162 | ```
|
163 |
|
164 |
|
165 | #### Using Grunt's `concat` task:
|
166 |
|
167 | This is my personal preference, since you don't have to worry about
|
168 | what the destination file is actually called.
|
169 |
|
170 | ```js
|
171 | concat: {
|
172 | app: {
|
173 | src: [ '**.js', '<%= ngtemplates.app.dest %>' ]
|
174 | dest: [ 'app.js' ]
|
175 | }
|
176 | }
|
177 | ```
|
178 |
|
179 | #### Using [grunt-usemin][5]
|
180 |
|
181 | Using the following HTML as an example:
|
182 |
|
183 | ```html
|
184 | <!-- build:js combined.js -->
|
185 | <script src="bower_components/angular/angular.js"></script>
|
186 | <script src="bower_components/angular-resource/angular-resource.js"></script>
|
187 | ```
|
188 |
|
189 | The name of your `build:js` file automatically becomes the `concat` target
|
190 | name. So, simply just copy/paste that name into the `concat` option:
|
191 |
|
192 | ```js
|
193 | ngtemplates: {
|
194 | app: {
|
195 | src: '**.html',
|
196 | dest: 'template.js',
|
197 | options: {
|
198 | concat: 'combined.js'
|
199 | }
|
200 | }
|
201 | }
|
202 | ```
|
203 |
|
204 | ## Examples
|
205 |
|
206 |
|
207 | ### Register HTML Templates in `app` Module
|
208 |
|
209 | ```js
|
210 | ngtemplates: {
|
211 | app: {
|
212 | src: '**.html',
|
213 | dest: 'templates.js'
|
214 | }
|
215 | }
|
216 | ```
|
217 |
|
218 |
|
219 | ### Register Relative Template URLs
|
220 |
|
221 | Normally, your app, templates, & server are in separate folders, which means
|
222 | that the template URL is **different** from the file path.
|
223 |
|
224 | ```js
|
225 | ngtemplates: {
|
226 | app: {
|
227 | cwd: 'src/app',
|
228 | src: 'templates/**.html',
|
229 | dest: 'build/app.templates.js'
|
230 | }
|
231 | }
|
232 | ```
|
233 |
|
234 | This will store the template URL as `templates/home.html` instead of
|
235 | `src/app/templates/home.html`, which would cause a 404.
|
236 |
|
237 |
|
238 | ### Minify Template HTML
|
239 |
|
240 | Simply pass the [same options][2] as the `htmlmin` task:
|
241 |
|
242 | ```js
|
243 | ngtemplates: {
|
244 | app: {
|
245 | src: '**.html',
|
246 | dest: 'templates.js',
|
247 | options: {
|
248 | htmlmin: { collapseWhitespace: true, collapseBooleanAttributes: true }
|
249 | }
|
250 | }
|
251 | }
|
252 | ```
|
253 |
|
254 | Or, if you already have an existing `htmlmin` task, you can reference it:
|
255 |
|
256 | ```js
|
257 | ngtemplates: {
|
258 | app: {
|
259 | src: '**.html',
|
260 | dest: 'templates.js',
|
261 | options: {
|
262 | htmlmin: '<%= htmlmin.app %>'
|
263 | }
|
264 | }
|
265 | }
|
266 | ```
|
267 |
|
268 |
|
269 | ### Customize Template URL
|
270 |
|
271 | Suppose you only use `ngtemplates` when on production, but locally you serve
|
272 | templates via Node, sans the `.html` extension.
|
273 |
|
274 | You can specify a `url` callback to further customize the registered URL:
|
275 |
|
276 | ```js
|
277 | ngtemplates: {
|
278 | app: {
|
279 | src: '**.html',
|
280 | dest: 'templates.js',
|
281 | options: {
|
282 | url: function(url) { return url.replace('.html', ''); }
|
283 | }
|
284 | }
|
285 | }
|
286 | ```
|
287 |
|
288 |
|
289 | ### Customize Output
|
290 |
|
291 | Some people like [AMD & RequireJS][3] and would like wrap the output
|
292 | in AMD or something else (don't ask me why!):
|
293 |
|
294 | ```js
|
295 | ngtemplates: {
|
296 | app: {
|
297 | src: '**.html',
|
298 | dest: 'templates.js',
|
299 | options: {
|
300 | bootstrap: function(module, script) {
|
301 | return 'define(module, [], function() { return { init: ' + script + ' }; });';
|
302 | }
|
303 | }
|
304 | }
|
305 | }
|
306 | ```
|
307 |
|
308 | You will be able to custom everything surrounding `$templateCache.put(...)`.
|
309 |
|
310 |
|
311 | ## Changelog
|
312 |
|
313 | - v0.4.3 - `options.concat` targets on Windows convert `/` to `\\`. [#48](https://github.com/ericclemmons/grunt-angular-templates/issues/48)
|
314 | - v0.4.2 - Fix for using `grunt-env` to change environments. Thanks to @FredrikAppelros ([#20](https://github.com/ericclemmons/grunt-express-server/pull/20))
|
315 | - v0.4.1 – Fix bug with empty files.
|
316 | - v0.4.0 – Complete rewrite.
|
317 | - v0.3.12 – Whoops, forgot to make `htmlmin` a regular dependency. Thanks @rubenv ([#37](https://github.com/ericclemmons/grunt-angular-templates/pull/37))
|
318 | - v0.3.11 – Add `htmlmin` option that supports both an `{ ... }` and `<%= htmlmin.options %>` for existing tasks.
|
319 | - v0.3.10 – Fix *unknown concat target* bug on windows, thanks to @trask ([#31](https://github.com/ericclemmons/grunt-angular-templates/pull/31))
|
320 | - v0.3.9 – Allow the creation of a new module via `module.define`, thanks to @sidwood ([#28](https://github.com/ericclemmons/grunt-angular-templates/pull/28))
|
321 | - v0.3.8 – Fix error that occurs when adding 0-length files, thanks to @robertklep ([#27](https://github.com/ericclemmons/grunt-angular-templates/pull/27))
|
322 | - v0.3.7 – Add `noConflict` option to work with [angular.noConflict](https://github.com/angular/angular.js/pull/1535), thanks to @mbrevoort ([#26](https://github.com/ericclemmons/grunt-angular-templates/pull/26))
|
323 | - v0.3.6 – Fix issue with dading to `concat` task when it's an array, thanks to @codefather ([#23](https://github.com/ericclemmons/grunt-angular-templates/pull/23))
|
324 | - v0.3.5 – Preserver line endings in templates, thanks to @groner ([#21](https://github.com/ericclemmons/grunt-angular-templates/pull/21))
|
325 | - v0.3.4 – Attempt to fix a bug with `Path`, thanks to @cgross ([#19](https://github.com/ericclemmons/grunt-angular-templates/issues/19))
|
326 | - v0.3.3 – Add `concat` option for automatically adding compiled template file to existing `concat` (or `usemin`-created) task, thanks to @cgross ([#17](https://github.com/ericclemmons/grunt-angular-templates/pull/17))
|
327 | - v0.3.2 – Add `module` option for setting which module the templates will be added to, thanks to @sidwood ([#20](https://github.com/ericclemmons/grunt-angular-templates/pull/20))
|
328 | - v0.3.1 – Add `prepend` option for modifying final `$templateCache` IDs, thanks to @mbarchein. ([#16](https://github.com/ericclemmons/grunt-angular-templates/pull/16))
|
329 | - 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](https://github.com/ericclemmons/grunt-angular-templates/issues/10))
|
330 | - v0.2.2 – Escape backslashes, thanks to @dallonf. ([#9](https://github.com/ericclemmons/grunt-angular-templates/pull/9))
|
331 | - v0.2.1 – Remove `./bin/grunt-angular-templates`. No need for it!
|
332 | - v0.2.0 – Update to Grunt 0.4, thanks to @jgrund. ([#5](https://github.com/ericclemmons/grunt-angular-templates/issues/5))
|
333 | - v0.1.3 – Convert `\\` to `/` in template IDs (for on win32 systems) ([#3](https://github.com/ericclemmons/grunt-angular-templates/issues/3))
|
334 | - v0.1.2 – Added NPM keywords
|
335 | - v0.1.1 – [Fails to combine multiple templates](https://github.com/ericclemmons/grunt-angular-templates/issues/1). Added directions to README on how to integrate with AngularJS app. Integrated with TravisCI
|
336 | - v0.1.0 – Released to [NPM](https://npmjs.org/package/grunt-angular-templates)
|
337 |
|
338 |
|
339 | ## License
|
340 |
|
341 | Copyright (c) 2013 Eric Clemmons
|
342 | Licensed under the MIT license.
|
343 |
|
344 |
|
345 | [1]: http://gruntjs.com/
|
346 | [2]: https://github.com/gruntjs/grunt-contrib-htmlmin
|
347 | [3]: http://requirejs.org/docs/whyamd.html
|
348 | [4]: https://github.com/gruntjs/grunt-contrib-concat
|
349 | [5]: https://github.com/yeoman/grunt-usemin
|