UNPKG

2.36 kBJavaScriptView Raw
1/*
2 * grunt-angular-templates
3 * https://github.com/ericclemmons/grunt-angular-templates
4 *
5 * Copyright (c) 2013 Eric Clemmons
6 * Licensed under the MIT license.
7 */
8
9'use strict';
10
11var Compiler = require('./lib/compiler');
12var Appender = require('./lib/appender');
13
14module.exports = function(grunt) {
15
16 var bootstrapper = function(module, script, options) {
17 return grunt.template.process(
18 "<%= angular %>.module('<%= module %>'<%= standalone %>).run(['$templateCache', function($templateCache) {\n<%= script %>\n}]);\n",
19 {
20 data: {
21 'angular': options.angular,
22 'module': module,
23 'standalone': options.standalone ? ', []' : '',
24 'script': script
25 }
26 }
27 );
28 };
29
30 var ngtemplatesTask = function() {
31 var options = this.options({
32 angular: 'angular',
33 bootstrap: bootstrapper,
34 concat: null,
35 htmlmin: {},
36 module: this.target,
37 prefix: '',
38 source: function(source) { return source; },
39 standalone: false,
40 url: function(path) { return path; },
41 usemin: null,
42 });
43
44 grunt.verbose.writeflags(options, 'Options');
45
46 this.files.forEach(function(file) {
47 if (!file.src.length) {
48 grunt.log.warn('No templates found');
49 }
50
51 var compiler = new Compiler(grunt, options, file.cwd);
52 var appender = new Appender(grunt);
53 var modules = compiler.modules(file.src);
54 var compiled = [];
55
56 for (var module in modules) {
57 compiled.push(compiler.compile(module, modules[module]));
58 }
59
60 grunt.file.write(file.dest, compiled.join('\n'));
61 grunt.log.writeln('File ' + file.dest.cyan + ' created.');
62
63 if (options.usemin) {
64 if (appender.save('generated', appender.concatUseminFiles(options.usemin, file))) {
65 grunt.log.writeln('Added ' + file.dest.cyan + ' to ' + ('<!-- build:js ' + options.usemin + ' -->').yellow);
66 }
67 }
68
69 if (options.concat) {
70 if (appender.save(options.concat, appender.concatFiles(options.concat, file))) {
71 grunt.log.writeln('Added ' + file.dest.cyan + ' to ' + ('concat:' + options.concat).yellow);
72 }
73 }
74 });
75 };
76
77 grunt.registerMultiTask('ngtemplates', 'Compile AngularJS templates for $templateCache', ngtemplatesTask);
78
79};