first commit

This commit is contained in:
Tyler Kellen 2012-09-10 13:12:23 -05:00
commit f0d8e0fcdf
9 changed files with 276 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
node_modules
npm-debug.log
tmp

3
AUTHORS Normal file
View File

@ -0,0 +1,3 @@
Tim Branyen (http://tbranyen.com)
Tyler Kellen (http://goingslowly.com/)
Chris Talkington (http://christalkington.com/)

22
LICENSE-MIT Normal file
View File

@ -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.

61
README.md Normal file
View File

@ -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"]
}
}
}
```

69
grunt.js Normal file
View File

@ -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', '<config:nodeunit.tasks>']
},
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');
};

41
package.json Normal file
View File

@ -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"
]
}

61
tasks/jst.js Normal file
View File

@ -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.");
}
});
});
};

1
test/fixtures/template.html vendored Normal file
View File

@ -0,0 +1 @@
<head><title><%= title %></title></head>

15
test/jst_test.js Normal file
View File

@ -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+='<head><title>'+\n( title )+\n'</title></head>';\n}\nreturn __p;\n};";
var result = grunt.file.read("tmp/jst.js");
test.equal(expect, result, "should compile underscore templates into JST");
test.done();
}
};