Skip to content

Commit 1c8ea6d

Browse files
committed
Merge pull request #20 from Alxandr/master
Enable configuration of harmony support
2 parents 3cbdd58 + 4f296a2 commit 1c8ea6d

File tree

9 files changed

+74
-25
lines changed

9 files changed

+74
-25
lines changed

src/React.MSBuild/TransformJsx.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,22 @@ public class TransformJsx : Task
3030
[Required]
3131
public string SourceDir { get; set; }
3232

33+
/// <summary>
34+
/// A value indicating if es6 syntax should be rewritten.
35+
/// </summary>
36+
/// <returns><c>true</c> if support for es6 syntax should be rewritten.</returns>
37+
public bool UseHarmony { get; set; }
38+
3339
/// <summary>
3440
/// Executes the task.
3541
/// </summary>
3642
/// <returns><c>true</c> on success</returns>
3743
public override bool Execute()
3844
{
3945
MSBuildHost.EnsureInitialized();
46+
var config = React.AssemblyRegistration.Container.Resolve<IReactSiteConfiguration>();
47+
config.UseHarmony = UseHarmony;
48+
4049
_environment = React.AssemblyRegistration.Container.Resolve<IReactEnvironment>();
4150

4251
Log.LogMessage("Starting TransformJsx");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static class ReactConfig
1515
{
1616
public static void Configure()
1717
{
18-
ReactSiteConfiguration.Configuration = new ReactSiteConfiguration()
18+
ReactSiteConfiguration.Configuration
1919
.AddScript("~/Content/Sample.jsx");
2020
}
2121
}

src/React.Tests/Core/JsxTransformerTests.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public void SetUp()
4040
_environment.Object,
4141
_cache.Object,
4242
_fileSystem.Object,
43-
_fileCacheHash.Object
43+
_fileCacheHash.Object,
44+
ReactSiteConfiguration.Configuration
4445
);
4546
}
4647

@@ -61,7 +62,8 @@ public void ShouldTransformJsxIfAnnotationPresent()
6162

6263
_environment.Verify(x => x.ExecuteWithLargerStackIfRequired<string>(
6364
"ReactNET_transform",
64-
"/** @jsx React.DOM */ <div>Hello World</div>"
65+
"/** @jsx React.DOM */ <div>Hello World</div>",
66+
false
6567
));
6668
}
6769

@@ -70,7 +72,8 @@ public void ShouldWrapExceptionsInJsxExeption()
7072
{
7173
_environment.Setup(x => x.ExecuteWithLargerStackIfRequired<string>(
7274
"ReactNET_transform",
73-
"/** @jsx React.DOM */ <div>Hello World</div>"
75+
"/** @jsx React.DOM */ <div>Hello World</div>",
76+
false
7477
)).Throws(new Exception("Something broke..."));
7578

7679
const string input = "/** @jsx React.DOM */ <div>Hello World</div>";
@@ -96,7 +99,7 @@ public void ShouldUseCacheProvider()
9699
/*slidingExpiration*/ It.IsAny<TimeSpan>(),
97100
/*getData*/ It.IsAny<Func<string>>(),
98101
/*cacheDependencyKeys*/ It.IsAny<IEnumerable<string>>(),
99-
/*cacheDependencyFiles*/ It.IsAny<IEnumerable<string>>()
102+
/*cacheDependencyFiles*/ It.IsAny<IEnumerable<string>>()
100103
)).Returns("/* cached */");
101104

102105
var result = _jsxTransformer.TransformJsxFile("foo.jsx");
@@ -127,7 +130,8 @@ public void ShouldTransformJsxIfFileCacheHashInvalid()
127130
_jsxTransformer.TransformJsxFile("foo.jsx");
128131
_environment.Verify(x => x.ExecuteWithLargerStackIfRequired<string>(
129132
"ReactNET_transform",
130-
"/** @jsx React.DOM */ <div>Hello World</div>"
133+
"/** @jsx React.DOM */ <div>Hello World</div>",
134+
false
131135
));
132136
}
133137

@@ -141,7 +145,8 @@ public void ShouldTransformJsxIfNoCache()
141145
_jsxTransformer.TransformJsxFile("foo.jsx");
142146
_environment.Verify(x => x.ExecuteWithLargerStackIfRequired<string>(
143147
"ReactNET_transform",
144-
"/** @jsx React.DOM */ <div>Hello World</div>"
148+
"/** @jsx React.DOM */ <div>Hello World</div>",
149+
false
145150
));
146151
}
147152

@@ -151,7 +156,8 @@ public void ShouldSaveTransformationResult()
151156
_fileSystem.Setup(x => x.ReadAsString("foo.jsx")).Returns("/** @jsx React.DOM */ <div>Hello World</div>");
152157
_environment.Setup(x => x.ExecuteWithLargerStackIfRequired<string>(
153158
"ReactNET_transform",
154-
"/** @jsx React.DOM */ <div>Hello World</div>"
159+
"/** @jsx React.DOM */ <div>Hello World</div>",
160+
false
155161
)).Returns("React.DOM.div('Hello World')");
156162

