From 96b71ffa8f15d7d6e4185762760bb8af24dc3b03 Mon Sep 17 00:00:00 2001 From: Quetzal Rivera Date: Sat, 9 Sep 2023 22:10:48 -0600 Subject: [PATCH] IViteManifest now implements IEnumerable --- .../IViteManifest.cs | 2 +- .../Vite.AspNetCore.Abstractions.csproj | 2 +- src/Vite.AspNetCore/Services/ViteManifest.cs | 35 ++++++++++++------- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/Vite.AspNetCore.Abstractions/IViteManifest.cs b/src/Vite.AspNetCore.Abstractions/IViteManifest.cs index 9d89408..f8beb76 100644 --- a/src/Vite.AspNetCore.Abstractions/IViteManifest.cs +++ b/src/Vite.AspNetCore.Abstractions/IViteManifest.cs @@ -6,7 +6,7 @@ namespace Vite.AspNetCore.Abstractions; /// /// Represents a Vite manifest file. /// -public interface IViteManifest +public interface IViteManifest : IEnumerable { /// /// Gets the Vite chunk for the specified entry point if it exists. diff --git a/src/Vite.AspNetCore.Abstractions/Vite.AspNetCore.Abstractions.csproj b/src/Vite.AspNetCore.Abstractions/Vite.AspNetCore.Abstractions.csproj index 5d39f5e..d85402c 100644 --- a/src/Vite.AspNetCore.Abstractions/Vite.AspNetCore.Abstractions.csproj +++ b/src/Vite.AspNetCore.Abstractions/Vite.AspNetCore.Abstractions.csproj @@ -14,7 +14,7 @@ LICENSE True true - 1.0.1 + 1.1.0 diff --git a/src/Vite.AspNetCore/Services/ViteManifest.cs b/src/Vite.AspNetCore/Services/ViteManifest.cs index 9f0d6bf..5f2b202 100644 --- a/src/Vite.AspNetCore/Services/ViteManifest.cs +++ b/src/Vite.AspNetCore/Services/ViteManifest.cs @@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using System.Collections; using System.Text.Json; using Vite.AspNetCore.Abstractions; @@ -18,8 +19,6 @@ public sealed class ViteManifest : IViteManifest private readonly ILogger _logger; // The chunks dictionary is used to store the chunks read from the manifest.json file. private readonly IReadOnlyDictionary _chunks; - // The base path is used to resolve the chunks paths. - private readonly string? _base; private static bool _warnAboutManifestOnce = true; @@ -53,10 +52,10 @@ public ViteManifest(ILogger logger, IOptions options, var manifest = viteOptions.Manifest; // If the manifest file is in a subfolder, get the subfolder path. - this._base = viteOptions.Base?.TrimStart('/'); + var basePath = viteOptions.Base?.TrimStart('/'); // Get the manifest.json file path - var manifestPath = Path.Combine(environment.WebRootPath, this._base ?? string.Empty, manifest); + var manifestPath = Path.Combine(environment.WebRootPath, basePath ?? string.Empty, manifest); // If the manifest.json file exists, deserialize it into a dictionary. if (File.Exists(manifestPath)) @@ -68,7 +67,7 @@ public ViteManifest(ILogger logger, IOptions options, })!; // If the base path is not null, add it to the chunks keys and values. - if (!string.IsNullOrEmpty(this._base)) + if (!string.IsNullOrEmpty(basePath)) { // Create a new dictionary. var chunks = new Dictionary(); @@ -77,17 +76,17 @@ public ViteManifest(ILogger logger, IOptions options, foreach (var chunk in this._chunks) { // Add the base path to the key. - var key = CombineUri(this._base, chunk.Key); + var key = CombineUri(basePath, chunk.Key); // Add the base path to the value. var value = chunk.Value with { - Css = chunk.Value.Css?.Select(css => CombineUri(this._base, css)), - File = CombineUri(this._base, chunk.Value.File), - Imports = chunk.Value.Imports?.Select(imports => CombineUri(this._base, imports)), - Src = string.IsNullOrEmpty(chunk.Value.Src) ? null : CombineUri(this._base, chunk.Value.Src), - Assets = chunk.Value.Assets?.Select(assets => CombineUri(this._base, assets)), - DynamicImports = chunk.Value.DynamicImports?.Select(dynamicImports => CombineUri(this._base, dynamicImports)), + Css = chunk.Value.Css?.Select(css => CombineUri(basePath, css)), + File = CombineUri(basePath, chunk.Value.File), + Imports = chunk.Value.Imports?.Select(imports => CombineUri(basePath, imports)), + Src = string.IsNullOrEmpty(chunk.Value.Src) ? null : CombineUri(basePath, chunk.Value.Src), + Assets = chunk.Value.Assets?.Select(assets => CombineUri(basePath, assets)), + DynamicImports = chunk.Value.DynamicImports?.Select(dynamicImports => CombineUri(basePath, dynamicImports)), IsDynamicEntry = chunk.Value.IsDynamicEntry, IsEntry = chunk.Value.IsEntry, }; @@ -155,4 +154,16 @@ private static string CombineUri(string uri, string path) return uri.EndsWith('/') ? uri + path : uri + "/" + path; } + + /// + IEnumerator IEnumerable.GetEnumerator() + { + return this._chunks.Values.GetEnumerator(); + } + + /// + IEnumerator IEnumerable.GetEnumerator() + { + return this._chunks.Values.GetEnumerator(); + } }