|
| 1 | +/* |
| 2 | + * Copyright (c) 2014, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | + |
| 10 | +using System; |
| 11 | +using System.Diagnostics; |
| 12 | +using Newtonsoft.Json; |
| 13 | +using React.Exceptions; |
| 14 | + |
| 15 | +namespace React |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// Handles compiling JSX to JavaScript. |
| 19 | + /// </summary> |
| 20 | + public class JsxTransformer : IJsxTransformer |
| 21 | + { |
| 22 | + /// <summary> |
| 23 | + /// Cache key for JSX to JavaScript compilation |
| 24 | + /// </summary> |
| 25 | + private const string JSX_CACHE_KEY = "JSX_{0}"; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Environment this JSX Transformer has been created in |
| 29 | + /// </summary> |
| 30 | + private readonly IReactEnvironment _environment; |
| 31 | + /// <summary> |
| 32 | + /// Cache used for storing compiled JSX |
| 33 | + /// </summary> |
| 34 | + private readonly ICache _cache; |
| 35 | + /// <summary> |
| 36 | + /// File system wrapper |
| 37 | + /// </summary> |
| 38 | + private readonly IFileSystem _fileSystem; |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Initializes a new instance of the <see cref="JsxTransformer"/> class. |
| 42 | + /// </summary> |
| 43 | + /// <param name="environment">The ReactJS.NET environment</param> |
| 44 | + /// <param name="cache">The cache to use for JSX compilation</param> |
| 45 | + /// <param name="fileSystem">File system wrapper</param> |
| 46 | + public JsxTransformer(IReactEnvironment environment, ICache cache, IFileSystem fileSystem) |
| 47 | + { |
| 48 | + _environment = environment; |
| 49 | + _cache = cache; |
| 50 | + _fileSystem = fileSystem; |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Loads a JSX file. Results of the JSX to JavaScript transformation are cached. |
| 55 | + /// </summary> |
| 56 | + /// <param name="filename">Name of the file to load</param> |
| 57 | + /// <returns>File contents</returns> |
| 58 | + public string LoadJsxFile(string filename) |
| 59 | + { |
| 60 | + var fullPath = _fileSystem.MapPath(filename); |
| 61 | + |
| 62 | + return _cache.GetOrInsert( |
| 63 | + key: string.Format(JSX_CACHE_KEY, filename), |
| 64 | + slidingExpiration: TimeSpan.FromMinutes(30), |
| 65 | + cacheDependencyFiles: new[] { fullPath }, |
| 66 | + getData: () => |
| 67 | + { |
| 68 | + Trace.WriteLine(string.Format("Parsing JSX from {0}", filename)); |
| 69 | + var contents = _fileSystem.ReadAsString(filename); |
| 70 | + return TransformJsx(contents); |
| 71 | + } |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Transforms JSX into regular JavaScript. The result is not cached. Use |
| 77 | + /// <see cref="LoadJsxFile"/> if loading from a file since this will cache the result. |
| 78 | + /// </summary> |
| 79 | + /// <param name="input">JSX</param> |
| 80 | + /// <returns>JavaScript</returns> |
| 81 | + public string TransformJsx(string input) |
| 82 | + { |
| 83 | + // Just return directly if there's no JSX annotation |
| 84 | + if (!input.Contains("@jsx")) |
| 85 | + { |
| 86 | + return input; |
| 87 | + } |
| 88 | + |
| 89 | + try |
| 90 | + { |
| 91 | + var encodedInput = JsonConvert.SerializeObject(input); |
| 92 | + var output = _environment.ExecuteWithLargerStackIfRequired<string>(string.Format( |
| 93 | + "global.JSXTransformer.transform({0}).code", |
| 94 | + encodedInput |
| 95 | + )); |
| 96 | + return output; |
| 97 | + } |
| 98 | + catch (Exception ex) |
| 99 | + { |
| 100 | + throw new JsxException(ex.Message, ex); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments