示例#1
0
        public bool Minimize()
        {
            if (String.IsNullOrEmpty(Input))
            {
                throw new Exception("Input must be specified.:");
            }
            if (CompressorType == 0)
            {
                throw new Exception("No compressor type specified.");
            }
            string compressedYui    = String.Empty;
            string compressedPacker = String.Empty;
            string header           = String.Empty;
            string javascript       = Input;

            //string javascript = File.ReadAllText(path);
            if (KeepHeader)
            {
                int    pos    = javascript.IndexOf("/*");
                int    endPos = -1;
                string leadin = String.Empty;
                if (pos >= 0)
                {
                    leadin = javascript.Substring(0, pos);
                    if (leadin.Trim() == string.Empty)
                    {
                        endPos     = javascript.IndexOf("*/", pos + 1);
                        header     = javascript.Substring(pos, endPos + 2) + Environment.NewLine;
                        javascript = javascript.Substring(endPos + 2);
                    }
                }
            }

            if (CompressorType == CompressorType.yui || CompressorType == CompressorType.best)
            {
                if (!compressYUI(javascript, out compressedYui))
                {
                    throw new Exception("The YUI compressor reported errors, which is strange because we already did a dry run to determine the best compressor.");
                }
            }
            if (CompressorType == CompressorType.packer || CompressorType == CompressorType.best)
            {
                JavascriptPacker jsPacker = new JavascriptPacker(JavascriptPacker.PackerEncoding.None, false, false);
                compressedPacker = jsPacker.Pack(javascript);
            }
            CompressorType finalPacker = CompressorType != CompressorType.best ? CompressorType :
                                         (compressedYui.Length < compressedPacker.Length ? CompressorType.yui : CompressorType.packer);

            Output = header + (finalPacker == CompressorType.yui ? compressedYui : compressedPacker);

            Statistics = finalPacker.ToString() + ": " + javascript.Length + "/" + Output.Length + ", "
                         + Math.Round(100 * ((decimal)Output.Length / (decimal)javascript.Length), 0) + "%";
            return(Success);
        }
示例#2
0
        public bool Minimize()
        {
            if (String.IsNullOrEmpty(Input)) {
                throw new Exception("Input must be specified.:");
            }
            if (CompressorType == 0)
            {
                throw new Exception("No compressor type specified.");
            }
            string compressedYui = String.Empty;
            string compressedPacker = String.Empty;
            string header = String.Empty;
            string javascript = Input;
            //string javascript = File.ReadAllText(path);
            if (KeepHeader)
            {
                int pos = javascript.IndexOf("/*");
                int endPos = -1;
                string leadin = String.Empty;
                if (pos >= 0)
                {
                    leadin = javascript.Substring(0, pos);
                    if (leadin.Trim() == string.Empty)
                    {
                        endPos = javascript.IndexOf("*/", pos + 1);
                        header = javascript.Substring(pos, endPos + 2) + Environment.NewLine;
                        javascript = javascript.Substring(endPos + 2);

                    }
                }
            }

            if (CompressorType == CompressorType.yui || CompressorType == CompressorType.best)
            {
                if (!compressYUI(javascript, out compressedYui)) {

                    throw new Exception("The YUI compressor reported errors, which is strange because we already did a dry run to determine the best compressor.");
                }
            }
            if (CompressorType == CompressorType.packer || CompressorType == CompressorType.best)
            {
                JavascriptPacker jsPacker = new JavascriptPacker(JavascriptPacker.PackerEncoding.None, false, false);
                compressedPacker = jsPacker.Pack(javascript);
            }
            CompressorType finalPacker = CompressorType != CompressorType.best ? CompressorType :
                    (compressedYui.Length < compressedPacker.Length ? CompressorType.yui : CompressorType.packer);

            Output = header + (finalPacker == CompressorType.yui ? compressedYui : compressedPacker);

            Statistics = finalPacker.ToString() + ": " + javascript.Length + "/" + Output.Length + ", " 
                + Math.Round(100 * ((decimal)Output.Length / (decimal)javascript.Length), 0) + "%";
            return Success;
        }
示例#3
0
        public bool Minimize()
        {
            if (String.IsNullOrEmpty(Input)) {
                throw new Exception("Input must be specified.:");
            }
            if (CompressorType == 0)
            {
                throw new Exception("No compressor type specified.");
            }
            string compressedYui = String.Empty;
            string compressedPacker = String.Empty;
            string header = String.Empty;
            string javascript = Input;
            //string javascript = File.ReadAllText(path);
            if (KeepHeader)
            {
                int pos = javascript.IndexOf("/*");
                int endPos = -1;
                string leadin = String.Empty;
                if (pos >= 0)
                {
                    leadin = javascript.Substring(0, pos);
                    if (leadin.Trim() == string.Empty)
                    {
                        endPos = javascript.IndexOf("*/", pos + 1);
                        header = javascript.Substring(pos, endPos + 2) + Environment.NewLine;
                        javascript = javascript.Substring(endPos + 2);

                    }
                }
            }

            if (CompressorType == CompressorType.yui || CompressorType == CompressorType.best)
            {

                var reporter = new LinterECMAErrorReporter();
                Yahoo.Yui.Compressor.JavaScriptCompressor compressor;
                compressor = new JavaScriptCompressor(javascript, true, Encoding.UTF8,
                System.Globalization.CultureInfo.CurrentCulture,
                    true,
                    reporter);
                compressedYui = compressor.Compress(true, true, false, 80);
            }
            if (CompressorType == CompressorType.packer || CompressorType == CompressorType.best)
            {
                JavascriptPacker jsPacker = new JavascriptPacker(JavascriptPacker.PackerEncoding.None, false, false);
                compressedPacker = jsPacker.Pack(javascript);
            }
            CompressorType finalPacker = CompressorType != CompressorType.best ? CompressorType :
                    (compressedYui.Length < compressedPacker.Length ? CompressorType.yui : CompressorType.packer);

            Output = header + (finalPacker == CompressorType.yui ? compressedYui : compressedPacker);

            Statistics = finalPacker.ToString() + ": " + javascript.Length + "/" + Output.Length + ", " + Math.Round(100 * ((decimal)Output.Length / (decimal)javascript.Length), 0) + "%";
            return Success;
        }