@@ -833,7 +833,41 @@ export interface IRootRequire extends IRequire {
833
833
834
834
var setGlobals : ( require : IRequire , define : IDefine ) => void ;
835
835
var injectUrl : ( url : string , callback : ( node ?: HTMLScriptElement ) => void , module : IModule , parent ?: IModule ) => void ;
836
- if ( has ( 'host-browser' ) ) {
836
+ if ( has ( 'host-node' ) ) {
837
+ var vm : any = require ( 'vm' ) ;
838
+ var fs : any = require ( 'fs' ) ;
839
+
840
+ // retain the ability to get node's require
841
+ req . nodeRequire = require ;
842
+ injectUrl = function ( url : string , callback : ( node ?: HTMLScriptElement ) => void , module : IModule , parent ?: IModule ) : void {
843
+ fs . readFile ( url , 'utf8' , function ( error : Error , data : string ) : void {
844
+ if ( error ) {
845
+ throw new Error ( 'Failed to load module ' + module . mid + ' from ' + url + ( parent ? ' (parent: ' + parent . mid + ')' : '' ) ) ;
846
+ }
847
+
848
+ // global `module` variable needs to be shadowed for UMD modules that are loaded in an Electron webview;
849
+ // in Node.js the `module` variable does not exist when using `vm.runInThisContext`, but in Electron it
850
+ // exists in the webview when Node.js integration is enabled which causes loaded modules to register
851
+ // with Node.js and break the loader
852
+ var oldModule = this . module ;
853
+ this . module = undefined ;
854
+ try {
855
+ vm . runInThisContext ( data , url ) ;
856
+ }
857
+ finally {
858
+ this . module = oldModule ;
859
+ }
860
+
861
+ callback ( ) ;
862
+ } ) ;
863
+ } ;
864
+
865
+ setGlobals = function ( require : IRequire , define : IDefine ) : void {
866
+ module . exports = this . require = require ;
867
+ this . define = define ;
868
+ } ;
869
+ }
870
+ else if ( has ( 'host-browser' ) ) {
837
871
injectUrl = function ( url : string , callback : ( node ?: HTMLScriptElement ) => void , module : IModule , parent ?: IModule ) : void {
838
872
// insert a script element to the insert-point element with src=url;
839
873
// apply callback upon detecting the script has loaded.
@@ -863,28 +897,6 @@ export interface IRootRequire extends IRequire {
863
897
this . define = define ;
864
898
} ;
865
899
}
866
- else if ( has ( 'host-node' ) ) {
867
- var vm : any = require ( 'vm' ) ;
868
- var fs : any = require ( 'fs' ) ;
869
-
870
- // retain the ability to get node's require
871
- req . nodeRequire = require ;
872
- injectUrl = function ( url : string , callback : ( node ?: HTMLScriptElement ) => void , module : IModule , parent ?: IModule ) : void {
873
- fs . readFile ( url , 'utf8' , function ( error : Error , data : string ) : void {
874
- if ( error ) {
875
- throw new Error ( 'Failed to load module ' + module . mid + ' from ' + url + ( parent ? ' (parent: ' + parent . mid + ')' : '' ) ) ;
876
- }
877
-
878
- vm . runInThisContext ( data , url ) ;
879
- callback ( ) ;
880
- } ) ;
881
- } ;
882
-
883
- setGlobals = function ( require : IRequire , define : IDefine ) : void {
884
- module . exports = this . require = require ;
885
- this . define = define ;
886
- } ;
887
- }
888
900
else {
889
901
throw new Error ( 'Unsupported platform' ) ;
890
902
}
@@ -944,9 +956,13 @@ export interface IRootRequire extends IRequire {
944
956
deps = < any > factory ;
945
957
factory = arguments [ 2 ] ;
946
958
947
- var module : IModule = getModule ( id ) ;
948
- module . injected = true ;
949
- defineModule ( module , deps , factory ) ;
959
+ // Some modules in the wild have an explicit module ID that is null; ignore the module ID in this case and
960
+ // register normally using the request module ID
961
+ if ( id != null ) {
962
+ var module : IModule = getModule ( id ) ;
963
+ module . injected = true ;
964
+ defineModule ( module , deps , factory ) ;
965
+ }
950
966
}
951
967
952
968
if ( arguments . length === 1 ) {
0 commit comments