Exemplo n.º 1
0
        private static void MinifyHtml(Bundle bundle, MinificationResult minResult)
        {
            var settings = HtmlOptions.GetSettings(bundle);

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

            WriteMinFile(bundle, minResult, uglifyResult);
        }
Exemplo n.º 2
0
        private static MinificationResult MinifyHtml(Bundle bundle)
        {
            string file     = bundle.GetAbsoluteOutputFile();
            string content  = ReadAllText(file);
            var    settings = HtmlOptions.GetSettings(bundle);
            string minFile  = GetMinFileName(file);

            var minifier  = new HtmlMinifier(settings);
            var minResult = new MinificationResult(file, null, null);

            try
            {
                MarkupMinificationResult result = minifier.Minify(content, generateStatistics: true);
                minResult.MinifiedContent = result.MinifiedContent.Trim();

                if (!result.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
                {
                    foreach (var error in result.Errors)
                    {
                        minResult.Errors.Add(new MinificationError
                        {
                            FileName     = file,
                            Message      = error.Message,
                            LineNumber   = error.LineNumber,
                            ColumnNumber = error.ColumnNumber
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                minResult.Errors.Add(new MinificationError
                {
                    FileName     = file,
                    Message      = ex.Message,
                    LineNumber   = 0,
                    ColumnNumber = 0
                });
            }

            return(minResult);
        }
Exemplo n.º 3
0
        private static MinificationResult MinifyHtml(Bundle bundle)
        {
            string file     = bundle.GetAbsoluteOutputFile();
            string content  = ReadAllText(file);
            var    settings = HtmlOptions.GetSettings(bundle);
            string minFile  = GetMinFileName(file);

            var minResult = new MinificationResult(file, null, null);

            try
            {
                UgliflyResult uglifyResult;

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

                minResult.MinifiedContent = uglifyResult.Code?.Trim();

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

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

                    GzipFile(minFile, bundle, containsChanges);
                }
                else
                {
                    foreach (var error in uglifyResult.Errors)
                    {
                        minResult.Errors.Add(new MinificationError
                        {
                            FileName     = file,
                            Message      = error.Message,
                            LineNumber   = error.StartLine,
                            ColumnNumber = error.StartColumn
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                minResult.Errors.Add(new MinificationError
                {
                    FileName     = file,
                    Message      = ex.Message,
                    LineNumber   = 0,
                    ColumnNumber = 0
                });
            }

            return(minResult);
        }