Skip to content

Commit dd53658

Browse files
authored
[DevTools] remove backend dependency from the global hook (#26563)
## Summary - #26234 is reverted and replaced with a better approach - introduce a new global devtools variable to decouple the global hook's dependency on backend/console.js, and add it to react-devtools-inline and react-devtools-standalone With this PR, I want to introduce a new principle to hook.js: we should always be alert when editing this file and avoid importing from other files. In the past, we try to inline a lot of the implementation because we use `.toString()` to inject this function from the extension (we still have some old comments left). Although it is no longer inlined that way, it has became now more important to keep it clean as it is a de facto global API people are using (9.9K files contains it on Github search as of today). **File size change for extension:** Before: 379K installHook.js After: 21K installHook.js 363K renderer.js
1 parent 85bb7b6 commit dd53658

File tree

12 files changed

+85
-40
lines changed

12 files changed

+85
-40
lines changed

packages/react-devtools-core/src/backend.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Agent from 'react-devtools-shared/src/backend/agent';
1111
import Bridge from 'react-devtools-shared/src/bridge';
1212
import {installHook} from 'react-devtools-shared/src/hook';
1313
import {initBackend} from 'react-devtools-shared/src/backend';
14+
import {installConsoleFunctionsToWindow} from 'react-devtools-shared/src/backend/console';
1415
import {__DEBUG__} from 'react-devtools-shared/src/constants';
1516
import setupNativeStyleEditor from 'react-devtools-shared/src/backend/NativeStyleEditor/setupNativeStyleEditor';
1617
import {getDefaultComponentFilters} from 'react-devtools-shared/src/utils';
@@ -38,6 +39,9 @@ type ConnectOptions = {
3839
...
3940
};
4041

42+
// Install a global variable to allow patching console early (during injection).
43+
// This provides React Native developers with components stacks even if they don't run DevTools.
44+
installConsoleFunctionsToWindow();
4145
installHook(window);
4246

4347
const hook: ?DevToolsHook = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;

packages/react-devtools-extensions/chrome/manifest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"panel.html",
3232
"build/react_devtools_backend.js",
3333
"build/proxy.js",
34+
"build/renderer.js",
3435
"build/installHook.js"
3536
],
3637
"matches": [

packages/react-devtools-extensions/edge/manifest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"panel.html",
3232
"build/react_devtools_backend.js",
3333
"build/proxy.js",
34+
"build/renderer.js",
3435
"build/installHook.js"
3536
],
3637
"matches": [

packages/react-devtools-extensions/firefox/manifest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"panel.html",
3333
"build/react_devtools_backend.js",
3434
"build/proxy.js",
35+
"build/renderer.js",
3536
"build/installHook.js"
3637
],
3738
"background": {

packages/react-devtools-extensions/src/background.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ if (!IS_FIREFOX) {
2222
runAt: 'document_start',
2323
world: chrome.scripting.ExecutionWorld.MAIN,
2424
},
25+
{
26+
id: 'renderer',
27+
matches: ['<all_urls>'],
28+
js: ['build/renderer.js'],
29+
runAt: 'document_start',
30+
world: chrome.scripting.ExecutionWorld.MAIN,
31+
},
2532
],
2633
function () {
2734
// When the content scripts are already registered, an error will be thrown.

packages/react-devtools-extensions/src/contentScripts/prepareInjection.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/* global chrome */
22

33
import nullthrows from 'nullthrows';
4+
import {SESSION_STORAGE_RELOAD_AND_PROFILE_KEY} from 'react-devtools-shared/src/constants';
5+
import {sessionStorageGetItem} from 'react-devtools-shared/src/storage';
46
import {IS_FIREFOX} from '../utils';
57

68
// We run scripts on the page via the service worker (backgroud.js) for
@@ -109,9 +111,15 @@ window.addEventListener('pageshow', function ({target}) {
109111
chrome.runtime.sendMessage(lastDetectionResult);
110112
});
111113

112-
// Inject a __REACT_DEVTOOLS_GLOBAL_HOOK__ global for React to interact with.
113-
// Only do this for HTML documents though, to avoid e.g. breaking syntax highlighting for XML docs.
114114
if (IS_FIREFOX) {
115+
// If we have just reloaded to profile, we need to inject the renderer interface before the app loads.
116+
if (
117+
sessionStorageGetItem(SESSION_STORAGE_RELOAD_AND_PROFILE_KEY) === 'true'
118+
) {
119+
injectScriptSync(chrome.runtime.getURL('build/renderer.js'));
120+
}
121+
// Inject a __REACT_DEVTOOLS_GLOBAL_HOOK__ global for React to interact with.
122+
// Only do this for HTML documents though, to avoid e.g. breaking syntax highlighting for XML docs.
115123
switch (document.contentType) {
116124
case 'text/html':
117125
case 'application/xhtml+xml': {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* In order to support reload-and-profile functionality, the renderer needs to be injected before any other scripts.
3+
* Since it is a complex file (with imports) we can't just toString() it like we do with the hook itself,
4+
* So this entry point (one of the web_accessible_resources) provides a way to eagerly inject it.
5+
* The hook will look for the presence of a global __REACT_DEVTOOLS_ATTACH__ and attach an injected renderer early.
6+
* The normal case (not a reload-and-profile) will not make use of this entry point though.
7+
*
8+
* @flow
9+
*/
10+
11+
import {attach} from 'react-devtools-shared/src/backend/renderer';
12+
import {SESSION_STORAGE_RELOAD_AND_PROFILE_KEY} from 'react-devtools-shared/src/constants';
13+
import {sessionStorageGetItem} from 'react-devtools-shared/src/storage';
14+
15+
if (
16+
sessionStorageGetItem(SESSION_STORAGE_RELOAD_AND_PROFILE_KEY) === 'true' &&
17+
!window.hasOwnProperty('__REACT_DEVTOOLS_ATTACH__')
18+
) {
19+
Object.defineProperty(
20+
window,
21+
'__REACT_DEVTOOLS_ATTACH__',
22+
({
23+
enumerable: false,
24+
// This property needs to be configurable to allow third-party integrations
25+
// to attach their own renderer. Note that using third-party integrations
26+
// is not officially supported. Use at your own risk.
27+
configurable: true,
28+
get() {
29+
return attach;
30+
},
31+
}: Object),
32+
);
33+
}

packages/react-devtools-extensions/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ module.exports = {
5555
panel: './src/panel.js',
5656
proxy: './src/contentScripts/proxy.js',
5757
prepareInjection: './src/contentScripts/prepareInjection.js',
58+
renderer: './src/contentScripts/renderer.js',
5859
installHook: './src/contentScripts/installHook.js',
5960
},
6061
output: {

packages/react-devtools-inline/src/backend.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import Agent from 'react-devtools-shared/src/backend/agent';
44
import Bridge from 'react-devtools-shared/src/bridge';
55
import {initBackend} from 'react-devtools-shared/src/backend';
6+
import {installConsoleFunctionsToWindow} from 'react-devtools-shared/src/backend/console';
67
import {installHook} from 'react-devtools-shared/src/hook';
78
import setupNativeStyleEditor from 'react-devtools-shared/src/backend/NativeStyleEditor/setupNativeStyleEditor';
89

@@ -119,5 +120,8 @@ export function createBridge(contentWindow: any, wall?: Wall): BackendBridge {
119120
}
120121

121122
export function initialize(contentWindow: any): void {
123+
// Install a global variable to allow patching console early (during injection).
124+
// This provides React Native developers with components stacks even if they don't run DevTools.
125+
installConsoleFunctionsToWindow();
122126
installHook(contentWindow);
123127
}

packages/react-devtools-shared/src/backend/console.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,3 +413,10 @@ export function writeConsolePatchSettingsToWindow(
413413
settings.hideConsoleLogsInStrictMode;
414414
window.__REACT_DEVTOOLS_BROWSER_THEME__ = settings.browserTheme;
415415
}
416+
417+
export function installConsoleFunctionsToWindow(): void {
418+
window.__REACT_DEVTOOLS_CONSOLE_FUNCTIONS__ = {
419+
patchConsoleUsingWindowValues,
420+
registerRendererWithConsole: registerRenderer,
421+
};
422+
}

packages/react-devtools-shared/src/backend/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ export function initBackend(
7676
}
7777

7878
// Notify the DevTools frontend about new renderers.
79-
// This includes any that were attached early
80-
// (when SESSION_STORAGE_RELOAD_AND_PROFILE_KEY is set to true).
79+
// This includes any that were attached early (via __REACT_DEVTOOLS_ATTACH__).
8180
if (rendererInterface != null) {
8281
hook.emit('renderer-attached', {
8382
id,

packages/react-devtools-shared/src/hook.js

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
22
* Install the hook on window, which is an event emitter.
3-
* Note because Chrome content scripts cannot directly modify the window object,
4-
* we are evaling this function by inserting a script tag.
5-
* That's why we have to inline the whole event emitter implementation,
3+
* Note: this global hook __REACT_DEVTOOLS_GLOBAL_HOOK__ is a de facto public API.
4+
* It's especially important to avoid creating direct dependency on the DevTools Backend.
5+
* That's why we still inline the whole event emitter implementation,
66
* the string format implementation, and part of the console implementation here.
77
*
88
* @flow
@@ -17,14 +17,6 @@ import type {
1717
RendererInterface,
1818
} from './backend/types';
1919

20-
import {
21-
patchConsoleUsingWindowValues,
22-
registerRenderer as registerRendererWithConsole,
23-
} from './backend/console';
24-
import {attach} from './backend/renderer';
25-
import {SESSION_STORAGE_RELOAD_AND_PROFILE_KEY} from './constants';
26-
import {sessionStorageGetItem} from './storage';
27-
2820
declare var window: any;
2921

3022
export function installHook(target: any): DevToolsHook | null {
@@ -340,37 +332,24 @@ export function installHook(target: any): DevToolsHook | null {
340332
// * Disabling or marking logs during a double render in Strict Mode
341333
// * Disable logging during re-renders to inspect hooks (see inspectHooksOfFiber)
342334
//
343-
// For React Native, we intentionally patch early (during injection).
344-
// This provides React Native developers with components stacks even if they don't run DevTools.
345-
//
346-
// This won't work for DOM though, since this entire file is eval'ed and inserted as a script tag.
347-
// In that case, we'll only patch parts of the console that are needed during the first render
348-
// and patch everything else later (when the frontend attaches).
349-
//
350-
// Don't patch in test environments because we don't want to interfere with Jest's own console overrides.
351-
//
352-
// Note that because this function is inlined, this conditional check must only use static booleans.
353-
// Otherwise the extension will throw with an undefined error.
354-
// (See comments in the try/catch below for more context on inlining.)
355-
if (!__TEST__ && !__EXTENSION__) {
356-
try {
357-
// The installHook() function is injected by being stringified in the browser,
358-
// so imports outside of this function do not get included.
359-
//
360-
// Normally we could check "typeof patchConsole === 'function'",
361-
// but Webpack wraps imports with an object (e.g. _backend_console__WEBPACK_IMPORTED_MODULE_0__)
362-
// and the object itself will be undefined as well for the reasons mentioned above,
363-
// so we use try/catch instead.
335+
// Allow patching console early (during injection) to
336+
// provide developers with components stacks even if they don't run DevTools.
337+
if (target.hasOwnProperty('__REACT_DEVTOOLS_CONSOLE_FUNCTIONS__')) {
338+
const {registerRendererWithConsole, patchConsoleUsingWindowValues} =
339+
target.__REACT_DEVTOOLS_CONSOLE_FUNCTIONS__;
340+
if (
341+
typeof registerRendererWithConsole === 'function' &&
342+
typeof patchConsoleUsingWindowValues === 'function'
343+
) {
364344
registerRendererWithConsole(renderer);
365345
patchConsoleUsingWindowValues();
366-
} catch (error) {}
346+
}
367347
}
368348

369349
// If we have just reloaded to profile, we need to inject the renderer interface before the app loads.
370350
// Otherwise the renderer won't yet exist and we can skip this step.
371-
if (
372-
sessionStorageGetItem(SESSION_STORAGE_RELOAD_AND_PROFILE_KEY) === 'true'
373-
) {
351+
const attach = target.__REACT_DEVTOOLS_ATTACH__;
352+
if (typeof attach === 'function') {
374353
const rendererInterface = attach(hook, id, renderer, target);
375354
hook.rendererInterfaces.set(id, rendererInterface);
376355
}

0 commit comments

Comments
 (0)