GithubHelp home page GithubHelp logo

sj's Introduction

sj

This module was an experiment to provide declarative require in the DOM markup.

Example

This markup will install google analytics script on your page.

<!DOCTYPE html>
<html>
  <head>
    <title>Declarative require from html</title>
  </head>
  <body xmlns:s="require:./googleAnalytics">
    <s:analytics domain='my.site.com' ua='UA-XXXXXXXX-1'/>

    <script src="bundle.js"></script>
  </body>
</html>

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:
module.exports.analytics = function (root) {
  var domain = root.attributes.getNamedItem('domain').nodeValue;
  var ua = root.attributes.getNamedItem('ua').nodeValue;

  // this is standard google analytics include script:
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  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.

install

With npm do:

npm install sj

license

MIT

sj's People

Contributors

anvaka avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.