Skip to content

Commit

Permalink
[1.x] Fix Vite Hot File Detection (#35)
Browse files Browse the repository at this point in the history
* added automatic vite versioning

* fix hot detection

* fix hot tests to match laravel vite plugin

* Fix manifest path to build

---------

Co-authored-by: kapi2289 <[email protected]>
  • Loading branch information
adrum and kapi2289 authored Feb 23, 2025
1 parent 1c6c064 commit 5a1c858
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
23 changes: 18 additions & 5 deletions InertiaCore/Utils/Vite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,22 @@ protected internal void UseFileSystem(IFileSystem fileSystem)
}

/// <summary>
/// Get the public directory and build path.
/// Get the public directory
/// </summary>
private string GetPublicPathForFile(string path)
{
var pieces = new List<string> {
_options.Value.PublicDirectory,
path
};

return string.Join("/", pieces);
}

/// <summary>
/// Get the public directory and build path.
/// </summary>
private string GetBuildPathForFile(string path)
{
var pieces = new List<string> { _options.Value.PublicDirectory };
if (!string.IsNullOrEmpty(_options.Value.BuildDirectory))
Expand All @@ -54,12 +67,12 @@ public HtmlString Input(string path)
return new HtmlString(MakeModuleTag("@vite/client").Value + MakeModuleTag(path).Value);
}

if (!_fileSystem.File.Exists(GetPublicPathForFile(_options.Value.ManifestFilename)))
if (!_fileSystem.File.Exists(GetBuildPathForFile(_options.Value.ManifestFilename)))
{
throw new Exception("Vite Manifest is missing. Run `npm run build` and try again.");
}

var manifest = _fileSystem.File.ReadAllText(GetPublicPathForFile(_options.Value.ManifestFilename));
var manifest = _fileSystem.File.ReadAllText(GetBuildPathForFile(_options.Value.ManifestFilename));
var manifestJson = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(manifest);

if (manifestJson == null)
Expand Down Expand Up @@ -209,8 +222,8 @@ private static string GetString(IHtmlContent content)

public string? GetManifest()
{
return _fileSystem.File.Exists(GetPublicPathForFile(_options.Value.ManifestFilename))
? _fileSystem.File.ReadAllText(GetPublicPathForFile(_options.Value.ManifestFilename))
return _fileSystem.File.Exists(GetBuildPathForFile(_options.Value.ManifestFilename))
? _fileSystem.File.ReadAllText(GetBuildPathForFile(_options.Value.ManifestFilename))
: null;
}
}
Expand Down
5 changes: 2 additions & 3 deletions InertiaCoreTests/UnitTestVite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void TestHot()
{
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ @"/wwwroot/build/hot", new MockFileData("http://127.0.0.1:5174") },
{ @"/wwwroot/hot", new MockFileData("http://127.0.0.1:5174") },
});
var options = new Mock<IOptions<ViteOptions>>();
options.SetupGet(x => x.Value).Returns(new ViteOptions());
Expand Down Expand Up @@ -130,13 +130,12 @@ public void TestViteInput()
result = mock.Object.Input("index.scss");
Assert.That(result.ToString(), Is.EqualTo("<link href=\"/assets/index.css\" rel=\"stylesheet\" />\n\t"));


// Hot file with css import
options.SetupGet(x => x.Value).Returns(new ViteOptions
{
BuildDirectory = "build"
});
fileSystem.AddFile(@"/wwwroot/build/hot", new MockFileData("http://127.0.0.1:5174"));
fileSystem.AddFile(@"/wwwroot/hot", new MockFileData("http://127.0.0.1:5174"));

result = mock.Object.Input("index.scss");
Assert.That(result.ToString(), Is.EqualTo(
Expand Down

0 comments on commit 5a1c858

Please sign in to comment.