Skip to content
This repository has been archived by the owner on Oct 19, 2023. It is now read-only.

Commit

Permalink
Drop handlebars-runtime dependency.
Browse files Browse the repository at this point in the history
We'll now use the handlebars module directly. Thanks to @kamicane

esamattis/node-handlebars-runtime#3
  • Loading branch information
esamattis committed Nov 3, 2013
1 parent 4b890e8 commit f3b2c79
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
language: node_js
before_script:
# Tests require that hbsfy is requireable by its name. We cannot use relative
# require calls because 'require("hbsfy/runtime");' will be compiled in to
# the browserify bundles.
- sudo npm link
node_js:
- "0.8"
- "0.10"
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

[Handlebars][] precompiler plugin for [Browserify v2][] without magic.

Compiles Handlebars templates to plain Javascript. The compiled templates
depend only on [handlebars-runtime][] so they are lightweight and fast!
Compiles Handlebars templates to plain Javascript. The compiled templates only
have one copy of the Handlebars runtime so they are lightweight and fast!

## Usage

Install hbsfy locally to your project:

npm install hbsfy

Handlebars runtime will be automatically installed as [peer dependency][].
Handlebars will be automatically installed as [peer dependency][].

Then use it as Browserify transform module with `-t`:

Expand All @@ -38,7 +38,7 @@ To register custom helpers just require the runtime use and `registerHelper` to
create helper:

```javascript
var Handlebars = require("handlebars-runtime");
var Handlebars = require("hbsfy/runtime");
Handlebars.registerHelper("upcase", function(s) {
return s.toUpperCase();
});
Expand All @@ -55,6 +55,16 @@ Handlebars.registerPartial('link', require("./partial.hbs"));

Checkout the example folder for details.

## Changelog

### 1.0.0

- Remove `handlebars-runtime` dependency and dependency directly on
`handlebars` module as [peer dependency][].
- Runtime must be now required with `require("hbsfy/runtime")` instead of
`require("handlebars-runtime")`.
- Thanks to @kamicane for teaching me how to do this.

## Browserify?

<https://github.com/substack/node-browserify>
Expand All @@ -63,5 +73,4 @@ Further reading: <http://esa-matti.suuronen.org/blog/2013/03/22/journey-from-req

[Handlebars]: http://handlebarsjs.com/
[Browserify v2]: https://github.com/substack/node-browserify
[handlebars-runtime]: https://npmjs.org/package/handlebars-runtime
[peer dependency]: http://blog.nodejs.org/2013/02/07/peer-dependencies/
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = function(file) {
function() {
var js = Handlebars.precompile(buffer);
// Compile only with the runtime dependency.
var compiled = "var Handlebars = require('handlebars-runtime');\n";
var compiled = "var Handlebars = require('hbsfy/runtime');\n";
compiled += "module.exports = Handlebars.template(" + js.toString() + ");\n";
this.queue(compiled);
this.queue(null);
Expand Down
15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "hbsfy",
"version": "0.1.5",
"version": "1.0.0",
"description": "Handlebars precompiler plugin for Browserify v2",
"main": "index.js",
"scripts": {
"test": "node test/test.js"
"test": "sh test.sh"
},
"repository": {
"type": "git",
Expand All @@ -21,14 +21,13 @@
"author": "Esa-Matti Suuronen",
"license": "MIT",
"dependencies": {
"through": "~2.2.7",
"handlebars": "~1.0.12"
"through": "~2.2.7"
},
"peerDependencies": {
"handlebars-runtime": "~1.0.12"
"handlebars": "~1.0.12"
},
"devDependencies": {
"browserify": "~2.4.0",
"handlebars-runtime": "~1.0.12"
"browserify": "~2.35.1",
"concat-stream": "~1.1.0"
}
}
}
11 changes: 11 additions & 0 deletions runtime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var hbsBase = require("handlebars/lib/handlebars/base");
var hbsUtils = require("handlebars/lib/handlebars/utils");
var hbsRuntime = require("handlebars/lib/handlebars/runtime");

var Handlebars = hbsBase.create();
hbsUtils.attach(Handlebars);
hbsRuntime.attach(Handlebars);

module.exports = Handlebars;

// module.exports = require("handlebars");
9 changes: 9 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh

set -eu

cd test/
npm link hbsfy
node test.js
node browserify_test.js

12 changes: 12 additions & 0 deletions test/browsercode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

var Handlebars = require("hbsfy/runtime");

Handlebars.registerHelper("upcase", function(s) {
return s.toUpperCase();
});

var template = require("./hello.hbs");

document.body.innerHTML = template({
msg: "hello"
});
26 changes: 26 additions & 0 deletions test/browserify_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

var concat = require("concat-stream");
var browserify = require("browserify");
var assert = require("assert");
var vm = require("vm");

var b = browserify(__dirname + "/browsercode.js");
b.transform(require("hbsfy"));

// Browser mock
var context = {
document: {
body: {}
}
};

b.bundle().pipe(concat(function(data) {
console.log("Bundle size", data.length);
assert(data.length < 15000, "Bundle is too big! Maybe full Handlebars got compiled in?");
vm.runInNewContext(data.toString(), context);
}));

setTimeout(function() {
assert.equal(context.document.body.innerHTML.trim(), "<h1>HELLO</h1>");
}, 400);

4 changes: 2 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
var fs = require("fs");
var assert = require("assert");

var hbsfy = require("../index");
var Handlebars = require("handlebars-runtime");
var hbsfy = require("hbsfy");
var Handlebars = require("hbsfy/runtime");

Handlebars.registerHelper("upcase", function(s) {
return s.toUpperCase();
Expand Down

0 comments on commit f3b2c79

Please sign in to comment.