示例#1
0
        private static void MinifyCss(Bundle bundle, MinificationResult minResult)
        {
            var settings = CssOptions.GetSettings(bundle);

            var uglifyResult = Uglify.Css(bundle.Output, minResult.FileName, settings);

            WriteMinFile(bundle, minResult, uglifyResult);
        }
示例#2
0
        private static MinificationResult MinifyCss(Bundle bundle)
        {
            string file     = bundle.GetAbsoluteOutputFile();
            string content  = ReadAllText(file);
            var    settings = CssOptions.GetSettings(bundle);
            string minFile  = GetMinFileName(file);

            var minifier = new Minifier();
            var result   = new MinificationResult(file, null, null);

            try
            {
                result.MinifiedContent = minifier.MinifyStyleSheet(content, settings).Trim();

                if (!minifier.Errors.Any())
                {
                    bool containsChanges = FileHelpers.HasFileContentChanged(minFile, result.MinifiedContent);

                    OnBeforeWritingMinFile(file, minFile, bundle, containsChanges);

                    if (containsChanges)
                    {
                        File.WriteAllText(minFile, result.MinifiedContent, new UTF8Encoding(false));
                    }

                    OnAfterWritingMinFile(file, minFile, bundle, containsChanges);

                    GzipFile(minFile, bundle, containsChanges);
                }
                else
                {
                    AddAjaxminErrors(minifier, result);
                }
            }
            catch (Exception ex)
            {
                result.Errors.Add(new MinificationError
                {
                    FileName     = file,
                    Message      = ex.Message,
                    LineNumber   = 0,
                    ColumnNumber = 0
                });
            }

            return(result);
        }
示例#3
0
        private static MinificationResult MinifyCss(Bundle bundle)
        {
            string file     = bundle.GetAbsoluteOutputFile();
            string content  = ReadAllText(file);
            var    settings = CssOptions.GetSettings(bundle);
            string minFile  = GetMinFileName(file);
            var    result   = new MinificationResult(file, null, null);

            try
            {
                UgliflyResult uglifyResult;

                try
                {
                    uglifyResult = Uglify.Css(content, file, settings);
                }
                catch
                {
                    uglifyResult = new UgliflyResult(null,
                                                     new List <UglifyError> {
                        new UglifyError
                        {
                            IsError = true,
                            File    = file,
                            Message = "Error processing file"
                        }
                    });
                }
                result.MinifiedContent = uglifyResult.Code?.Trim();

                if (!uglifyResult.HasErrors && !string.IsNullOrEmpty(result.MinifiedContent))
                {
                    bool containsChanges = FileHelpers.HasFileContentChanged(minFile, result.MinifiedContent);
                    result.Changed |= containsChanges;

                    OnBeforeWritingMinFile(file, minFile, bundle, containsChanges);

                    if (containsChanges)
                    {
                        File.WriteAllText(minFile, result.MinifiedContent, new UTF8Encoding(false));
                        OnAfterWritingMinFile(file, minFile, bundle, containsChanges);
                    }

                    GzipFile(minFile, bundle, containsChanges);
                }
                else
                {
                    AddAjaxminErrors(uglifyResult, result);
                }
            }
            catch (Exception ex)
            {
                result.Errors.Add(new MinificationError
                {
                    FileName     = file,
                    Message      = ex.Message,
                    LineNumber   = 0,
                    ColumnNumber = 0
                });
            }

            return(result);
        }