node package manager
Painless code sharing. npm Orgs help your team discover, share, and reuse code. Create a free org »



This module provides declarative require in your DOM markup.


This markup will install google analytics script on your page.

<!DOCTYPE html>
    <title>Declarative require from html</title>
  <body xmlns:s="require:./googleAnalytics">
    <s:analytics domain='' ua='UA-XXXXXXXX-1'/>
    <script src="bundle.js"></script> 

Notice special namespace on our body tag? When sj parses xml it finds all custom prefixes and checks if their URN starts with require:XXX. This gives it a hint where to look actual analytics export when it comes across s:analytics tag. In this case it's relative file, googleAnalytics.js:

// googleAnalytics.js: = function (root) {
  var domain = root.attributes.getNamedItem('domain').nodeValue;
  var ua = root.attributes.getNamedItem('ua').nodeValue;
  // this is standard google analytics include script: 
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  ga('create', ua, domain); // except that we pass our own arguments here 
  ga('send', 'pageview');

To make this all work we browserify our input files into a bundle:

sj index.html > bundle.js

Under the hood sj binary finds all xmlns requires, and uses browserify to create a bundle.

Note: sj runtime is super tiny. As of this writing it's less than 100 lines of unminified code. This entire google analytics example is only 121 lines long, unminified. That maps to 2KB of gzipped, unminified code. Check network requests on this demo page: google analytics tag.

With this tiny size it gives you incredible power: Anyone can use custom dom elements via simple npm install ... command.


With npm do:

npm install sj