@@ -16,10 +16,67 @@ ExternalModuleFactoryDecorator.prototype.plugin = function() {
1616} ;
1717
1818ExternalModuleFactoryDecorator . prototype . create = function ( context , dependency , callback ) {
19- if ( Object . prototype . hasOwnProperty . call ( this . externals , dependency . request ) ) {
20- var value = this . externals [ dependency . request ] ;
19+ var factory = this . factory ;
20+ var globalType = this . type ;
21+ function handleExternal ( value , type , callback ) {
22+ if ( typeof type === "function" ) {
23+ callback = type ;
24+ type = undefined ;
25+ }
26+ if ( value === false ) return factory . create ( context , dependency , callback ) ;
2127 if ( value === true ) value = dependency . request ;
22- return callback ( null , new ExternalModule ( value , this . type ) ) ;
28+ if ( typeof type === "undefined" && / ^ [ a - z 0 - 9 ] + / . test ( value ) ) {
29+ var idx = value . indexOf ( " " ) ;
30+ type = value . substr ( 0 , idx ) ;
31+ value = value . substr ( idx + 1 ) ;
32+ }
33+ callback ( null , new ExternalModule ( value , type || globalType ) ) ;
34+ return true ;
2335 }
24- return this . factory . create ( context , dependency , callback ) ;
36+ ( function handleExternals ( externals , callback ) {
37+ if ( typeof externals === "string" ) {
38+ if ( externals === dependency . request ) {
39+ return handleExternal ( dependency . request , callback ) ;
40+ }
41+ } else if ( Array . isArray ( externals ) ) {
42+ var i = 0 ;
43+ ( function next ( ) {
44+ do {
45+ var async = true ;
46+ if ( i >= externals . length ) return callback ( ) ;
47+ handleExternals ( externals [ i ++ ] , function ( err , module ) {
48+ if ( err ) return callback ( err ) ;
49+ if ( ! module ) {
50+ if ( async ) return async = false ; ;
51+ return next ( ) ;
52+ }
53+ callback ( null , module ) ;
54+ } ) ;
55+ } while ( ! async ) ;
56+ async = false ;
57+ } ( ) ) ;
58+ return ;
59+ } else if ( externals instanceof RegExp ) {
60+ if ( externals . test ( dependency . request ) ) {
61+ return handleExternal ( dependency . request , callback ) ;
62+ }
63+ } else if ( typeof externals === "function" ) {
64+ externals . call ( null , context , dependency . request , function ( err , value , type ) {
65+ if ( err ) return callback ( err ) ;
66+ if ( value ) {
67+ handleExternal ( value , type , callback ) ;
68+ } else {
69+ callback ( ) ;
70+ }
71+ } ) ;
72+ return ;
73+ } else if ( typeof externals === "object" && Object . prototype . hasOwnProperty . call ( externals , dependency . request ) ) {
74+ return handleExternal ( externals [ dependency . request ] , callback ) ;
75+ }
76+ callback ( ) ;
77+ } ( this . externals , function ( err , module ) {
78+ if ( err ) return callback ( err ) ;
79+ if ( ! module ) return handleExternal ( false , callback ) ;
80+ return callback ( null , module ) ;
81+ } ) ) ;
2582} ;
0 commit comments