Search code examples
gruntjsassemble

How can I use assemble plugins in a Grunt setup using grunt-load-config


In my setup, I have split my gruntfile up into separate files using grunt-load-config. That means that I have one file for each grunt plugin I want to use.

My gruntfile.js looks like this:

module.exports = function(grunt) {
    var path = require('path');

    // measures the time each task takes
    require('time-grunt')(grunt);

    // load grunt config
    require('load-grunt-config')(grunt, {
        jitGrunt: true,
        configPath: path.join(process.cwd(), 'Interface/grunttasks')
    });
};

And my assemble setup in Interface/grunttasks/assemble.js looks like this

module.exports = {

    options: {
        flatten: true,
        partials: ['<%= package.html %>/_partials/*.html'],
        layout: '<%= package.html %>/_layouts/main.html'
    },

    pages: {
        src: ['<%= package.pages %>/**/*.html',
        dest: '<%= package.prototype %>'
    }

};

This works perfectly and as expected, but now I want to use a set of assemble helpers. But I am unsure how I am supposed to add them to my grunt setup so that assemble (and in turn handlebars) can use them.

I've looked at the prettify helper, and their install instructions is simply to 'add the following to your application'

var helpers = require('prettify');

And then I should just be able to add the configuration to my assemble block in my gruntfile, like this

grunt.initConfig({
    assemble: {
        options: {
            prettify: {
                mode: 'js',  // 'html' is defined by default
                condense: true,
                padcomments: true,
                indent: 4
            }
        },
        ...
      }
});

But I cant seem to get the plugin properly registered. I guess its because I've split up my grunt file?

Anyone able to explain how to add assemble plugins/helpers in this grunt setup?


Solution

  • Looks like the readme should be updated on that repository.

    Since the name is now handlebars-helper-prettify you should be able to add it as a dev dependency:

    $ npm i handlebars-helper-prettify -D
    

    Then include it in the helpers option in your assemble configuration:

    grunt.initConfig({
        assemble: {
            options: {
                helpers: ['handlebars-helpers-prettify'],
                prettify: {
                    mode: 'js',  // 'html' is defined by default
                    condense: true,
                    padcomments: true,
                    indent: 4
                }
            },
            ...
          }
    });
    

    Hope this helps. Feel free to open an issue or PR about the readme so it can be updated.