157163
string result = null;

src/React/IJsxTransformer.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,27 @@ public interface IJsxTransformer
1818
/// Transforms a JSX file. Results of the JSX to JavaScript transformation are cached.
1919
/// </summary>
2020
/// <param name="filename">Name of the file to load</param>
21+
/// <param name="useHarmony"><c>true</c> if support for es6 syntax should be rewritten.</param>
2122
/// <returns>JavaScript</returns>
22-
string TransformJsxFile(string filename);
23+
string TransformJsxFile(string filename, bool? useHarmony = null);
2324

2425
/// <summary>
2526
/// Transforms JSX into regular JavaScript. The result is not cached. Use
2627
/// <see cref="TransformJsxFile"/> if loading from a file since this will cache the result.
2728
/// </summary>
2829
/// <param name="input">JSX</param>
30+
/// <param name="useHarmony"><c>true</c> if support for es6 syntax should be rewritten.</param>
2931
/// <returns>JavaScript</returns>
30-
string TransformJsx(string input);
32+
string TransformJsx(string input, bool? useHarmony = null);
3133

3234
/// <summary>
3335
/// Transforms a JSX file to JavaScript, and saves the result into a ".generated.js" file
3436
/// alongside the original file.
3537
/// </summary>
3638
/// <param name="filename">Name of the file to load</param>
39+
/// <param name="useHarmony"><c>true</c> if support for es6 syntax should be rewritten.</param>
3740
/// <returns>File contents</returns>
38-
string TransformAndSaveJsxFile(string filename);
41+
string TransformAndSaveJsxFile(string filename, bool? useHarmony = null);
3942

4043
/// <summary>
4144
/// Returns the path the specified JSX file's compilation will be cached to

src/React/IReactSiteConfiguration.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,11 @@ public interface IReactSiteConfiguration
3131
/// Gets a list of all the scripts that have been added to this configuration.
3232
/// </summary>
3333
IList<string> Scripts { get; }
34+
35+
/// <summary>
36+
/// A value indicating if es6 syntax should be rewritten.
37+
/// </summary>
38+
/// <returns><c>true</c> if support for es6 syntax should be rewritten.</returns>
39+
bool UseHarmony { get; set; }
3440
}
3541
}

src/React/JsxTransformer.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public class JsxTransformer : IJsxTransformer
4343
/// Hash algorithm for file-based cache
4444
/// </summary>
4545
private readonly IFileCacheHash _fileCacheHash;
46+
/// <summary>
47+
/// Site-wide configuration
48+
/// </summary>
49+
private readonly IReactSiteConfiguration _config;
4650

4751
/// <summary>
4852
/// Initializes a new instance of the <see cref="JsxTransformer"/> class.
@@ -51,20 +55,23 @@ public class JsxTransformer : IJsxTransformer
5155
/// <param name="cache">The cache to use for JSX compilation</param>
5256
/// <param name="fileSystem">File system wrapper</param>
5357
/// <param name="fileCacheHash">Hash algorithm for file-based cache</param>
54-
public JsxTransformer(IReactEnvironment environment, ICache cache, IFileSystem fileSystem, IFileCacheHash fileCacheHash)
58+
/// <param name="siteConfig">Site-wide configuration</param>
59+
public JsxTransformer(IReactEnvironment environment, ICache cache, IFileSystem fileSystem, IFileCacheHash fileCacheHash, IReactSiteConfiguration siteConfig)
5560
{
5661
_environment = environment;
5762
_cache = cache;
5863
_fileSystem = fileSystem;
5964
_fileCacheHash = fileCacheHash;
65+
_config = siteConfig;
6066
}
6167

6268
/// <summary>
6369
/// Transforms a JSX file. Results of the JSX to JavaScript transformation are cached.
6470
/// </summary>
6571
/// <param name="filename">Name of the file to load</param>
72+
/// <param name="useHarmony"><c>true</c> if support for es6 syntax should be rewritten.</param>
6673
/// <returns>JavaScript</returns>
67-
public string TransformJsxFile(string filename)
74+
public string TransformJsxFile(string filename, bool? useHarmony = null)
6875
{
6976
var fullPath = _fileSystem.MapPath(filename);
7077

@@ -91,7 +98,7 @@ public string TransformJsxFile(string filename)
9198
}
9299

