|
| 1 | +import * as React from 'react'; |
| 2 | +import * as ReactDOM from 'react-dom'; |
| 3 | +import {createRoot} from 'react-dom/client'; |
| 4 | +import { |
| 5 | + activate as activateBackend, |
| 6 | + initialize as initializeBackend, |
| 7 | +} from 'react-devtools-inline/backend'; |
| 8 | +import {initialize as createDevTools} from 'react-devtools-inline/frontend'; |
| 9 | + |
| 10 | +// This is a pretty gross hack to make the runtime loaded named-hooks-code work. |
| 11 | +// TODO (Webpack 5) Hoepfully we can remove this once we upgrade to Webpack 5. |
| 12 | +// $FlowFixMer |
| 13 | +__webpack_public_path__ = '/dist/'; // eslint-disable-line no-undef |
| 14 | + |
| 15 | +// TODO (Webpack 5) Hopefully we can remove this prop after the Webpack 5 migration. |
| 16 | +function hookNamesModuleLoaderFunction() { |
| 17 | + return import('react-devtools-inline/hookNames'); |
| 18 | +} |
| 19 | + |
| 20 | +function inject(contentDocument, sourcePath, callback) { |
| 21 | + const script = contentDocument.createElement('script'); |
| 22 | + script.onload = callback; |
| 23 | + script.src = sourcePath; |
| 24 | + |
| 25 | + ((contentDocument.body: any): HTMLBodyElement).appendChild(script); |
| 26 | +} |
| 27 | + |
| 28 | +function init(appIframe, devtoolsContainer, appSource) { |
| 29 | + const {contentDocument, contentWindow} = appIframe; |
| 30 | + |
| 31 | + initializeBackend(contentWindow); |
| 32 | + |
| 33 | + const DevTools = createDevTools(contentWindow); |
| 34 | + |
| 35 | + inject(contentDocument, appSource, () => { |
| 36 | + // $FlowFixMe Flow doesn't know about createRoot() yet. |
| 37 | + createRoot(devtoolsContainer).render( |
| 38 | + <DevTools |
| 39 | + hookNamesModuleLoaderFunction={hookNamesModuleLoaderFunction} |
| 40 | + showTabBar={true} |
| 41 | + />, |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + activateBackend(contentWindow); |
| 46 | +} |
| 47 | + |
| 48 | +const iframe = document.getElementById('iframe'); |
| 49 | +const devtoolsContainer = document.getElementById('devtools'); |
| 50 | + |
| 51 | +init(iframe, devtoolsContainer, 'dist/e2e-app-regression.js'); |
| 52 | + |
| 53 | +// ReactDOM Test Selector APIs used by Playwright e2e tests |
| 54 | +window.parent.REACT_DOM_DEVTOOLS = ReactDOM; |
0 commit comments