Skip to content

Commit

Permalink
modifications to the file pre-compilation step
Browse files Browse the repository at this point in the history
  • Loading branch information
olado committed Jan 2, 2013
1 parent 91d649b commit f3c90a8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ Added parameters support in partials:

{{#def.macro:myvariable}}

Compile tool to compile dot templates into js (thanks to @Katahdin https://github.com/Katahdin/dot-packer ):
Compile tool to compile dot templates into js files (thanks to @Katahdin https://github.com/Katahdin/dot-packer ):

./bin/dottojs -s examples/views -d out/views
./bin/dot-packer -s examples/views -d out/views

## Notes:
doU.js is here only so that legacy external tests do not break. Use doT.js.
Expand Down
3 changes: 2 additions & 1 deletion bin/dottojs → bin/dot-packer
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env node
/* Continuation of https://github.com/Katahdin/dot-packer */

var program = require('commander'),
dot = require('../');
Expand All @@ -8,7 +9,7 @@ program
.usage('dottojs')
.option('-s, --source [value]', 'source folder/file path')
.option('-d, --dest [value]', 'destination folder')
.option('-g, --global [value]', 'the global variable to install the templates in',"_render")
.option('-g, --global [value]', 'the global variable to install the templates in',"window.render")
.option('-p, --package [value]', 'if specified, package all templates from destination folder into specified file')
.parse(process.argv);

Expand Down
21 changes: 15 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
* can be accessed as renderer.filename
* Files with .jst extension are compiled into .js files. Produced .js file can be
* loaded as a commonJS, AMD module, or just installed into a global variable
* (default is set to _render). All inline defines defined in the .jst file are
* (default is set to window.render).
* All inline defines defined in the .jst file are
* compiled into separate functions and are available via _render.filename.definename
*
* Basic usage:
Expand Down Expand Up @@ -39,7 +40,7 @@ function InstallDots(o) {
if (this.__path[this.__path.length-1] !== '/') this.__path += '/';
this.__destination = o.destination || this.__path;
if (this.__destination[this.__destination.length-1] !== '/') this.__destination += '/';
this.__global = o.global || "_render";
this.__global = o.global || "window.render";
this.__rendermodule = o.rendermodule || {};
this.__settings = o.templateSettings ? copy(o.templateSettings, copy(doT.templateSettings)) : undefined;
this.__includes = {};
Expand All @@ -52,7 +53,7 @@ InstallDots.prototype.compileToFile = function(path, template, def) {
, settings = this.__settings || doT.templateSettings
, compileoptions = copy(settings)
, defaultcompiled = doT.template(template, settings, defs)
, exports = "dot:" + modulename
, exports = []
, compiled = ""
, fn;

Expand All @@ -69,17 +70,25 @@ InstallDots.prototype.compileToFile = function(path, template, def) {
}
if (fn) {
compiled += fn.toString().replace('anonymous', property);
exports += ',' + property + ":" + property;
exports.push(property);
}
}
}
compiled += defaultcompiled.toString().replace('anonymous', modulename);
fs.writeFileSync(path, "(function(){" + compiled
+ "var itself={" + exports
+ "};if(typeof module!=='undefined' && module.exports) module.exports=itself;else if(typeof define==='function')define(function(){return itself;});else {"
+ "var itself=" + modulename + ";"
+ addexports(exports)
+ "if(typeof module!=='undefined' && module.exports) module.exports=itself;else if(typeof define==='function')define(function(){return itself;});else {"
+ this.__global + "=" + this.__global + "||{};" + this.__global + "['" + modulename + "']=itself;}}());");
};

function addexports(exports) {
for (var ret ='', i=0; i< exports.length; i++) {
ret += "itself." + exports[i]+ "=" + exports[i]+";";
}
return ret;
}

function copy(o, to) {
to = to || {};
for (var property in o) {
Expand Down

0 comments on commit f3c90a8

Please sign in to comment.