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. __webpack_public_path__ = '/dist/'; // eslint-disable-line no-undef
    
  13. 
    
  14. // TODO (Webpack 5) Hopefully we can remove this prop after the Webpack 5 migration.
    
  15. function hookNamesModuleLoaderFunction() {
    
  16.   return import('react-devtools-inline/hookNames');
    
  17. }
    
  18. 
    
  19. function inject(contentDocument, sourcePath, callback) {
    
  20.   const script = contentDocument.createElement('script');
    
  21.   script.onload = callback;
    
  22.   script.src = sourcePath;
    
  23. 
    
  24.   ((contentDocument.body: any): HTMLBodyElement).appendChild(script);
    
  25. }
    
  26. 
    
  27. function init(appIframe, devtoolsContainer, appSource) {
    
  28.   const {contentDocument, contentWindow} = appIframe;
    
  29. 
    
  30.   initializeBackend(contentWindow);
    
  31. 
    
  32.   const DevTools = createDevTools(contentWindow);
    
  33. 
    
  34.   inject(contentDocument, appSource, () => {
    
  35.     createRoot(devtoolsContainer).render(
    
  36.       <DevTools
    
  37.         hookNamesModuleLoaderFunction={hookNamesModuleLoaderFunction}
    
  38.         showTabBar={true}
    
  39.       />,
    
  40.     );
    
  41.   });
    
  42. 
    
  43.   activateBackend(contentWindow);
    
  44. }
    
  45. 
    
  46. const iframe = document.getElementById('iframe');
    
  47. const devtoolsContainer = document.getElementById('devtools');
    
  48. 
    
  49. const {protocol, hostname} = window.location;
    
  50. const port = 8181; // secondary webpack server port
    
  51. init(
    
  52.   iframe,
    
  53.   devtoolsContainer,
    
  54.   `${protocol}//${hostname}:${port}/dist/e2e-app-regression.js`,
    
  55. );
    
  56. 
    
  57. // ReactDOM Test Selector APIs used by Playwright e2e tests
    
  58. window.parent.REACT_DOM_DEVTOOLS = ReactDOM;