1
+ /* eslint-disable no-underscore-dangle */
1
2
import React from 'react' ;
2
3
import configuration from '../configuration' ;
3
4
import { enterHotUpdate } from '../global/generation' ;
@@ -6,10 +7,36 @@ import { resolveType } from './resolver';
6
7
7
8
const lazyConstructor = '_ctor' ;
8
9
10
+ const getLazyConstructor = target => {
11
+ // React 16
12
+ if ( target [ lazyConstructor ] ) {
13
+ return target [ lazyConstructor ] ;
14
+ }
15
+
16
+ // React 17
17
+ if ( target . _payload ) {
18
+ return target . _payload . _result ;
19
+ }
20
+ return null ;
21
+ } ;
22
+
23
+ const setLazyConstructor = ( target , replacement ) => {
24
+ replacement . isPatchedByReactHotLoader = true ;
25
+
26
+ // React 16
27
+ if ( target [ lazyConstructor ] ) {
28
+ target [ lazyConstructor ] = replacement ;
29
+ }
30
+ // React 17
31
+ if ( target . _payload ) {
32
+ target . _payload . _result = replacement ;
33
+ }
34
+ } ;
35
+
9
36
const patchLazyConstructor = target => {
10
- if ( ! configuration . trackTailUpdates && ! target [ lazyConstructor ] . isPatchedByReactHotLoader ) {
11
- const ctor = target [ lazyConstructor ] ;
12
- target [ lazyConstructor ] = ( ) =>
37
+ if ( ! configuration . trackTailUpdates && ! getLazyConstructor ( target ) . isPatchedByReactHotLoader ) {
38
+ const ctor = getLazyConstructor ( target ) ;
39
+ setLazyConstructor ( target , ( ) =>
13
40
ctor ( ) . then ( m => {
14
41
const C = resolveType ( m . default ) ;
15
42
// chunks has been updated - new hot loader process is taking a place
@@ -30,14 +57,14 @@ const patchLazyConstructor = target => {
30
57
</ AppContainer >
31
58
) ) ,
32
59
} ;
33
- } ) ;
34
- target [ lazyConstructor ] . isPatchedByReactHotLoader = true ;
60
+ } ) ,
61
+ ) ;
35
62
}
36
63
} ;
37
64
38
65
export const updateLazy = ( target , type ) => {
39
- const ctor = type [ lazyConstructor ] ;
40
- if ( target [ lazyConstructor ] !== type [ lazyConstructor ] ) {
66
+ const ctor = getLazyConstructor ( type ) ;
67
+ if ( getLazyConstructor ( target ) !== ctor ) {
41
68
// just execute `import` and RHL.register will do the job
42
69
ctor ( ) ;
43
70
}
0 commit comments