Bug 1259045 - Prevent loading Loader.jsm when using event-emitter.js as a JSM. r=jryans
authorAlexandre Poirot <poirot.alex@gmail.com>
Tue, 29 Mar 2016 06:28:17 -0700
changeset 290793 e56e76ec40cb0e8f11278a5e61414972b771e9fd
parent 290792 bc86e440410d9a6ce93c8f41f37fc4f94ee404da
child 290794 1e908cdc6c424be15d00ddefa9667f9b07531ccb
push id19656
push usergwagner@mozilla.com
push dateMon, 04 Apr 2016 13:43:23 +0000
treeherderb2g-inbound@e99061fde28a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjryans
bugs1259045
milestone48.0a1
Bug 1259045 - Prevent loading Loader.jsm when using event-emitter.js as a JSM. r=jryans
browser/components/newtab/NewTabWebChannel.jsm
devtools/shared/event-emitter.js
--- a/browser/components/newtab/NewTabWebChannel.jsm
+++ b/browser/components/newtab/NewTabWebChannel.jsm
@@ -20,17 +20,17 @@ Cu.import("resource://gre/modules/Prefer
 
 XPCOMUtils.defineLazyModuleGetter(this, "NewTabPrefsProvider",
                                   "resource:///modules/NewTabPrefsProvider.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "NewTabRemoteResources",
                                   "resource:///modules/NewTabRemoteResources.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "WebChannel",
                                   "resource://gre/modules/WebChannel.jsm");
 XPCOMUtils.defineLazyGetter(this, "EventEmitter", function() {
-  const {EventEmitter} = Cu.import("resource://gre/modules/devtools/event-emitter.js", {});
+  const {EventEmitter} = Cu.import("resource://devtools/shared/event-emitter.js", {});
   return EventEmitter;
 });
 
 const CHAN_ID = "newtab";
 const PREF_ENABLED = "browser.newtabpage.remote";
 const PREF_MODE = "browser.newtabpage.remote.mode";
 
 /**
--- a/devtools/shared/event-emitter.js
+++ b/devtools/shared/event-emitter.js
@@ -5,28 +5,44 @@
 /**
  * EventEmitter.
  */
 
 (function (factory) { // Module boilerplate
   if (this.module && module.id.indexOf("event-emitter") >= 0) { // require
     factory.call(this, require, exports, module);
   } else { // Cu.import
-      const Cu = Components.utils;
-      const { require } = Cu.import("resource://devtools/shared/Loader.jsm", {});
       this.isWorker = false;
-      this.promise = Cu.import("resource://gre/modules/Promise.jsm", {}).Promise;
+      // Bug 1259045: This module is loaded early in firefox startup as a JSM,
+      // but it doesn't depends on any real module. We can save a few cycles
+      // and bytes by not loading Loader.jsm.
+      let require = function(module) {
+        const Cu = Components.utils;
+        switch(module) {
+          case "promise":
+            return Cu.import("resource://gre/modules/Promise.jsm", {}).Promise;
+          case "Services":
+            return Cu.import("resource://gre/modules/Services.jsm", {}).Services;
+          case "chrome":
+            return {
+              Cu,
+              components: Components
+            };
+        }
+        return null;
+      }
       factory.call(this, require, this, { exports: this });
       this.EXPORTED_SYMBOLS = ["EventEmitter"];
   }
 }).call(this, function (require, exports, module) {
 
 this.EventEmitter = function EventEmitter() {};
 module.exports = EventEmitter;
 
+// See comment in JSM module boilerplate when adding a new dependency.
 const { Cu, components } = require("chrome");
 const Services = require("Services");
 const promise = require("promise");
 var loggingEnabled = true;
 
 if (!isWorker) {
   loggingEnabled = Services.prefs.getBoolPref("devtools.dump.emit");
   Services.prefs.addObserver("devtools.dump.emit", {