npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

grunt-handlebars-universal

v0.1.0

Published

A grunt task to compile handlebars templates into universal AMD/CommonJS modules.

Downloads

5

Readme

grunt-handlebars-universal

GHU is a plugin for the Grunt task runner for pre-compiling Handlebars templates.

Why make another Grunt Handlebars plugin? Because none of the other ones I found did what I actually wanted, which is create templates that could be used in any environment. grunt-contrib-handlebars, for example, will let you compile with wrappers for either CommonJS loading, AMD loading, or a global namespace. I wanted to be able to compile to all three in the same file.

GHU does not do any special processing for partials. It is up to you to register your templates as partials within your own environment.

GHU does not support multiple templates per file. If you need multiple template files, it is recommended that you use grunt-contrib-concat or grunt-requirejs to combine this plugin's output.

Getting Started

This plugin requires Grunt ~0.4.0

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-handlebars-universal --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

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

Handlebars task

Run this task with the grunt handlebars command.

Task targets, files and options may be specified according to the grunt Configuring tasks guide.

Options

namespace

Type: String or false or function
Default: 'HandlebarsTemplates'

The namespace in which the precompiled template will be assigned. Use dot notation (e.g. App.Templates) for nested namespaces or false for no namespace wrapping.

Example:

options: {
  namespace: 'MyApp.Templates'
}

You can generate nested namespaces based on the file system paths of your templates by providing a function. The function will be called with one argument (the template filepath). The function must return a dot notation based on the filepath.

Example:

options: {
  namespace: function(filename) {
    var names = filename.replace(/modules\/(.*)(\/\w+\.hbs)/, '$1');
    return names.split('/').join('.');
  },
  files: {
    'ns_nested_tmpls.js' : [ 'modules/**/*.hbs']
  }
}

global

Type: Boolean
Default: 'this'

Defines the variable name for the global context that the namespace will be applied to within the template.

amd

Type: Boolean
Default: true

Wraps the compiled template with an AMD define function. The template wrapper will require Handlebars itself in order to prepare a ready-to-use template function.

node

Type: Boolean
Default: true

Wraps the compiled template with a CommonJS module export. The template wrapper will require Handlebars directly in order to prepare a ready-to-use template function.

nodePassIn

Type: Boolean
Default: false

By default the compiled templates will load Handlebars internally and return a template function. Depending upon node_modules structure, this may result in a node.js process passing in a different Handlebars instance than the module your code registers partials and helpers on. Setting this option to true will cause the wrapper to instead generate a closure that you will need to call to get the ready to use template, passing in a specific Handlebars instance.

Example:

var handlebars = require('handlebars');
var template = require('path/to/compiled/template.js')(handlebars);
// template now contains a ready-to-use function

Usage Examples

handlebars: {
  compile: {
    options: {
      namespace: "app.templates"
    },
    files: [{
      expand: true,
      cwd: 'views/',
      src: [
        '**/*.hbs.html',
      ],
      dest: 'views/',
      ext: '.hbs.js'
    }]
  }
}

##License and Accreditation

grunt-handlebars-universal is released under a standard MIT license, as defined in the included LICENSE file.

grunt-handlebars-universal is copyrighted 2014 by Jarvis Badgley.