Skip to content

Commit 3ae436a

Browse files
committed
Add AddScriptWithoutTransform method for loading JavaScript file without JSX transformation.
It's becoming more and more common to use external build systems (Gulp, Grunt) or bundlers (Webpack, Browserify) that handle the JSX transformation. The output of these can be loaded directly into ReactJS.NET as it's just vanilla JavaScript. `AddScriptWithoutTransform` makes loading these files more efficient by just loading them directly. Closes #91
1 parent 059a656 commit 3ae436a

File tree

7 files changed

+89
-11
lines changed

7 files changed

+89
-11
lines changed

build.proj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ of patent rights can be found in the PATENTS file in the same directory.
1010
<Project ToolsVersion="4.0" DefaultTargets="Build;Test;Package" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
1111
<PropertyGroup>
1212
<Major>1</Major>
13-
<Minor>4</Minor>
14-
<Build>1</Build>
13+
<Minor>5</Minor>
14+
<Build>0</Build>
1515
<Revision>0</Revision>
1616
<DevNuGetServer>http://reactjs.net/packages/</DevNuGetServer>
1717
<MSBuildCommunityTasksPath>$(MSBuildProjectDirectory)\tools\MSBuildTasks</MSBuildCommunityTasksPath>

src/React.AspNet/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "1.4.1-*",
2+
"version": "1.5.0-*",
33
"configurations": {
44
"Debug": {
55
"compilationOptions": {

src/React.Core/IReactSiteConfiguration.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public interface IReactSiteConfiguration
1919
{
2020
/// <summary>
2121
/// Adds a script to the list of scripts that are executed. This should be called for all
22-
/// React components and their dependencies.
22+
/// React components and their dependencies. If the script does not have any JSX in it
23+
/// (for example, it's built using Webpack or Gulp), use
24+
/// <see cref="AddScriptWithoutTransform"/> instead.
2325
/// </summary>
2426
/// <param name="filename">
2527
/// Name of the file to execute. Should be a server relative path starting with ~ (eg.
@@ -29,10 +31,29 @@ public interface IReactSiteConfiguration
2931
IReactSiteConfiguration AddScript(string filename);
3032

3133
/// <summary>
32-
/// Gets a list of all the scripts that have been added to this configuration.
34+
/// Adds a script to the list of scripts that are executed. This is the same as
35+
/// <see cref="AddScript"/> except it does not run JSX transformation on the script and thus is
36+
/// more efficient.
37+
/// </summary>
38+
/// <param name="filename">
39+
/// Name of the file to execute. Should be a server relative path starting with ~ (eg.
40+
/// <c>~/Scripts/Awesome.js</c>)
41+
/// </param>
42+
/// <returns>The configuration, for chaining</returns>
43+
IReactSiteConfiguration AddScriptWithoutTransform(string filename);
44+
45+
/// <summary>
46+
/// Gets a list of all the scripts that have been added to this configuration and require JSX
47+
/// transformation to be run.
3348
/// </summary>
3449
IList<string> Scripts { get; }
3550

51+
/// <summary>
52+
/// Gets a list of all the scripts that have been added to this configuration and do not
53+
/// require JSX transformation to be run.
54+
/// </summary>
55+
IList<string> ScriptsWithoutTransform { get; }
56+
3657
/// <summary>
3758
/// A value indicating if es6 syntax should be rewritten.
3859
/// </summary>

src/React.Core/JavaScriptEngineFactory.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ IFileSystem fileSystem
7878
{
7979
_pool = CreatePool();
8080
_resetPoolTimer = new Timer(OnResetPoolTimer);
81-
_watchedFiles = new HashSet<string>(_config.Scripts.Select(
81+
82+
var allFiles = _config.Scripts.Concat(_config.ScriptsWithoutTransform);
83+
_watchedFiles = new HashSet<string>(allFiles.Select(
8284
fileName => _fileSystem.MapPath(fileName).ToLowerInvariant()
8385
));
8486
try
@@ -136,6 +138,27 @@ protected virtual void InitialiseEngine(IJsEngine engine)
136138
engine.ExecuteResource("React.Resources.react-with-addons.js", thisAssembly);
137139
engine.Execute("var React = global.React");
138140
engine.ExecuteResource("React.Resources.JSXTransformer.js", thisAssembly);
141+
142+
// Only scripts that don't need JSX transformation can run immediately here
143+
// JSX files are loaded in ReactEnvironment.
144+
foreach (var file in _config.ScriptsWithoutTransform)
145+
{
146+
try
147+
{
148+
var contents = _fileSystem.ReadAsString(file);
149+
engine.Execute(contents);
150+
}
151+
catch (JsRuntimeException ex)
152+
{
153+
throw new ReactScriptLoadException(string.Format(
154+
"Error while loading \"{0}\": {1}\r\nLine: {2}\r\nColumn: {3}",
155+
file,
156+
ex.Message,
157+
ex.LineNumber,
158+
ex.ColumnNumber
159+
));
160+
}
161+
}
139162
}
140163

141164
/// <summary>

src/React.Core/ReactEnvironment.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ public virtual string Version
157157
}
158158

159159
/// <summary>
160-
/// Ensures any user-provided scripts have been loaded
160+
/// Ensures any user-provided scripts have been loaded. This only loads JSX files; files
161+
/// that need no transformation are loaded in JavaScriptEngineFactory.
161162
/// </summary>
162163
protected virtual void EnsureUserScriptsLoaded()
163164
{

src/React.Core/ReactSiteConfiguration.cs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,21 @@ public ReactSiteConfiguration()
3939
}
4040

4141
/// <summary>
42-
/// All the scripts that have been added to this configuration
42+
/// All the scripts that have been added to this configuration and require JSX
43+
/// transformation to be run.
4344
/// </summary>
4445
private readonly IList<string> _scriptFiles = new List<string>();
46+
/// <summary>
47+
/// All the scripts that have been added to this configuration and do not require JSX
48+
/// transformation to be run.
49+
/// </summary>
50+
private readonly IList<string> _scriptFilesWithoutTransform = new List<string>();
4551

4652
/// <summary>
4753
/// Adds a script to the list of scripts that are executed. This should be called for all
48-
/// React components and their dependencies.
54+
/// React components and their dependencies. If the script does not have any JSX in it
55+
/// (for example, it's built using Webpack or Gulp), use
56+
/// <see cref="AddScriptWithoutTransform"/> instead.
4957
/// </summary>
5058
/// <param name="filename">
5159
/// Name of the file to execute. Should be a server relative path starting with ~ (eg.
@@ -59,13 +67,38 @@ public IReactSiteConfiguration AddScript(string filename)
5967
}
6068

6169
/// <summary>
62-
/// Gets a list of all the scripts that have been added to this configuration.
70+
/// Adds a script to the list of scripts that are executed. This is the same as
71+
/// <see cref="AddScript"/> except it does not run JSX transformation on the script and thus is
72+
/// more efficient.
73+
/// </summary>
74+
/// <param name="filename">
75+
/// Name of the file to execute. Should be a server relative path starting with ~ (eg.
76+
/// <c>~/Scripts/Awesome.js</c>)
77+
/// </param>
78+
/// <returns>The configuration, for chaining</returns>
79+
public IReactSiteConfiguration AddScriptWithoutTransform(string filename)
80+
{
81+
_scriptFilesWithoutTransform.Add(filename);
82+
return this;
83+
}
84+
85+
/// <summary>
86+
/// Gets a list of all the scripts that have been added to this configuration and require JSX
87+
/// transformation to be run.
6388
/// </summary>
6489
public IList<string> Scripts
6590
{
6691
get { return new ReadOnlyCollection<string>(_scriptFiles); }
6792
}
6893

94+
/// <summary>
95+
/// Gets a list of all the scripts that have been added to this configuration.
96+
/// </summary>
97+
public IList<string> ScriptsWithoutTransform
98+
{
99+
get { return new ReadOnlyCollection<string>(_scriptFilesWithoutTransform); }
100+
}
101+
69102
/// <summary>
70103
/// A value indicating if es6 syntax should be rewritten.
71104
/// </summary>

src/React.Sample.Webpack/App_Start/ReactConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static class ReactConfig
1616
public static void Configure()
1717
{
1818
ReactSiteConfiguration.Configuration
19-
.AddScript("~/build/server.bundle.js");
19+
.AddScriptWithoutTransform("~/build/server.bundle.js");
2020
}
2121
}
2222
}

0 commit comments

Comments
 (0)