using System; using System.IO; using System.Text; using Yahoo.Yui.Compressor; namespace learun.util { /// /// 版 本 EasyCode EC管理后台 /// Copyright (c) 2019-present EC管理有限公司 /// 创建人:tobin /// 日 期:2019.09.02 /// 描 述:js,css压缩类 /// public static class JsCssHelper { private static readonly JavaScriptCompressor javaScriptCompressor = new JavaScriptCompressor(); private static readonly CssCompressor cssCompressor = new CssCompressor(); #region Js 文件操作 /// /// 读取js文件内容并压缩 /// /// /// public static string ReadJSFile(string[] filePathlist) { StringBuilder jsStr = new StringBuilder(); try { string rootPath = ConfigHelper.GetValue("baseDir") + "/wwwroot"; foreach (var filePath in filePathlist) { string path = rootPath + filePath; if (DirFileHelper.IsExistFile(path)) { string content = File.ReadAllText(path, Encoding.Default); if (ConfigHelper.GetValue("env") != "dev") { content = javaScriptCompressor.Compress(content); } jsStr.Append(content); } } return jsStr.ToString(); } catch (Exception) { return ""; } } #endregion Js 文件操作 #region Css 文件操作 /// /// 读取css 文件内容并压缩 /// /// /// public static string ReadCssFile(string[] filePathlist) { StringBuilder cssStr = new StringBuilder(); try { string rootPath = ConfigHelper.GetValue("baseDir") + "/wwwroot"; foreach (var filePath in filePathlist) { string path = rootPath + filePath; if (DirFileHelper.IsExistFile(path)) { string content = File.ReadAllText(path, Encoding.Default); content = cssCompressor.Compress(content); cssStr.Append(content); } } return cssStr.ToString(); } catch (Exception) { return cssStr.ToString(); } } #endregion Css 文件操作 #region js 操作 /// /// 压缩js /// /// /// public static string CompressJS(string strJs) { if (ConfigHelper.GetValue("env") != "dev") { strJs = javaScriptCompressor.Compress(strJs); } return strJs; } /// /// 压缩Css /// /// /// public static string CompressCss(string strCss) { if (ConfigHelper.GetValue("env") != "dev") { strCss = cssCompressor.Compress(strCss); } return strCss; } #endregion js 操作 #region 读取文件 /// /// 读取js文件 /// /// 文件夹目录 /// public static string ReadJS(string filePath) { StringBuilder str = new StringBuilder(); try { string rootPath = AppContext.BaseDirectory; rootPath = rootPath.Replace("\\", "/"); string path = rootPath + filePath; if (DirFileHelper.IsExistFile(path)) { string content = File.ReadAllText(path, Encoding.UTF8); if (ConfigHelper.GetValue("env") != "dev") { content = javaScriptCompressor.Compress(content); } str.Append(content); } return str.ToString(); } catch (Exception) { return ""; } } /// /// 读取css文件 /// /// /// public static string ReadCss(string filePath) { StringBuilder str = new StringBuilder(); try { string rootPath = AppContext.BaseDirectory; rootPath = rootPath.Replace("\\", "/"); string path = rootPath + filePath; if (DirFileHelper.IsExistFile(path)) { string content = File.ReadAllText(path, Encoding.UTF8); content = cssCompressor.Compress(content); str.Append(content); } return str.ToString(); } catch (Exception) { return ""; } } #endregion 读取文件 } }