carlitoplatanito / gulp-nunjucks-render

[Gulp](https://github.com/wearefractal/gulp) plugin to render [Nunjucks](http://mozilla.github.io/nunjucks/) templates

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Nunjucks-render won't build on windows

jimboobrien opened this issue · comments

Hello,

I have a project that uses your npm package and it runs fine on linux, but it hangs on Windows. Both Windows 10 (my local machine) and our Jenkins servers (Windows Server 2013).

Windows 10:
node -v | v10.16.0
npm -v | 6.9.0

I'm using gulp and here is my nunjucksrender() function:

.pipe( nunjucksRender( { path: './demo/nunjucks/', // Relative path to templates ext: '.html', //Extension for compiled templates data: { //Data passed to template cdc_path: 'https://cdc.gov/', }, inheritExtension: false, //If true, uses same extension that is used for template envOptions: { //These are options provided for nunjucks Environment. watch: false }, manageEnv: manageEnvironment, //Hook for managing environment before compilation. loaders: null //If provided, uses that as first parameter to Environment constructor. } ) ) //nunjucks render

I've tried removing all options, just adding in the path, etc. etc.

I've confirmed on multiple machines both linux and windows the same behavior. It works on linux and mac but not on windows.

It literally just sits there in gitbash and hangs, taking up CPU usage without ever finishing.

Just trying to give you the right information to help troubleshoot. I've been researching for days, please tell me if I'm missing something.

Thanks

  • Jim

I ended up writing my on npm module and copying a lot of the source code from this repo. Thank you.

In gulpfile:

const nunjucks_test = require( './nunjucks' )

function nunjucks() {
	return src( './demo/**/*.+(html|nunjucks|njk)' )
		.pipe( nunjucks_test( {
			path: './demo/nunjucks/', // Relative path to templates
			ext: '.html', //Extension for compiled templates
			data: { //Data passed to template
				sites: JSON.parse( fs.readFileSync( './demo/json/nav.json' ) )
			},
			inheritExtension: false, //If true, uses same extension that is used for template
			envOptions: { //These are options provided for nunjucks Environment.
				watch: false
			},
			manageEnv: manageEnvironment, //Hook for managing environment before compilation.
			loaders: null //If provided, uses that as first parameter to Environment constructor.
		} ) )
		.pipe( dest( './dist/' ) )
}

nunjucks.js file:

use strict';
const through = require( 'through2' );
const nunjucks = require( 'nunjucks' );
const PluginError = require( 'plugin-error' );

//https://github.com/carlitoplatanito/gulp-nunjucks-render/blob/master/index.js

function compile( options ) {
	//set base dir
	nunjucks.configure( options.path );

	//nunjucks setup
	var env = new nunjucks.FileSystemLoader( options.path );

	//nunjucks env
	var compile_test = new nunjucks.Environment( env, {
		watch: false
	} );

	//add environment variables
	options.manageEnv.call( null, compile_test );

	/*
	 * file = file
	 * enc = encoding - utf-8
	 * cb   = callback function
	 */
	return through.obj( function( file, enc, cb ) {
		//spread data
		var data = { ...options.data };

		//if data is null exit
		if ( file.isNull() ) {
			this.push( file );
			return cb();
		}

		//spread even more if necessary
		if ( file.data ) {
			data = { ...file.data, ...data };
		}

		//if stream exit
		if ( file.isStream() ) {
			this.emit( 'error', new PluginError( 'namespace-nunjucks', 'Streaming not supported' ) );
			return cb();
		}

		//get content for this
		var _this = this;
		var filePath = file.path;

		//try to compile nunjucks
		try {
			//take nunjucks env and run renderString on it
			compile_test.renderString( file.contents.toString(), data, function( err, result ) {
				if ( err ) {
					_this.emit( 'error', new PluginError( 'namespace-nunjucks', err, {fileName: filePath} ) );
					return cb();
				}

				//take the compiled contents and add it to the file as a buffer
				file.contents = Buffer.from( result );

				//force extention if necessary
				//file.path = replaceExtension( filePath, '.html' );

				//this is needed to finally push the file
				_this.push( file );
				cb();
			} );
		} catch ( err ) {
			_this.emit( 'error', new PluginError( 'namespace-nunjucks', err, {fileName: filePath} ) );
			cb();
		}
	} );

}

module.exports = compile;

My Implementation now works on Windows 10, I think it was an issue with lodash or the replaceExtension library maybe.

Also had to change the buffer calls to the new syntax.