Bug 1309866 - Add option for loading common libraries to BrowserLoader r=Honza,jlongster
☠☠ backed out by 6a75cf810d96 ☠ ☠
authorJarda Snajdr <jsnajdr@gmail.com>
Wed, 23 Nov 2016 16:43:41 +0100
changeset 325417 ec1ebd2666c7e7b9379ccc19c01a0764436a0d6b
parent 325416 abcb8d9139a2d3b0cee9084313ef58326409b349
child 325418 f09e9013044d352d51c3dea33826884188a59622
push id24
push usermaklebus@msu.edu
push dateTue, 20 Dec 2016 03:11:33 +0000
reviewersHonza, jlongster
bugs1309866
milestone53.0a1
Bug 1309866 - Add option for loading common libraries to BrowserLoader r=Honza,jlongster MozReview-Commit-ID: HGS3T6QcOvF
devtools/client/shared/browser-loader.js
--- a/devtools/client/shared/browser-loader.js
+++ b/devtools/client/shared/browser-loader.js
@@ -13,16 +13,20 @@ const { AppConstants } = devtools.requir
 
 const BROWSER_BASED_DIRS = [
   "resource://devtools/client/inspector/layout",
   "resource://devtools/client/jsonview",
   "resource://devtools/client/shared/vendor",
   "resource://devtools/client/shared/redux",
 ];
 
+const COMMON_LIBRARY_DIRS = [
+  "resource://devtools/client/shared/vendor",
+];
+
 // Any directory that matches the following regular expression
 // is also considered as browser based module directory.
 // ('resource://devtools/client/.*/components/')
 //
 // An example:
 // * `resource://devtools/client/inspector/components`
 // * `resource://devtools/client/inspector/shared/components`
 const browserBasedDirsRegExp =
@@ -76,18 +80,22 @@ function BrowserLoader(options) {
  *
  * @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.
+ * @param Function commonLibRequire
+ *        Require function that should be used to load common libraries, like React.
+ *        Allows for sharing common modules between tools, instead of loading a new
+ *        instance into each tool. For example, pass "toolbox.browserRequire" here.
  */
-function BrowserLoaderBuilder({ baseURI, window, useOnlyShared }) {
+function BrowserLoaderBuilder({ baseURI, window, useOnlyShared, commonLibRequire }) {
   assert(!!baseURI !== !!useOnlyShared,
     "Cannot use both `baseURI` and `useOnlyShared`.");
 
   const loaderOptions = devtools.require("@loader/options");
   const dynamicPaths = {};
   const componentProxies = new Map();
 
   if (AppConstants.DEBUG || AppConstants.DEBUG_JS_MODULES) {
@@ -104,25 +112,25 @@ function BrowserLoaderBuilder({ baseURI,
     requireHook: (id, require) => {
       // If |id| requires special handling, simply defer to devtools
       // immediately.
       if (devtools.isLoaderPluginId(id)) {
         return devtools.require(id);
       }
 
       const uri = require.resolve(id);
-      let isBrowserDir = BROWSER_BASED_DIRS.filter(dir => {
-        return uri.startsWith(dir);
-      }).length > 0;
 
-      // If the URI doesn't match hardcoded paths try the regexp.
-      if (!isBrowserDir) {
-        isBrowserDir = uri.match(browserBasedDirsRegExp) != null;
+      if (commonLibRequire && COMMON_LIBRARY_DIRS.some(dir => uri.startsWith(dir))) {
+        return commonLibRequire(uri);
       }
 
+      // Check if the URI matches one of hardcoded paths or a regexp.
+      let isBrowserDir = BROWSER_BASED_DIRS.some(dir => uri.startsWith(dir)) ||
+                         uri.match(browserBasedDirsRegExp) != null;
+
       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