From d86b818a2096a788b2212da8e14fef750255f428 Mon Sep 17 00:00:00 2001 From: Rafael Hengles Date: Sat, 18 Oct 2014 10:43:28 -0300 Subject: [PATCH 1/2] Added option to customize the wrapper for precompiled templates --- src/precompile-global.js | 18 ++++++++++++++++++ src/precompile.js | 37 ++++++++++++++++++------------------- 2 files changed, 36 insertions(+), 19 deletions(-) create mode 100644 src/precompile-global.js diff --git a/src/precompile-global.js b/src/precompile-global.js new file mode 100644 index 00000000..cac5619e --- /dev/null +++ b/src/precompile-global.js @@ -0,0 +1,18 @@ +function precompileGlobal(name, template, opts) { + opts = opts || {}; + + name = JSON.stringify(name); + + var out = '(function() {' + + '(window.nunjucksPrecompiled = window.nunjucksPrecompiled || {})' + + '[' + name + '] = (function() {\n' + template + '\n})();\n'; + + if(opts.asFunction) { + out += 'return function(ctx, cb) { return nunjucks.render(' + name + ', ctx, cb); }\n'; + } + + out += '})();\n'; + return out; +} + +module.exports = precompileGlobal; \ No newline at end of file diff --git a/src/precompile.js b/src/precompile.js index 6af48bb9..1e7c8d1a 100644 --- a/src/precompile.js +++ b/src/precompile.js @@ -3,6 +3,7 @@ var path = require('path'); var lib = require('./lib'); var compiler = require('./compiler'); var Environment = require('./environment').Environment; +var precompileGlobal = require('./precompile-global'); function match(filename, patterns) { if (!Array.isArray(patterns)) return false; @@ -27,11 +28,16 @@ function precompile(input, opts) { // * env: the Environment to use (gets extensions and async filters from it) // * include: which file/folders to include (folders are auto-included, files are auto-excluded) // * exclude: which file/folders to exclude (folders are auto-included, files are auto-excluded) + // * wrapper: function(name, template, opts) {...} + // Customize the output format to store the compiled template. + // By default, templates are stored in a global variable used by the runtime. + // A custom loader will be necessary to load your custom wrapper. opts = opts || {}; var env = opts.env || new Environment([]); var asyncFilters = env.asyncFilters; var extensions = env.extensionsList; + var wrapper = opts.wrapper || precompileGlobal; var pathStats = fs.existsSync(input) && fs.statSync(input); var output = ''; @@ -42,16 +48,18 @@ function precompile(input, opts) { 'compiling a string'); } - return _precompile(input, + return _precompile(wrapper, + input, opts.name, env, - opts.asFunction); + opts); } else if(pathStats.isFile()) { - return _precompile(fs.readFileSync(input, 'utf-8'), + return _precompile(wrapper, + fs.readFileSync(input, 'utf-8'), opts.name || input, env, - opts.asFunction); + opts); } else if(pathStats.isDirectory()) { var templates = []; @@ -82,10 +90,11 @@ function precompile(input, opts) { var name = templates[i].replace(path.join(input, '/'), ''); try { - output += _precompile(fs.readFileSync(templates[i], 'utf-8'), + output += _precompile(wrapper, + fs.readFileSync(templates[i], 'utf-8'), name, env, - opts.asFunction); + opts); } catch(e) { if(opts.force) { // Don't stop generating the output if we're @@ -102,17 +111,13 @@ function precompile(input, opts) { } } -function _precompile(str, name, env, asFunction) { +function _precompile(wrapper, str, name, env, opts) { env = env || new Environment([]); var asyncFilters = env.asyncFilters; var extensions = env.extensionsList; - var out = '(function() {' + - '(window.nunjucksPrecompiled = window.nunjucksPrecompiled || {})' + - '["' + name.replace(/\\/g, '/') + '"] = (function() {'; - - out += lib.withPrettyErrors( + var template = lib.withPrettyErrors( name, false, function() { @@ -122,14 +127,8 @@ function _precompile(str, name, env, asFunction) { name); } ); - out += '})();\n'; - - if(asFunction) { - out += 'return function(ctx, cb) { return nunjucks.render("' + name + '", ctx, cb); }'; - } - out += '})();\n'; - return out; + return wrapper(name, template, opts); } module.exports = { From 482b223c3418246c357206ec1f69711ab758d6f9 Mon Sep 17 00:00:00 2001 From: Rafael Hengles Date: Sat, 18 Oct 2014 10:45:07 -0300 Subject: [PATCH 2/2] Added option to customize the wrapper for precompiled templates --- src/precompile-global.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/precompile-global.js b/src/precompile-global.js index cac5619e..9525236f 100644 --- a/src/precompile-global.js +++ b/src/precompile-global.js @@ -1,7 +1,7 @@ function precompileGlobal(name, template, opts) { opts = opts || {}; - name = JSON.stringify(name); + name = JSON.stringify(name); var out = '(function() {' + '(window.nunjucksPrecompiled = window.nunjucksPrecompiled || {})' +