2019-01-23 03:04:37 +08:00
|
|
|
/** @flow */
|
|
|
|
|
|
2019-08-14 08:58:03 +08:00
|
|
|
import {createElement} from 'react';
|
2019-01-27 23:58:09 +08:00
|
|
|
// $FlowFixMe Flow does not yet know about createRoot()
|
2021-05-12 23:28:14 +08:00
|
|
|
import {createRoot} from 'react-dom';
|
2019-08-06 01:09:26 +08:00
|
|
|
import {
|
|
|
|
|
activate as activateBackend,
|
|
|
|
|
initialize as initializeBackend,
|
|
|
|
|
} from 'react-devtools-inline/backend';
|
2019-08-14 08:58:03 +08:00
|
|
|
import {initialize as initializeFrontend} from 'react-devtools-inline/frontend';
|
2019-08-14 09:11:59 +08:00
|
|
|
import {initDevTools} from 'react-devtools-shared/src/devtools';
|
2019-01-23 03:04:37 +08:00
|
|
|
|
2021-09-10 03:25:26 +08:00
|
|
|
// This is a pretty gross hack to make the runtime loaded named-hooks-code work.
|
|
|
|
|
// TODO (Webpack 5) Hoepfully we can remove this once we upgrade to Webpack 5.
|
|
|
|
|
// $FlowFixMe
|
|
|
|
|
__webpack_public_path__ = '/dist/'; // eslint-disable-line no-undef
|
|
|
|
|
|
2019-01-23 03:04:37 +08:00
|
|
|
const iframe = ((document.getElementById('target'): any): HTMLIFrameElement);
|
|
|
|
|
|
2019-08-14 08:58:03 +08:00
|
|
|
const {contentDocument, contentWindow} = iframe;
|
2019-01-23 03:04:37 +08:00
|
|
|
|
2019-08-06 01:09:26 +08:00
|
|
|
// Helps with positioning Overlay UI.
|
|
|
|
|
contentWindow.__REACT_DEVTOOLS_TARGET_WINDOW__ = window;
|
2019-05-09 05:22:06 +08:00
|
|
|
|
2019-08-06 01:09:26 +08:00
|
|
|
initializeBackend(contentWindow);
|
2019-01-23 03:04:37 +08:00
|
|
|
|
2021-08-26 06:35:38 +08:00
|
|
|
// Initialize the front end and activate the backend early so that we are able
|
|
|
|
|
// to pass console settings in local storage to the backend before initial render
|
|
|
|
|
const DevTools = initializeFrontend(contentWindow);
|
|
|
|
|
|
|
|
|
|
// Activate the backend only once the DevTools frontend Store has been initialized.
|
|
|
|
|
// Otherwise the Store may miss important initial tree op codes.
|
|
|
|
|
activateBackend(contentWindow);
|
|
|
|
|
|
2019-01-27 23:58:09 +08:00
|
|
|
const container = ((document.getElementById('devtools'): any): HTMLElement);
|
|
|
|
|
|
|
|
|
|
let isTestAppMounted = true;
|
|
|
|
|
|
|
|
|
|
const mountButton = ((document.getElementById(
|
2019-08-14 09:12:19 +08:00
|
|
|
'mountButton',
|
2019-01-27 23:58:09 +08:00
|
|
|
): any): HTMLButtonElement);
|
|
|
|
|
mountButton.addEventListener('click', function() {
|
|
|
|
|
if (isTestAppMounted) {
|
|
|
|
|
if (typeof window.unmountTestApp === 'function') {
|
|
|
|
|
window.unmountTestApp();
|
|
|
|
|
mountButton.innerText = 'Mount test app';
|
|
|
|
|
isTestAppMounted = false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (typeof window.mountTestApp === 'function') {
|
|
|
|
|
window.mountTestApp();
|
|
|
|
|
mountButton.innerText = 'Unmount test app';
|
|
|
|
|
isTestAppMounted = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2021-09-10 03:25:26 +08:00
|
|
|
// TODO (Webpack 5) Hopefully we can remove this prop after the Webpack 5 migration.
|
|
|
|
|
function hookNamesModuleLoaderFunction() {
|
|
|
|
|
return import('react-devtools-inline/hookNames');
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-03 23:55:31 +08:00
|
|
|
inject('dist/app.js', () => {
|
2019-02-01 06:27:16 +08:00
|
|
|
initDevTools({
|
|
|
|
|
connect(cb) {
|
2019-01-27 23:58:09 +08:00
|
|
|
const root = createRoot(container);
|
2019-08-06 01:09:26 +08:00
|
|
|
root.render(
|
2019-02-13 09:41:39 +08:00
|
|
|
createElement(DevTools, {
|
|
|
|
|
browserTheme: 'light',
|
2019-12-19 04:12:34 +08:00
|
|
|
enabledInspectedElementContextMenu: true,
|
2021-09-10 03:25:26 +08:00
|
|
|
hookNamesModuleLoaderFunction,
|
2019-02-13 09:41:39 +08:00
|
|
|
showTabBar: true,
|
2019-07-19 07:24:22 +08:00
|
|
|
warnIfLegacyBackendDetected: true,
|
2019-09-26 23:41:46 +08:00
|
|
|
warnIfUnsupportedVersionDetected: true,
|
2019-08-14 09:12:19 +08:00
|
|
|
}),
|
2019-01-23 03:04:37 +08:00
|
|
|
);
|
2019-02-01 06:27:16 +08:00
|
|
|
},
|
2019-01-23 03:04:37 +08:00
|
|
|
|
2019-02-01 06:27:16 +08:00
|
|
|
onReload(reloadFn) {
|
|
|
|
|
iframe.onload = reloadFn;
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
2019-01-23 03:04:37 +08:00
|
|
|
|
|
|
|
|
function inject(sourcePath, callback) {
|
2019-01-24 00:45:19 +08:00
|
|
|
const script = contentDocument.createElement('script');
|
2019-01-23 03:04:37 +08:00
|
|
|
script.onload = callback;
|
|
|
|
|
script.src = sourcePath;
|
|
|
|
|
|
|
|
|
|
((contentDocument.body: any): HTMLBodyElement).appendChild(script);
|
2019-01-24 00:45:19 +08:00
|
|
|
}
|