-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(devtools): refactor mounting logic and inject options to client (#…
- Loading branch information
Showing
7 changed files
with
78 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@modern-js/devtools-client': patch | ||
'@modern-js/plugin-devtools': patch | ||
--- | ||
|
||
refactor(devtools): mounting logic and inject options to client |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,38 @@ | ||
import './react-devtools-backend'; | ||
import './state'; | ||
import { createRoot } from 'react-dom/client'; | ||
import { parseQuery } from 'ufo'; | ||
import _ from 'lodash'; | ||
import { SetupClientParams } from '@modern-js/devtools-kit'; | ||
import styles from './index.module.scss'; | ||
import { DevtoolsActionButton } from '@/components/Devtools/Action'; | ||
|
||
// @ts-expect-error | ||
const { container, resourceQuery } = window._modern_js_devtools_app as { | ||
container: HTMLDivElement; | ||
resourceQuery: string; | ||
}; | ||
container.classList.add(styles.container); | ||
const root = createRoot(container); | ||
const parsed = parseQuery(resourceQuery); | ||
if ( | ||
!_.isString(parsed.dataSource) || | ||
!_.isString(parsed.def) || | ||
!_.isString(parsed.endpoint) | ||
) { | ||
throw new TypeError( | ||
`Failed to parse client definitions of devtools: ${resourceQuery}`, | ||
); | ||
declare global { | ||
interface Window { | ||
__MODERN_JS_DEVTOOLS_OPTIONS__: SetupClientParams; | ||
} | ||
} | ||
const options: SetupClientParams = { | ||
dataSource: parsed.dataSource, | ||
endpoint: parsed.endpoint, | ||
def: JSON.parse(parsed.def), | ||
}; | ||
|
||
root.render(<DevtoolsActionButton {...options} />); | ||
document.addEventListener('DOMContentLoaded', () => { | ||
const outer = document.createElement('div'); | ||
outer.className = '_modern_js_devtools_container'; | ||
document.body.appendChild(outer); | ||
|
||
const shadow = outer.attachShadow({ mode: 'closed' }); | ||
|
||
const template = document.getElementById('_modern_js_devtools_styles'); | ||
if (!(template instanceof HTMLTemplateElement)) { | ||
throw new Error('template not found'); | ||
} | ||
shadow.appendChild(template.content); | ||
|
||
const container = document.createElement('div'); | ||
container.classList.add( | ||
'_modern_js_devtools_mountpoint', | ||
'theme-register', | ||
styles.container, | ||
); | ||
shadow.appendChild(container); | ||
|
||
const options = window.__MODERN_JS_DEVTOOLS_OPTIONS__; | ||
const root = createRoot(container); | ||
root.render(<DevtoolsActionButton {...options} />); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters