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

re-define-umd-template

v0.0.5

Published

`n` attempt to embrace all module system in one template.

Downloads

5

Readme

UMD template

n attempt to embrace all module system in one template.

Requirements

  • support for amd AND global AND cjs when available
  • handling async nature of requirejs
  • do not register any global when dealing with node
  • resolve dependency based upon one system scope, if global take global, if amd take am...

Caveats (some obvious facts)

  • module not available via globalorCJS - when you requires a module which only is reachable from requirejs then you need to require your main in requirejs manner in order to keep order in async libs.

Examples

Check how it deals with globals; requirejs; cjs

Template in action A, B

Grunt with re-define

  grunt.initConfig({
    redefine: {
      options: { 
        wrappers: { 
          'umd-template': require('re-define-umd-template')
        }
      , wrapper: 'umd-template'
  ....

Template

with async support

(function (parent, factory){
  var _instance

  //PARAMS
  var externals = ['jquery', 'lodash']
    , globals = { jquery: $, lodash: _ }
    , exports = { amd: 'MODULE-A', global: 'MODULE-A' }
    , registerGlobal = function() {
        /**
         * a.b.c => var a = a || {}; a.b = a.b || {}; a.b.c = _instance
         **/
        parent[ exports.global ] = _instance
    }

  var hasAMD = typeof define === 'function' && define.amd
    , hasCJS = typeof module === 'object' || typeof exports === 'object'
    , hasWindow = typeof window != 'undefined'
    , hasRequire = typeof require == 'function'

  var amdDeps = []
    , globalDeps = []
    , cjsDeps = []
    , deps = []

  //Only for node
  if(!hasWindow) {
    cjsDeps = exports
    initFactory()
    registerCJS()
    return
  }

  //Check dependency availability whether is registered as amd,global or cjs
  for(var i = 0; i < exports.length; i++) {
    var name = exports[i]
      , _name = globals[name] || name
      , dep 

    if(dep = (_find(parent, _name) || _find(window, _name))) {
      globalDeps.push(_name)

      deps[i] = dep
      continue
    }

    if(!hasAMD && hasRequire) {
      cjsDeps.push(name)
      deps[i] = require(name)
      continue
    }

    if(!hasAMD) throw new Error('Module does not exists within any known module system ' + name)

    amdDeps.push(name)
  }
  
  _initFactory()
  _registerAMD()
  _registerCJS()
  _registerGlobal()

  function _registerAMD() {
    if (!hasAMD) return

    if (!amdDeps.length) {
      define(exports.amd, function() { return _instance })
      return
    }

    //Load missing dependencies
    require(amdDeps, function() {
      var asyncDeps = arguments
        , current = 0

      //After all dependencies are loaded - register a module
      define(exports.amd, function() { 
        //Fill missing dependencies with right async instances
        //[dep, undefined, dep, undefined] -> [dep, arguments[0], dep, arguments[1]]
        for(var i = 0; i < exports.length; i++) {
          if(typeof deps[i] == 'undefined') {
            deps[i] = asyncDeps[current]
            current++
          }
        }

        amdDeps = []

        _initFactory()
        _registerGlobal()
        _registerCJS()

        return _instance
      })
    })
  }

  function _registerCJS() { 
    if (!amdDeps.length && hasCJS)
      module.exports = _instance 
  }
  
  //resolving long dots path, like window.foo.baz['bar']
  function _find(parent, path) {
    var _d, _p, k;
    _p = path.match(/([\w|\-\_]+)/g)
    for(k = 0; k < _p.length; k++) { 
      if(k == 0) { _d = parent[_p[k]] }
      else { _d && (_d = _d[_p[k]]) }
    }
    return _d
  }

  function _registerGlobal() {
    if(!amdDeps.length && hasWindow) 
      _registerGlobal && _registerGlobal()
  }

  function _initFactory() { 

    isReady() && (_instance = factory.apply(null, deps))

    function isReady() {
      var ready = true
      for(var i = 0; i < args.length; i++) {
        if(!ready) break
        ready = typeof deps[i] != 'undefined'
      }
      return ready
    }
  }
  })(this, function (jquery, lodash) {
  
  return EXPORT_YOUR_SPELLS
})
sync only
  • register: amd OR global' or cjs`
  • when exporting global or cjs then all dependencies needs to be available in global scope or via requirejs, amd deps are omitted completely here.
(function (parent, factory){
  var __f

  if (typeof exports === 'object')
    module.exports = __f = factory(require('jquery'), require('lodash'))

  if (typeof window != 'undefined') {
    var hasAMD = typeof define === 'function' && define.amd

    __f = __f || factory(this.$, this._) 
    parent["MODULE-A"] = __f

    if (hasAMD) define('MODULE-A', function() { return __f })
  }
  })(this, function (jquery, lodash) {

  return EXPORT_YOUR_SPELLS

})