main
js 54 lines 1.61 KB
Raw
1 import * as React from 'react';
2 import {createRoot} from 'react-dom/client';
3 import {
4 activate as activateBackend,
5 initialize as initializeBackend,
6 } from 'react-devtools-inline/backend';
7 import {initialize as createDevTools} from 'react-devtools-inline/frontend';
8
9 // This is a pretty gross hack to make the runtime loaded named-hooks-code work.
10 // TODO (Webpack 5) Hoepfully we can remove this once we upgrade to Webpack 5.
11 __webpack_public_path__ = '/dist/'; // eslint-disable-line no-undef
12
13 // TODO (Webpack 5) Hopefully we can remove this prop after the Webpack 5 migration.
14 function hookNamesModuleLoaderFunction() {
15 return import('react-devtools-inline/hookNames');
16 }
17
18 function inject(contentDocument, sourcePath) {
19 const script = contentDocument.createElement('script');
20 script.src = sourcePath;
21
22 ((contentDocument.body: any): HTMLBodyElement).appendChild(script);
23 }
24
25 function init(
26 appSource: string,
27 appIframe: HTMLIFrameElement,
28 devtoolsContainer: HTMLElement,
29 loadDevToolsButton: HTMLButtonElement,
30 ) {
31 const {contentDocument, contentWindow} = appIframe;
32
33 initializeBackend(contentWindow);
34
35 inject(contentDocument, appSource);
36
37 loadDevToolsButton.addEventListener('click', () => {
38 const DevTools = createDevTools(contentWindow);
39 createRoot(devtoolsContainer).render(
40 <DevTools
41 hookNamesModuleLoaderFunction={hookNamesModuleLoaderFunction}
42 showTabBar={true}
43 />,
44 );
45 activateBackend(contentWindow);
46 });
47 }
48
49 init(
50 'dist/perf-regression-app.js',
51 document.getElementById('iframe'),
52 document.getElementById('devtools'),
53 document.getElementById('load-devtools'),
54 );