component-builder-handlebars
Advanced tools
Comparing version 0.3.0 to 0.4.0
75
index.js
@@ -1,1 +0,74 @@ | ||
module.exports = require('./lib/plugin'); | ||
'use strict'; | ||
var path = require('path'); | ||
var join = path.join; | ||
var fs = require('fs'); | ||
var exists = fs.existsSync; | ||
var read = fs.readFileSync; | ||
var defaults = require('defaults'); | ||
var Handlebars = require('handlebars'); | ||
var partials = []; | ||
var runtime = runtimeFile(); | ||
exports = module.exports = function(opts) { | ||
opts = defaults(opts, { | ||
extname: 'hbs', | ||
partialRegex: /^_/ | ||
}); | ||
partials = []; | ||
return function builderHandlebars(file, done) { | ||
if (file.extension !== opts.extname) return done(); | ||
file.read(function(err, string) { | ||
if (err) return done(err); | ||
var filename = path.basename(file.path, '.' + file.extension); | ||
var compiled = Handlebars.precompile(string); | ||
var output = 'Handlebars.template(' + compiled + ')'; | ||
if (opts.partialRegex.test(filename)) { | ||
// Register the partial as: moduleName/_fileName | ||
output = 'Handlebars.registerPartial("' | ||
+ file.branch.name + '/' + file.path.replace('.' + file.extension, '') | ||
+ '", ' + output + ');\n'; | ||
partials.push(file.branch.canonical + '/' + file.path); | ||
} else { | ||
output = 'module.exports = ' + output; | ||
} | ||
file.string = output; | ||
done(); | ||
}); | ||
}; | ||
}; | ||
exports.includeRuntime = function includeRuntime() { | ||
var string = runtime; | ||
string += 'this.Handlebars = window.Handlebars = Handlebars;\n'; | ||
if (!partials.length) return string; | ||
// Auto requires partials. | ||
partials.forEach(function(p) { | ||
string += 'require("' + p + '");\n'; | ||
}); | ||
return string; | ||
}; | ||
function runtimeFile() { | ||
var runtimePath = join(__dirname, 'node_modules/handlebars/dist/handlebars.runtime.js'); | ||
if (!exists(runtimePath)) { | ||
runtimePath = join(__dirname, 'handlebars.runtime.js'); | ||
} | ||
return read(runtimePath, 'utf-8'); | ||
} |
{ | ||
"name": "component-builder-handlebars", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Builder.js plugin to precompile Handlebars templates", | ||
"author": "Antoine Lehurt <keuwah@gmail.com>", | ||
"author": { | ||
"name": "Antoine Lehurt", | ||
"email": "hello@kewah.com", | ||
"url": "http://kewah.com" | ||
}, | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "make test" | ||
"test": "./node_modules/mocha/bin/mocha --reporter spec", | ||
"watch": "./node_modules/mocha/bin/mocha --watch --reporter spec", | ||
"example": "node ./example/builder.js && node ./example/server.js" | ||
}, | ||
@@ -20,3 +26,3 @@ "repository": { | ||
"component.js", | ||
"builder", | ||
"builder2", | ||
"handlebars", | ||
@@ -26,10 +32,12 @@ "precompile" | ||
"dependencies": { | ||
"handlebars": "~1.0.8" | ||
"defaults": "^1.0.0", | ||
"handlebars": "^1.3.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "~1.8.1", | ||
"should": "~1.2.1", | ||
"component-builder": "~0.12.0", | ||
"jshint": "~0.9.1" | ||
"component-builder": "^1.1.5", | ||
"component-resolver": "^1.1.5", | ||
"express": "^3.4.8", | ||
"fs-extra": "^0.9.1", | ||
"mocha": "*" | ||
} | ||
} | ||
} |
@@ -1,32 +0,44 @@ | ||
# component-builder-handlebars | ||
# component-builder-handlebars [![Build Status](https://travis-ci.org/kewah/component-builder-handlebars.svg?branch=master)](https://travis-ci.org/kewah/component-builder-handlebars) | ||
> [Builder.js](https://github.com/component/builder.js) plugin to precompile Handlebars templates to [Component.js](https://github.com/component/component) modules. | ||
> [Builder2.js](https://github.com/component/builder2.js) plugin to precompile Handlebars templates to [Component.js](https://github.com/component/component) modules. | ||
## Install | ||
With [npm](http://npmjs.org) do: | ||
```bash | ||
$ npm install component-builder-handlebars --save-dev | ||
``` | ||
## Usage | ||
```javascript | ||
var handlebarsPlugin = require('component-builder-handlebars'); | ||
var builder = new Builder('test/fixtures'); | ||
builder.use(handlebarsPlugin({ | ||
extname: '.hbs', | ||
### Build | ||
```js | ||
var builder = require('component-builder'); | ||
var hbs = require('component-builder-handlebars'); | ||
var options = { | ||
extname: 'hbs', | ||
partialRegex: /^_/ | ||
})); | ||
}; | ||
builder.scripts(tree) | ||
.use('scripts', Builder.plugins.js()) | ||
.use('templates', hbs(options)) | ||
.end(function(err, string) { | ||
fs.writeFileSync(dest, string); | ||
}); | ||
``` | ||
Or with [grunt-component-build](https://github.com/anthonyshort/grunt-component-build): | ||
```javascript | ||
component: { | ||
app: { | ||
output: './build/', | ||
scripts: true, | ||
configure: function(builder) { | ||
builder.use(handlebarsPlugin({ | ||
extname: '.hbs', | ||
partialRegex: /^_/ | ||
})); | ||
} | ||
} | ||
} | ||
### Partials | ||
To include a partial inside a template: | ||
```html | ||
{{> componentName/path/to/_partial }} | ||
``` | ||
For [instance](test/fixture/with-partials/_nav.hbs). | ||
## Options | ||
@@ -47,38 +59,12 @@ | ||
```html | ||
[componentName/path/to/_navPartial.hbs] | ||
See [example](example) folder. | ||
<nav> | ||
<ul> | ||
<li>…</li> | ||
... | ||
</ul> | ||
</nav> | ||
``` | ||
To build it: | ||
```html | ||
[componentName/path/to/myTemplate.hbs] | ||
<h1>{{title}}</h1> | ||
<!-- When you include a partial don't use the prefix --> | ||
{{> componentName/path/to/navPartial}} | ||
```bash | ||
$ npm run example | ||
``` | ||
```javascript | ||
[componentName/path/to/module.js] | ||
var myTpl = require('./myTemplate'); | ||
var output = myTpl({ | ||
title: 'Ready to start' | ||
}); | ||
``` | ||
## Contributing | ||
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code. | ||
## License | ||
Copyright (c) 2013 Antoine Lehurt | ||
Licensed under the MIT license. |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
49906
34
1363
2
5
70
3
1
+ Addeddefaults@^1.0.0
+ Addedclone@1.0.4(transitive)
+ Addeddefaults@1.0.4(transitive)
+ Addedhandlebars@1.3.0(transitive)
- Removedhandlebars@1.0.12(transitive)
Updatedhandlebars@^1.3.0