Skip to content

Commit 5a1c858

Browse files
adrumkapi2289
andauthored
[1.x] Fix Vite Hot File Detection (#35)
* 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]>
1 parent 1c6c064 commit 5a1c858

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

InertiaCore/Utils/Vite.cs

+18-5
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,22 @@ protected internal void UseFileSystem(IFileSystem fileSystem)
3030
}
3131

3232
/// <summary>
33-
/// Get the public directory and build path.
33+
/// Get the public directory
3434
/// </summary>
3535
private string GetPublicPathForFile(string path)
36+
{
37+
var pieces = new List<string> {
38+
_options.Value.PublicDirectory,
39+
path
40+
};
41+
42+
return string.Join("/", pieces);
43+
}
44+
45+
/// <summary>
46+
/// Get the public directory and build path.
47+
/// </summary>
48+
private string GetBuildPathForFile(string path)
3649
{
3750
var pieces = new List<string> { _options.Value.PublicDirectory };
3851
if (!string.IsNullOrEmpty(_options.Value.BuildDirectory))
@@ -54,12 +67,12 @@ public HtmlString Input(string path)
5467
return new HtmlString(MakeModuleTag("@vite/client").Value + MakeModuleTag(path).Value);
5568
}
5669

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

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

6578
if (manifestJson == null)
@@ -209,8 +222,8 @@ private static string GetString(IHtmlContent content)
209222

210223
public string? GetManifest()
211224
{
212-
return _fileSystem.File.Exists(GetPublicPathForFile(_options.Value.ManifestFilename))
213-
? _fileSystem.File.ReadAllText(GetPublicPathForFile(_options.Value.ManifestFilename))
225+
return _fileSystem.File.Exists(GetBuildPathForFile(_options.Value.ManifestFilename))
226+
? _fileSystem.File.ReadAllText(GetBuildPathForFile(_options.Value.ManifestFilename))
214227
: null;
215228
}
216229
}

InertiaCoreTests/UnitTestVite.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void TestHot()
3737
{
3838
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
3939
{
40-
{ @"/wwwroot/build/hot", new MockFileData("http://127.0.0.1:5174") },
40+
{ @"/wwwroot/hot", new MockFileData("http://127.0.0.1:5174") },
4141
});
4242
var options = new Mock<IOptions<ViteOptions>>();
4343
options.SetupGet(x => x.Value).Returns(new ViteOptions());
@@ -130,13 +130,12 @@ public void TestViteInput()
130130
result = mock.Object.Input("index.scss");
131131
Assert.That(result.ToString(), Is.EqualTo("<link href=\"/assets/index.css\" rel=\"stylesheet\" />\n\t"));
132132

133-
134133
// Hot file with css import
135134
options.SetupGet(x => x.Value).Returns(new ViteOptions
136135
{
137136
BuildDirectory = "build"
138137
});
139-
fileSystem.AddFile(@"/wwwroot/build/hot", new MockFileData("http://127.0.0.1:5174"));
138+
fileSystem.AddFile(@"/wwwroot/hot", new MockFileData("http://127.0.0.1:5174"));
140139

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

0 commit comments

Comments
 (0)