Bug 1309866 - Add option for loading common libraries to BrowserLoader r?Honza
MozReview-Commit-ID: HGS3T6QcOvF
--- 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