@@ -12,130 +12,117 @@ var define;
12
12
13
13
namespace AMDLoader {
14
14
15
+ const env = new Environment ( ) ;
16
+
15
17
let moduleManager : ModuleManager = null ;
16
- let DefineFunc : IDefineFunc = null ;
17
- let RequireFunc : IRequireFunc = null ;
18
18
19
- function createGlobalAMDFuncs ( ) : void {
19
+ const DefineFunc : IDefineFunc = < any > function ( id : any , dependencies : any , callback : any ) : void {
20
+ if ( typeof id !== 'string' ) {
21
+ callback = dependencies ;
22
+ dependencies = id ;
23
+ id = null ;
24
+ }
25
+ if ( typeof dependencies !== 'object' || ! Array . isArray ( dependencies ) ) {
26
+ callback = dependencies ;
27
+ dependencies = null ;
28
+ }
29
+ if ( ! dependencies ) {
30
+ dependencies = [ 'require' , 'exports' , 'module' ] ;
31
+ }
20
32
21
- const _defineFunc = function ( id : any , dependencies : any , callback : any ) : void {
22
- if ( typeof id !== 'string' ) {
23
- callback = dependencies ;
24
- dependencies = id ;
25
- id = null ;
26
- }
27
- if ( typeof dependencies !== 'object' || ! Array . isArray ( dependencies ) ) {
28
- callback = dependencies ;
29
- dependencies = null ;
33
+ if ( id ) {
34
+ moduleManager . defineModule ( id , dependencies , callback , null , null ) ;
35
+ } else {
36
+ moduleManager . enqueueDefineAnonymousModule ( dependencies , callback ) ;
37
+ }
38
+ } ;
39
+ DefineFunc . amd = {
40
+ jQuery : true
41
+ } ;
42
+
43
+ const _requireFunc_config = function ( params : IConfigurationOptions , shouldOverwrite : boolean = false ) : void {
44
+ moduleManager . configure ( params , shouldOverwrite ) ;
45
+ } ;
46
+ const RequireFunc : IRequireFunc = < any > function ( ) {
47
+ if ( arguments . length === 1 ) {
48
+ if ( ( arguments [ 0 ] instanceof Object ) && ! Array . isArray ( arguments [ 0 ] ) ) {
49
+ _requireFunc_config ( arguments [ 0 ] ) ;
50
+ return ;
30
51
}
31
- if ( ! dependencies ) {
32
- dependencies = [ 'require' , 'exports' , 'module' ] ;
52
+ if ( typeof arguments [ 0 ] === 'string' ) {
53
+ return moduleManager . synchronousRequire ( arguments [ 0 ] ) ;
33
54
}
34
-
35
- if ( id ) {
36
- moduleManager . defineModule ( id , dependencies , callback , null , null ) ;
37
- } else {
38
- moduleManager . enqueueDefineAnonymousModule ( dependencies , callback ) ;
55
+ }
56
+ if ( arguments . length === 2 || arguments . length === 3 ) {
57
+ if ( Array . isArray ( arguments [ 0 ] ) ) {
58
+ moduleManager . defineModule ( Utilities . generateAnonymousModule ( ) , arguments [ 0 ] , arguments [ 1 ] , arguments [ 2 ] , null ) ;
59
+ return ;
39
60
}
40
- } ;
41
-
42
- DefineFunc = < any > _defineFunc ;
43
- DefineFunc . amd = {
44
- jQuery : true
45
- } ;
61
+ }
62
+ throw new Error ( 'Unrecognized require call' ) ;
63
+ } ;
64
+ RequireFunc . config = _requireFunc_config ;
65
+ RequireFunc . getConfig = function ( ) : IConfigurationOptions {
66
+ return moduleManager . getConfig ( ) . getOptionsLiteral ( ) ;
67
+ } ;
68
+ RequireFunc . reset = function ( ) : void {
69
+ moduleManager = moduleManager . reset ( ) ;
70
+ } ;
71
+ RequireFunc . getBuildInfo = function ( ) : IBuildModuleInfo [ ] {
72
+ return moduleManager . getBuildInfo ( ) ;
73
+ } ;
74
+ RequireFunc . getStats = function ( ) : LoaderEvent [ ] {
75
+ return moduleManager . getLoaderEvents ( ) ;
76
+ } ;
46
77
47
- const _requireFunc_config = function ( params : IConfigurationOptions , shouldOverwrite : boolean = false ) : void {
48
- moduleManager . configure ( params , shouldOverwrite ) ;
49
- } ;
78
+ export function init ( ) : void {
79
+ if ( typeof global . require !== 'undefined' || typeof require !== 'undefined' ) {
80
+ const _nodeRequire = ( global . require || require ) ;
81
+ if ( typeof _nodeRequire === 'function' && typeof _nodeRequire . resolve === 'function' ) {
82
+ // re-expose node's require function
83
+ const nodeRequire = function ( what ) {
84
+ moduleManager . getRecorder ( ) . record ( LoaderEventType . NodeBeginNativeRequire , what ) ;
85
+ try {
86
+ return _nodeRequire ( what ) ;
87
+ } finally {
88
+ moduleManager . getRecorder ( ) . record ( LoaderEventType . NodeEndNativeRequire , what ) ;
89
+ }
90
+ } ;
50
91
51
- const _requireFunc = function ( ) {
52
- if ( arguments . length === 1 ) {
53
- if ( ( arguments [ 0 ] instanceof Object ) && ! Array . isArray ( arguments [ 0 ] ) ) {
54
- _requireFunc_config ( arguments [ 0 ] ) ;
55
- return ;
56
- }
57
- if ( typeof arguments [ 0 ] === 'string' ) {
58
- return moduleManager . synchronousRequire ( arguments [ 0 ] ) ;
59
- }
60
- }
61
- if ( arguments . length === 2 || arguments . length === 3 ) {
62
- if ( Array . isArray ( arguments [ 0 ] ) ) {
63
- moduleManager . defineModule ( Utilities . generateAnonymousModule ( ) , arguments [ 0 ] , arguments [ 1 ] , arguments [ 2 ] , null ) ;
64
- return ;
65
- }
92
+ global . nodeRequire = nodeRequire ;
93
+ ( < any > RequireFunc ) . nodeRequire = nodeRequire ;
94
+ ( < any > RequireFunc ) . __$__nodeRequire = nodeRequire ;
66
95
}
67
- throw new Error ( 'Unrecognized require call' ) ;
68
- } ;
69
-
70
- RequireFunc = < any > _requireFunc ;
71
- RequireFunc . config = _requireFunc_config ;
72
- RequireFunc . getConfig = function ( ) : IConfigurationOptions {
73
- return moduleManager . getConfig ( ) . getOptionsLiteral ( ) ;
74
- } ;
75
-
76
- RequireFunc . reset = function ( ) : void {
77
- moduleManager = moduleManager . reset ( ) ;
78
- } ;
79
-
80
- RequireFunc . getBuildInfo = function ( ) : IBuildModuleInfo [ ] {
81
- return moduleManager . getBuildInfo ( ) ;
82
- } ;
83
-
84
- RequireFunc . getStats = function ( ) : LoaderEvent [ ] {
85
- return moduleManager . getLoaderEvents ( ) ;
86
- } ;
87
- }
88
-
89
- export function init ( ) : void {
90
- createGlobalAMDFuncs ( ) ;
91
-
92
- const env = Environment . detect ( ) ;
93
- const scriptLoader : IScriptLoader = createScriptLoader ( env ) ;
94
- moduleManager = new ModuleManager ( env , scriptLoader , DefineFunc , RequireFunc , Utilities . getHighPerformanceTimestamp ( ) ) ;
95
-
96
- if ( env . isNode ) {
97
- var _nodeRequire = ( global . require || require ) ;
98
- var nodeRequire = function ( what ) {
99
- moduleManager . getRecorder ( ) . record ( LoaderEventType . NodeBeginNativeRequire , what ) ;
100
- try {
101
- return _nodeRequire ( what ) ;
102
- } finally {
103
- moduleManager . getRecorder ( ) . record ( LoaderEventType . NodeEndNativeRequire , what ) ;
104
- }
105
- } ;
106
-
107
- global . nodeRequire = nodeRequire ;
108
- ( < any > RequireFunc ) . nodeRequire = nodeRequire ;
109
96
}
110
97
111
98
if ( env . isNode && ! env . isElectronRenderer ) {
112
99
module . exports = RequireFunc ;
113
- // These two defs are fore the local closure defined in node in the case that the loader is concatenated
114
- define = function ( ) {
115
- DefineFunc . apply ( null , arguments ) ;
116
- } ;
117
100
require = < any > RequireFunc ;
118
101
} else {
119
- // The global variable require can configure the loader
120
- if ( typeof global . require !== 'undefined' && typeof global . require !== 'function' ) {
121
- RequireFunc . config ( global . require ) ;
122
- }
123
102
if ( ! env . isElectronRenderer ) {
124
- global . define = define = DefineFunc ;
125
- } else {
126
- define = function ( ) {
127
- DefineFunc . apply ( null , arguments ) ;
128
- } ;
103
+ global . define = DefineFunc ;
129
104
}
130
105
global . require = RequireFunc ;
131
- global . require . __$__nodeRequire = nodeRequire ;
132
106
}
133
107
}
134
108
135
- if (
136
- typeof doNotInitLoader === 'undefined' &&
137
- ( typeof global . define !== 'function' || ! global . define . amd )
138
- ) {
139
- init ( ) ;
109
+ if ( typeof global . define !== 'function' || ! global . define . amd ) {
110
+ moduleManager = new ModuleManager ( env , createScriptLoader ( env ) , DefineFunc , RequireFunc , Utilities . getHighPerformanceTimestamp ( ) ) ;
111
+
112
+ // The global variable require can configure the loader
113
+ if ( typeof global . require !== 'undefined' && typeof global . require !== 'function' ) {
114
+ RequireFunc . config ( global . require ) ;
115
+ }
116
+
117
+ // This define is for the local closure defined in node in the case that the loader is concatenated
118
+ define = function ( ) {
119
+ return DefineFunc . apply ( null , arguments ) ;
120
+ } ;
121
+ define . amd = DefineFunc . amd ;
122
+
123
+ if ( typeof doNotInitLoader === 'undefined' ) {
124
+ init ( ) ;
125
+ }
140
126
}
127
+
141
128
}
0 commit comments