Git Product home page Git Product logo

rollup-plugin-handlebars's Introduction

rollup-plugin-handlebars Travis Build Status

Rollup plugin to compile handlebar templates.

Install

npm i rollup-plugin-handlebars -D

Usage

import { rollup } from 'rollup';
import handlebars from 'rollup-plugin-handlebars';

rollup({
	entry: 'main.js',
	plugins: [
		handlebars()
	]
});
<p>Hello {{name}}</p>

import the template.

import template from 'template.hbs';

console.log(template({name:'world'}))

Example

import { rollup } from 'rollup';
import handlebars from 'rollup-plugin-handlebars';
import babel from 'rollup-plugin-babel';
import { minify } from 'uglify-js';

rollup({
	entry: 'main.js',
	plugins: [
		handlebars({
			//handlebar options
		})
	]
});

License

MIT © Benjamin Legrand

rollup-plugin-handlebars's People

Contributors

benjilegnard avatar

Stargazers

John avatar

Watchers

 avatar

rollup-plugin-handlebars's Issues

Option to automatically import partials

I'd like the option to import required partials automatically, rather than needing to define them globally. I've started work on this in #3, but ran into the following conundrum:

The partial references within the handlebars templates will need to be resolved, more than just when they're imported. That is to say, the following:

{{!body.hbs}}
<body>
  {{> ./content}}
</body>
{{!content.hbs}}
<p>Hello, world!</p>

Will need to produce:

// This will be resolved by rollup or a plugin defined on rollup.
import './content';

import HandlebarsCompiler from 'handlebars/runtime';
const Template = HandlebarsCompiler.template({"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) {
    var stack1;

  return "<body>\n"
    + ((stack1 = this.invokePartial(partials['/path/to/content'],depth0,{"name":"./content","data":data,"indent":"  ","helpers":helpers,"partials":partials})) != null ? stack1 : "")
    + "</body>";
},"usePartial":true,"useData":true});
export default Template;
import HandlebarsCompiler from 'handlebars/runtime';
const Template = HandlebarsCompiler.template(/* template data */);
HandlebarsCompiler.registerPartial('/path/to/content.hbs', Template);
export default Template;

Where the body.hbs template data differs as follows:

diff --git a/a.txt b/a.txt
--- a/a.txt
+++ b/a.txt
@@ -2,6 +2,6 @@
     var stack1;

   return "<body>\n"
-    + ((stack1 = this.invokePartial(partials['./content'],depth0,{"name":"./content","data":data,"indent":"  ","helpers":helpers,"partials":partials})) != null ? stack1 : "")
+    + ((stack1 = this.invokePartial(partials['/path/to/content'],depth0,{"name":"/path/to/content","data":data,"indent":"  ","helpers":helpers,"partials":partials})) != null ? stack1 : "")
     + "</body>";
 },"usePartial":true,"useData":true}

This would require hooking into rollup's resolveId options, which are not exposed in any manner, public or otherwise.

Option to specify compiler

Currently the plugin has hardcoded into it:

compiled += "const HandlebarsCompiler = require('handlebars/runtime')['default'];\n";

However, I want to use the web runtime (i.e. window.Handlebars), not using require.

So it would be helpful to have an option called compiler that lets the user define a string containing the compiler they want to use.

That would make the line I specified above:

compiled += "const HandlebarsCompiler = " + options.compiler + ";\n";

(with options.compiler defaulting to "require('handlebars/runtime')['default']")

handlebars module not being compiled

hi there.
My output bundle looks all wrong:


(function () {
'use strict';

const HandlebarsCompiler = require('handlebars/runtime')['default'];
var graphicTemplate = HandlebarsCompiler.template({"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data) {
    var helper;

  return "\n<p>Hello "
    + container.escapeExpression(((helper = (helper = helpers.name || (depth0 != null ? depth0.name : depth0)) != null ? helper : helpers.helperMissing),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),{"name":"name","hash":{},"data":data}) : helper)))
    + "</p>\n";
},"useData":true});

var no = graphicTemplate({name:'world'});

d3.select('.test-div').html(no);

}());

Any ideas where I'm wrong? I have the same rollup config file as you suggest.

Push /dist to git repo

Hi

I am currently looking to use the 2.0 branch in my project to make use of the custom compiler, however pulling the branch via npm means there is no dist folder. Could you add?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.