93100
// 3. Not cached, perform the transformation
94-
return TransformJsxWithHeader(contents, hash);
101+
return TransformJsxWithHeader(contents, hash, useHarmony);
95102
}
96103
);
97104
}
@@ -102,23 +109,25 @@ public string TransformJsxFile(string filename)
102109
/// </summary>
103110
/// <param name="contents">Contents of the input file</param>
104111
/// <param name="hash">Hash of the input. If null, it will be calculated</param>
112+
/// <param name="useHarmony"><c>true</c> if support for es6 syntax should be rewritten.</param>
105113
/// <returns>JavaScript</returns>
106-
private string TransformJsxWithHeader(string contents, string hash = null)
114+
private string TransformJsxWithHeader(string contents, string hash = null, bool? useHarmony = null)
107115
{
108116
if (string.IsNullOrEmpty(hash))
109117
{
110118
hash = _fileCacheHash.CalculateHash(contents);
111119
}
112-
return GetFileHeader(hash) + TransformJsx(contents);
120+
return GetFileHeader(hash) + TransformJsx(contents, useHarmony);
113121
}
114122

115123
/// <summary>
116124
/// Transforms JSX into regular JavaScript. The result is not cached. Use
117125
/// <see cref="TransformJsxFile"/> if loading from a file since this will cache the result.
118126
/// </summary>
119127
/// <param name="input">JSX</param>
128+
/// <param name="useHarmony"><c>true</c> if support for es6 syntax should be rewritten.</param>
120129
/// <returns>JavaScript</returns>
121-
public string TransformJsx(string input)
130+
public string TransformJsx(string input, bool? useHarmony = null)
122131
{
123132
// Just return directly if there's no JSX annotation
124133
if (!input.Contains("@jsx"))
@@ -131,7 +140,8 @@ public string TransformJsx(string input)
131140
{
132141
var output = _environment.ExecuteWithLargerStackIfRequired<string>(
133142
"ReactNET_transform",
134-
input
143+
input,
144+
useHarmony.HasValue ? useHarmony.Value : _config.UseHarmony
135145
);
136146
return output;
137147
}
@@ -177,12 +187,13 @@ public string GetJsxOutputPath(string path)
177187
/// alongside the original file.
178188
/// </summary>
179189
/// <param name="filename">Name of the file to load</param>
190+
/// <param name="useHarmony"><c>true</c> if support for es6 syntax should be rewritten.</param>
180191
/// <returns>File contents</returns>
181-
public string TransformAndSaveJsxFile(string filename)
192+
public string TransformAndSaveJsxFile(string filename, bool? useHarmony = null)
182193
{
183194
var outputPath = GetJsxOutputPath(filename);
184195
var contents = _fileSystem.ReadAsString(filename);
185-
var result = TransformJsxWithHeader(contents);
196+
var result = TransformJsxWithHeader(contents, useHarmony: useHarmony);
186197
_fileSystem.WriteAsString(outputPath, result);
187198
return outputPath;
188199
}

src/React/ReactEnvironment.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ IFileCacheHash fileCacheHash
9898
_fileSystem = fileSystem;
9999
_fileCacheHash = fileCacheHash;
100100
_jsxTransformer = new Lazy<IJsxTransformer>(() =>
101-
new JsxTransformer(this, _cache, _fileSystem, _fileCacheHash)
101+
new JsxTransformer(this, _cache, _fileSystem, _fileCacheHash, _config)
102102
);
103103
}
104104

src/React/ReactSiteConfiguration.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ namespace React
1717
/// </summary>
1818
public class ReactSiteConfiguration : IReactSiteConfiguration
1919
{
20+
private static IReactSiteConfiguration _instance = new ReactSiteConfiguration();
21+
2022
/// <summary>
2123
/// All the scripts that have been added to this configuration
2224
/// </summary>
@@ -25,7 +27,13 @@ public class ReactSiteConfiguration : IReactSiteConfiguration
2527
/// <summary>
2628
/// Gets or sets the site-side configuration
2729
/// </summary>
28-
public static IReactSiteConfiguration Configuration { get; set; }
30+
public static IReactSiteConfiguration Configuration
31+
{
32+
get
33+
{
34+
return _instance;
35+
}
36+
}
2937

3038
/// <summary>
3139
/// Adds a script to the list of scripts that are executed. This should be called for all
@@ -48,6 +56,12 @@ public IReactSiteConfiguration AddScript(string filename)
4856
public IList<string> Scripts
4957
{
5058
get { return new ReadOnlyCollection<string>(_scriptFiles); }
51-
}
59+
}
60+
61+
/// <summary>
62+
/// A value indicating if es6 syntax should be rewritten.
63+
/// </summary>
64+
/// <returns><c>true</c> if support for es6 syntax should be rewritten.</returns>
65+
public bool UseHarmony { get; set; }
5266
}
5367
}

src/React/Resources/shims.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ if (!Object.freeze) {
1919
Object.freeze = function() { };
2020
}
2121

22-
function ReactNET_transform(input) {
23-
return global.JSXTransformer.transform(input, { harmony: true }).code;
22+
function ReactNET_transform(input, harmony) {
23+
return global.JSXTransformer.transform(input, { harmony: !!harmony }).code;
2424
}

0 commit comments

Comments
 (0)