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

webpack-compile-templates

v0.3.0

Published

Compiles templates from HTML script tags into CommonJS modules in a webpack-loader

Downloads

146

Readme

#webpack-compile-templates

Webpack version of this.

Compiles underscore templates from HTML script tags into CommonJS in a webpack loader. You can add multiple script tags to each file. Requiring the file will return an object with a property for each script tag. The ID attribute is the key and the compiled template function as the value.

See the underscore documentation for more details.

Usage

Install

npm install --save-dev webpack-compile-templates

Create a template file

myTemplates.html

Use the ID attribute to identify the template from your JS source. Use data-variable-name to change the variable name that is used in the underscore template. obj is the default

<script type="text/template" id="template1">
	<h2><%- obj.title %></h2>
</script>

<script type="text/template" id="template2" data-variable-name="data">
	<li><%- data.name %> <<%- data.email %>></li>
</script>

Require the template file

A JS file

var $ = require('jquery');
var templates = require('/path/to/myTemplates');

$('.container').html(templates.template1({ title: 'My Page Title' }));
$('.container').append(templates.template2({
	name: 'Rob',
	email '[email protected]'
}));

Add to webpack

Register the template and tell webpack to look for html extensions

 loaders: [
            { test: /\.html$/, loader: 'webpack-compile-templates' },

Why?

The advantage of this transform over other transforms or plugins is that the templates are backwards compatible with non-CommonJS code. Template files formatted this way can also be included directly in HTML. A UMD module that is shared in both a CommonJS and non-commonJS codebase can be used like this:

(function (root, factory) {
    if (typeof module !== 'undefined') {
        // CommonJS - templates are precompiled and bundled in with JS
        var templates = require('/path/to/myTemplates');
        factory(
            module,
            templates.template1,
            templates.tempalte2
        );
    } else {
        // Browser globals. Templates are included in html and need to be compiled client-side
        var $template1 = $('#template1');
        var $tempalte2 = $('#template2');
        factory(
            _module,
            _.template($template1.html(), null, {variable: $template.attr('data-variable-name')),
            _.template($template2.html(), null, {variable: $template.attr('data-variable-name'))
        );
    }
}(window || global, function (module, template1, template2) {
// Use compliled templates in here
}));

The commonJS environment gets the benefit of the precompiled template. Other environments can still include the file on the page and access it by ID.