Skip to content

Commit

Permalink
IViteManifest now implements IEnumerable
Browse files Browse the repository at this point in the history
  • Loading branch information
Eptagone committed Sep 10, 2023
1 parent b48cc02 commit 96b71ff
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/Vite.AspNetCore.Abstractions/IViteManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Vite.AspNetCore.Abstractions;
/// <summary>
/// Represents a Vite manifest file.
/// </summary>
public interface IViteManifest
public interface IViteManifest : IEnumerable<IViteChunk>
{
/// <summary>
/// Gets the Vite chunk for the specified entry point if it exists.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<IsPackable>true</IsPackable>
<Version>1.0.1</Version>
<Version>1.1.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
35 changes: 23 additions & 12 deletions src/Vite.AspNetCore/Services/ViteManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -18,8 +19,6 @@ public sealed class ViteManifest : IViteManifest
private readonly ILogger<ViteManifest> _logger;
// The chunks dictionary is used to store the chunks read from the manifest.json file.
private readonly IReadOnlyDictionary<string, ViteChunk> _chunks;
// The base path is used to resolve the chunks paths.
private readonly string? _base;

private static bool _warnAboutManifestOnce = true;

Expand Down Expand Up @@ -53,10 +52,10 @@ public ViteManifest(ILogger<ViteManifest> logger, IOptions<ViteOptions> 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))
Expand All @@ -68,7 +67,7 @@ public ViteManifest(ILogger<ViteManifest> logger, IOptions<ViteOptions> 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<string, ViteChunk>();
Expand All @@ -77,17 +76,17 @@ public ViteManifest(ILogger<ViteManifest> logger, IOptions<ViteOptions> 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,
};
Expand Down Expand Up @@ -155,4 +154,16 @@ private static string CombineUri(string uri, string path)

return uri.EndsWith('/') ? uri + path : uri + "/" + path;
}

/// <inheritdoc/>
IEnumerator<IViteChunk> IEnumerable<IViteChunk>.GetEnumerator()
{
return this._chunks.Values.GetEnumerator();
}

/// <inheritdoc/>
IEnumerator IEnumerable.GetEnumerator()
{
return this._chunks.Values.GetEnumerator();
}
}

0 comments on commit 96b71ff

Please sign in to comment.