File tree Expand file tree Collapse file tree 6 files changed +96
-1
lines changed Expand file tree Collapse file tree 6 files changed +96
-1
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ * Copyright (c) 2014-2015, Facebook, Inc.
3+ * All rights reserved.
4+ *
5+ * This source code is licensed under the BSD-style license found in the
6+ * LICENSE file in the root directory of this source tree. An additional grant
7+ * of patent rights can be found in the PATENTS file in the same directory.
8+ */
9+
10+ using System ;
11+ using System . Runtime . Serialization ;
12+
13+ namespace React . Exceptions
14+ {
15+ /// <summary>
16+ /// Thrown when Babel is required but has not been loaded.
17+ /// </summary>
18+ [ Serializable ]
19+ public class BabelNotLoadedException : ReactException
20+ {
21+ /// <summary>
22+ /// Initializes a new instance of the <see cref="BabelNotLoadedException"/> class.
23+ /// </summary>
24+ public BabelNotLoadedException ( ) : base ( GetMessage ( ) ) { }
25+
26+ /// <summary>
27+ /// Used by deserialization
28+ /// </summary>
29+ protected BabelNotLoadedException ( SerializationInfo info , StreamingContext context )
30+ : base ( info , context )
31+ { }
32+
33+ /// <summary>
34+ /// Gets a message that describes the current exception.
35+ /// </summary>
36+ private static string GetMessage ( )
37+ {
38+ return
39+ "Babel has not been loaded, so JSX transformation can not be done. Please either " +
40+ "transform your JavaScript files through an external tool (such as Babel, " +
41+ "Webpack, Browserify or Gulp) and use the \" AddScriptWithoutTransform\" method to load " +
42+ "them for server-side rendering, or enable the \" LoadBabel\" option in the ReactJS.NET " +
43+ "configuration. Refer to the ReactJS.NET documentation for more details." ;
44+ }
45+ }
46+ }
Original file line number Diff line number Diff line change 77 * of patent rights can be found in the PATENTS file in the same directory.
88 */
99
10+ using System ;
1011using Newtonsoft . Json ;
1112using System . Collections . Generic ;
1213
@@ -123,6 +124,19 @@ public interface IReactSiteConfiguration
123124 /// <returns>The configuration, for chaining</returns>
124125 IReactSiteConfiguration SetLoadReact ( bool loadReact ) ;
125126
127+ /// <summary>
128+ /// Gets or sets whether Babel is loading. Disabling the loading of Babel can improve startup
129+ /// performance, but all your JSX files must be transformed beforehand (eg. through Babel,
130+ /// Webpack or Browserify).
131+ /// </summary>
132+ bool LoadBabel { get ; set ; }
133+ /// <summary>
134+ /// Sets whether Babel is loading. Disabling the loading of Babel can improve startup
135+ /// performance, but all your JSX files must be transformed beforehand (eg. through Babel,
136+ /// Webpack or Browserify).
137+ /// </summary>
138+ IReactSiteConfiguration SetLoadBabel ( bool loadBabel ) ;
139+
126140 /// <summary>
127141 /// Gets or sets the Babel configuration to use.
128142 /// </summary>
Original file line number Diff line number Diff line change @@ -100,7 +100,9 @@ protected virtual void InitialiseEngine(IJsEngine engine)
100100 {
101101 engine . ExecuteResource ( "React.Resources.react-with-addons.js" , thisAssembly ) ;
102102 engine . Execute ( "React = global.React" ) ;
103- // TODO: Make this configurable, so we don't load Babel if it's not needed.
103+ }
104+ if ( _config . LoadBabel )
105+ {
104106 engine . ExecuteResource ( "React.node_modules.babel_core.browser.js" , thisAssembly ) ;
105107 }
106108
Original file line number Diff line number Diff line change @@ -253,6 +253,7 @@ protected virtual JavaScriptWithSourceMap TransformJsxWithHeader(
253253 /// <returns>JavaScript</returns>
254254 public virtual string TransformJsx ( string input , string filename = "unknown" )
255255 {
256+ EnsureBabelLoaded ( ) ;
256257 try
257258 {
258259 var output = _environment . ExecuteWithLargerStackIfRequired < string > (
@@ -281,6 +282,7 @@ public virtual JavaScriptWithSourceMap TransformJsxWithSourceMap(
281282 string filename = "unknown"
282283 )
283284 {
285+ EnsureBabelLoaded ( ) ;
284286 try
285287 {
286288 return _environment . ExecuteWithLargerStackIfRequired < JavaScriptWithSourceMap > (
@@ -356,5 +358,16 @@ string filename
356358 _fileSystem . WriteAsString ( sourceMapPath , result . SourceMap == null ? string . Empty : result . SourceMap . ToJson ( ) ) ;
357359 return outputPath ;
358360 }
361+
362+ /// <summary>
363+ /// Ensures that Babel has been loaded into the JavaScript engine.
364+ /// </summary>
365+ private void EnsureBabelLoaded ( )
366+ {
367+ if ( ! _config . LoadBabel )
368+ {
369+ throw new BabelNotLoadedException ( ) ;
370+ }
371+ }
359372 }
360373}
Original file line number Diff line number Diff line change 9090 </Compile >
9191 <Compile Include =" AssemblyRegistration.cs" />
9292 <Compile Include =" BabelConfig.cs" />
93+ <Compile Include =" Exceptions\BabelNotLoadedException.cs" />
9394 <Compile Include =" Exceptions\ReactNotInitialisedException.cs" />
9495 <Compile Include =" FileSystemExtensions.cs" />
9596 <Compile Include =" JavaScriptEngineUtils.cs" />
Original file line number Diff line number Diff line change @@ -36,6 +36,7 @@ public ReactSiteConfiguration()
3636 BabelConfig = new BabelConfig ( ) ;
3737 ReuseJavaScriptEngines = true ;
3838 AllowMsieEngine = true ;
39+ LoadBabel = true ;
3940 LoadReact = true ;
4041 JsonSerializerSettings = new JsonSerializerSettings
4142 {
@@ -220,6 +221,24 @@ public IReactSiteConfiguration SetLoadReact(bool loadReact)
220221 return this ;
221222 }
222223
224+ /// <summary>
225+ /// Gets or sets whether Babel is loading. Disabling the loading of Babel can improve startup
226+ /// performance, but all your JSX files must be transformed beforehand (eg. through Babel,
227+ /// Webpack or Browserify).
228+ /// </summary>
229+ public bool LoadBabel { get ; set ; }
230+
231+ /// <summary>
232+ /// Sets whether Babel is loading. Disabling the loading of Babel can improve startup
233+ /// performance, but all your JSX files must be transformed beforehand (eg. through Babel,
234+ /// Webpack or Browserify).
235+ /// </summary>
236+ public IReactSiteConfiguration SetLoadBabel ( bool loadBabel )
237+ {
238+ LoadBabel = loadBabel ;
239+ return this ;
240+ }
241+
223242 /// <summary>
224243 /// Gets or sets the Babel configuration to use.
225244 /// </summary>
You can’t perform that action at this time.
0 commit comments