Comparing version 2.4.1 to 2.5.0
82
index.js
@@ -66,3 +66,3 @@ /*jshint node: true*/ | ||
function hbsfy(file, opts) { | ||
function getOptions(opts) { | ||
var extensions = defaultExtensions; | ||
@@ -93,2 +93,50 @@ var compiler = defaultCompiler; | ||
return xtend({}, opts, { | ||
extensions: extensions, | ||
precompiler: precompiler, | ||
compiler: compiler, | ||
traverse: traverse | ||
}); | ||
} | ||
function compile(file, opts) { | ||
var options = getOptions(opts); | ||
var compiler = options.compiler; | ||
var precompiler = options.precompiler; | ||
var traverse = options.traverse; | ||
var js; | ||
var compiled = "// hbsfy compiled Handlebars template\n"; | ||
var parsed = null; | ||
var partials = null; | ||
// Kill BOM | ||
file = file.replace(/^\uFEFF/, ''); | ||
if (traverse) { | ||
parsed = precompiler.parse(file); | ||
partials = findPartials(parsed); | ||
} | ||
js = precompiler.precompile(file, options.precompilerOptions); | ||
// Compile only with the runtime dependency. | ||
compiled += "var HandlebarsCompiler = " + compiler + ";\n"; | ||
if (partials && partials.length) { | ||
partials.forEach(function(p, i) { | ||
var ident = "partial$" + i; | ||
compiled += "var " + ident + " = require('" + p + "');\n"; | ||
compiled += "HandlebarsCompiler.registerPartial('" + p + "', " + ident + ");\n"; | ||
}); | ||
} | ||
compiled += "module.exports = HandlebarsCompiler.template(" + js.toString() + ");\n"; | ||
return compiled; | ||
} | ||
function hbsfy(file, opts) { | ||
var extensions = getOptions(opts).extensions; | ||
if (!extensions[file.split(".").pop()]) return through(); | ||
@@ -102,17 +150,6 @@ | ||
function() { | ||
var js; | ||
var compiled = "// hbsfy compiled Handlebars template\n"; | ||
var parsed = null; | ||
var partials = null; | ||
var compiled; | ||
// Kill BOM | ||
buffer = buffer.replace(/^\uFEFF/, ''); | ||
try { | ||
if (traverse) { | ||
parsed = precompiler.parse(buffer); | ||
partials = findPartials(parsed); | ||
} | ||
js = precompiler.precompile(buffer, opts.precompilerOptions); | ||
compiled = compile(buffer, opts); | ||
} catch (e) { | ||
@@ -123,14 +160,2 @@ this.emit('error', e); | ||
// Compile only with the runtime dependency. | ||
compiled += "var HandlebarsCompiler = " + compiler + ";\n"; | ||
if (partials && partials.length) { | ||
partials.forEach(function(p, i) { | ||
var ident = "partial$" + i; | ||
compiled += "var " + ident + " = require('" + p + "');\n"; | ||
compiled += "HandlebarsCompiler.registerPartial('" + p + "', " + ident + ");\n"; | ||
}); | ||
} | ||
compiled += "module.exports = HandlebarsCompiler.template(" + js.toString() + ");\n"; | ||
this.queue(compiled); | ||
@@ -148,3 +173,4 @@ this.queue(null); | ||
module.exports = hbsfy; | ||
exports = module.exports = hbsfy; | ||
exports.findPartials = findPartials; | ||
exports.compile = compile; |
{ | ||
"name": "hbsfy", | ||
"version": "2.4.1", | ||
"version": "2.5.0", | ||
"description": "Handlebars precompiler plugin for Browserify v2", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -14,12 +14,9 @@ [![Build Status](https://travis-ci.org/epeli/node-hbsfy.png?branch=master)](https://travis-ci.org/epeli/node-hbsfy) | ||
npm install hbsfy | ||
npm install --save-dev hbsfy | ||
You will also need Handlebars installed. Handlebars 1.x is officially supported | ||
for now: | ||
You will also need Handlebars installed. Handlebars 1, 2, 3, and 4 are supported | ||
for now (use 4 for best results): | ||
npm install handlebars@1 | ||
npm install --save-dev handlebars | ||
Although the alpha version of Handlebars 2.0 should also work. Just drop the | ||
`@1` to try it. | ||
Then use it as Browserify transform module with `-t`: | ||
@@ -74,3 +71,3 @@ | ||
Using the `--traverse` or `-t` option will cause partials to be resolved using node's [module resolution algorithm](https://nodejs.org/docs/latest/api/modules.html#modules_all_together). Be sure to prefix relative paths with `./` or `../` as needed. Otherwise the algorithm assumes a node_module is being referenced. | ||
Using the `--traverse` or `-t` option will cause partials to be resolved using node's [module resolution algorithm](https://nodejs.org/docs/latest/api/modules.html#modules_all_together). Be sure to prefix relative paths with `./` or `../` as needed. Otherwise the algorithm assumes a `node_module` is being referenced. | ||
@@ -143,3 +140,3 @@ Example: | ||
To register custom helpers just require the runtime use and `registerHelper` to | ||
To register custom helpers, require the runtime and run `registerHelper` to | ||
create helper: | ||
@@ -167,5 +164,49 @@ | ||
### .compile | ||
This synchronous method can be used to enable all hsbfy functionality in another environment, such as node or a test runner (such as mocha). | ||
```js | ||
// mocha-hbs.js | ||
var fs = require("fs"); | ||
var hbsfy = require("hbsfy"); | ||
require.extensions['.hbs'] = function (module, filename) { | ||
var file = fs.readFileSync(filename, "utf8"); | ||
var opts = { traverse: true }; | ||
return module._compile(hbsfy.compile(file, opts), filename); | ||
} | ||
``` | ||
```sh | ||
$ mocha -r hbs:./mocha-hbs.js tests/ | ||
``` | ||
Remember to register your custom helpers as well! Ideally your templates themselves `require` your helpers and runtime, and call `registerHelper`. But if they don't, all helpers can be loaded at once as part of the require hook above: | ||
```js | ||
// mocha-hbs.js | ||
var fs = require("fs"); | ||
var hbsfy = require("hbsfy"); | ||
var runtime = require("hbsfy/runtime"); | ||
var helpers = require("./path/to/my/exported/helpers"); | ||
Object.keys(helpers).forEach(function (key) { | ||
runtime.registerHelper(key, helpers[key]); | ||
}); | ||
require.extensions['.hbs'] = function (module, filename) { | ||
var file = fs.readFileSync(filename, "utf8"); | ||
var opts = { traverse: true }; | ||
return module._compile(hbsfy.compile(file, opts), filename); | ||
} | ||
``` | ||
## Changelog | ||
### 2.5.0 | ||
- Export `findPartials` and `compile` for use in utilities / test frameworks [#49](https://github.com/epeli/node-hbsfy/pull/49). | ||
### 2.4.1 | ||
@@ -172,0 +213,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35301
715
264