You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 19, 2018. It is now read-only.
I am doing html minify on razortemplate engine on compiletime on asp.net core, but razortemplate engine does not work after updating project asp.net core 2.1.0.
It does not call CreateCodeDocument method. sample code below.
public class CustomRazorTemplateEngine : RazorTemplateEngine
{
private HtmlMinifier _htmlMinifier = new HtmlMinifier();
public CustomRazorTemplateEngine(RazorEngine engine, RazorProject project) : base(engine, project)
{
Options.ImportsFileName = "_ViewImports.cshtml";
}
public override RazorCodeDocument CreateCodeDocument(RazorProjectItem projectItem)
{
using (var inputStream = projectItem.Read())
{
using (var reader = new StreamReader(inputStream))
{
var text = reader.ReadToEnd();
var markupStart = text.IndexOf("<!DOCTYPE");
var directives = text.Substring(0, markupStart);
var markup = text.Substring(markupStart);
text = directives + Minify(markup);
var byteArray = Encoding.UTF8.GetBytes(text);
var minifiedInputStream = new MemoryStream(byteArray);
var source = RazorSourceDocument.ReadFrom(minifiedInputStream, projectItem.PhysicalPath);
var imports = GetImports(projectItem);
return RazorCodeDocument.Create(source, imports);
}
}
}
private string Minify(string markup)
{
MarkupMinificationResult result = _htmlMinifier.Minify(markup, string.Empty, Encoding.UTF8, true);
if (result.Errors.Count == 0)
{
MinificationStatistics statistics = result.Statistics;
if (statistics != null)
{
Console.WriteLine();
Console.WriteLine($"Original size: {statistics.OriginalSize:N0} Bytes | Minified size: {statistics.MinifiedSize:N0} Bytes | Saved: {statistics.SavedInPercent:N2}%");
}
//Console.WriteLine($"{Environment.NewLine}Minified content:{Environment.NewLine}{Environment.NewLine}{result.MinifiedContent}");
return result.MinifiedContent;
}
else
{
IList<MinificationErrorInfo> errors = result.Errors;
Console.WriteLine();
Console.WriteLine($"Found {errors.Count:N0} error(s):");
foreach (var error in errors)
{
Console.WriteLine($" - Line {error.LineNumber}, Column {error.ColumnNumber}: {error.Message}");
}
return markup;
}
}
}