Global Libraries
A global library is one that can be accessed from the global scope (i.e. without using any form of import).
Many libraries simply expose one or more global variables for use.
For example, if you were using jQuery, the $ variable can be used by simply referring to it:
ts
You’ll usually see guidance in the documentation of a global library of how to use the library in an HTML script tag:
html
Today, most popular globally-accessible libraries are actually written as UMD libraries (see below). UMD library documentation is hard to distinguish from global library documentation. Before writing a global declaration file, make sure the library isn’t actually UMD.
Identifying a Global Library from Code
Global library code is usually extremely simple. A global “Hello, world” library might look like this:
js
or like this:
js
When looking at the code of a global library, you’ll usually see:
- Top-level varstatements orfunctiondeclarations
- One or more assignments to window.someName
- Assumptions that DOM primitives like documentorwindowexist
You won’t see:
- Checks for, or usage of, module loaders like requireordefine
- CommonJS/Node.js-style imports of the form var fs = require("fs");
- Calls to define(...)
- Documentation describing how to requireor import the library
Examples of Global Libraries
Because it’s usually easy to turn a global library into a UMD library, very few popular libraries are still written in the global style. However, libraries that are small and require the DOM (or have no dependencies) may still be global.
Global Library Template
You can see an example DTS below:
ts