@@ -36,6 +36,33 @@ function loadScript(src) {
36
36
} ) ;
37
37
}
38
38
39
+ function loadModules ( SymbolSrcPairs ) {
40
+ let firstScript = document . getElementsByTagName ( 'script' ) [ 0 ] ;
41
+
42
+ let imports = '' ;
43
+ SymbolSrcPairs . map ( ( [ symbol , src ] ) => {
44
+ imports += `import ${ symbol } from "${ src } ";\n` ;
45
+ imports += `window.${ symbol } = ${ symbol } ;\n` ;
46
+ } ) ;
47
+
48
+ return new Promise ( ( resolve , reject ) => {
49
+ const timeout = setTimeout (
50
+ ( ) => reject ( new Error ( 'Timed out loading react modules over esm' ) ) ,
51
+ 5000
52
+ ) ;
53
+ window . __loaded = ( ) => {
54
+ clearTimeout ( timeout ) ;
55
+ resolve ( ) ;
56
+ } ;
57
+
58
+ const moduleScript = document . createElement ( 'script' ) ;
59
+ moduleScript . type = 'module' ;
60
+ moduleScript . textContent = imports + 'window.__loaded();' ;
61
+
62
+ firstScript . parentNode . insertBefore ( moduleScript , firstScript ) ;
63
+ } ) ;
64
+ }
65
+
39
66
function getVersion ( ) {
40
67
let query = parseQuery ( window . location . search ) ;
41
68
return query . version || 'local' ;
@@ -47,12 +74,15 @@ export function reactPaths(version = getVersion()) {
47
74
let environment = isProduction ? 'production.min' : 'development' ;
48
75
let reactPath = `react.${ environment } .js` ;
49
76
let reactDOMPath = `react-dom.${ environment } .js` ;
77
+ let reactDOMClientPath = `react-dom.${ environment } .js` ;
50
78
let reactDOMServerPath = `react-dom-server.browser.${ environment } .js` ;
51
79
let needsCreateElement = true ;
52
80
let needsReactDOM = true ;
81
+ let usingModules = false ;
53
82
54
83
if ( version !== 'local' ) {
55
84
const { major, minor, prerelease} = semver ( version ) ;
85
+ console . log ( 'semver' , semver ( version ) ) ;
56
86
57
87
if ( major === 0 ) {
58
88
needsCreateElement = minor >= 12 ;
@@ -62,7 +92,16 @@ export function reactPaths(version = getVersion()) {
62
92
const [ preReleaseStage ] = prerelease ;
63
93
// The file structure was updated in 16. This wasn't the case for alphas.
64
94
// Load the old module location for anything less than 16 RC
65
- if ( major >= 16 && ! ( minor === 0 && preReleaseStage === 'alpha' ) ) {
95
+ if ( major >= 19 ) {
96
+ usingModules = true ;
97
+ const devQuery = environment === 'development' ? '?dev' : '' ;
98
+ reactPath = 'https://esm.sh/react@' + version + '/' + devQuery ;
99
+ reactDOMPath = 'https://esm.sh/react-dom@' + version + '/' + devQuery ;
100
+ reactDOMClientPath =
101
+ 'https://esm.sh/react-dom@' + version + '/client' + devQuery ;
102
+ reactDOMServerPath =
103
+ 'https://esm.sh/react-dom@' + version + '/server.browser' + devQuery ;
104
+ } else if ( major >= 16 && ! ( minor === 0 && preReleaseStage === 'alpha' ) ) {
66
105
reactPath =
67
106
'https://unpkg.com/react@' +
68
107
version +
@@ -90,30 +129,50 @@ export function reactPaths(version = getVersion()) {
90
129
reactPath =
91
130
'https://cdnjs.cloudflare.com/ajax/libs/react/' + version + '/react.js' ;
92
131
}
132
+ } else {
133
+ throw new Error (
134
+ 'This fixture no longer works with local versions. Provide a version query parameter that matches a version published to npm to use the fixture.'
135
+ ) ;
93
136
}
94
137
95
138
return {
96
139
reactPath,
97
140
reactDOMPath,
141
+ reactDOMClientPath,
98
142
reactDOMServerPath,
99
143
needsCreateElement,
100
144
needsReactDOM,
145
+ usingModules,
101
146
} ;
102
147
}
103
148
104
149
export default function loadReact ( ) {
105
- const { reactPath, reactDOMPath, needsReactDOM} = reactPaths ( ) ;
106
-
107
- let request = loadScript ( reactPath ) ;
108
-
109
- if ( needsReactDOM ) {
110
- request = request . then ( ( ) => loadScript ( reactDOMPath ) ) ;
150
+ console . log ( 'reactPaths' , reactPaths ( ) ) ;
151
+ const {
152
+ reactPath,
153
+ reactDOMPath,
154
+ reactDOMClientPath,
155
+ needsReactDOM,
156
+ usingModules,
157
+ } = reactPaths ( ) ;
158
+
159
+ if ( usingModules ) {
160
+ return loadModules ( [
161
+ [ 'React' , reactPath ] ,
162
+ [ 'ReactDOM' , reactDOMPath ] ,
163
+ [ 'ReactDOMClient' , reactDOMClientPath ] ,
164
+ ] ) ;
111
165
} else {
112
- // Aliasing React to ReactDOM for compatibility.
113
- request = request . then ( ( ) => {
114
- window . ReactDOM = window . React ;
115
- } ) ;
116
- }
166
+ let request = loadScript ( reactPath , usingModules ) ;
117
167
118
- return request ;
168
+ if ( needsReactDOM ) {
169
+ request = request . then ( ( ) => loadScript ( reactDOMPath , usingModules ) ) ;
170
+ } else {
171
+ // Aliasing React to ReactDOM for compatibility.
172
+ request = request . then ( ( ) => {
173
+ window . ReactDOM = window . React ;
174
+ } ) ;
175
+ }
176
+ return request ;
177
+ }
119
178
}
0 commit comments