Thursday, 16 March 2017

Gulp: avoid intermediate files when compiling mustache

I have the following directory structure in my repos:

src
  some-project
    some-file.html
    some-file.yml
  other-project
    foo.html
    foo.yml
    bar.html
    bar.yml
  stylesheet.css

dist
  some-project
    some-file.html
    some-file.yml
  other-project
    foo.html
    foo.yml
    bar.html
    bar.yml

I have a gulp task that takes the styles in any stylesheet added by an html file in the /src folder and automatically inlines them in the html (these are emails). The yml are meta-information used when sending data.

Now I want to add mustache templating to my HTML. The data for the templates is going to be in the yml files. The issue I have is that the gulp-mustache plugin takes a stream for its template input and an object in parameter for its data.

gulp.task('build', () => {
  gulp.src('src/**/*.html')
    .pipe(mustache(data)) // I don't have the data!
    .pipe(inlineCss({
      removeStyleTags: false
    }))
    .pipe(prefix(BASE_URL, [ { match: "img[src]", attr: "src"} ]))
    .pipe(gulp.dest('dist'))
});

I have another task that can compile YML to JSON, but I was trying to avoid creating temporary files since it defeats the whole point of having gulp's virtual file streams. What would you do?

It seems like a very simple task and I've searched for hours without any progress.



via Jacque Goupil

No comments:

Post a Comment