How to use the nunjucks.precompile function in nunjucks

To help you get started, we’ve selected a few nunjucks examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github accordproject / cicero / packages / cicero-core / scripts / nunjucks-precompile.js View on Github external
* limitations under the License.
 */

'use strict';
const nunjucks = require('nunjucks');
const fs = require('fs');
const path = require('path');

const env = new nunjucks.Environment(new nunjucks.FileSystemLoader('../'), {
    tags: {
        blockStart: '<%',
        blockEnd: '%>'
    },
    autoescape: false  // Required to allow nearley syntax strings
});
const compiled = nunjucks.precompile('src/template.ne', {env, name: 'template.ne' });
fs.writeFileSync(path.resolve(__dirname,'../lib/compiled_template.js'),compiled);
github SudoCat / Nunjucks-Isomorphic-Loader / src / node-loader.js View on Github external
this.addDependency(njkPath);

	var loaderPath = require.resolve('./fs-loader');
	this.addDependency(loaderPath);

	var njkSlimPath = require.resolve('nunjucks/browser/nunjucks-slim');
	var nunjucksSlim = utils.stringifyRequest(this, '!' + njkSlimPath);
	this.addDependency(njkSlimPath);

	var env = new nunjucks.Environment(new fsLoader(paths, this.addDependency));

	var name = path.relative(rootPath, this.resourcePath);

	this.addContextDependency(rootPath);

	var precompiledTemplates = nunjucks.precompile(rootPath, {
		env: env,
		include: [/.*\.(njk|nunjucks|html|tpl|tmpl)$/]
	});

  	return `// Return function to HtmlWebpackPlugin
		// Allows Data var to be passed to templates
		// Then render templates with both HtmlWebpackPlugin Data
		// and Nunjucks Context
		var nunjucks = require(${nunjucksSlim});

		// Create fake window object to store nunjucks precompiled templates
		global.window = {};

		${precompiledTemplates}

		var env = new nunjucks.Environment(new nunjucks.PrecompiledLoader());
github kieranelby / KingOfTheEtherThrone / bin / build-public.js View on Github external
console.log("Using template to write home-page html ...");

var indexTemplateSource = fs.readFileSync('templates/index.nunjucks.html', 'utf8');
var indexContext = {
  readmeHtml: readmeHtml,
  contractAddress: contractAddress,
  contractAbiJson: contractAbiJson,
  cacheBuster: (new Date()).getTime()
};
var indexHtml = nunjucks.renderString(indexTemplateSource, indexContext);
fs.writeFileSync('public/index.html', indexHtml, 'utf8');

console.log("Pre-compiling templates needed by public web content at run-time");

var precompileOpts = {};
var precompiledTemplatesJs = nunjucks.precompile("templates/interface.nunjucks.html", precompileOpts);
fs.writeFileSync('public/templates.js', precompiledTemplatesJs, 'utf8');

console.log("Copying files needed by public web content at run-time");

var minStr = "";
cp.sync("node_modules/web3/dist/web3" + minStr + ".js", "public/web3.js");
cp.sync("node_modules/nunjucks/browser/nunjucks-slim" + minStr + ".js", "public/nunjucks-slim.js");

if (commitEnabled) {

  console.log("Comitting to git");

  child_process.exec("git commit -a -m 'Autogenerated from build-public'", {timeout: 60000});
  child_process.exec("git push origin master", {timeout: 120000});

}
github JasonEtco / flintcms / server / utils / nunjucks.js View on Github external
noCache: process.env.NODE_ENV !== 'production',
    autoescape: false
  })

  Object.keys(global.FLINT).forEach((key) => {
    if (key === 'nun') return
    nun.addGlobal(key, global.FLINT[key])
  })

  nun.addGlobal('getContext', () => this.ctx)

  nun.addFilter('json', obj =&gt; `<pre><code>${JSON.stringify(obj, null, 2)}</code></pre>`)
  nun.addFilter('date', dateFilter)
  nun.addFilter('field', fieldFilter)

  nunjucks.precompile(pathToTemplates, { env: nun })

  return nun
}
github JasonEtco / flintcms / server / templates / nunjucks.js View on Github external
const path = require('path')

const pathToTemplates = path.join(process.cwd(), 'templates')

const nun = nunjucks.configure(pathToTemplates, {
  noCache: process.env.NODE_ENV !== 'production',
  autoescape: false
})

nun.addGlobal('getContext', () =&gt; this.ctx)

nun.addFilter('json', obj =&gt; `<pre><code>${JSON.stringify(obj, null, 2)}</code></pre>`)
nun.addFilter('date', dateFilter)
awaitFilter(nun)

nunjucks.precompile(pathToTemplates, { env: nun })

module.exports = nun
github mozilla / openbadges-discovery / clientapp / index.js View on Github external
beforeBuildJS: function () {
      util.log("Precompiling templates...");
      var templates = nunjucks.precompile(path.join(__dirname, '/templates'), {
        include: [/.*\.html/]
      });
      fs.writeFileSync(path.join(__dirname, 'build/precompiled.js'), templates);
      util.log("  Done.");
    },
    server: app