sindresorhus / gulp-nunjucks

Precompile Nunjucks templates
MIT License
153 stars 19 forks source link

TypeError: First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object. #22

Open KatieMFritz opened 7 years ago

KatieMFritz commented 7 years ago

Hello! I'm trying to use gulp-nunjucks to compile Twig syntax in PatternLab Node Gulp edition.

I keep getting the following error, and I'm not sure how to resolve it:

TypeError in plugin 'gulp-nunjucks'
Message: First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.
Details:
    fileName: // first .twig file in my gulp.src
    domainEmitter: [object Object]
    domain: [object Object]
    domainThrown: false

Here's the relevant part of my gulpfile.js:

const nunjucks_gulp = require('gulp-nunjucks');
const nunjucks_lib = require('nunjucks');

gulp.task('nunjucks', () =>
  gulp.src(normalizePath(paths().source.templates + '**/*.twig'))
   .pipe(nunjucks_gulp.compile(
      {},
      {env: new nunjucks_lib.Environment(new nunjucks_lib.FileSystemLoader(normalizePath(paths().source.patterns)))}
    ))
    .pipe(gulp.dest(normalizePath(paths().public.templates)))
);

I understand that compile takes two arguments, data and options. It seems like I have two arguments there, {} and my env option.

My end goal is to get html files in my public.templates directory, so maybe there is a function I could use besides compile? I'm open. If I replace compile with precompile and remove the first argument, gulp nunjucks completes with no errors, but it doesn't seem like it actually does anything.

Please help me figure out what I'm missing! Thank you in advance. 🙇‍♀️

kevva commented 7 years ago

Do you have any empty files? Might be Buffer.from here that throws because of an empty string.

KatieMFritz commented 7 years ago

@kevva Thanks for responding! I don't have any empty files in source.templates. Here's my folder structure: source-templates

brokolja commented 7 years ago

Hi,

same problem here. It throws: First argument must be a string, Buffer... if i use something wrong as a macro-parameter for example. Its ok to show an error-message here but if it throws the gulp-process with all watchers gets canceled so users have to manually restart it. I use plumber for catching those plugins but here it does not work :/ Is it possible to just not throw the errors?

My code...

.pipe(gulpNunjucks.compile({}, {
  env: nunjucksEnv
}))
.pipe(plumber({ handleError: handleError }))

Thank you.

tortilaman commented 6 years ago

I'm getting this error when I try to use macros that are defined in a file that's included. As soon as I remove the macros, the error goes away.

Error

[00:14:11] Starting 'compHTML'...
[00:14:11] 'compHTML' errored after 152 ms
[00:14:11] TypeError in plugin "gulp-nunjucks"
Message:
    First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.
Details:
    fileName: /Users/nick/Development/**/cause.njk
    domainEmitter: [object Object]
    domain: [object Object]
    domainThrown: false

Nunjucks Config

/**
 * COMP_HTML
 *
 * Compile nunjucks into html
 */
const compHTML = () => {
  htmlComped = true
  return gulp
    .src('src/**/*.njk')
    .pipe($.data(config))
    .pipe($.nunjucks.compile())
    .pipe($.filter(['**', '!_*'])) // Remove files we don't want exported
    .pipe(
      $.rename(function (path) {
        path.extname = '.html'
      })
    )
    .pipe(gulp.dest('public'))
}

Layout

{% extends "./templates/_base.njk" %}
{% import "includes/mixins.njk" as mixins with context %}
{% import "includes/mxCause.njk" as mxCause with context %}
{% block main %}
  {{ mxCause.circleStat('Education-circle-2', '5,390hrs', 'Volunteered') }}
{% endblock %}

Include

{% macro circleStat(imgName, number, stat) %}
  <div>
    <img class="width-6 height-6" src="/assets/img/circles/{{imgName}}.svg"/>
    <h1 class="type-h mb-0 pb-0">{{number}}</h1>
    <spam class="type-d">{{stat}}</span>
  </div>
{% endmacro %}

Let me know if there's any additional information I can provide, or some type of debug mode I can enable on the plugin.