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

rv

v0.1.8

Published

RequireJS plugin to load and optimise Ractive templates

Downloads

97

Readme

rv.js

The rv plugin for RequireJS allows you to load Ractive.js templates from HTML files, and optimise them as part of your build. This means you don't need to use multiline strings in your JavaScript, or load files with AJAX or embed templates in <script> tags.

It's basically the older (and, frankly, less clever and good-looking) brother of rvc - whereas rvc can convert component files with encapsulated styles and behaviours, rv only handles templates.

You should probably check out rvc first, and come back here if you only want the template parsing stuff.

Installation

Grab rv.js or install it with npm (npm i rv) or bower (bower i rv).

Usage

First, RequireJS needs to be able to find rv.js and ractive.js. Either they should be in the root of your project (or whatever baseUrl is configured to be), or you'll need to set up the paths config (obviously, change the paths as appropriate):

require.config({
  paths: {
    ractive: 'lib/ractive',
    rv: 'plugins/rv'
  }
});

Once RequireJS is configured, you can import templates like so:

// At the top-level of your app, e.g. inside your main.js file
require([ 'ractive', 'rv!template' ], function ( Ractive, parsedTemplate ) {
  var ractive = new Ractive({
    el: 'body',
    template: parsedTemplate
  });
});

// Inside a module
define([ 'ractive', 'rv!template' ], function ( Ractive, parsedTemplate ) {
  var MyView = Ractive.extend({
    template: parsedTemplate
  });

  return MyView;
});

Note that the .html file extension is omitted - this is assumed.

Template paths work just like regular module paths, so they can be relative (rv!../template), or below an entry in the paths config:

require.config({
  paths: {
    ractive: 'lib/ractive',
    rv: 'plugins/rv',
    templates: 'path/to/ractive_templates'
  }
});

require([ 'ractive', 'rv!templates/foo' ], function ( Ractive, fooTemplate ) {
  var ractive = new Ractive({
    el: 'body',
    template: fooTemplate
  });
});

Optimisation

The great feature of RequireJS is that while you can develop your app without having to rebuild it every time you change a file, you can also bundle it into a single file for production using the optimiser.

In addition to this 'inlining' of your templates, rv will parse them so that no additional computation needs to happen in the browser.

Once your project is optimised, you don't need the plugin itself, so add rv to the stubModules option:

// optimiser config
{
  paths: {
    ractive: 'lib/ractive',
    rv: 'plugins/rv'
  },
  stubModules: [ 'rv' ]
}

Consult the documentation for more information on using the optimiser.

Using Ractive Runtime

Because rv parses your templates during the build phase it is not necessary to include Ractive's parse method in the built script. A version of Ractive that does not include the parse method is included in the Ractive directory under the name of ractive.runtime.js. By building with this file you can save 15KB or so of code.

You need to include both ractive and ractive.runtime in your RequireJS configuration.

require.config({
  paths: {
    rv: 'plugins/rv',
    ractive: 'lib/ractive', // The original Ractive library - used by the rv plugin
    ractivejs: "lib/ractive.runtime" // The runtime version of Ractive - this is the path you should use in your require statements
  }
});

You should then always use the ractivejs path when loading modules (the original ractive path is only there for the rv plugin):

require([ 'ractivejs' ], function ( Ractive ) {
  var ractive = new Ractive({
    el: 'body',
    template: "<h1>Hello world</h1>"
  });
});

You should also stub the ractive module in the build.js file, otherwise both versions will be included in the final script:

  stubModules: [ 'rv', 'ractive' ]

Changelog

  • 0.1.6 - rvc.js and rv.js now live in separate repos. Switch to using toSource for rv.js instead of JSON.stringify
  • 0.1.5 - rewrote loader to use ractivejs/rcu, plus toSource by marcello3d. CSS is now minified (a bit) on build.
  • 0.1.4 - switched from text plugin to Guy Bedford's [amd-loader][https://github.com/guybedford/amd-loader] plugin, and added the rvc.js loader
  • 0.1.3 - file extension bug fix
  • 0.1.2 - Updated to use Ractive 0.3.0 API
  • 0.1.1 - renamed. Anglebars is now Ractive
  • 0.1.0 - first version

Credits

Many thanks to Guy Bedford for creating amd-loader, and Marcello Bastéa-Forte for tosource.

License

MIT.