Created
June 19, 2014 04:09
-
-
Save Elbone/916df43609d75efae21e to your computer and use it in GitHub Desktop.
Grunt task to write some .html files as templates into a js object
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) { | |
// Create the template files | |
grunt.registerMultiTask("templates", "Build a js file from html templates", function() { | |
var paths = grunt.file.expand( this.data.paths ); | |
var out = this.data.output; | |
var contents = "var Templates = {"; | |
var count = 0; | |
// count files | |
paths.forEach(function(path) { | |
count++; | |
}); | |
var i = 0; | |
paths.forEach(function(path) { | |
var html = grunt.file.read(path).replace(/(\r\n|\n|\r|\t)/gm,""); | |
var file = path.replace(/^.*[\\\/]/, '').replace('.html', ''); | |
var json = JSON.stringify(html, null, 2); | |
var comma = (i == count-1) ? '' : ','; | |
contents += '"' + file + '": ' + json + comma; | |
i++; | |
}); | |
contents += '}'; | |
grunt.file.write( out, contents ); | |
grunt.log.writeln('File ' + out + ' sucessfully written from ' + count + ' source files'); | |
}); | |
// Project configuration. | |
grunt.initConfig({ | |
// Package file | |
pkg: grunt.file.readJSON('package.json'), | |
// build templates as js | |
templates: { | |
html: { | |
paths: ["src/templates/*.html"], | |
output: "assets/templates.js" | |
} | |
} | |
}); | |
// Tasks | |
grunt.registerTask('default', ['templates']); // Default tasks | |
grunt.registerTask('build', ['templates']); // Production tasks | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment