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

swig-package-tmpl-loader

v1.3.0

Published

Customized swig template filesystem loader which can load template from other packages

Downloads

8

Readme

Swig template loader

Allow you to reference npm://<package name>/filepath in your Swig template's include and import tags.

It extends swig default loader: swig.loaders.fs Swig builtin loaders

e.g.

{% import "npm://package-a/spec/template-3.html" as foo %}

{% include "npm://@scope-name/package-b/views/template-2.html" %}

{{foo.doSomething()}}

It uses require.resolve() to locate and cache package path by its name, replace npm://... with exact package path when swig loads your template

npm install swig-package-tmpl-loader
var swig = require('swig');
var templateLoader = require('swig-package-tmpl-loader');
templateLoader.swigSetup(swig); // Calls swig.setDefaults({loader: templateLoader.createLoader()});

swig.renderFile('sampleFile', {locals: {}});

Now you can split your template files to different Node modules , publish and share them with fellows.

Injection

When Working with require-injector, it supports package Name and package path injection, like IoC container does.

var rj = require('require-injector');
var tmploader = require('swig-package-tmpl-loader');
// Setup injector
var injector = rj();
injector.fromDir(process.cwd())
	.substitute('oldPackage', 'newPackage');
// Or
injector.fromDir(process.cwd())
	.swigTemplateDir('oldPackage', 'node_modules/newPackage');

// Setup Swig
tmploader.swigSetup(swig, {injector: injector});
var out = swig.renderFile('./template.html'), {locals: {}});

While in template.html

{% include "npm://oldPackage/includedTemplate.html" %}

Will actually work as {% include "npm://newPackage/includedTemplate.html" %}

API

1. swigSetup(swig, opts)

It calls swig.setDefaults({loader: templateLoader.createLoader()})
Parameters:

  • swig
    Instance of require('swig')
  • opts.memoize, True if use _.memoize() to cache require.resolve's result, default is true.
  • opts.prefix, default is npm://
  • opts.injector require-injector

2. createLoader(swig, basepath, encoding, opts)

create file system loader.
Parameter is same as what swig.loaders.fs has, except:

  • swig
    Instance of require('swig')
  • opts.memoize, True if use _.memoize() to cache require.resolve's result, default is true.
  • opts.prefix, default is npm://