karma-handlebars-preprocessor

A Karma plugin. Compile handlebars template on the fly.

karma-handlebars-preprocessor

Preprocessor to compile Handlebars on the fly.

Forked from hanachin's code (kudos!)

Works with Karma 0.9 or later.

For more information on Karma see the homepage.

  1. Install karma-handlebars-preprocessor plugin.
$ npm install karma-handlebars-preprocessor --save-dev
  1. Define it as a preprocessor in the config file
  preprocessors: {
    '**/*.hbs': 'handlebars'
  }

or pass through the command line

  $ karma start --preprocessors handlebars

You can configure default behaviour in the handlebarsPreprocessor section of the config file. The following shows the default implementation:

// karma.conf.js 
module.exports = function(config) {
  config.set({
    preprocessors: {
      '**/*.hbs': ['handlebars']
    },
 
    handlebarsPreprocessor: {
      // name of the variable to store the templates hash 
      templates: "Handlebars.templates",
 
      // translates original file path to template name 
      templateNamefunction(filepath) {
        return filepath.replace(/^.*\/([^\/]+)\.hbs$/, '$1');
      },
 
      // transforms original file path to path of the processed file 
      transformPathfunction(path) {
        return path.replace(/\.hbs$/, '.js');
      }
    }
  });
};

If you want to export your compiled templates as anonymous AMD modules, use the amd option in the config as shown below:

    handlebarsPreprocessor: {
      amd: true
    }

MIT License