1
+ /* eslint-disable global-require,@typescript-eslint/no-require-imports */
1
2
import type { ReactElement } from 'react' ;
2
- import * as ReactDOM from 'react-dom' ;
3
3
import type { RenderReturnType } from './_types.ts' ;
4
4
import { supportsRootApi } from './reactApis.ts' ;
5
5
@@ -8,26 +8,33 @@ type HydrateOrRenderType = (domNode: Element, reactElement: ReactElement) => Ren
8
8
// TODO: once React dependency is updated to >= 18, we can remove this and just
9
9
// import ReactDOM from 'react-dom/client';
10
10
let reactDomClient : typeof import ( 'react-dom/client' ) ;
11
+ // Can't just import react-dom because that breaks ESM under React 19
12
+ let reactDom : typeof import ( 'react-dom' ) ;
11
13
if ( supportsRootApi ) {
12
14
// This will never throw an exception, but it's the way to tell Webpack the dependency is optional
13
15
// https://github.com/webpack/webpack/issues/339#issuecomment-47739112
14
16
// Unfortunately, it only converts the error to a warning.
15
17
try {
16
- // eslint-disable-next-line global-require,@typescript-eslint/no-require-imports
17
18
reactDomClient = require ( 'react-dom/client' ) as typeof import ( 'react-dom/client' ) ;
18
19
} catch ( _e ) {
19
20
// We should never get here, but if we do, we'll just use the default ReactDOM
20
21
// and live with the warning.
21
- reactDomClient = ReactDOM as unknown as typeof import ( 'react-dom/client' ) ;
22
+ reactDomClient = require ( 'react-dom' ) as unknown as typeof import ( 'react-dom/client' ) ;
23
+ }
24
+ } else {
25
+ try {
26
+ reactDom = require ( 'react-dom' ) as typeof import ( 'react-dom' ) ;
27
+ } catch ( _e ) {
28
+ // Also should never happen
22
29
}
23
30
}
24
31
25
- /* eslint-disable react/no-deprecated, @typescript-eslint/no-deprecated,@typescript-eslint/no-non-null-assertion --
32
+ /* eslint-disable @typescript-eslint/no-deprecated,@typescript-eslint/no-non-null-assertion --
26
33
* while we need to support React 16
27
34
*/
28
35
const reactHydrate : HydrateOrRenderType = supportsRootApi
29
36
? reactDomClient ! . hydrateRoot
30
- : ( domNode , reactElement ) => ReactDOM . hydrate ( reactElement , domNode ) ;
37
+ : ( domNode , reactElement ) => reactDom ! . hydrate ( reactElement , domNode ) ;
31
38
32
39
function reactRender ( domNode : Element , reactElement : ReactElement ) : RenderReturnType {
33
40
if ( supportsRootApi ) {
@@ -36,10 +43,9 @@ function reactRender(domNode: Element, reactElement: ReactElement): RenderReturn
36
43
return root ;
37
44
}
38
45
39
- // eslint-disable-next-line react/no-render-return-value
40
- return ReactDOM . render ( reactElement , domNode ) ;
46
+ return reactDom ! . render ( reactElement , domNode ) ;
41
47
}
42
- /* eslint-enable react/no-deprecated, @typescript-eslint/no-deprecated,@typescript-eslint/no-non-null-assertion */
48
+ /* eslint-enable @typescript-eslint/no-deprecated,@typescript-eslint/no-non-null-assertion */
43
49
44
50
export default function reactHydrateOrRender (
45
51
domNode : Element ,
0 commit comments