Undefine
Universal Module Loader, for node.js, browser scripts, bundles, and AMD define loaders
< 1 kB minified gzip, does not reinvent the wheel, if you need a dynamic AMD script loader pick one, undefine cooperates.
- Start module with
( this.undefine || require( 'undefine' )( module, require ) )()
- Define module using named AMD signature
( 'fibonacci', [], function() {} )
- Require anywhere
![NPM version](https://badge.fury.io/js/undefine.png)
Stability: Feature complete, used by toubkal, needs CI testing.
Example
Fibonacci number calculator module
( this.undefine || require( 'undefine' )( module, require ) )()
( 'fibonacci', [], function() {
return fibonacci;
function fibonacci( n ) {
if ( n < 0 ) return;
if ( n < 2 ) return n;
return fibonacci( n - 2 ) + fibonacci( n - 1 );
}
} )
Require fibonacci.js
var fibonacci = require( 'fibonacci' );
var n = 10;
console.log( 'fibonacci( ' + n + ' ) =', fibonacci( n ) );
<script src="undefine.js"></script>
<script src="fibonacci.js"></script>
<script>
require( [ 'fibonacci' ], function( fibonacci ) {
var n = 10;
console.log( 'fibonacci( ' + n + ' ) =', fibonacci( n ) );
}
</script>
fibonacci.js loaded dynamically:
<script src="require.js"></script>
<script src="undefine.js"></script>
<script>
require( [ './fibonacci' ], function( fibonacci ) {
var n = 10;
console.log( 'fibonacci( ' + n + ' ) =', fibonacci( n ) );
} )
</script>
Also works with curl-amd.
How does it work?
In first line of module:
( this.undefine || require( 'undefine' )( module, require ) )()
- this.undefine returns the function window.undefine( options ) in the browser,
undefined in Node
- In Node require( 'undefine' )( module, require ) returns a function
undefine( options ) bound to Node module and require
- In both Node and the browser undefine( options ) is evaluated and
returns a define() function with the same signature as
AMD define
The module definition is then equivalent to:
define( 'fibonacci', [], function() {
} )
Module ids must be set to the filename without the js extension, allowing to require
modules loaded using script tags. To override global module names registered in the
window Object, use the global option described bellow.
undefine( options )
The optional Object parameter options can have the following attributes which
all default to false and are browser-only options at this time:
- global: register into the global window Object e.g. window[ 'fibonacci' ].
If global is a string, register into window using this string.
- no_conflict: if global is truly, add no_conflict() to module.
Additional options when used in conjunction with an AMD loader:
- annonymous: register annonymously.
- amd_id: register under this id instead of filename.
Example usage to declare the fibonacci module as window.fib with no_conflict():
( this.undefine || require( 'undefine' )( module, require ) )
( { global: 'fib', no_conflict: true } )
( 'fibonacci', [], function() {
} )
Licence
The MIT License (MIT)
Copyright (c) 2015, Reactive Sets
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.