Skip to content

Commit 8a1eb19

Browse files
Add test for TSX rendering
1 parent 062e10e commit 8a1eb19

File tree

7 files changed

+67
-1157
lines changed

7 files changed

+67
-1157
lines changed

Diff for: src/React.Core/Babel.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
* Copyright (c) Facebook, Inc. and its affiliates.
33
*
44
* This source code is licensed under the MIT license found in the
@@ -74,7 +74,7 @@ public Babel(IReactEnvironment environment, ICache cache, IFileSystem fileSystem
7474
_fileSystem = fileSystem;
7575
_fileCacheHash = fileCacheHash;
7676
_config = siteConfig;
77-
_babelConfig = siteConfig.BabelConfig.Serialize();
77+
_babelConfig = siteConfig.BabelConfig.Serialize(_config.BabelVersion);
7878
}
7979

8080
/// <summary>

Diff for: src/React.Core/BabelConfig.cs

+27-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using Newtonsoft.Json;
34
using Newtonsoft.Json.Serialization;
5+
using React.Core;
6+
using React.Exceptions;
47

58
namespace React
69
{
@@ -22,28 +25,34 @@ public class BabelConfig
2225
/// </summary>
2326
public ISet<string> Presets { get; set; }
2427

25-
/// <summary>
26-
/// Creates a new instance of <see cref="BabelConfig" />.
27-
/// </summary>
28-
public BabelConfig()
29-
{
30-
// Use es2015-no-commonjs by default so Babel doesn't prepend "use strict" to the start of the
31-
// output. This messes with the top-level "this", as we're not actually using JavaScript modules
32-
// in ReactJS.NET yet.
33-
Presets = new HashSet<string> { "es2015-no-commonjs", "stage-1", "react" };
34-
Plugins = new HashSet<string>();
35-
}
36-
3728
/// <summary>
3829
/// Serializes this Babel configuration into the format required for Babel.
3930
/// </summary>
4031
/// <returns></returns>
41-
public string Serialize()
32+
public string Serialize(string babelVersion)
4233
{
43-
return JsonConvert.SerializeObject(this, new JsonSerializerSettings
44-
{
45-
ContractResolver = new CamelCasePropertyNamesContractResolver(),
46-
});
34+
ISet<string> defaultPresets = babelVersion == BabelVersions.Babel7
35+
? new HashSet<string> { "typescript", "react" }
36+
: babelVersion == BabelVersions.Babel6 || babelVersion == null
37+
? new HashSet<string> { "es2015-no-commonjs", "stage-1", "react" }
38+
: throw new ArgumentException(nameof(babelVersion));
39+
40+
ISet<string> defaultPlugins = babelVersion == BabelVersions.Babel7
41+
? new HashSet<string> { "proposal-class-properties", "proposal-object-rest-spread" }
42+
: babelVersion == BabelVersions.Babel6 || babelVersion == null
43+
? new HashSet<string>()
44+
: throw new ArgumentException(nameof(babelVersion));
45+
46+
return JsonConvert.SerializeObject(
47+
new BabelConfig
48+
{
49+
Plugins = Plugins ?? defaultPlugins,
50+
Presets = Presets ?? defaultPresets,
51+
},
52+
new JsonSerializerSettings
53+
{
54+
ContractResolver = new CamelCasePropertyNamesContractResolver(),
55+
});
4756
}
4857
}
4958
}

0 commit comments

Comments
 (0)