Last active
December 12, 2015 01:49
-
-
Save joshdmiller/4693995 to your computer and use it in GitHub Desktop.
This is a Grunt 0.4 task for AngularJS projects. It facilitates the automatic compiling of HTML templates to pure JavaScript through AngularJS's `$templateCache`, so these files can be downloaded as part of an initial app payload. This is based on the Grunt 0.3.x version from angular-ui/bootstrap.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Based on html2js by the AngularUI/bootstrap team: https://angular-ui.github.com/bootstrap | |
* Updated to Grunt ~0.4.0 by Josh David Miller | |
* | |
* Licensed MIT | |
*/ | |
module.exports = function (grunt) { | |
// HTML-2-JS Templates | |
var path = require('path'), | |
TPL = 'angular.module("<%= id %>", []).run(["$templateCache", function($templateCache) {\n $templateCache.put("<%= id %>",\n "<%= content %>");\n}]);\n', | |
templateModule = "angular.module('templates', [<%= templates %>]);\n", | |
escapeContent = function(content) { | |
return content.replace(/"/g, '\\"').replace(/\r?\n/g, '" +\n "'); | |
}, | |
normalizePath = function(p) { | |
if ( path.sep !== '/' ) { | |
p = p.replace(/\\/g, '/'); | |
} | |
return p; | |
}; | |
grunt.registerTask('html2js', 'Generate js version of html template.', function() { | |
this.requiresConfig('html2js.src'); | |
var files = grunt.file.expand( grunt.config('html2js.src') ), | |
base = grunt.config('html2js.base') || '.', | |
dest = grunt.config('html2js.dest') || '.', | |
templates = []; | |
files.forEach(function(file) { | |
var id = normalizePath( path.relative(base, file) ); | |
templates.push("'" + id + "'"); | |
grunt.file.write( path.resolve( dest, id + '.js' ), grunt.template.process( TPL, { | |
data: { | |
id: id, | |
content: escapeContent( grunt.file.read( file ) ) | |
} | |
})); | |
}); | |
grunt.file.write( path.resolve( dest,'templates.js' ), grunt.template.process( templateModule, { | |
data: { | |
templates: templates.join(', ') | |
} | |
})); | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function ( grunt ) { | |
// Load required Grunt tasks | |
// ... | |
grunt.loadTasks('build'); // <- place grunt-html2js in this folder | |
// Project Configuration | |
grunt.initConfig({ | |
distdir: 'dist', | |
src: { | |
// ... | |
tpl: [ 'src/app/**/*.tpl.html' ] | |
}, | |
// ... | |
html2js: { | |
src: [ '<%= src.tpl %>' ], | |
base: 'src/app', | |
dest: 'dist/tmp' | |
} | |
}); | |
// The default task | |
grunt.registerTask( 'default', [ 'build' ] ); | |
grunt.registerTask( 'build', [ '...', 'html2js', '...'] ); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment