How to use the nunjucks.precompileString 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 at0g / nunjucks-loader / old / index.js View on Github external
}

            // Enable experimental Jinja compatibility to be enabled
            if(query.jinjaCompat){
                jinjaCompatStr = 'nunjucks.installJinjaCompat();\n';
            }
        }
        else {
            env = new nunjucks.Environment([]);
        }
        hasRun = true;
    }

    var name = slash(path.relative(root || this.options.context, this.resourcePath));

    var nunjucksCompiledStr = nunjucks.precompileString(source, {
            env: env,
            name: name
        });

    nunjucksCompiledStr = nunjucksCompiledStr.replace(/window\.nunjucksPrecompiled/g, 'nunjucks.nunjucksPrecompiled');

    // ==============================================================================
    // replace 'require' filter with a webpack require expression (to resolve assets)
    // ==============================================================================
    var filterReg = /env\.getFilter\(\"require\"\)\.call\(context, \"(.*?)\"/g;
    nunjucksCompiledStr = nunjucksCompiledStr.replace(filterReg, 'require("$1"');

    // ================================================================
    // Begin to write the compiled template output to return to webpack
    // ================================================================
    var compiledTemplate = '';
github at0g / nunjucks-loader / packages / nunjucks-loader / src / parseNunjucks.js View on Github external
if (!env) {
	    if (target === 'node') {
	        // perform some messy crap to use the nunjucks precompiled loader in node.
	        const window = global.window || {};
	        window.nunjucksPrecompiled = false;
	        global.window = window;
        }
        env = new nunjucks.Environment([], envOpts);
	}

    const root = loaderOpts.root || webpackOpts.context;
    const contextRelativePath = path.relative(root, resourcePath);
    const key = slash(contextRelativePath);

	const nunjucksCompiledStr = nunjucks.precompileString(source, {
		env,
		name: key,
        asFunction: true,
	});

    const convertedCompiledStr = nunjucksCompiledStr
		// replace the window global with a nunjucks global
        .replace(/window\.nunjucksPrecompiled/g, 'nunjucks.nunjucksPrecompiled')
        // replace 'require' filter with a webpack require expression (to resolve assets)
		.replace(requireFilterReg, 'require("$1"');

	const templateDependencies = parseDependencies(convertedCompiledStr);
	const pathToShim = slash(path.resolve(this.context, __dirname + '/runtimeShim.js'));
	const pathToRuntime = 'nunjucks/browser/nunjucks-slim.min';
    const output = `
var nunjucks = require("${pathToRuntime}");
github sindresorhus / gulp-nunjucks / index.js View on Github external
if (file.isNull()) {
			callback(null, file);
			return;
		}

		if (file.isStream()) {
			callback(new PluginError('gulp-nunjucks', 'Streaming not supported'));
			return;
		}

		const localOptions = {...options};
		const filePath = file.path;

		try {
			localOptions.name = (typeof localOptions.name === 'function' && localOptions.name(file)) || file.relative;
			file.contents = Buffer.from(nunjucks.precompileString(file.contents.toString(), localOptions));
			file.extname = '.js';
			this.push(file);
		} catch (error) {
			this.emit('error', new PluginError('gulp-nunjucks', error, {fileName: filePath}));
		}

		callback();
	});
}
github edge-js / edge / benchmarks / conditional.js View on Github external
function compileNunjucks () {
  return nunjucks.precompileString(nunjucksStatement, {
    name: 'foo'
  })
}
github alphagov / paas-admin / config / njk-loader.js View on Github external
if (typeof raw !== 'string' && raw.default) {
            raw = raw.default;
          }
          if (typeof raw !== 'string') {
            throw new Error('expected a string template got'+typeof raw);
          }
          return raw;
        })(),
        path: '${opts.name}',
        noCache: false
      }
    `;
    return {source};
  }

  source = nunjucks.precompileString(source, {
    name: opts.name,
    wrapper: (templates, _opts) => {
      return `
        const { runtime } = require('${ctx.data.runtimePath}');
        module.exports = {
          src: {
            type: 'code',
            obj: (function(){
              ${templates[0].template};
            })()
          },
          path: '${opts.name}',
          noCache: true,
        };
      `;
    },
github edge-js / edge / benchmarks / loop.js View on Github external
function compileNunjucks () {
  return nunjucks.precompileString(nunjucksStatement, {
    name: 'foo',
    env: env
  })
}