angular-templates.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. 'use strict';
  9. var Compiler = require('./lib/compiler');
  10. var Appender = require('./lib/appender');
  11. var fs = require('fs');
  12. module.exports = function(grunt) {
  13. var bootstrapper = function(module, script, options) {
  14. return options.angular+".module('"+module+"'"+(options.standalone ? ', []' : '')+").run(['$templateCache', function($templateCache) {\n"+script+"\n}]);\n";
  15. };
  16. var ngtemplatesTask = function() {
  17. var options = this.options({
  18. angular: 'angular',
  19. bootstrap: bootstrapper,
  20. concat: null,
  21. htmlmin: {},
  22. module: this.target,
  23. prefix: '',
  24. source: function(source) { return source; },
  25. standalone: false,
  26. url: function(path) { return path; },
  27. usemin: null,
  28. append: false
  29. });
  30. grunt.verbose.writeflags(options, 'Options');
  31. this.files.forEach(function(file) {
  32. if (!file.src.length) {
  33. grunt.log.warn('No templates found');
  34. }
  35. var expanded = file.orig.expand;
  36. var cwd = file.orig.expand ? file.orig.cwd : file.cwd;
  37. var compiler = new Compiler(grunt, options, cwd, expanded);
  38. var appender = new Appender(grunt);
  39. var modules = compiler.modules(file.src);
  40. var compiled = [];
  41. for (var module in modules) {
  42. compiled.push(compiler.compile(module, modules[module]));
  43. }
  44. if (options.append){
  45. fs.appendFileSync(file.dest, compiled.join('\n'));
  46. grunt.log.writeln('File ' + file.dest.cyan + ' updated.');
  47. }
  48. else{
  49. grunt.file.write(file.dest, compiled.join('\n'));
  50. grunt.log.writeln('File ' + file.dest.cyan + ' created.');
  51. }
  52. if (options.usemin) {
  53. if (appender.save('generated', appender.concatUseminFiles(options.usemin, file))) {
  54. grunt.log.writeln('Added ' + file.dest.cyan + ' to ' + ('<!-- build:js ' + options.usemin + ' -->').yellow);
  55. }
  56. }
  57. if (options.concat) {
  58. if (appender.save(options.concat, appender.concatFiles(options.concat, file))) {
  59. grunt.log.writeln('Added ' + file.dest.cyan + ' to ' + ('concat:' + options.concat).yellow);
  60. }
  61. }
  62. });
  63. };
  64. grunt.registerMultiTask('ngtemplates', 'Compile AngularJS templates for $templateCache', ngtemplatesTask);
  65. };