Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using commonjs:true generates incorrect JS code (0.10.2) #151

Open
rally25rs opened this issue Sep 3, 2015 · 2 comments
Open

Using commonjs:true generates incorrect JS code (0.10.2) #151

rally25rs opened this issue Sep 3, 2015 · 2 comments

Comments

@rally25rs
Copy link

Using v0.10.2, if commonjs:true is set in options, then the output module JS file puts each template into a return statement, so the resulting file has multiple returns from the same function.

Gruntfile.js:

'use strict';

module.exports = function (grunt) {
  grunt.initConfig({
    handlebars: {
        compile: {
          files: [{
            dest: 'dist/templates.js',
            src: 'src/**/*.hbs'
          }],
          options: {
            commonjs: true,
            namespace: false
          }
        }
    }
  });

  grunt.loadNpmTasks('grunt-contrib-handlebars');

  grunt.registerTask('default', ['handlebars:compile']);
};

The /src directory contains 2 files:

src/one.hbs

<h1>template one</h1>

src/two.hbs

<h2>template two</h2>

The resulting dist/templates.js is:

module.exports = function(Handlebars) {

return Handlebars.template({"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) {
    return "<h1>template one</h1>";
},"useData":true});

return Handlebars.template({"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) {
    return "<h2>template two</h2>";
},"useData":true});

};

Note that the generated function has 2 return statements, one for each template.

module.exports = function(Handlebars) {
    return Handlebars.template(...
    return Handlebars.template(...
}

This was discovered after an upgrade from v0.9.1 (which used Handlebars 2) to v0.10.2 (which uses Handlebars 3)

If I backdate grunt-contrib-handlebars to v0.9.1 then the generated dist/templates.js file is:

module.exports = function(Handlebars) {

var templates = {};

templates["src/one.hbs"] = Handlebars.template({"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) {
  return "<h1>template one</h1>";
  },"useData":true});

templates["src/two.hbs"] = Handlebars.template({"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) {
  return "<h2>template two</h2>";
  },"useData":true});

return templates;

};

which is the correct/expected output.

@rally25rs
Copy link
Author

Workaround is to add a namespace option set to a string. Then the generated code is usable.

For example, with the option namespace:'templates' :

module.exports = function(Handlebars) {

this["templates"] = this["templates"] || {};

this["templates"]["src/one.hbs"] = Handlebars.template({"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) {
    return "<h1>template one</h1>";
},"useData":true});

this["templates"]["src/two.hbs"] = Handlebars.template({"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) {
    return "<h2>template two</h2>";
},"useData":true});

return this["templates"];

};

@skanaar
Copy link

skanaar commented Feb 10, 2016

We have stumbled on this pretty hard.
While the workaround works it does not work as it says in the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants