Esempio n. 1
0
        /// <summary>
        /// Converts a uglification options to JSON
        /// </summary>
        /// <param name="options">Uglification options</param>
        /// <returns>Uglification options in JSON format</returns>
        private static JObject ConvertUglificationOptionsToJson(UglificationOptions options)
        {
            ParsingOptions        parsingOptions        = options.ParsingOptions;
            CompressionOptions    compressionOptions    = options.CompressionOptions;
            ManglingOptions       manglingOptions       = options.ManglingOptions;
            CodeGenerationOptions codeGenerationOptions = options.CodeGenerationOptions;

            var optionsJson = new JObject(
                new JProperty("warnings", options.Severity > 0
                              ));

            optionsJson.Add("parse", new JObject(
                                new JProperty("bare_returns", parsingOptions.BareReturns),
                                new JProperty("shebang", false),
                                new JProperty("strict", parsingOptions.Strict)
                                ));

            if (compressionOptions.Compress)
            {
                optionsJson.Add("compress", new JObject(
                                    new JProperty("angular", compressionOptions.Angular),
                                    new JProperty("booleans", compressionOptions.Booleans),
                                    new JProperty("cascade", compressionOptions.Cascade),
                                    new JProperty("collapse_vars", compressionOptions.CollapseVars),
                                    new JProperty("comparisons", compressionOptions.Comparisons),
                                    new JProperty("conditionals", compressionOptions.Conditionals),
                                    new JProperty("dead_code", compressionOptions.DeadCode),
                                    new JProperty("drop_console", compressionOptions.DropConsole),
                                    new JProperty("drop_debugger", compressionOptions.DropDebugger),
                                    new JProperty("evaluate", compressionOptions.Evaluate),
                                    new JProperty("global_defs",
                                                  ParseGlobalDefinitions(compressionOptions.GlobalDefinitions)),
                                    new JProperty("hoist_funs", compressionOptions.HoistFunctions),
                                    new JProperty("hoist_vars", compressionOptions.HoistVars),
                                    new JProperty("if_return", compressionOptions.IfReturn),
                                    new JProperty("join_vars", compressionOptions.JoinVars),
                                    new JProperty("keep_fargs", compressionOptions.KeepFunctionArgs),
                                    new JProperty("keep_fnames", options.KeepFunctionNames),
                                    new JProperty("keep_infinity", compressionOptions.KeepInfinity),
                                    new JProperty("loops", compressionOptions.Loops),
                                    new JProperty("negate_iife", compressionOptions.NegateIife),
                                    new JProperty("passes", compressionOptions.Passes),
                                    new JProperty("properties", compressionOptions.PropertiesDotNotation),
                                    new JProperty("pure_getters", compressionOptions.PureGetters),
                                    new JProperty("pure_funcs",
                                                  ConvertCommaSeparatedListToJson(compressionOptions.PureFunctions)),
                                    new JProperty("reduce_vars", compressionOptions.ReduceVars),
                                    new JProperty("screw_ie8", options.ScrewIe8),
                                    new JProperty("sequences", compressionOptions.Sequences),
                                    new JProperty("toplevel", compressionOptions.TopLevel),
                                    new JProperty("top_retain",
                                                  ConvertCommaSeparatedListToJson(compressionOptions.TopRetain, new JArray())),
                                    new JProperty("unsafe", compressionOptions.Unsafe),
                                    new JProperty("unsafe_math", compressionOptions.UnsafeMath),
                                    new JProperty("unsafe_proto", compressionOptions.UnsafeProto),
                                    new JProperty("unsafe_regexp", compressionOptions.UnsafeRegExp),
                                    new JProperty("unused", compressionOptions.Unused)
                                    ));
            }
            else
            {
                optionsJson.Add("compress", false);
            }


            if (manglingOptions.Mangle)
            {
                optionsJson.Add("mangle", new JObject(
                                    new JProperty("eval", manglingOptions.Eval),
                                    new JProperty("except",
                                                  ConvertCommaSeparatedListToJson(manglingOptions.Except, new JArray())),
                                    new JProperty("keep_fnames", options.KeepFunctionNames),
                                    new JProperty("screw_ie8", options.ScrewIe8),
                                    new JProperty("toplevel", manglingOptions.TopLevel)
                                    ));
            }
            else
            {
                optionsJson.Add("mangle", false);
            }

            optionsJson.Add("output", new JObject(
                                new JProperty("ascii_only", codeGenerationOptions.AsciiOnly),
                                new JProperty("beautify", codeGenerationOptions.Beautify),
                                new JProperty("bracketize", codeGenerationOptions.Bracketize),
                                new JProperty("comments", codeGenerationOptions.Comments),
                                new JProperty("indent_level", codeGenerationOptions.IndentLevel),
                                new JProperty("indent_start", codeGenerationOptions.IndentStart),
                                new JProperty("inline_script", codeGenerationOptions.InlineScript),
                                new JProperty("keep_quoted_props", codeGenerationOptions.KeepQuotedProperties),
                                new JProperty("max_line_len", codeGenerationOptions.MaxLineLength),
                                new JProperty("preserve_line", codeGenerationOptions.PreserveLine),
                                new JProperty("quote_keys", codeGenerationOptions.QuoteKeys),
                                new JProperty("quote_style", codeGenerationOptions.QuoteStyle),
                                new JProperty("screw_ie8", options.ScrewIe8),
                                new JProperty("semicolons", codeGenerationOptions.Semicolons),
                                new JProperty("shebang", false),
                                new JProperty("space_colon", codeGenerationOptions.SpaceColon),
                                new JProperty("unescape_regexps", codeGenerationOptions.UnescapeRegexps),
                                new JProperty("width", codeGenerationOptions.Width),
                                new JProperty("wrap_iife", codeGenerationOptions.WrapIife)
                                ));

            return(optionsJson);
        }
Esempio n. 2
0
 /// <summary>
 /// Constructs a instance of JS uglifier
 /// </summary>
 /// <param name="createJsEngineInstance">Delegate that creates an instance of JS engine</param>
 /// <param name="options">Uglification options</param>
 public JsUglifier(Func <IJsEngine> createJsEngineInstance, UglificationOptions options)
 {
     _jsEngine      = createJsEngineInstance();
     _options       = options ?? new UglificationOptions();
     _optionsString = ConvertUglificationOptionsToJson(_options).ToString();
 }