@@ -135,12 +135,29 @@ protected virtual void InitialiseEngine(IJsEngine engine)
135135 {
136136 var thisAssembly = typeof ( ReactEnvironment ) . Assembly ;
137137 engine . ExecuteResource ( "React.Resources.shims.js" , thisAssembly ) ;
138- engine . ExecuteResource ( "React.Resources.react-with-addons.js" , thisAssembly ) ;
139- engine . Execute ( "var React = global.React" ) ;
140- engine . ExecuteResource ( "React.Resources.JSXTransformer.js" , thisAssembly ) ;
138+ if ( _config . LoadReact )
139+ {
140+ engine . ExecuteResource ( "React.Resources.react-with-addons.js" , thisAssembly ) ;
141+ engine . Execute ( "React = global.React" ) ;
142+ engine . ExecuteResource ( "React.Resources.JSXTransformer.js" , thisAssembly ) ;
143+ }
141144
142- // Only scripts that don't need JSX transformation can run immediately here
143- // JSX files are loaded in ReactEnvironment.
145+ LoadUserScripts ( engine ) ;
146+ if ( ! _config . LoadReact )
147+ {
148+ // We expect to user to have loaded their own versino of React in the scripts that
149+ // were loaded above, let's ensure that's the case.
150+ EnsureReactLoaded ( engine ) ;
151+ }
152+ }
153+
154+ /// <summary>
155+ /// Loads any user-provided scripts. Only scripts that don't need JSX transformation can
156+ /// run immediately here. JSX files are loaded in ReactEnvironment.
157+ /// </summary>
158+ /// <param name="engine">Engine to load scripts into</param>
159+ private void LoadUserScripts ( IJsEngine engine )
160+ {
144161 foreach ( var file in _config . ScriptsWithoutTransform )
145162 {
146163 try
@@ -161,6 +178,23 @@ protected virtual void InitialiseEngine(IJsEngine engine)
161178 }
162179 }
163180
181+ /// <summary>
182+ /// Ensures that React has been correctly loaded into the specified engine.
183+ /// </summary>
184+ /// <param name="engine">Engine to check</param>
185+ private static void EnsureReactLoaded ( IJsEngine engine )
186+ {
187+ var result = engine . CallFunction < bool > ( "ReactNET_initReact" ) ;
188+ if ( ! result )
189+ {
190+ throw new ReactNotInitialisedException (
191+ "React has not been loaded correctly. Please expose your version of React as a global " +
192+ "variable named 'React', or enable the 'LoadReact' configuration option to " +
193+ "use the built-in version of React."
194+ ) ;
195+ }
196+ }
197+
164198 /// <summary>
165199 /// Gets the JavaScript engine for the current thread. It is recommended to use
166200 /// <see cref="GetEngine"/> instead, which will pool/reuse engines.
0 commit comments