-
Notifications
You must be signed in to change notification settings - Fork 0
/
undefine.js
225 lines (166 loc) · 7.14 KB
/
undefine.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* undefine.js
The MIT License (MIT)
Copyright (c) 2015-2020, 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.
*/
!function undefine_factory( global ) {
'use strict';
var me = 'undefine'
, c = typeof console == 'object' && console
, clog = c && c.log
, log = clog && clog.bind( c, me + ':' ) || function() {}
, de = 0
, ug = log
, modules = {}
, node = 0;
;
if ( has_define() ) {
de&&ug( 'Cooperating with AMD loader, define.amd:', has_define() );
// AMD loader provides its own require function
} else {
// @Node.js_code
if ( typeof exports == 'object' ) {
de&&ug( 'Loaded by node' );
node = 1;
// Requires to first set node module and require, that of the module being defined, not this global object
return module.exports = set_module;
}
de&&ug( 'Standalone globally' );
global.require = function _require( dependencies, factory ) {
if ( ! factory ) {
factory = dependencies;
dependencies = null;
}
get_dependencies( require_global, dependencies, global, factory );
}; // _require()
}
// In browser, AMD or Globals
// global is set to the global window object
global[ me ] = set_module( global );
function set_module( module, node_require, node_exports ) {
return _undefine;
function _undefine( options ) {
options = options || {};
return _define;
function _define( name, dependencies, factory ) {
// name and factory are required, only dependencies is an optional parameter w/ undefine()
// name is required to allow bundling without code transforms.
if ( ! factory ) {
factory = dependencies;
dependencies = null;
}
if ( has_define() ) {
// AMD define
de&&ug( 'AMD loading', name, options );
var parameters = [ factory ];
dependencies && parameters.unshift( dependencies.map( disambiguate ) );
options.annonymous || parameters.unshift( options.amd_id || name );
define.apply( module, parameters );
/*
If module is to be defined as a window global, require it immediately
This guaranties that factory is executed and allows to set window.
https://github.com/umdjs/umd/blob/master/amdWebGlobal.js does not work
unless the module is later required, which is not guarantied if other modules
assume global dependencies.
*/
options.global && ( global.require || global.curl || fatal( 'AMD require() not found' ) )( [ name ], function( exports ) {
register( name, exports, options );
} );
} else {
// @Node.js_code
if ( typeof exports == 'object' ) {
de&&ug( 'Node loading', name );
return call_factory(
function( dependency ) {
return node_require( disambiguate( dependency ) )
},
function( result ) {
if ( result ) module.exports = result;
}
);
}
// Standalone
de&&ug( 'Standalone loading', name, options );
call_factory( require_global, function( result ) {
register( name, result, options );
} );
}
function call_factory( require, then ) {
if ( typeof factory != 'function' ) return factory;
return get_dependencies( require, dependencies, module, factory, node_exports, then );
} // call_factory()
} // _define()
} // _undefine()
} // set_module()
function has_define() {
return typeof define == 'function' && define.amd;
} // has_define()
function get_dependencies( require, dependencies, module, factory, node_exports, then ) {
var specials_dependencies = {
require: require,
exports: node_exports || {},
module : module
};
dependencies = ( dependencies || [ 'require', 'exports', 'module' ] ).map( _require );
// Wish: wait if not all dependencies are met
// This would be backward compatible since today most programs would fail if a dependency
// is not met.
var result = factory.apply( module, dependencies );
then && then( result );
function _require( dependency ) {
return specials_dependencies[ dependency ] || require( dependency );
} // _require()
} // get_dependencies()
function disambiguate( dependency ) {
return dependency.pop ? dependency[ node ] : dependency
} // disambiguate()
function require_global( dependency ) {
var f = 'require_global()'
, name = disambiguate( dependency )
;
if ( typeof name != 'string' ) return name; // dependency name is the exported value
name = name.split( '/' ).pop();
de&&ug( f, 'name:', name );
// Wish: a 'lazy' option to call factory function only upon first require, unless global
// This option would be backward compatible because it would be an option
var exports = modules[ name ] || global[ name ];
exports || fatal( f + ' not found: ' + name );
return exports;
} // require_global()
function register( name, exports, options ) {
var f = 'register()'
, _global = options.global
;
de&&ug( f, name, exports, options );
modules[ name ] && fatal( f + ' allready loaded: ' + name );
modules[ name ] = exports || {};
if ( exports && _global ) {
name = typeof _global == 'string' ? _global : name;
if ( options.no_conflict ) {
var previous = global[ name ];
exports.no_conflict = function() {
global[ name ] = previous;
return exports;
};
}
global[ name ] = exports;
}
} // register()
function fatal( message ) {
throw new Error( me + ', ' + message );
} // fatal()
}( this ); // undefine.js