11import { version as reactVersion } from 'react' ;
22import ReactDOM from 'react-dom' ;
3- import type { Root } from 'react-dom/client' ;
3+ import type { Root , createRoot as createRootOriginal } from 'react-dom/client' ;
44
55type Renderable = Parameters < ReactDOM . Renderer > [ 0 ] [ number ] ;
66
77const [ , major ] = / ^ ( \d + ) \. \d + \. \d + $ / . exec ( reactVersion ) || [ undefined , '16' ] ;
88const reactMajor = Number ( major ) ;
99
1010const isPreEighteen = reactMajor < 18 ;
11+ const REACT_DOM_CLIENT_IMPORT = isPreEighteen
12+ ? './react-dom-client-polyfill'
13+ : 'react-dom/client' ;
1114
1215class ReactHTMLElement extends HTMLElement {
1316 private _initialized ?: boolean ;
@@ -49,7 +52,12 @@ class ReactHTMLElement extends HTMLElement {
4952 async root ( ) : Promise < Root > {
5053 if ( this . _root ) return this . _root ;
5154
52- const { createRoot } = await import ( 'react-dom/client' ) ;
55+ const { createRoot } = ( await import (
56+ /* webpackExports: ['createRoot'] */
57+ `${ REACT_DOM_CLIENT_IMPORT } `
58+ ) ) as {
59+ createRoot : typeof createRootOriginal ;
60+ } ;
5361 this . _root = createRoot ( this . mountPoint ) ;
5462 return this . _root ;
5563 }
@@ -62,10 +70,10 @@ class ReactHTMLElement extends HTMLElement {
6270 return ;
6371 }
6472
65- void this . rootRender ( app ) ;
73+ void this . renderRoot ( app ) ;
6674 }
6775
68- async rootRender ( app : Renderable ) : Promise < void > {
76+ async renderRoot ( app : Renderable ) : Promise < void > {
6977 const root = await this . root ( ) ;
7078 root . render ( app ) ;
7179 }
0 commit comments