/// <summary> /// Produces a code minifiction of JS assets by using Uglify JS minifier /// </summary> /// <param name="assets">Set of JS assets</param> /// <returns>Set of JS assets with minified text content</returns> public IList <IAsset> Minify(IList <IAsset> assets) { if (assets == null) { throw new ArgumentException(CoreStrings.Common_ValueIsEmpty, "assets"); } if (assets.Count == 0) { return(assets); } var assetsToProcessing = assets.Where(a => a.IsScript && !a.Minified).ToList(); if (assetsToProcessing.Count == 0) { return(assets); } UglificationOptions options = CreateUglificationOptions(); using (var jsUglifier = new JsUglifier(_createJsEngineInstance, options)) { foreach (var asset in assetsToProcessing) { InnerMinify(asset, jsUglifier); } } return(assets); }
/// <summary> /// Creates a uglification options /// </summary> /// <returns>Uglification options</returns> private UglificationOptions CreateUglificationOptions() { var options = new UglificationOptions { ParsingOptions = ParsingOptions, CompressionOptions = CompressionOptions, ManglingOptions = ManglingOptions, CodeGenerationOptions = CodeGenerationOptions, KeepFunctionNames = KeepFunctionNames, ScrewIe8 = ScrewIe8, Severity = Severity }; return(options); }
/// <summary> /// Produces a code minifiction of JS asset by using Uglify JS minifier /// </summary> /// <param name="asset">JS asset</param> /// <returns>JS asset with minified text content</returns> public IAsset Minify(IAsset asset) { if (asset == null) { throw new ArgumentException(CoreStrings.Common_ValueIsEmpty, "asset"); } if (asset.Minified) { return(asset); } UglificationOptions options = CreateUglificationOptions(); using (var jsUglifier = new JsUglifier(_createJsEngineInstance, options)) { InnerMinify(asset, jsUglifier); } return(asset); }
/// <summary> /// Produces a code minifiction of JS asset by using Uglify JS minifier /// </summary> /// <param name="asset">JS asset</param> /// <returns>JS asset with minified text content</returns> public IAsset Minify(IAsset asset) { if (asset == null) { throw new ArgumentNullException( nameof(asset), string.Format(CoreStrings.Common_ArgumentIsNull, nameof(asset)) ); } if (asset.Minified) { return(asset); } UglificationOptions options = CreateUglificationOptions(); using (var jsUglifier = new JsUglifier(_createJsEngineInstance, options)) { InnerMinify(asset, jsUglifier); } return(asset); }
/// <summary> /// Creates a uglification options /// </summary> /// <returns>Uglification options</returns> private UglificationOptions CreateUglificationOptions() { var options = new UglificationOptions { ParsingOptions = ParsingOptions, CompressionOptions = CompressionOptions, ManglingOptions = ManglingOptions, CodeGenerationOptions = CodeGenerationOptions, ScrewIe8 = ScrewIe8, KeepFunctionNames = KeepFunctionNames, Severity = Severity }; return options; }
/// <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("strict", parsingOptions.Strict), new JProperty("bare_returns", parsingOptions.BareReturns) )); if (compressionOptions.Compress) { optionsJson.Add("compress", new JObject( new JProperty("sequences", compressionOptions.Sequences), new JProperty("properties", compressionOptions.PropertiesDotNotation), new JProperty("dead_code", compressionOptions.DeadCode), new JProperty("drop_debugger", compressionOptions.DropDebugger), new JProperty("unsafe", compressionOptions.Unsafe), new JProperty("conditionals", compressionOptions.Conditionals), new JProperty("comparisons", compressionOptions.Comparisons), new JProperty("evaluate", compressionOptions.Evaluate), new JProperty("booleans", compressionOptions.Booleans), new JProperty("loops", compressionOptions.Loops), new JProperty("unused", compressionOptions.Unused), new JProperty("hoist_funs", compressionOptions.HoistFunctions), new JProperty("keep_fargs", compressionOptions.KeepFunctionArgs), new JProperty("keep_fnames", options.KeepFunctionNames), new JProperty("hoist_vars", compressionOptions.HoistVars), new JProperty("if_return", compressionOptions.IfReturn), new JProperty("join_vars", compressionOptions.JoinVars), new JProperty("cascade", compressionOptions.Cascade), new JProperty("screw_ie8", options.ScrewIe8), new JProperty("global_defs", ParseGlobalDefinitions(compressionOptions.GlobalDefinitions)), new JProperty("pure_getters", compressionOptions.PureGetters), new JProperty("pure_funcs", ParsePureFunctions(compressionOptions.PureFunctions)), new JProperty("drop_console", compressionOptions.DropConsole), new JProperty("angular", compressionOptions.Angular) )); } else { optionsJson.Add("compress", false); } if (manglingOptions.Mangle) { optionsJson.Add("mangle", new JObject( new JProperty("except", ParseExcept(manglingOptions.Except)), new JProperty("eval", manglingOptions.Eval), new JProperty("sort", manglingOptions.Sort), new JProperty("toplevel", manglingOptions.TopLevel), new JProperty("screw_ie8", options.ScrewIe8), new JProperty("keep_fnames", options.KeepFunctionNames) )); } else { optionsJson.Add("mangle", false); } optionsJson.Add("output", new JObject( new JProperty("beautify", codeGenerationOptions.Beautify), new JProperty("indent_level", codeGenerationOptions.IndentLevel), new JProperty("indent_start", codeGenerationOptions.IndentStart), new JProperty("quote_keys", codeGenerationOptions.QuoteKeys), new JProperty("space_colon", codeGenerationOptions.SpaceColon), new JProperty("ascii_only", codeGenerationOptions.AsciiOnly), new JProperty("inline_script", codeGenerationOptions.InlineScript), new JProperty("width", codeGenerationOptions.Width), new JProperty("max_line_len", codeGenerationOptions.MaxLineLength), new JProperty("bracketize", codeGenerationOptions.Bracketize), new JProperty("semicolons", codeGenerationOptions.Semicolons), new JProperty("comments", codeGenerationOptions.Comments), new JProperty("preserve_line", codeGenerationOptions.PreserveLine), new JProperty("screw_ie8", options.ScrewIe8), new JProperty("unescape_regexps", codeGenerationOptions.UnescapeRegexps), new JProperty("quote_style", codeGenerationOptions.QuoteStyle) )); return optionsJson; }
/// <summary> /// "Uglifies" a JS-code by using UglifyJS /// </summary> /// <param name="content">Text content of JS-asset</param> /// <param name="path">Path to JS-file</param> /// <param name="options">Uglification options</param> /// <returns>Minified text content of JS-asset</returns> public string Uglify(string content, string path, UglificationOptions options = null) { string newContent; UglificationOptions currentOptions; string currentOptionsString; if (options != null) { currentOptions = options; currentOptionsString = ConvertUglificationOptionsToJson(options).ToString(); } else { currentOptions = _defaultOptions; currentOptionsString = _defaultOptionsString; } lock (_uglificationSynchronizer) { Initialize(); try { var result = _jsEngine.Evaluate<string>( string.Format(UGLIFICATION_FUNCTION_CALL_TEMPLATE, JsonConvert.SerializeObject(content), currentOptionsString)); var json = JObject.Parse(result); var errors = json["errors"] != null ? json["errors"] as JArray : null; if (errors != null && errors.Count > 0) { throw new JsUglifyingException(FormatErrorDetails(errors[0], true, content, path)); } if (currentOptions.Severity > 0) { var warnings = json["warnings"] != null ? json["warnings"] as JArray : null; if (warnings != null && warnings.Count > 0) { throw new JsUglifyingException(FormatErrorDetails(warnings[0], false, content, path)); } } newContent = json.Value<string>("minifiedCode"); } catch (JsRuntimeException e) { throw new JsUglifyingException(JsRuntimeErrorHelpers.Format(e)); } } return newContent; }
/// <summary> /// Constructs a instance of JS-uglifier /// </summary> /// <param name="createJsEngineInstance">Delegate that creates an instance of JavaScript engine</param> /// <param name="defaultOptions">Default uglification options</param> public JsUglifier(Func<IJsEngine> createJsEngineInstance, UglificationOptions defaultOptions) { _jsEngine = createJsEngineInstance(); _defaultOptions = defaultOptions; _defaultOptionsString = (defaultOptions != null) ? ConvertUglificationOptionsToJson(defaultOptions).ToString() : "null"; }