| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | * |
| 7 | * @noflow |
| 8 | */ |
| 9 | |
| 10 | import * as React from 'react'; |
| 11 | import * as ReactDOMClient from 'react-dom/client'; |
| 12 | import { |
| 13 | activate as activateBackend, |
| 14 | initialize as initializeBackend, |
| 15 | } from 'react-devtools-inline/backend'; |
| 16 | import {initialize as createDevTools} from 'react-devtools-inline/frontend'; |
| 17 | |
| 18 | // This is a pretty gross hack to make the runtime loaded named-hooks-code work. |
| 19 | // TODO (Webpack 5) Hoepfully we can remove this once we upgrade to Webpack 5. |
| 20 | __webpack_public_path__ = '/dist/'; // eslint-disable-line no-undef |
| 21 | |
| 22 | // TODO (Webpack 5) Hopefully we can remove this prop after the Webpack 5 migration. |
| 23 | function hookNamesModuleLoaderFunction() { |
| 24 | return import('react-devtools-inline/hookNames'); |
| 25 | } |
| 26 | |
| 27 | function inject(contentDocument, sourcePath, callback) { |
| 28 | const script = contentDocument.createElement('script'); |
| 29 | script.onload = callback; |
| 30 | script.src = sourcePath; |
| 31 | |
| 32 | ((contentDocument.body: any): HTMLBodyElement).appendChild(script); |
| 33 | } |
| 34 | |
| 35 | function init(appIframe, devtoolsContainer, appSource) { |
| 36 | const {contentDocument, contentWindow} = appIframe; |
| 37 | |
| 38 | initializeBackend(contentWindow); |
| 39 | |
| 40 | const DevTools = createDevTools(contentWindow); |
| 41 | |
| 42 | inject(contentDocument, appSource, () => { |
| 43 | ReactDOMClient.createRoot(devtoolsContainer).render( |
| 44 | <DevTools |
| 45 | hookNamesModuleLoaderFunction={hookNamesModuleLoaderFunction} |
| 46 | showTabBar={true} |
| 47 | />, |
| 48 | ); |
| 49 | }); |
| 50 | |
| 51 | activateBackend(contentWindow); |
| 52 | } |
| 53 | |
| 54 | const iframe = document.getElementById('iframe'); |
| 55 | const devtoolsContainer = document.getElementById('devtools'); |
| 56 | |
| 57 | init(iframe, devtoolsContainer, 'dist/e2e-app.js'); |
| 58 | |
| 59 | // ReactDOM Test Selector APIs used by Playwright e2e tests |
| 60 | window.parent.REACT_DOM_DEVTOOLS = ReactDOMClient; |