Skip to content

Commit 12b8d53

Browse files
committed
Only load Babel when needed. Closes #172
1 parent 945cc5a commit 12b8d53

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
lines changed

src/React.Core/Babel.cs

+2-13
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ protected virtual JavaScriptWithSourceMap TransformWithHeader(
243243
/// <returns>JavaScript</returns>
244244
public virtual string Transform(string input, string filename = "unknown")
245245
{
246-
EnsureBabelLoaded();
246+
_environment.EnsureBabelLoaded();
247247
try
248248
{
249249
var output = _environment.ExecuteWithLargerStackIfRequired<string>(
@@ -272,7 +272,7 @@ public virtual JavaScriptWithSourceMap TransformWithSourceMap(
272272
string filename = "unknown"
273273
)
274274
{
275-
EnsureBabelLoaded();
275+
_environment.EnsureBabelLoaded();
276276
try
277277
{
278278
return _environment.ExecuteWithLargerStackIfRequired<JavaScriptWithSourceMap>(
@@ -347,16 +347,5 @@ string filename
347347
_fileSystem.WriteAsString(sourceMapPath, result.SourceMap == null ? string.Empty : result.SourceMap.ToJson());
348348
return outputPath;
349349
}
350-
351-
/// <summary>
352-
/// Ensures that Babel has been loaded into the JavaScript engine.
353-
/// </summary>
354-
private void EnsureBabelLoaded()
355-
{
356-
if (!_config.LoadBabel)
357-
{
358-
throw new BabelNotLoadedException();
359-
}
360-
}
361350
}
362351
}

src/React.Core/IReactEnvironment.cs

+5
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,10 @@ public interface IReactEnvironment
9191
/// Gets the JSX Transformer for this environment.
9292
/// </summary>
9393
IBabel Babel { get; }
94+
95+
/// <summary>
96+
/// Ensures that Babel has been loaded into the JavaScript engine.
97+
/// </summary>
98+
void EnsureBabelLoaded();
9499
}
95100
}

src/React.Core/JavaScriptEngineFactory.cs

-4
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,6 @@ protected virtual void InitialiseEngine(IJsEngine engine)
101101
engine.ExecuteResource("React.Resources.react-with-addons.js", thisAssembly);
102102
engine.Execute("React = global.React");
103103
}
104-
if (_config.LoadBabel)
105-
{
106-
engine.ExecuteResource("React.node_modules.babel_core.browser.js", thisAssembly);
107-
}
108104

109105
LoadUserScripts(engine);
110106
if (!_config.LoadReact)

src/React.Core/ReactEnvironment.cs

+18
Original file line numberDiff line numberDiff line change
@@ -413,5 +413,23 @@ public virtual IReactSiteConfiguration Configuration
413413
{
414414
get { return _config; }
415415
}
416+
417+
/// <summary>
418+
/// Ensures that Babel has been loaded into the JavaScript engine.
419+
/// </summary>
420+
public void EnsureBabelLoaded()
421+
{
422+
// If Babel is disabled in the config, don't even try loading it
423+
if (!_config.LoadBabel)
424+
{
425+
throw new BabelNotLoadedException();
426+
}
427+
428+
var babelLoaded = Engine.Evaluate<bool>("typeof global.Babel !== 'undefined'");
429+
if (!babelLoaded)
430+
{
431+
Engine.ExecuteResource("React.node_modules.babel_core.browser.js", typeof(ReactEnvironment).Assembly);
432+
}
433+
}
416434
}
417435
}

0 commit comments

Comments
 (0)