Skip to content

Commit c33a145

Browse files
committed
Incorporate loader changes made in the dojo/dojo2 repo.
1 parent 3c4d36a commit c33a145

File tree

1 file changed

+42
-26
lines changed

1 file changed

+42
-26
lines changed

src/loader.ts

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,41 @@ export interface IRootRequire extends IRequire {
833833

834834
var setGlobals: (require: IRequire, define: IDefine) => void;
835835
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')) {
837871
injectUrl = function (url: string, callback: (node?: HTMLScriptElement) => void, module: IModule, parent?: IModule): void {
838872
// insert a script element to the insert-point element with src=url;
839873
// apply callback upon detecting the script has loaded.
@@ -863,28 +897,6 @@ export interface IRootRequire extends IRequire {
863897
this.define = define;
864898
};
865899
}
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-
}
888900
else {
889901
throw new Error('Unsupported platform');
890902
}
@@ -944,9 +956,13 @@ export interface IRootRequire extends IRequire {
944956
deps = <any> factory;
945957
factory = arguments[2];
946958

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+
}
950966
}
951967

952968
if (arguments.length === 1) {

0 commit comments

Comments
 (0)