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

rollup-plugin-underscorify

v1.0.0

Published

A rollup plugin to convert underscore templates into modules

Downloads

19

Readme

rollup-plugin-underscorify

Introduction

The plugin was written for Backbone- and Marionette-based applications and converts underscore static templates into template function modules.

Installation

npm install rollup-plugin-underscorify --save

Build example

/* rollup.config.js */

import underscorify from 'rollup-plugin-underscorify';

export default {
  entry: 'index.js',
  plugins: [
    underscorify({
      include: ['**/*.tpl'],
      exclude: ['**/some-other-tpl-file.tpl'],
      variable: 'p'
    })
  ]
};

Plugin options

  • include: specifies a minimatch pattern to determine the template files that are converted to underscore templates (default: ['**/*.tpl'])

  • exclude: specifies a minimatch pattern to determine the template files that are ignored by the plugin (default: undefined)

  • variable: sets a namespace variable that is used within a template function to access other data objects passed to the function (default value: 'p' [short for parameters])

Caveats and examples

Use of namespace variable within templates is required

Compiled underscore templates use with statement internally to scope local variables to the passed data object. However, the with clause fails the ES2015 'use strict'; mode. This is why the use namespace of variable within templates (processed by this plugin) is required to asure that passed data can be correctly accessed.

For example, the following data object is passed to the template function:

let data = {
  username: 'username',
  city: 'Blacksburg, VA'
};

Within a template, username and city properties are accessed through the namespace variable (i.e., p or whatever a developer sets it to be):

<h2>Welcome <%= p.username %></h2>
<h3>Upcoming events in <a href = "..."><%= p.city %></a></h3>

Any library instance used within a template, must be passed to the template's function explicitly

Because of the way rollup and its related modules (e.g., rollup-plugin-commonjs) bundle and include imported code, there is no guarantee that import _ from 'underscore'; statement will include underscore library as either _ or underscore variable within an ES5-type function scope. If underscore or some other library is used within a template, then it must be explicitly passed to the template's function.

For example, the following template generates a list of links by iterating over a passed links collection:

<ul>
  <% p._.each(p.links, function(address, name) { %>
    <li><a href = "<%= address %>"><%= name %></a></li>
  <% }); %>
</ul>

Note that even the _ instance is prefixed with a namespace variable.

To pass, the underscore library to the template function, the following could be done:

import $        from 'jquery';
import _        from 'underscore';
import linksTpl from './links.tpl';

let tplObject = {
  _,
  links: {
    'recent blog posts': 'http://www.example.com/recent-blogs',
    'online shop': 'http://www.example.com/shop'
  }
};
let html = linksTpl(tplObject);

$('#side-bar').append(html);

When working with frameworks such as Backbone or Marionette that would invoke template functions themselves when rendering a view, either a template function, a view constructor, or a serializeData method should to be overridden (in some way) to inject required library instances into the template.