forked from reactjs/React.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReactSiteConfiguration.cs
More file actions
251 lines (229 loc) · 7.71 KB
/
ReactSiteConfiguration.cs
File metadata and controls
251 lines (229 loc) · 7.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* Copyright (c) 2014-2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
namespace React
{
/// <summary>
/// Site-wide configuration for ReactJS.NET
/// </summary>
public class ReactSiteConfiguration : IReactSiteConfiguration
{
/// <summary>
/// Gets or sets the site-side configuration
/// </summary>
public static IReactSiteConfiguration Configuration { get; set; }
static ReactSiteConfiguration()
{
Configuration = new ReactSiteConfiguration();
}
/// <summary>
/// Initializes a new instance of the <see cref="ReactSiteConfiguration"/> class.
/// </summary>
public ReactSiteConfiguration()
{
UseHarmony = true;
ReuseJavaScriptEngines = true;
AllowMsieEngine = true;
LoadReact = true;
JsonSerializerSettings = new JsonSerializerSettings
{
StringEscapeHandling = StringEscapeHandling.EscapeHtml
};
}
/// <summary>
/// All the scripts that have been added to this configuration and require JSX
/// transformation to be run.
/// </summary>
private readonly IList<string> _scriptFiles = new List<string>();
/// <summary>
/// All the scripts that have been added to this configuration and do not require JSX
/// transformation to be run.
/// </summary>
private readonly IList<string> _scriptFilesWithoutTransform = new List<string>();
/// <summary>
/// Adds a script to the list of scripts that are executed. This should be called for all
/// React components and their dependencies. If the script does not have any JSX in it
/// (for example, it's built using Webpack or Gulp), use
/// <see cref="AddScriptWithoutTransform"/> instead.
/// </summary>
/// <param name="filename">
/// Name of the file to execute. Should be a server relative path starting with ~ (eg.
/// <c>~/Scripts/Awesome.js</c>)
/// </param>
/// <returns>This configuration, for chaining</returns>
public IReactSiteConfiguration AddScript(string filename)
{
_scriptFiles.Add(filename);
return this;
}
/// <summary>
/// Adds a script to the list of scripts that are executed. This is the same as
/// <see cref="AddScript"/> except it does not run JSX transformation on the script and thus is
/// more efficient.
/// </summary>
/// <param name="filename">
/// Name of the file to execute. Should be a server relative path starting with ~ (eg.
/// <c>~/Scripts/Awesome.js</c>)
/// </param>
/// <returns>The configuration, for chaining</returns>
public IReactSiteConfiguration AddScriptWithoutTransform(string filename)
{
_scriptFilesWithoutTransform.Add(filename);
return this;
}
/// <summary>
/// Gets all the file paths that match the specified pattern. If the pattern is a plain
/// path, just returns that path verbatim.
/// </summary>
/// <param name="glob">
/// Patterns to search for (eg. <c>~/Scripts/*.js</c> or <c>~/Scripts/Awesome.js</c>
/// </param>
/// <returns>File paths that match this pattern</returns>
private IEnumerable<string> Glob(string glob)
{
if (!glob.IsGlobPattern())
{
return new[] {glob};
}
// Directly touching the IoC container is not ideal, but we only want to pull the FileSystem
// dependency if it's absolutely necessary.
var fileSystem = AssemblyRegistration.Container.Resolve<IFileSystem>();
return fileSystem.Glob(glob);
}
/// <summary>
/// Gets a list of all the scripts that have been added to this configuration and require JSX
/// transformation to be run.
/// </summary>
public IEnumerable<string> Scripts
{
// TODO: It's a bit strange to do the globbing here, ideally this class should just be a simple
// bag of settings with no logic.
get { return _scriptFiles.SelectMany(Glob); }
}
/// <summary>
/// Gets a list of all the scripts that have been added to this configuration.
/// </summary>
public IEnumerable<string> ScriptsWithoutTransform
{
get { return _scriptFilesWithoutTransform.SelectMany(Glob); }
}
/// <summary>
/// A value indicating if es6 syntax should be rewritten.
/// </summary>
/// <returns><c>true</c> if support for es6 syntax should be rewritten.</returns>
public bool UseHarmony { get; set; }
/// <summary>
/// Specifies whether ES6 (harmony) syntax should be transformed
/// </summary>
public IReactSiteConfiguration SetUseHarmony(bool useHarmony)
{
UseHarmony = useHarmony;
return this;
}
/// <summary>
/// Gets or sets the configuration for JSON serializer.
/// </summary>
public JsonSerializerSettings JsonSerializerSettings { get; set; }
/// <summary>
/// Sets the configuration for json serializer.
/// </summary>
/// <param name="settings">Settings.</param>
/// <remarks>
/// Thic confiquration is used when component initialization script
/// is being generated server-side.
/// </remarks>
public IReactSiteConfiguration SetJsonSerializerSettings(JsonSerializerSettings settings)
{
JsonSerializerSettings = settings;
return this;
}
/// <summary>
/// Gets or sets whether JavaScript engines should be reused across requests.
/// </summary>
public bool ReuseJavaScriptEngines { get; set; }
/// <summary>
/// Sets whether JavaScript engines should be reused across requests.
/// </summary>
public IReactSiteConfiguration SetReuseJavaScriptEngines(bool value)
{
ReuseJavaScriptEngines = value;
return this;
}
/// <summary>
/// Gets or sets whether Flow types should be stripped.
/// </summary>
public bool StripTypes { get; set; }
/// <summary>
/// Sets whether Flow types should be stripped
/// </summary>
public IReactSiteConfiguration SetStripTypes(bool stripTypes)
{
StripTypes = stripTypes;
return this;
}
/// <summary>
/// Gets or sets the number of engines to initially start when a pool is created.
/// Defaults to <c>10</c>.
/// </summary>
public int? StartEngines { get; set; }
/// <summary>
/// Sets the number of engines to initially start when a pool is created.
/// Defaults to <c>10</c>.
/// </summary>
public IReactSiteConfiguration SetStartEngines(int? startEngines)
{
StartEngines = startEngines;
return this;
}
/// <summary>
/// Gets or sets the maximum number of engines that will be created in the pool.
/// Defaults to <c>25</c>.
/// </summary>
public int? MaxEngines { get; set; }
/// <summary>
/// Sets the maximum number of engines that will be created in the pool.
/// Defaults to <c>25</c>.
/// </summary>
public IReactSiteConfiguration SetMaxEngines(int? maxEngines)
{
MaxEngines = maxEngines;
return this;
}
/// <summary>
/// Gets or sets whether the MSIE engine should be used if V8 is unavailable.
/// </summary>
public bool AllowMsieEngine { get; set; }
/// <summary>
/// Sets whether the MSIE engine should be used if V8 is unavailable.
/// </summary>
/// <returns></returns>
public IReactSiteConfiguration SetAllowMsieEngine(bool allowMsieEngine)
{
AllowMsieEngine = allowMsieEngine;
return this;
}
/// <summary>
/// Gets or sets whether the built-in version of React is loaded. If <c>false</c>, you must
/// provide your own version of React.
/// </summary>
public bool LoadReact { get; set; }
/// <summary>
/// Sets whether the built-in version of React is loaded. If <c>false</c>, you must
/// provide your own version of React.
/// </summary>
/// <returns>The configuration, for chaining</returns>
public IReactSiteConfiguration SetLoadReact(bool loadReact)
{
LoadReact = loadReact;
return this;
}
}
}