Bug 1251033 - Part 1 - BrowserLoader now has an option to only load shared components and not require an additional directory. r=jryans
authorJordan Santell <jsantell@mozilla.com>
Tue, 08 Mar 2016 17:21:11 -0800
changeset 287331 10ecf21c6b4809053b69bf333fcaaa96df8c6b56
parent 287330 990089efe3f6495607dcd68b8f8b103d145e34a9
child 287332 eb9b84702b2ccd7b3bf655ae4d705d299f42dc73
push id18081
push userjsantell@mozilla.com
push dateWed, 09 Mar 2016 03:19:43 +0000
treeherderfx-team@eb9b84702b2c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjryans
bugs1251033
milestone48.0a1
Bug 1251033 - Part 1 - BrowserLoader now has an option to only load shared components and not require an additional directory. r=jryans
devtools/client/aboutdebugging/initializer.js
devtools/client/debugger/debugger-controller.js
devtools/client/memory/initializer.js
devtools/client/memory/test/chrome/head.js
devtools/client/performance/components/test/head.js
devtools/client/performance/performance-controller.js
devtools/client/responsive.html/index.js
devtools/client/shared/browser-loader.js
devtools/client/shared/components/test/mochitest/head.js
--- a/devtools/client/aboutdebugging/initializer.js
+++ b/devtools/client/aboutdebugging/initializer.js
@@ -13,18 +13,20 @@ loader.lazyRequireGetter(this, "Debugger
   "devtools/shared/client/main", true);
 loader.lazyRequireGetter(this, "DebuggerServer",
   "devtools/server/main", true);
 loader.lazyRequireGetter(this, "Telemetry",
   "devtools/client/shared/telemetry");
 
 const { BrowserLoader } = Components.utils.import(
   "resource://devtools/client/shared/browser-loader.js", {});
