Пример #1
0
        public string Compile(string content, string path, BabelJsCompilationOptions options = null)
        {
            string newContent;
            var    currentOptionsString = ConvertCompilationOptionsToJson(options ?? _defaultOptions, path).ToString(Formatting.None);

            lock (_compilationSynchronizer)
            {
                Initialize();

                try
                {
                    var result = _jsEngine.Evaluate <string>(
                        string.Format(COMPILATION_FUNCTION_CALL_TEMPLATE,
                                      JsonConvert.SerializeObject(content),
                                      currentOptionsString));
                    var json = JObject.Parse(result);

                    var errors = json["errors"] as JArray;
                    if (errors != null && errors.Count > 0)
                    {
                        throw new BabelJsCompilerException(FormatErrorDetails(errors[0], content, path));
                    }

                    newContent = json.Value <string>("compiledCode");
                }
                catch (JsRuntimeException e)
                {
                    throw new Exception(JsRuntimeErrorHelpers.Format(e));
                }
            }

            return(newContent);
        }
        public string Compile(string content, string path, BabelJsCompilationOptions options = null)
        {
            string newContent;
            var currentOptionsString = ConvertCompilationOptionsToJson(options ?? _defaultOptions, path).ToString(Formatting.None);

            lock (_compilationSynchronizer)
            {
                Initialize();

                try
                {
                    var result = _jsEngine.Evaluate<string>(
                        string.Format(COMPILATION_FUNCTION_CALL_TEMPLATE,
                            JsonConvert.SerializeObject(content),
                            currentOptionsString));
                    var json = JObject.Parse(result);

                    var errors = json["errors"] as JArray;
                    if (errors != null && errors.Count > 0)
                    {
                        throw new BabelJsCompilerException(FormatErrorDetails(errors[0], content, path));
                    }

                    newContent = json.Value<string>("compiledCode");
                }
                catch (JsRuntimeException e)
                {
                    throw new Exception(JsRuntimeErrorHelpers.Format(e));
                }
            }

            return newContent;
        }
        /// <summary>
        /// Converts a compilation options to JSON
        /// </summary>
        /// <param name="options">Compilation options</param>
        /// <returns>Compilation options in JSON format</returns>
        private static JObject ConvertCompilationOptionsToJson(BabelJsCompilationOptions options, string fileName)
        {
            var configuration = options.ToJObject();
            configuration.Add("filename", fileName);

            return configuration;
        }
Пример #4
0
        /// <summary>
        /// Converts a compilation options to JSON
        /// </summary>
        /// <param name="options">Compilation options</param>
        /// <returns>Compilation options in JSON format</returns>
        private static JObject ConvertCompilationOptionsToJson(BabelJsCompilationOptions options, string fileName)
        {
            var configuration = options.ToJObject();

            configuration.Add("filename", fileName);

            return(configuration);
        }
Пример #5
0
 public BabelJsCompiler(Func <IJsEngine> createJsEngineInstance, BabelJsCompilationOptions options)
 {
     _jsEngine       = createJsEngineInstance();
     _defaultOptions = options ?? new BabelJsCompilationOptions();
 }
 public BabelJsCompiler(Func<IJsEngine> createJsEngineInstance, BabelJsCompilationOptions options)
 {
     _jsEngine = createJsEngineInstance();
     _defaultOptions = options ?? new BabelJsCompilationOptions();
 }