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