@@ -36,6 +36,33 @@ function loadScript(src) {
3636 } ) ;
3737}
3838
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+
3966function getVersion ( ) {
4067 let query = parseQuery ( window . location . search ) ;
4168 return query . version || 'local' ;
@@ -47,12 +74,15 @@ export function reactPaths(version = getVersion()) {
4774 let environment = isProduction ? 'production.min' : 'development' ;
4875 let reactPath = `react.${ environment } .js` ;
4976 let reactDOMPath = `react-dom.${ environment } .js` ;
77+ let reactDOMClientPath = `react-dom.${ environment } .js` ;
5078 let reactDOMServerPath = `react-dom-server.browser.${ environment } .js` ;
5179 let needsCreateElement = true ;
5280 let needsReactDOM = true ;
81+ let usingModules = false ;
5382
5483 if ( version !== 'local' ) {
5584 const { major, minor, prerelease} = semver ( version ) ;
85+ console . log ( 'semver' , semver ( version ) ) ;
5686
5787 if ( major === 0 ) {
5888 needsCreateElement = minor >= 12 ;
@@ -62,7 +92,16 @@ export function reactPaths(version = getVersion()) {
6292 const [ preReleaseStage ] = prerelease ;
6393 // The file structure was updated in 16. This wasn't the case for alphas.
6494 // 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' ) ) {
66105 reactPath =
67106 'https://unpkg.com/react@' +
68107 version +
@@ -90,30 +129,50 @@ export function reactPaths(version = getVersion()) {
90129 reactPath =
91130 'https://cdnjs.cloudflare.com/ajax/libs/react/' + version + '/react.js' ;
92131 }
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+ ) ;
93136 }
94137
95138 return {
96139 reactPath,
97140 reactDOMPath,
141+ reactDOMClientPath,
98142 reactDOMServerPath,
99143 needsCreateElement,
100144 needsReactDOM,
145+ usingModules,
101146 } ;
102147}
103148
104149export 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+ ] ) ;
111165 } 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 ) ;
117167
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+ }
119178}
0 commit comments