Skip to content

Commit

Permalink
HMR fixed
Browse files Browse the repository at this point in the history
Vite Records options are now reassignable.
  • Loading branch information
Eptagone committed Oct 9, 2023
1 parent c2412d2 commit f5941b0
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 22 deletions.
34 changes: 34 additions & 0 deletions src/Vite.AspNetCore/Extensions/ViteOptionsExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2023 Quetzal Rivera.
// Licensed under the MIT License, See LICENCE in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;

namespace Vite.AspNetCore.Extensions;

/// <summary>
/// Define extension methods for <see cref="ViteOptions"/>
/// </summary>
internal static class ViteOptionsExtensions
{
/// <summary>
/// Returns the Vite Dev Server Url.
/// </summary>
/// <param name="options"></param>
/// <returns></returns>
internal static string GetViteDevServerUrl(this ViteOptions options)
{
// Get the port and host from the configuration.
var host = options.Server.Host;
var port = options.Server.Port;
// Check if https is enabled.
var https = options.Server.Https;

// Return the url.
return $"{(https ? "https" : "http")}://{host}:{port}";
}
}
8 changes: 2 additions & 6 deletions src/Vite.AspNetCore/Services/ViteDevMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Vite.AspNetCore.Extensions;

namespace Vite.AspNetCore.Services;

Expand Down Expand Up @@ -44,13 +45,8 @@ public ViteDevMiddleware(

// Read the Vite options from the configuration.
this._viteOptions = options.Value;
// Get the port and host from the configuration.
var host = options.Value.Server.Host;
var port = options.Value.Server.Port;
// Check if https is enabled.
var https = options.Value.Server.Https;
// Build the base url.
this._viteServerBaseUrl = $"{(https ? "https" : "http")}://{host}:{port}";
this._viteServerBaseUrl = options.Value.GetViteDevServerUrl();

// Prepare and run the Vite Dev Server if AutoRun is true and the middleware is enabled.
if (options.Value.Server.AutoRun && ViteStatusService.IsMiddlewareRegistered)
Expand Down
11 changes: 8 additions & 3 deletions src/Vite.AspNetCore/TagHelpers/ViteTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.ComponentModel;
using System.Text.RegularExpressions;
using Vite.AspNetCore.Abstractions;
using Vite.AspNetCore.Extensions;
using Vite.AspNetCore.Services;

namespace Vite.AspNetCore.TagHelpers;
Expand All @@ -30,18 +32,20 @@ public class ViteTagHelper : TagHelper
private readonly IViteManifest _manifest;
private readonly IUrlHelperFactory _urlHelperFactory;
private readonly ViteStatusService _status;
private readonly ViteOptions _viteOptions;

/// <summary>
/// Initialize a new instance of <see cref="ViteTagHelper"/>
/// </summary>
/// <param name="logger">The logger.</param>
/// <param name="manifest">The manifest service.</param>
public ViteTagHelper(ILogger<ViteTagHelper> logger, IViteManifest manifest, IUrlHelperFactory urlHelperFactory, ViteStatusService viteStatusService)
public ViteTagHelper(ILogger<ViteTagHelper> logger, IViteManifest manifest, IOptions<ViteOptions> options, IUrlHelperFactory urlHelperFactory, ViteStatusService viteStatusService)
{
this._logger = logger;
this._manifest = manifest;
this._urlHelperFactory = urlHelperFactory;
this._status = viteStatusService;
this._viteOptions = options.Value;
}

/// <summary>
Expand Down Expand Up @@ -108,9 +112,10 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
// If the Vite script was not inserted, it will be prepended to the current element tag.
if (ViteStatusService.IsMiddlewareRegistered && !this._status.IsDevScriptInserted)
{
var viteClientPath = urlHelper.Content("~/@vite/client");
// Build the uri to the vite client script.
var viteClientUrl = this._viteOptions.GetViteDevServerUrl() + "/@vite/client";
// Add the script tag to the output
output.PreElement.AppendHtml($"<script type=\"module\" src=\"{viteClientPath}\"></script>");
output.PreElement.AppendHtml($"<script type=\"module\" src=\"{viteClientUrl}\"></script>");
// Set the flag to true to avoid adding the script tag multiple times
this._status.IsDevScriptInserted = true;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Vite.AspNetCore/Vite.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
<PackageTags>Vite;ViteJS;AspNet;MPA;Middleware;Server;Assets;Mvc;WebPages</PackageTags>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<Version>1.10.0</Version>
<PackageReleaseNotes>The launch manager will check if the server is already running before start a new process.</PackageReleaseNotes>
<Version>1.10.1</Version>
<PackageReleaseNotes>HMR fixed
Vite Records options are now reassignable.</PackageReleaseNotes>
</PropertyGroup>

<!-- Add ASP.NET Core references -->
Expand Down
10 changes: 5 additions & 5 deletions src/Vite.AspNetCore/ViteOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@ public record ViteOptions
/// <summary>
/// The manifest file name. Default is "manifest.json".
/// </summary>
public string Manifest { get; init; } = "manifest.json";
public string Manifest { get; set; } = "manifest.json";

/// <summary>
/// The subfolder where your assets will be located, including the manifest file.
/// This value is relative to the web root path.
/// </summary>
public string? Base { get; init; }
public string? Base { get; set; }

/// <summary>
/// The name of the package manager to use. Default value is "npm".
/// </summary>
public string PackageManager { get; init; } = "npm";
public string PackageManager { get; set; } = "npm";

/// <summary>
/// The directory where the package.json file is located.
/// Default value is the .NET project working directory.
/// </summary>
public string? PackageDirectory { get; init; }
public string? PackageDirectory { get; set; }

/// <summary>
/// Options for the Vite Dev Server.
/// </summary>
public ViteServerOptions Server { get; init; } = new ViteServerOptions();
public ViteServerOptions Server { get; set; } = new ViteServerOptions();
}
12 changes: 6 additions & 6 deletions src/Vite.AspNetCore/ViteServerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,30 @@ public record ViteServerOptions
/// <summary>
/// Enable or disable the automatic start of the Vite Dev Server. Default value is "false".
/// </summary>
public bool AutoRun { get; init; } = false;
public bool AutoRun { get; set; } = false;

/// <summary>
/// The port where the Vite Dev Server will be running. Default value is "5173".
/// </summary>
public ushort Port { get; init; } = 5173;
public ushort Port { get; set; } = 5173;

/// <summary>
/// The host where the Vite Dev Server will be running. Default value is "localhost".
/// </summary>
public string Host { get; init; } = "localhost";
public string Host { get; set; } = "localhost";

/// <summary>
/// Wait for Vite Server to load in seconds. Default value is "5".
/// </summary>
public int TimeOut { get; init; } = 5;
public int TimeOut { get; set; } = 5;

/// <summary>
/// If true, the middleware will use HTTPS to connect to the Vite Dev Server. Default value is "false".
/// </summary>
public bool Https { get; init; }
public bool Https { get; set; }

/// <summary>
/// The script name to run the Vite Dev Server. Default value is "dev".
/// </summary>
public string ScriptName { get; init; } = "dev";
public string ScriptName { get; set; } = "dev";
}

0 comments on commit f5941b0

Please sign in to comment.