Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for documenting javascript library dependencies

So you're creating a bunch of code in an external .js file that requires jQuery and a few of its plugins, or MooTools, or perhaps some more esoteric libraries. Obviously the actual "include" is done in the host HTML page in the HEAD section as you load in each script.

But as a best practice for portability, what built-in features, or widely-adopted conventions exist within your JavaScript .js file to ensure that the next schmoe who uses your code remembers to also include those other required libraries?

I'm looking for some consensus from the developer community, so please be sure to vote for the answer that seems most common or that you are the most familiar with.

like image 897
Tom Auger Avatar asked Jun 22 '11 19:06

Tom Auger


People also ask

How do I keep JavaScript libraries up to date?

The standard method of updating packages is to use npm update , which updates all packages to the latest version that is OK according to semver. In this case, you will update Lodash to version 3.10. 1. Even though version 4.17.

What are library dependencies?

By definition, every shared library system provides a way for executables to depend on libraries, so that symbol resolution is deferred until runtime. An inter-library dependency is where a library depends on other libraries.

What are JavaScript dependencies?

A dependency is some third-party code that your application depends on. Just like a child depends on its parent, your application depends on other people's code. A piece of code becomes a true dependency when your own application cannot function without it.

What is dependency injection in JavaScript?

Dependency Injection (or DI for short) is a way of constructing your application so that you are not creating dependencies locally, but instead are being provided those dependencies from somewhere else in your application.


3 Answers

jQuery UI adds the dependencies of their widgets in the file header:

/*
* jQuery UI Effects Bounce @VERSION
*
* Copyright 2011, AUTHORS.txt (http://jqueryui.com/about)
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*
* http://docs.jquery.com/UI/Effects/Bounce
*
* Depends:
* jquery.effects.core.js
*/

Now unfortunately JavaScript dependency managers are used way less than they should, but if you can make your libraries users switch to one you wouldn't have to worry about that at all:

  • StealJS
  • Jingo
  • YUI loader
  • Pyramid Dependency Manager

Checking explicitly might be a good idea, too, since you can dynamically react if certain plugins are or are not available (e.g. either throw an exception if the jQuery UI dialog hasn't been found or just degrade gracefully and show a simple modal window):

if(!$.isFunction($.fn.dialog)) {
    throw "Could not find jQueryUI dialog. Please include jQuery UI";
}

That way your script doesn't have to break entirely if an optional dependency is not met.

like image 130
Daff Avatar answered Oct 23 '22 10:10

Daff


For the Visual Studio developers out there you may want to try blocks like these in your header

/// <reference path="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6-vsdoc.js" />
/// <reference path="thirdparty/ba-debug.js" />
/// <reference path="thirdparty/underscore.js" />

While this doesn't resolve your dependencies, it does document them, AND it gives you intellisense in Visual Studio...

See http://msdn.microsoft.com/en-us/library/bb385682.aspx, then look for References Directives (no a name or id to link directly to, sorry...)

like image 32
Oskar Austegard Avatar answered Oct 23 '22 11:10

Oskar Austegard


my js headers look like this:

////////////////////////////////////////////////////////////////////////////////
//
//  src:        www.someDomain.com/js/modules/etc
//  author:     someguy
//  date:       6-22-11
//  intent:     what is the purpose / use of this module
//  package:    prototype parent
//  requires:   jquery.1.4.js
//              fancybox
//              etc
//
////////////////////////////////////////////////////////////////////////////////

Any dependencies are then quite clear to anyone on my team, and this has proven pretty reliable. As a (hopefully) secondary measure, I will always test for those dependencies at runtime and throw up an alert should a script not be included.

like image 23
Bosworth99 Avatar answered Oct 23 '22 12:10

Bosworth99