1
- using System . Collections . Generic ;
1
+ using System ;
2
+ using System . Collections . Generic ;
2
3
using Newtonsoft . Json ;
3
4
using Newtonsoft . Json . Serialization ;
5
+ using React . Core ;
6
+ using React . Exceptions ;
4
7
5
8
namespace React
6
9
{
@@ -22,28 +25,34 @@ public class BabelConfig
22
25
/// </summary>
23
26
public ISet < string > Presets { get ; set ; }
24
27
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
-
37
28
/// <summary>
38
29
/// Serializes this Babel configuration into the format required for Babel.
39
30
/// </summary>
40
31
/// <returns></returns>
41
- public string Serialize ( )
32
+ public string Serialize ( string babelVersion )
42
33
{
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
+ } ) ;
47
56
}
48
57
}
49
58
}
0 commit comments