-const { require } =
-  BrowserLoader("resource://devtools/client/aboutdebugging/", window);
+const { require } = BrowserLoader({
+  baseURI: "resource://devtools/client/aboutdebugging/",
+  window
+});
 
 const {
   createFactory,
   render,
   unmountComponentAtNode } = require("devtools/client/shared/vendor/react");
 const AboutDebuggingApp = createFactory(require("./components/aboutdebugging"));
 
 var AboutDebugging = {
--- a/devtools/client/debugger/debugger-controller.js
+++ b/devtools/client/debugger/debugger-controller.js
@@ -106,17 +106,20 @@ Cu.import("resource://devtools/client/sh
 Cu.import("resource://devtools/client/shared/widgets/ViewHelpers.jsm");
 
 /**
  * Localization convenience methods.
  */
 var L10N = new ViewHelpers.L10N(DBG_STRINGS_URI);
 
 Cu.import("resource://devtools/client/shared/browser-loader.js");
-const require = BrowserLoader("resource://devtools/client/debugger/", window).require;
+const { require } = BrowserLoader({
+  baseURI: "resource://devtools/client/debugger/",
+  window,
+});
 XPCOMUtils.defineConstant(this, "require", require);
 const { gDevTools } = require("devtools/client/framework/devtools");
 
 // React
 const React = require("devtools/client/shared/vendor/react");
 const ReactDOM = require("devtools/client/shared/vendor/react-dom");
 const { Provider } = require("devtools/client/shared/vendor/react-redux");
 
--- a/devtools/client/memory/initializer.js
+++ b/devtools/client/memory/initializer.js
@@ -2,17 +2,20 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 "use strict";
 
 const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
 const BrowserLoaderModule = {};
 Cu.import("resource://devtools/client/shared/browser-loader.js", BrowserLoaderModule);
-const { require } = BrowserLoaderModule.BrowserLoader("resource://devtools/client/memory/", this);
+const { require } = BrowserLoaderModule.BrowserLoader({
+  baseURI: "resource://devtools/client/memory/",
+  window: this
+});
 const { Task } = require("resource://gre/modules/Task.jsm");
 const { createFactory, createElement } = require("devtools/client/shared/vendor/react");
 const ReactDOM = require("devtools/client/shared/vendor/react-dom");
 const { Provider } = require("devtools/client/shared/vendor/react-redux");
 const App = createFactory(require("devtools/client/memory/app"));
 const Store = require("devtools/client/memory/store");
 const { assert } = require("devtools/shared/DevToolsUtils");
 
--- a/devtools/client/memory/test/chrome/head.js
+++ b/devtools/client/memory/test/chrome/head.js
@@ -3,17 +3,20 @@
 "use strict";
 
 var { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
 
 Cu.import("resource://testing-common/Assert.jsm");
 Cu.import("resource://gre/modules/Task.jsm");
 
 Cu.import("resource://devtools/client/shared/browser-loader.js");
-var { require } = BrowserLoader("resource://devtools/client/memory/", this);
+var { require } = BrowserLoader({
+  baseURI: "resource://devtools/client/memory/",
+  window: this
+});
 var Services = require("Services");
 
 var DevToolsUtils = require("devtools/shared/DevToolsUtils");
 DevToolsUtils.testing = true;
 var { immutableUpdate } = DevToolsUtils;
 
 var constants = require("devtools/client/memory/constants");
 var {
--- a/devtools/client/performance/components/test/head.js
+++ b/devtools/client/performance/components/test/head.js
@@ -10,17 +10,20 @@ Cu.import("resource://gre/modules/Task.j
 var { Promise: promise } = Cu.import("resource://gre/modules/Promise.jsm", {});
 var { require } = Cu.import("resource://gre/modules/devtools/shared/Loader.jsm", {});
 var { BrowserLoader } = Cu.import("resource://devtools/client/shared/browser-loader.js", {});
 var DevToolsUtils = require("devtools/shared/DevToolsUtils");
 var { TargetFactory } = require("devtools/client/framework/target");
 var { Toolbox } = require("devtools/client/framework/toolbox");
 
 DevToolsUtils.testing = true;
-var { require: browserRequire } = BrowserLoader("resource://devtools/client/performance/", this);
+var { require: browserRequire } = BrowserLoader({
+  baseURI: "resource://devtools/client/performance/",
+  window: this
+});
 
 var $ = (selector, scope=document) => scope.querySelector(selector);
 var $$ = (selector, scope=document) => scope.querySelectorAll(selector);
 
 function forceRender(comp) {
   return setState(comp, {})
     .then(() => setState(comp, {}));
 }
--- a/devtools/client/performance/performance-controller.js
+++ b/devtools/client/performance/performance-controller.js
@@ -1,17 +1,20 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 "use strict";
 
 var { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
 var BrowserLoaderModule = {};
 Cu.import("resource://devtools/client/shared/browser-loader.js", BrowserLoaderModule);
-var { loader, require } = BrowserLoaderModule.BrowserLoader("resource://devtools/client/performance/", this);
+var { loader, require } = BrowserLoaderModule.BrowserLoader({
+  baseURI: "resource://devtools/client/performance/",
+  window: this
+});
 var { Task } = require("resource://gre/modules/Task.jsm");
 var { Heritage, ViewHelpers, WidgetMethods } = require("resource://devtools/client/shared/widgets/ViewHelpers.jsm");
 var { gDevTools } = require("devtools/client/framework/devtools");
 
 // Events emitted by various objects in the panel.
 var EVENTS = require("devtools/client/performance/events");
 Object.defineProperty(this, "EVENTS", {
   value: EVENTS,
--- a/devtools/client/responsive.html/index.js
+++ b/devtools/client/responsive.html/index.js
@@ -4,18 +4,20 @@
 
 /* eslint-env browser */
 
 "use strict";
 
 const { utils: Cu } = Components;
 const { BrowserLoader } =
   Cu.import("resource://devtools/client/shared/browser-loader.js", {});
-const { require } =
-  BrowserLoader("resource://devtools/client/responsive.html/", this);
+const { require } = BrowserLoader({
+  baseURI: "resource://devtools/client/responsive.html/",
+  window: this
+});
 const Telemetry = require("devtools/client/shared/telemetry");
 
 const { createFactory, createElement } =
   require("devtools/client/shared/vendor/react");
 const ReactDOM = require("devtools/client/shared/vendor/react-dom");
 const { Provider } = require("devtools/client/shared/vendor/react-redux");
 
 const App = createFactory(require("./app"));
--- a/devtools/client/shared/browser-loader.js
+++ b/devtools/client/shared/browser-loader.js
@@ -1,18 +1,18 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 "use strict";
 
-var { classes: Cc, interfaces: Ci, utils: Cu } = Components;
-
+var Cu = Components.utils;
 const loaders = Cu.import("resource://gre/modules/commonjs/toolkit/loader.js", {});
 const { devtools } = Cu.import("resource://devtools/shared/Loader.jsm", {});
 const { joinURI } = devtools.require("devtools/shared/path");
+const { assert } = devtools.require("devtools/shared/DevToolsUtils");
 const Services = devtools.require("Services");
 Cu.import("resource://gre/modules/AppConstants.jsm");
 
 const BROWSER_BASED_DIRS = [
   "resource://devtools/client/jsonview",
   "resource://devtools/client/shared/vendor",
   "resource://devtools/client/shared/components",
   "resource://devtools/client/shared/redux"
@@ -34,65 +34,76 @@ function clearCache() {
  * deals with modules loaded from under `baseURI`. Anything loaded
  * outside of that path will still be loaded from the devtools loader,
  * so all system modules are still shared and cached across instances.
  * An exception to this is anything under
  * `devtools/client/shared/{vendor/components}`, which is where shared libraries
  * and React components live that should be evaluated in a browser environment.
  *
  * @param string baseURI
- *        Base path to load modules from.
+ *        Base path to load modules from. If null or undefined, only
+ *        the shared vendor/components modules are loaded with the browser
+ *        loader.
  * @param Object window
  *        The window instance to evaluate modules within
+ * @param Boolean useOnlyShared
+ *        If true, ignores `baseURI` and only loads the shared
+ *        BROWSER_BASED_DIRS via BrowserLoader.
  * @return Object
  *         An object with two properties:
  *         - loader: the Loader instance
  *         - require: a function to require modules with
  */
-function BrowserLoader(baseURI, window) {
-  const browserLoaderBuilder = new BrowserLoaderBuilder(baseURI, window);
+function BrowserLoader(options) {
+  const browserLoaderBuilder = new BrowserLoaderBuilder(options);
   return {
     loader: browserLoaderBuilder.loader,
     require: browserLoaderBuilder.require
   };
 }
 
 /**
  * Private class used to build the Loader instance and require method returned
  * by BrowserLoader(baseURI, window).
  *
  * @param string baseURI
  *        Base path to load modules from.
  * @param Object window
  *        The window instance to evaluate modules within
+ * @param Boolean useOnlyShared
+ *        If true, ignores `baseURI` and only loads the shared
+ *        BROWSER_BASED_DIRS via BrowserLoader.
  */
-function BrowserLoaderBuilder(baseURI, window) {
+function BrowserLoaderBuilder({ baseURI, window, useOnlyShared }) {
+  assert(!!baseURI !== !!useOnlyShared,
+    "Cannot use both `baseURI` and `useOnlyShared`.");
+
   const loaderOptions = devtools.require("@loader/options");
   const dynamicPaths = {};
   const componentProxies = new Map();
   const hotReloadEnabled = Services.prefs.getBoolPref("devtools.loader.hotreload");
 
-  if(AppConstants.DEBUG || AppConstants.DEBUG_JS_MODULES) {
+  if (AppConstants.DEBUG || AppConstants.DEBUG_JS_MODULES) {
     dynamicPaths["devtools/client/shared/vendor/react"] =
       "resource://devtools/client/shared/vendor/react-dev";
-  };
+  }
 
   const opts = {
     id: "browser-loader",
     sharedGlobal: true,
     sandboxPrototype: window,
     paths: Object.assign({}, dynamicPaths, loaderOptions.paths),
     invisibleToDebugger: loaderOptions.invisibleToDebugger,
     requireHook: (id, require) => {
       const uri = require.resolve(id);
       const isBrowserDir = BROWSER_BASED_DIRS.filter(dir => {
         return uri.startsWith(dir);
       }).length > 0;
 
-      if (!uri.startsWith(baseURI) && !isBrowserDir) {
+      if ((useOnlyShared || !uri.startsWith(baseURI)) && !isBrowserDir) {
         return devtools.require(uri);
       }
 
       return require(uri);
     },
     globals: {
       // Allow modules to use the window's console to ensure logs appear in a
       // tab toolbox, if one exists, instead of just the browser console.
@@ -118,17 +129,17 @@ function BrowserLoaderBuilder(baseURI, w
         lazyGetter: devtools.lazyGetter,
         lazyImporter: devtools.lazyImporter,
         lazyServiceGetter: devtools.lazyServiceGetter,
         lazyRequireGetter: this.lazyRequireGetter.bind(this),
       },
     }
   };
 
-  if(hotReloadEnabled) {
+  if (hotReloadEnabled) {
     opts.loadModuleHook = (module, require) => {
       const { uri, exports } = module;
 
       if (exports.prototype &&
           exports.prototype.isReactComponent) {
         const { createProxy, getForceUpdate } =
               require("devtools/client/shared/vendor/react-proxy");
         const React = require("devtools/client/shared/vendor/react");
--- a/devtools/client/shared/components/test/mochitest/head.js
+++ b/devtools/client/shared/components/test/mochitest/head.js
@@ -14,17 +14,20 @@ var promise = require("promise");
 var Services = require("Services");
 var { DebuggerServer } = require("devtools/server/main");
 var { DebuggerClient } = require("devtools/shared/client/main");
 var DevToolsUtils = require("devtools/shared/DevToolsUtils");
 var { TargetFactory } = require("devtools/client/framework/target");
 var { Toolbox } = require("devtools/client/framework/toolbox");
 
 DevToolsUtils.testing = true;
-var { require: browserRequire } = BrowserLoader("resource://devtools/client/shared/", this);
+var { require: browserRequire } = BrowserLoader({
+  baseURI: "resource://devtools/client/shared/",
+  window: this
+});
 
 var EXAMPLE_URL = "http://example.com/browser/browser/devtools/shared/test/";
 
 function forceRender(comp) {
   return setState(comp, {})
     .then(() => setState(comp, {}));
 }