Skip to content
Dependency Management got Awesome
JavaScript CoffeeScript
Pull request Compare This branch is 453 commits behind linkedin:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Inject: Dependency Management Got Awesome

Inject (Apache Software License 2.0) is a revolutionary way to manage your dependencies in a Library Agnostic way. Some of its major features include:

  • CommonJS Compliance in the Browser (exports.*)
  • Cross domain retrieval of files (via Porthole)
  • localStorage (load a module once)
  • Frustratingly Simple

Some of the awesome roadmap things coming soon(ish)

  • versioning (once we re-expose modules.* interface most likely)

Let's Start With Examples!

We have a nodejs test server for both the examples and development. Install node, npm, & coffeescript, then:

cake build
npm install
node testserver.js

You can visit http://localhost:4000/examples/ for viewing some sample code, or http://localhost:4000/tests for running our unit test suite. We use alternate ports to create the cross domain environment instead of a CDN.

Getting Started

In case you're looking: Building Inject From Source

We've put together a Getting Started With inject Guide which is a launching point to all to functionality inject has to offer. If you're already familiar with CommonJS-style modules, than you can probably start right there.

Writing CommonJS Compliant Modules

While not a requirement, the natural encapsulation CommonJS provides allows you to make only specific parts of your file available to the function that requested its injection. The local variable exports is made available as an object literal. At the terminus of your file, anything assigned to exports will be available as part of the module's package. If you want something private, simply don't export it.

A very simple module could be the following

var waterfowl = function() {};
waterfowl.prototype.quack = function() {
}; = waterfowl

If you injected this file, you could then say var duck = new and instantiate your object.

Modifying your Library to Work with Inject

Sometimes, you have a library (jQuery, or Modernizr for example) which isn't CommonJS compliant. We've put together a page of recipes for using the addRule() API with your favorite library.

JavaScript Minifiers

If you're using a JS Minifier for your module files (and you probably should), there are some important compilation options you need. Many minifiers obfuscate and optimize variables inside of functions, which will affect the ability of inject to identify your dependencies before runtime. For the greatest in-browser optimization, we recommend using UglifyJS since it allows you to protect specific reserved names while still mazimixing the amount of compression you can do. If running node isn't an option, YUI and Google compressors can also be used with the following options:

  • UglifyJS: --reserved-names “require,exports,module"
  • YUI Compressor: --nomunge
  • Google Closure: --compilation_level WHITESPACE_ONLY

If your compression engine of choice isn't on the list, and has the ability to protect/preserve certain names, send us a bug with the settings and we'll add it to the list. As a last ditch effort, you can often put an eval(); after your return statement. For most optimizers, the addition of an eval will prevent variable munging within the current scope chain.

API Notes

Path Resolution

By default, inject tries to do the best it can, but in complex environments, that's not enough. The following behaviors can change / simplify the injection of modules.

  • call require.setModuleRoot with a function if config.path resolves to a function, the function will be called instead of standard path evaluation
  • use require.addRule(match, rules) the addRule() syntax allows you to match regex statements against a module path, and resolve items dynamically

Expiring Content

By default, inject() will cache things for one day (1440 minutes). You can change the default behavior through the config object:

// files now last for one week

Setting an expiry value of "0" means that client side caching will not be used. There will need to be a balance between caching items in the browser, and letting localStorage also do caching for you. At any time, you can always clear the cache with the below code, for example if a user has not been to your site since your last major code push.


Cross Domain

In CDN-like environments, the data you need to include may be on a separate domain. If that's the case, you'll need to do 3 extra steps to get inject up and running.

  1. edit relay.html from the artifacts directory. You'll need to call require.setCrossDomain(local, remote) with the path to your two proxy files. The "local" is on the same domain as your application code. The "remote" is on the same domain as the JS you intend to load, and should be the same domain you supplied to require.setModuleRoot()
  2. edit your code use the same require.setCrossDomain(local, remote) to set up the configuration for cross domain
  3. upload both relay.html files to your servers

When you add the XD config, you'll use the same paths you used in #1 above

require.setCrossDomain("", "");

You can then carry on with your injecting. To support the cross domain, we use window.postMessage in the browsers that support it, and fall back to fragment transports with window.resize monitoring. To make that happen, we use Porthole by the awesome Ternary Labs folks (MIT License).

Also Starring

  • Porthole: Cross Domain Communication
  • lscache: LocalStorage Cache Provider
  • Google Closure Compiler
Something went wrong with that request. Please try again.