Fixed bundle generation service

This commit is contained in:
2024-12-10 22:29:53 +01:00
parent e63a3db8b9
commit e6c9feed6b

View File

@@ -41,11 +41,18 @@ public class BundleGenerationService : IHostedService
continue; continue;
} }
Logger.LogTrace("Discovered css file '{path}' at '{physicalPath}'", cssFile, fileInfo.PhysicalPath);
physicalCssFiles.Add(fileInfo.PhysicalPath); physicalCssFiles.Add(fileInfo.PhysicalPath);
} }
if(physicalCssFiles.Count == 0)
Logger.LogWarning("No physical paths to css files loaded. The generated bundle will be empty. Unless this is intended by you this is a bug");
// TODO: Implement cache // TODO: Implement cache
// TODO: File system watcher for development
var bundleContent = await CreateCssBundle(physicalCssFiles); var bundleContent = await CreateCssBundle(physicalCssFiles);
@@ -65,27 +72,43 @@ public class BundleGenerationService : IHostedService
return await File.ReadAllTextAsync(physicalPaths[0]); return await File.ReadAllTextAsync(physicalPaths[0]);
// Create bundle by stripping out double declared classes and combining all css files into one bundle // Create bundle by stripping out double declared classes and combining all css files into one bundle
var content = "";
var styleSheets = new List<Stylesheet>();
var parser = new StylesheetParser(); var parser = new StylesheetParser();
string? content = null;
Stylesheet? mainStylesheet = null;
var additionalStyleSheets = new List<Stylesheet>();
foreach (var fileContent in physicalPaths) foreach (var physicalPath in physicalPaths)
{ {
try try
{ {
var sh = await parser.ParseAsync(fileContent); var fileContent = await File.ReadAllTextAsync(physicalPath);
styleSheets.Add(sh); var stylesheet = await parser.ParseAsync(fileContent);
// Check if it's the first stylesheet we are loading
if (mainStylesheet == null || content == null)
{
// Delegate the first stylesheet to be the main one
content = fileContent + "\n";
mainStylesheet = stylesheet;
}
else
additionalStyleSheets.Add(stylesheet); // All other stylesheets are to be processed
} }
catch (Exception e) catch (Exception e)
{ {
Logger.LogWarning("An error occured while parsing css file: {e}", e); Logger.LogError("An error occured while parsing css file: {e}", e);
} }
} }
// Delegate the first stylesheet as the main stylesheet // Handle an empty main stylesheet delegation
var mainStylesheet = styleSheets.First(); if (mainStylesheet == null || content == null)
{
foreach (var stylesheet in styleSheets.Skip(1)) // Process all stylesheets expect the first (main) one Logger.LogError("An unable to delegate main stylesheet. Did every load attempt of an stylesheet fail?");
return "";
}
// Process stylesheets against the main one
foreach (var stylesheet in additionalStyleSheets)
{ {
// Style // Style
foreach (var styleRule in stylesheet.StyleRules) foreach (var styleRule in stylesheet.StyleRules)
@@ -93,7 +116,7 @@ public class BundleGenerationService : IHostedService
if (mainStylesheet.StyleRules.Any(x => x.Selector.Text == styleRule.Selector.Text)) if (mainStylesheet.StyleRules.Any(x => x.Selector.Text == styleRule.Selector.Text))
continue; continue;
content += styleRule.ToCss() + "\n"; content += styleRule.StylesheetText.Text + "\n";
} }
// Container // Container
@@ -102,7 +125,7 @@ public class BundleGenerationService : IHostedService
if (mainStylesheet.ContainerRules.Any(x => x.ConditionText == containerRule.ConditionText)) if (mainStylesheet.ContainerRules.Any(x => x.ConditionText == containerRule.ConditionText))
continue; continue;
content += containerRule.ToCss() + "\n"; content += containerRule.StylesheetText.Text + "\n";
} }
// Import Rule // Import Rule
@@ -111,7 +134,7 @@ public class BundleGenerationService : IHostedService
if (mainStylesheet.ImportRules.Any(x => x.Text == importRule.Text)) if (mainStylesheet.ImportRules.Any(x => x.Text == importRule.Text))
continue; continue;
content += importRule.ToCss() + "\n"; content += importRule.StylesheetText.Text + "\n";
} }
// Media Rules // Media Rules
@@ -124,7 +147,7 @@ public class BundleGenerationService : IHostedService
if (mainStylesheet.PageRules.Any(x => x.SelectorText == pageRule.SelectorText)) if (mainStylesheet.PageRules.Any(x => x.SelectorText == pageRule.SelectorText))
continue; continue;
content += pageRule.ToCss() + "\n"; content += pageRule.StylesheetText.Text + "\n";
} }
} }