Repository: umdjs/umd
Branch: master
Commit: 293fd4c57525
Files: 15
Total size: 22.8 KB
Directory structure:
gitextract_5eh5xk9i/
├── .gitignore
├── .travis.yml
├── CHANGES
├── LICENSE.md
├── README.md
├── package.json
└── templates/
├── amdWeb.js
├── amdWebGlobal.js
├── commonjsAdapter.js
├── commonjsStrict.js
├── commonjsStrictGlobal.js
├── jqueryPlugin.js
├── nodeAdapter.js
├── returnExports.js
└── returnExportsGlobal.js
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitignore
================================================
# Created by https://www.gitignore.io/api/node
### Node ###
# Logs
logs
*.log
npm-debug.log*
# Runtime data
pids
*.pid
*.seed
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# node-waf configuration
.lock-wscript
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Dependency directory
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
node_modules
================================================
FILE: .travis.yml
================================================
language: node_js
node_js:
- "stable"
sudo: false
================================================
FILE: CHANGES
================================================
v2.0.0
* jQuery is now provided by only one template. This template normalizes how
modules built with it are used in CommonJS environments:
require('./myJqueryPlugin')(window, [jQuery]);
Providing window is mandated. This is because jQuery upstream,
1. Sometimes returns a factory, and only sometimes.
2. We *always* have access to window.
3. It makes the use of window explicit.
4. If jQuery ever provides access to the factory we are forward-compatable.
For more information on this, read
* http://stackoverflow.com/q/33404108/124486
* https://github.com/umdjs/umd/issues/68
================================================
FILE: LICENSE.md
================================================
The MIT License (MIT)
Copyright (c) 2014 the [UMD contributors](https://github.com/umdjs/umd/graphs/contributors).
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
================================================
FILE: README.md
================================================
## UMD (Universal Module Definition)
[](https://travis-ci.org/umdjs/umd)
This repository formalizes the design and implementation of the Universal Module Definition (UMD) API for JavaScript modules. These are modules which are capable of working everywhere, be it in the client, on the server or elsewhere.
The UMD pattern typically attempts to offer compatibility with the most popular script loaders of the day (e.g RequireJS amongst others). The repository aims to formalize the *definition* of *universal modules*. It does not provide a *universal definition*. The standard was written at the time because everyone defined their universal modules differently - there were some common approaches, with varying support for different environments, but nothing really established.
A universal module, as already noted, works in multiple environments. Before universal modules, libraries would distribute separate files for the different environments, e.g. `library.amd.js`, `library.common.js` and `library.global.js`. This was a hassle for maintainers, since it required the usage of a build tool (which JavaScript <s>does</s> did generally not need) and extra documentation. So the aim was to define a universal module, a single file, which was all that was needed for the author to write and distribute and for the user to (down)load. Notice this was before package managers and repositories were common (or: when they started to become more popular, and you had to change your library into a module to support them).
Yet, there were still variations. Modules written for web browsers did not need to consider supporting the commonjs format, as they wouldn't work in Node.JS anyway. Modules that had no dependencies wouldn't need a UMD pattern that supported `require`. Modules that did not use circular dependencies wouldn't need a pattern that supports a mutable `exports` object. You can find the explanation of these differences, and their trade-offs, in the comments documenting the patterns below. A library author can read through them and pick the right one for their supported environments.
In many cases, UMD uses [AMD](https://github.com/amdjs/amdjs-api/wiki/AMD) as a base, with special-casing added to handle [CommonJS](http://wiki.commonjs.org/wiki/CommonJS) compatibility.
### Variations
#### Regular Module
* [amdWeb.js](https://github.com/umdjs/umd/blob/master/templates/amdWeb.js) -
Defines a module that works with AMD and browser globals. If you also want
to export a global even when AMD is in play (useful if you are loading other
scripts that still expect that global), use
[amdWebGlobal.js](https://github.com/umdjs/umd/blob/master/templates/amdWebGlobal.js).
* [returnExports.js](https://github.com/umdjs/umd/blob/master/templates/returnExports.js) -
Defines a module that works in Node, AMD and browser globals. If you also want
to export a global even when AMD is in play (useful if you are loading other
scripts that still expect that global), use
[returnExportsGlobal.js](https://github.com/umdjs/umd/blob/master/templates/returnExportsGlobal.js).
* [commonjsStrict.js](https://github.com/umdjs/umd/blob/master/templates/commonjsStrict.js) -
Defines a module that works with more CommonJS runtimes, and for modules that
will have a circular dependency. If you also want
to export a global even when AMD is in play (useful if you are loading other
scripts that still expect that global), use
[commonjsStrictGlobal.js](https://github.com/umdjs/umd/blob/master/templates/commonjsStrictGlobal.js)
#### jQuery Plugin
* [jqueryPlugin.js](https://github.com/umdjs/umd/blob/master/templates/jqueryPlugin.js) -
Defines a jQuery plugin that works with AMD and browser globals.
#### AMD with simple Node/CommonJS adapter
These are useful for using AMD style while still making modules that can be
used in Node and installed via npm without extra dependencies to set up the
full AMD API.
This approach does not allow the use of [AMD loader plugins](https://github.com/amdjs/amdjs-api/wiki/Loader-Plugins),
just basic JS module dependencies. It also does not support the
[callback-style require](https://github.com/amdjs/amdjs-api/wiki/require) that
is usable in AMD.
* [nodeAdapter.js](https://github.com/umdjs/umd/blob/master/templates/nodeAdapter.js) -
Best for when using AMD style but want it to work in Node without a helper library
that sets up AMD.
* [commonjsAdapter.js](https://github.com/umdjs/umd/blob/master/templates/commonjsAdapter.js) -
Similar to nodeAdapter.js, but compatible with more CommonJS runtimes, and if
you want to define a circular dependency.
### Tooling
#### Build tools
* [docpad-plugin-umd](https://github.com/docpad/docpad-plugin-umd) is a [DocPad](http://docpad.org) plugin for surrounding JavaScript code with UMD boilerplate
* [grunt-umd](https://github.com/alexlawrence/grunt-umd) is a [Grunt](http://gruntjs.com) task for surrounding JavaScript code with UMD boilerplate
* [gulp-umd](https://github.com/eduardolundgren/gulp-umd) is a [Gulp](http://gulpjs.com/) task for surrounding JavaScript code with UMD boilerplate
* [grunt-urequire](https://github.com/aearly/grunt-urequire) is a Grunt wrapper for [uRequire](https://github.com/anodynos/uRequire) a conversion tool for universal JavaScript modules.
* [generator-umd](https://github.com/ruyadorno/generator-umd) is an Yeoman Generator that creates a single module project with UMD boilerplate
#### Testing
* [Unit testing UMD with grunt-contrib-jasmine](http://stackoverflow.com/questions/16940548/grunt-test-for-umd)
### Resources
* [Browserify and the Universal Module Definition](http://dontkry.com/posts/code/browserify-and-the-universal-module-definition.html)
### Todos
* noConflict. Although with AMD loaders and build tools, it should be possible to get version specific bindings,
maybe show a version that has a noConflict option.
* Variation with attaching some functionality to a $ impersonator. Although, it is
tempting to say for that case, ask for 'jquery' as a dependency, and if the developer
wants to use something different than the actual 'jquery', map that file to the 'jquery' name.
That is one of the strengths of module names, they can be mapped to different implementations.
* Usage examples
* Further justifications for usage
* Gotchas/custom-tweaks we're aware of, but would rather not apply to the default UMD boilerplate
### Influences
The basic pattern for the UMD variations in this repository was derived from the approach [@kriskowal](https://github.com/kriskowal) used for the [Q promise library](https://github.com/kriskowal/q).
Earlier UMD variations were also of influence, ranging from Kit-Cambridge's
[UMD](https://gist.github.com/1251221), through to [patterns](https://github.com/addyosmani/jquery-plugin-patterns/issues/1) discussed by Addy Osmani, Thomas Davis and Ryan Florence and most recently the UMD patterns proposed by [James Burke](https://gist.github.com/1262861).
### License
Copyright (c) the UMD contributors
Licensed under the [MIT License](https://github.com/umdjs/umd/blob/master/LICENSE.md).
================================================
FILE: package.json
================================================
{
"name": "umdjs",
"version": "2.0.0",
"description": "UMD (Universal Module Definition)",
"main": "amdWeb.js",
"repository": {
"type": "git",
"url": "https://github.com/umdjs/umd"
},
"keywords": [
"umd",
"umdjs",
"universal",
"module",
"definition"
],
"author": "The UMD Authors",
"license": "MIT",
"bugs": {
"url": "https://github.com/umdjs/umd/issues"
},
"homepage": "https://github.com/umdjs/umd",
"scripts": {
"test": "npm run lint",
"lint": "jshint templates/*.js"
},
"devDependencies": {
"jshint": "^2.8.0"
},
"dependencies": {
"jquery": "^2.1.4"
}
}
================================================
FILE: templates/amdWeb.js
================================================
// Uses AMD or browser globals to create a module.
// If you want something that will also work in Node, see returnExports.js
// If you want to support other stricter CommonJS environments,
// or if you need to create a circular dependency, see commonJsStrict.js
// Defines a module "amdWeb" that depends on another module called "b".
// Note that the name of the module is implied by the file name. It is best
// if the file name and the exported global have matching names.
// If the 'b' module also uses this type of boilerplate, then
// in the browser, it will create a global .b that is used below.
// If you do not want to support the browser global path, then you
// can remove the `root` use and the passing of `this` as the first arg to
// the top function.
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['b'], factory);
} else {
// Browser globals
root.amdWeb = factory(root.b);
}
}(typeof self !== 'undefined' ? self : this, function (b) {
// Use b in some fashion.
// Just return a value to define the module export.
// This example returns an object, but the module
// can return a function as the exported value.
return {};
}));
================================================
FILE: templates/amdWebGlobal.js
================================================
// Uses AMD or browser globals to create a module. This example creates a
// global even when AMD is used. This is useful if you have some scripts
// that are loaded by an AMD loader, but they still want access to globals.
// If you do not need to export a global for the AMD case, see amdWeb.js.
// If you want something that will also work in Node, and still export a
// global in the AMD case, see returnExportsGlobal.js
// If you want to support other stricter CommonJS environments,
// or if you need to create a circular dependency, see commonJsStrictGlobal.js
// Defines a module "amdWebGlobal" that depends another module called "b".
// Note that the name of the module is implied by the file name. It is best
// if the file name and the exported global have matching names.
// If the 'b' module also uses this type of boilerplate, then
// in the browser, it will create a global .b that is used below.
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['b'], function (b) {
// Also create a global in case some scripts
// that are loaded still are looking for
// a global even when an AMD loader is in use.
return (root.amdWebGlobal = factory(b));
});
} else {
// Browser globals
root.amdWebGlobal = factory(root.b);
}
}(typeof self !== 'undefined' ? self : this, function (b) {
// Use b in some fashion.
// Just return a value to define the module export.
// This example returns an object, but the module
// can return a function as the exported value.
return {};
}));
================================================
FILE: templates/commonjsAdapter.js
================================================
// Defines a module that works in CommonJS and AMD.
// This version can be used as common boilerplate for a library module
// that you only want to expose to CommonJS and AMD loaders. It will not work
// well for defining browser globals.
// If you only want to target Node and AMD or a CommonJS environment that
// supports assignment to module.exports and you are not defining a module
// that has a circular dependency, see nodeAdapter.js
// Help Node out by setting up define.
if (typeof exports === 'object' && typeof exports.nodeName !== 'string' && typeof define !== 'function') {
var define = function (factory) {
factory(require, exports, module);
};
}
define(function (require, exports, module) {
var b = require('b');
// Only attach properties to the exports object to define
// the module's properties.
exports.action = function () {};
});
================================================
FILE: templates/commonjsStrict.js
================================================
// Uses CommonJS, AMD or browser globals to create a module.
// If you just want to support Node, or other CommonJS-like environments that
// support module.exports, and you are not creating a module that has a
// circular dependency, then see returnExports.js instead. It will allow
// you to export a function as the module value.
// Defines a module "commonJsStrict" that depends another module called "b".
// Note that the name of the module is implied by the file name. It is best
// if the file name and the exported global have matching names.
// If the 'b' module also uses this type of boilerplate, then
// in the browser, it will create a global .b that is used below.
// If you do not want to support the browser global path, then you
// can remove the `root` use and the passing `this` as the first arg to
// the top function.
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['exports', 'b'], factory);
} else if (typeof exports === 'object' && typeof exports.nodeName !== 'string') {
// CommonJS
factory(exports, require('b'));
} else {
// Browser globals
factory((root.commonJsStrict = {}), root.b);
}
}(typeof self !== 'undefined' ? self : this, function (exports, b) {
// Use b in some fashion.
// attach properties to the exports object to define
// the exported module properties.
exports.action = function () {};
}));
================================================
FILE: templates/commonjsStrictGlobal.js
================================================
// Uses CommonJS, AMD or browser globals to create a module. This example
// creates a global even when AMD is used. This is useful if you have some
// scripts that are loaded by an AMD loader, but they still want access to
// globals. If you do not need to export a global for the AMD case, see
// commonjsStrict.js.
// If you just want to support Node, or other CommonJS-like environments that
// support module.exports, and you are not creating a module that has a
// circular dependency, then see returnExportsGlobal.js instead. It will allow
// you to export a function as the module value.
// Defines a module "commonJsStrictGlobal" that depends another module called
// "b". Note that the name of the module is implied by the file name. It is
// best if the file name and the exported global have matching names.
// If the 'b' module also uses this type of boilerplate, then
// in the browser, it will create a global .b that is used below.
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['exports', 'b'], function (exports, b) {
factory((root.commonJsStrictGlobal = exports), b);
});
} else if (typeof exports === 'object' && typeof exports.nodeName !== 'string') {
// CommonJS
factory(exports, require('b'));
} else {
// Browser globals
factory((root.commonJsStrictGlobal = {}), root.b);
}
}(typeof self !== 'undefined' ? self : this, function (exports, b) {
// Use b in some fashion.
// attach properties to the exports object to define
// the exported module properties.
exports.action = function () {};
}));
================================================
FILE: templates/jqueryPlugin.js
================================================
// Uses CommonJS, AMD or browser globals to create a jQuery plugin.
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else if (typeof module === 'object' && module.exports) {
// Node/CommonJS
module.exports = function( root, jQuery ) {
if ( jQuery === undefined ) {
// require('jQuery') returns a factory that requires window to
// build a jQuery instance, we normalize how we use modules
// that require this pattern but the window provided is a noop
// if it's defined (how jquery works)
if ( typeof window !== 'undefined' ) {
jQuery = require('jquery');
}
else {
jQuery = require('jquery')(root);
}
}
factory(jQuery);
return jQuery;
};
} else {
// Browser globals
factory(jQuery);
}
}(function ($) {
$.fn.jqueryPlugin = function () { return true; };
}));
================================================
FILE: templates/nodeAdapter.js
================================================
// Defines a module that works in Node and AMD.
// This version can be used as common boilerplate for a library module
// that you only want to expose to Node and AMD loaders. It will not work
// well for defining browser globals.
// If you need a version of this file that works CommonJS-like environments
// that do not support module.exports or if you want to define a module
// with a circular dependency, see commonjsAdapter.js
(function(define) {
define(function (require, exports, module) {
var b = require('b');
return function () {};
});
}( // Help Node out by setting up define.
typeof module === 'object' && module.exports && typeof define !== 'function' ?
function (factory) { module.exports = factory(require, exports, module); } :
define
));
================================================
FILE: templates/returnExports.js
================================================
// Uses Node, AMD or browser globals to create a module.
// If you want something that will work in other stricter CommonJS environments,
// or if you need to create a circular dependency, see commonJsStrict.js
// Defines a module "returnExports" that depends another module called "b".
// Note that the name of the module is implied by the file name. It is best
// if the file name and the exported global have matching names.
// If the 'b' module also uses this type of boilerplate, then
// in the browser, it will create a global .b that is used below.
// If you do not want to support the browser global path, then you
// can remove the `root` use and the passing `this` as the first arg to
// the top function.
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['b'], factory);
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory(require('b'));
} else {
// Browser globals (root is window)
root.returnExports = factory(root.b);
}
}(typeof self !== 'undefined' ? self : this, function (b) {
// Use b in some fashion.
// Just return a value to define the module export.
// This example returns an object, but the module
// can return a function as the exported value.
return {};
}));
// if the module has no dependencies, the above pattern can be simplified to
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.returnExports = factory();
}
}(typeof self !== 'undefined' ? self : this, function () {
// Just return a value to define the module export.
// This example returns an object, but the module
// can return a function as the exported value.
return {};
}));
================================================
FILE: templates/returnExportsGlobal.js
================================================
// Uses Node, AMD or browser globals to create a module. This example creates
// a global even when AMD is used. This is useful if you have some scripts
// that are loaded by an AMD loader, but they still want access to globals.
// If you do not need to export a global for the AMD case,
// see returnExports.js.
// If you want something that will work in other stricter CommonJS environments,
// or if you need to create a circular dependency, see commonJsStrictGlobal.js
// Defines a module "returnExportsGlobal" that depends another module called
// "b". Note that the name of the module is implied by the file name. It is
// best if the file name and the exported global have matching names.
// If the 'b' module also uses this type of boilerplate, then
// in the browser, it will create a global .b that is used below.
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['b'], function (b) {
return (root.returnExportsGlobal = factory(b));
});
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory(require('b'));
} else {
// Browser globals
root.returnExportsGlobal = factory(root.b);
}
}(typeof self !== 'undefined' ? self : this, function (b) {
// Use b in some fashion.
// Just return a value to define the module export.
// This example returns an object, but the module
// can return a function as the exported value.
return {};
}));
gitextract_5eh5xk9i/
├── .gitignore
├── .travis.yml
├── CHANGES
├── LICENSE.md
├── README.md
├── package.json
└── templates/
├── amdWeb.js
├── amdWebGlobal.js
├── commonjsAdapter.js
├── commonjsStrict.js
├── commonjsStrictGlobal.js
├── jqueryPlugin.js
├── nodeAdapter.js
├── returnExports.js
└── returnExportsGlobal.js
Condensed preview — 15 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (25K chars).
[
{
"path": ".gitignore",
"chars": 591,
"preview": "# Created by https://www.gitignore.io/api/node\n\n### Node ###\n# Logs\nlogs\n*.log\nnpm-debug.log*\n\n# Runtime data\npids\n*.pid"
},
{
"path": ".travis.yml",
"chars": 52,
"preview": "language: node_js\nnode_js:\n - \"stable\"\nsudo: false\n"
},
{
"path": "CHANGES",
"chars": 616,
"preview": "\nv2.0.0\n\n\t* jQuery is now provided by only one template. This template normalizes how\n\t modules built with it are used "
},
{
"path": "LICENSE.md",
"chars": 1140,
"preview": "The MIT License (MIT)\n\nCopyright (c) 2014 the [UMD contributors](https://github.com/umdjs/umd/graphs/contributors).\n\nPer"
},
{
"path": "README.md",
"chars": 7191,
"preview": "## UMD (Universal Module Definition)\n\n[](https://travis-ci.org/umdjs"
},
{
"path": "package.json",
"chars": 644,
"preview": "{\n \"name\": \"umdjs\",\n \"version\": \"2.0.0\",\n \"description\": \"UMD (Universal Module Definition)\",\n \"main\": \"amdWeb.js\",\n"
},
{
"path": "templates/amdWeb.js",
"chars": 1293,
"preview": "// Uses AMD or browser globals to create a module.\n\n// If you want something that will also work in Node, see returnExpo"
},
{
"path": "templates/amdWebGlobal.js",
"chars": 1681,
"preview": "// Uses AMD or browser globals to create a module. This example creates a\n// global even when AMD is used. This is usefu"
},
{
"path": "templates/commonjsAdapter.js",
"chars": 889,
"preview": "// Defines a module that works in CommonJS and AMD.\n\n// This version can be used as common boilerplate for a library mod"
},
{
"path": "templates/commonjsStrict.js",
"chars": 1501,
"preview": "// Uses CommonJS, AMD or browser globals to create a module.\n\n// If you just want to support Node, or other CommonJS-lik"
},
{
"path": "templates/commonjsStrictGlobal.js",
"chars": 1704,
"preview": "// Uses CommonJS, AMD or browser globals to create a module. This example\n// creates a global even when AMD is used. Thi"
},
{
"path": "templates/jqueryPlugin.js",
"chars": 1145,
"preview": "// Uses CommonJS, AMD or browser globals to create a jQuery plugin.\n\n(function (factory) {\n if (typeof define === 'fu"
},
{
"path": "templates/nodeAdapter.js",
"chars": 799,
"preview": "// Defines a module that works in Node and AMD.\n\n// This version can be used as common boilerplate for a library module\n"
},
{
"path": "templates/returnExports.js",
"chars": 2360,
"preview": "// Uses Node, AMD or browser globals to create a module.\n\n// If you want something that will work in other stricter Comm"
},
{
"path": "templates/returnExportsGlobal.js",
"chars": 1701,
"preview": "// Uses Node, AMD or browser globals to create a module. This example creates\n// a global even when AMD is used. This is"
}
]
About this extraction
This page contains the full source code of the umdjs/umd GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 15 files (22.8 KB), approximately 5.7k tokens. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.