Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4376702
Showing
7 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.DS_Store | ||
node_modules |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
support | ||
test | ||
examples |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
jsttojs | ||
======= | ||
|
||
A node.js module for precompile JavaScript templates (ex. mustache or jQuery.tmpl) to one file. | ||
|
||
|
||
You can to precompile some static mustache, hogan.js, jQuery.tmpl, underscore.js or any other templates and include them in generated javascript function that could be Stitched and compressed in a single file. | ||
|
||
|
||
Installation | ||
----- | ||
|
||
$ npm install jsttojs -g | ||
|
||
|
||
Usage | ||
----- | ||
|
||
jsttojs allows templates to be precompiled and included in javascript code | ||
|
||
``` | ||
Precompile JavaScript templates. | ||
Usage: jsttojs <input> <output> [options] | ||
Options: | ||
-h, --help output usage information | ||
-V, --version output the version number | ||
-e, --ext <n> extension for templates, default "jst" | ||
-n, --name <n> name variable stores temapltes, default "JSTmpl" | ||
Examples: | ||
$ jsttojs templates/ compiled/templates/index.js --ext mustache | ||
``` | ||
|
||
Samples | ||
----- | ||
|
||
Templates: | ||
|
||
```javascript | ||
// tamplates/index.jst | ||
Hello world {{ username }} | ||
second line | ||
``` | ||
|
||
```javascript | ||
// tamplates/video/index.jst | ||
Hello {{ username }} on index video page | ||
``` | ||
|
||
Run precompile templates | ||
|
||
$ jsttojs templates/ compiled/templates/index.js --name MyGlobalVariable | ||
|
||
Output: | ||
|
||
```javascript | ||
// compiled/templates/index.js | ||
window.MyGlobalVariable = { | ||
"index" : "Hello world {{ username }} second line", | ||
"video/index" : "Hello {{ username }} on index video page" | ||
} | ||
``` | ||
|
||
License | ||
----- | ||
|
||
Copyright (c) 2012 Alexey Kupriyanenko a.kupriyanenko@gmail.com | ||
|
||
jsttojs is released under the MIT license. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env node | ||
|
||
module.exports = require('../lib/jsttojs'); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
module.exports = require('./lib/jsttojs'); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/*! | ||
* JavaScript template precompiler | ||
* Copyright(c) 2012 Alexey Kupriyanenko <a.kupriyanenko@gmail.com> | ||
* MIT Licensed | ||
*/ | ||
|
||
var fs = require('fs'); | ||
var walk = require('walk'); | ||
var options = require('commander'); | ||
|
||
// Get options | ||
options | ||
.version('0.0.1') | ||
.usage('<input> <output> [options]') | ||
.option('-e, --ext <n>', 'extension for templates, default "jst"', 'jst') | ||
.option('-n, --name <n>', 'name variable stores temapltes, default "JSTmpl"', 'JSTmpl') | ||
.parse(process.argv); | ||
|
||
options.root = process.argv[2]; | ||
options.output = process.argv[3]; | ||
|
||
// Check options | ||
if (!options.root || !options.output) | ||
options.help(); | ||
|
||
// Get files from root derictory | ||
var files = []; | ||
var walker = walk.walk(options.root, { followLinks: false }); | ||
|
||
walker.on('file', function(root, stat, next) { | ||
if (stat.name.indexOf(options.ext) + options.ext.length === stat.name.length) | ||
files.push(root + '/' + stat.name); | ||
|
||
next(); | ||
}); | ||
|
||
// Generate templates after read templates | ||
walker.on('end', function() { | ||
var templates = readTemplates(files); | ||
|
||
generateTemplates(templates); | ||
}); | ||
|
||
/** | ||
* Read files and get templates | ||
* @param Array files | ||
* @return Object | ||
*/ | ||
var readTemplates = function(files) { | ||
var templates = {} | ||
, template; | ||
|
||
files.forEach(function(path) { | ||
try { | ||
template = fs.readFileSync(path, 'utf8'); | ||
} catch(e) { | ||
console.log(e); | ||
return; | ||
} | ||
|
||
template = template.replace(/(\n(\r)?)/g, ' '); | ||
var index = path.replace(options.root, '').replace('.' + options.ext, '').substring(1); | ||
|
||
templates[index] = template; | ||
}); | ||
|
||
return templates; | ||
} | ||
|
||
/** | ||
* Generate template and write in output file | ||
* @param Object templates | ||
*/ | ||
var generateTemplates = function(templates) { | ||
var data = 'window.' + options.name + ' = ' + JSON.stringify(templates, null, 4); | ||
|
||
fs.writeFile(options.output, data, function(err) { | ||
if (err) | ||
return console.log(err); | ||
|
||
console.log('complete: ' + options.output); | ||
}); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "jsttojs", | ||
"version": "0.0.1", | ||
"description": "A node.js module for precompile JavaScript templates (ex. mustache or jQuery.tmpl) to one file", | ||
"preferGlobal": "true", | ||
"main": "index", | ||
"bin": { | ||
"jsttojs": "bin/jsttojs" | ||
}, | ||
"author": { | ||
"name": "Alexey Kupriyanenko", | ||
"email": "a.kupriyanenko@gmail.com" | ||
}, | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "http://www.opensource.org/licenses/mit-license.php" | ||
} | ||
], | ||
"keywords": ["jst", "template", "precompile", "compile", "mustache"], | ||
"repository" : { | ||
"type": "git", | ||
"url": "https://github.com/upwards/jsttojs.git" | ||
}, | ||
"dependencies": { | ||
"commander": "1.0.x", | ||
"walk": "2.2.x" | ||
}, | ||
"engines": { "node": ">= 0.6.x" } | ||
} |