Bug 1309866 - Add option for loading common libraries to BrowserLoader r?Honza draft
authorJarda Snajdr <jsnajdr@gmail.com>
Wed, 23 Nov 2016 16:43:41 +0100
changeset 447951 776cbed734ccb4268aa5ecc88ed866fec344208f
parent 447950 17df428fb1f7ccd29c041058cf028db12f69be47
child 447952 485bccccaed0149f70a7b666a96c36cd008a7f26
push id38210
push userbmo:jsnajdr@gmail.com
push dateThu, 08 Dec 2016 19:03:32 +0000
reviewersHonza
bugs1309866
milestone53.0a1
Bug 1309866 - Add option for loading common libraries to BrowserLoader r?Honza 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