commit f0d8e0fcdf67537179c0d1f8ae480651fa6ff724 Author: Tyler Kellen Date: Mon Sep 10 13:12:23 2012 -0500 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5cb6bfd --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules +npm-debug.log +tmp \ No newline at end of file diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 0000000..e4e5d6e --- /dev/null +++ b/AUTHORS @@ -0,0 +1,3 @@ +Tim Branyen (http://tbranyen.com) +Tyler Kellen (http://goingslowly.com/) +Chris Talkington (http://christalkington.com/) \ No newline at end of file diff --git a/LICENSE-MIT b/LICENSE-MIT new file mode 100644 index 0000000..b8a710f --- /dev/null +++ b/LICENSE-MIT @@ -0,0 +1,22 @@ +Copyright (c) 2012 Tim Branyen, contributors. + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..d7f7183 --- /dev/null +++ b/README.md @@ -0,0 +1,61 @@ +# grunt-contrib-jst +> Compile underscore templates to JST file (part of the [grunt-contrib](https://github.com/gruntjs/grunt-contrib) collection). Submitted by [Tim Branyen](/tbranyen). + +## Getting Started +Install this grunt plugin next to your project's [grunt.js gruntfile][getting_started] with: `npm install grunt-contrib-jst` + +Then add this line to your project's `grunt.js` gruntfile: + +```javascript +grunt.loadNpmTasks('grunt-contrib-jst'); +``` + +[grunt]: https://github.com/cowboy/grunt +[getting_started]: https://github.com/cowboy/grunt/blob/master/docs/getting_started.md + +### Overview + +This task compiles Underscore compatible templates into functions that can be concatenated and minified with existing source files. + +Inside your `grunt.js` file, add a section named `jst`. This section specifies the files to compile and the options passed to [underscore.template](http://underscorejs.org/#template). + +#### Parameters + +##### files ```object``` + +This defines what files this task will process and should contain key:value pairs. + +The key (destination) should be an unique filepath (supports [grunt.template](https://github.com/cowboy/grunt/blob/master/docs/api_template.md)) and the value (source) should be a filepath or an array of filepaths (supports [minimatch](https://github.com/isaacs/minimatch)). + +Note: Values are precompiled to the namespaced JST array in the order passed. + +##### options ```object``` + +This controls how this task (and its helpers) operate and should contain key:value pairs, see options below. + +#### Options + +##### namespace ```string``` + +The namespace in which the resulting JST templates are assigned to. + +##### templateSettings ```object``` + +The settings passed to underscore when compiling templates. + +#### Config Examples + +``` javascript +jst: { + compile: { + options: { + templateSettings: { + interpolate : /\{\{(.+?)\}\}/g + } + }, + files: { + "path/to/compiled/templates.js": ["path/to/source/**/*.html"] + } + } +} +``` \ No newline at end of file diff --git a/grunt.js b/grunt.js new file mode 100644 index 0000000..af79abe --- /dev/null +++ b/grunt.js @@ -0,0 +1,69 @@ +/* + * grunt-contrib-jst + * http://gruntjs.com/ + * + * Copyright (c) 2012 Tim Branyen, contributors + * Licensed under the MIT license. + * https://github.com/gruntjs/grunt-contrib-jst/blob/master/LICENSE-MIT + */ + +module.exports = function(grunt) { + 'use strict'; + + // Project configuration. + grunt.initConfig({ + lint: { + all: ['grunt.js', 'tasks/*.js', ''] + }, + + jshint: { + options: { + curly: true, + eqeqeq: true, + immed: true, + latedef: true, + newcap: true, + noarg: true, + sub: true, + undef: true, + boss: true, + eqnull: true, + node: true, + es5: true + } + }, + + // Before generating any new files, remove any previously-created files. + clean: { + test: ['tmp'] + }, + + // Configuration to be run (and then tested). + jst: { + compile: { + files: { + "tmp/jst.js": ["test/fixtures/*.html"] + } + } + }, + + // Unit tests. + nodeunit: { + tasks: ['test/*_test.js'] + } + }); + + // Actually load this plugin's task(s). + grunt.loadTasks('tasks'); + + // The clean plugin helps in testing. + grunt.loadNpmTasks('grunt-contrib-clean'); + + // Whenever the "test" task is run, first clean the "tmp" dir, then run this + // plugin's task(s), then test the result. + grunt.renameTask('test', 'nodeunit'); + grunt.registerTask('test', 'clean jst nodeunit'); + + // By default, lint and run all tests. + grunt.registerTask('default', 'lint test'); +}; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..db8b5fd --- /dev/null +++ b/package.json @@ -0,0 +1,41 @@ +{ + "name": "grunt-contrib-jst", + "description": "Precompile Handlebars templates to JST file.", + "version": "0.2.0", + "homepage": "https://github.com/gruntjs/grunt-contrib-jst", + "author": { + "name": "Tim Branyen", + "url": "http://tbranyen.com/" + }, + "repository": { + "type": "git", + "url": "git://github.com/gruntjs/grunt-contrib-jst.git" + }, + "bugs": { + "url": "https://github.com/gruntjs/grunt-contrib-jst/issues" + }, + "licenses": [ + { + "type": "MIT", + "url": "https://github.com/gruntjs/grunt-contrib-jst/blob/master/LICENSE-MIT" + } + ], + "main": "grunt.js", + "engines": { + "node": "*" + }, + "scripts": { + "test": "grunt test" + }, + "dependencies": { + "underscore": "~1.3.3", + "grunt-contrib-lib": "~0.2.0" + }, + "devDependencies": { + "grunt": "~0.3.15", + "grunt-contrib-clean": "~0.2.0" + }, + "keywords": [ + "gruntplugin" + ] +} \ No newline at end of file diff --git a/tasks/jst.js b/tasks/jst.js new file mode 100644 index 0000000..93a24a6 --- /dev/null +++ b/tasks/jst.js @@ -0,0 +1,61 @@ +/* + * grunt-contrib-jst + * http://gruntjs.com/ + * + * Copyright (c) 2012 Tim Branyen, contributors + * Licensed under the MIT license. + * https://github.com/gruntjs/grunt-contrib-jst/blob/master/LICENSE-MIT + */ + +module.exports = function(grunt) { + "use strict"; + + var _ = require("underscore"); + var helpers = require("grunt-contrib-lib").init(grunt); + + var jst = function(source, filepath, namespace, templateSettings) { + try { + return namespace + "['" + filepath + "'] = " + _.template(source, false, templateSettings).source + ";"; + } catch (e) { + grunt.log.error(e); + grunt.fail.warn("JST failed to compile."); + } + }; + + grunt.registerMultiTask("jst", "Compile underscore templates to JST file", function() { + var options = helpers.options(this, {namespace: "JST", templateSettings: {}}); + + grunt.verbose.writeflags(options, "Options"); + + // TODO: ditch this when grunt v0.4 is released + this.files = this.files || helpers.normalizeMultiTaskFiles(this.data, this.target); + + var srcFiles; + var taskOutput; + var sourceCode; + var sourceCompiled; + + var helperNamespace = "this['" + options.namespace + "']"; + + this.files.forEach(function(file) { + srcFiles = grunt.file.expandFiles(file.src); + + taskOutput = []; + taskOutput.push(helperNamespace + " = " + helperNamespace + " || {};"); + + srcFiles.forEach(function(srcFile) { + sourceCode = grunt.file.read(srcFile); + + sourceCompiled = jst(sourceCode, srcFile, helperNamespace, options.templateSettings); + + taskOutput.push(sourceCompiled); + }); + + if (taskOutput.length > 0) { + grunt.file.write(file.dest, taskOutput.join("\n\n")); + grunt.log.writeln("File '" + file.dest + "' created."); + } + }); + }); + +}; diff --git a/test/fixtures/template.html b/test/fixtures/template.html new file mode 100644 index 0000000..d7a184a --- /dev/null +++ b/test/fixtures/template.html @@ -0,0 +1 @@ +<%= title %> \ No newline at end of file diff --git a/test/jst_test.js b/test/jst_test.js new file mode 100644 index 0000000..9fdb404 --- /dev/null +++ b/test/jst_test.js @@ -0,0 +1,15 @@ +var grunt = require('grunt'); + +exports['jst'] = { + main: function(test) { + 'use strict'; + + test.expect(1); + + var expect = "this['JST'] = this['JST'] || {};\n\nthis['JST']['test/fixtures/template.html'] = function(obj){\nvar __p='';var print=function(){__p+=Array.prototype.join.call(arguments, '')};\nwith(obj||{}){\n__p+=''+\n( title )+\n'';\n}\nreturn __p;\n};"; + var result = grunt.file.read("tmp/jst.js"); + test.equal(expect, result, "should compile underscore templates into JST"); + + test.done(); + } +}; \ No newline at end of file