Skip to content

Commit 5171dbf

Browse files
[Blazor] Fix hot reload for components with collocated JS files (#43199)
1 parent dec1a28 commit 5171dbf

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src/StaticWebAssetsSdk/Targets/Microsoft.NET.Sdk.StaticWebAssets.JSModules.targets

+7-3
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,14 @@ Copyright (c) .NET Foundation. All rights reserved.
3636

3737
<ResolveCoreStaticWebAssetsDependsOn>
3838
$(ResolveCoreStaticWebAssetsDependsOn);
39-
ResolveJSModuleStaticWebAssets;
4039
ResolveJsInitializerModuleStaticWebAssets;
4140
</ResolveCoreStaticWebAssetsDependsOn>
4241

42+
<ResolveCoreStaticWebAssetsDependsOn Condition="'$(UsingMicrosoftNETSdkRazor)' == 'true'">
43+
$(ResolveCoreStaticWebAssetsDependsOn);
44+
ResolveJSModuleStaticWebAssets;
45+
</ResolveCoreStaticWebAssetsDependsOn>
46+
4347
</PropertyGroup>
4448

4549
<PropertyGroup>
@@ -229,7 +233,7 @@ Copyright (c) .NET Foundation. All rights reserved.
229233
>
230234
<Output TaskParameter="ResolvedEndpoints" ItemName="_ResolvedJSBuildModuleEndpoints" />
231235
</ResolveStaticWebAssetEndpointRoutes>
232-
236+
233237
<GenerateJsModuleManifest
234238
Condition="'@(_ExistingBuildJSModules)' != ''"
235239
OutputFile="@(_JsModuleBuildManifestCandidate)"
@@ -341,7 +345,7 @@ Copyright (c) .NET Foundation. All rights reserved.
341345
>
342346
<Output TaskParameter="ResolvedEndpoints" ItemName="_ResolvedJSPublishModuleEndpoints" />
343347
</ResolveStaticWebAssetEndpointRoutes>
344-
348+
345349
<GenerateJsModuleManifest
346350
Condition="'@(_ExistingPublishJSModules)' != ''"
347351
OutputFile="@(_JsModulePublishManifestCandidate)"

test/Microsoft.NET.Sdk.Razor.Tests/ScopedCssIntegrationTests.cs

+21-4
Original file line numberDiff line numberDiff line change
@@ -410,15 +410,32 @@ public void RegeneratingScopedCss_ForProject()
410410

411411
// Make an edit
412412
var scopedCssFile = Path.Combine(ProjectDirectory.TestRoot, "Components", "Pages", "Index.razor.css");
413-
File.WriteAllLines(scopedCssFile, File.ReadAllLines(scopedCssFile).Concat(new[] { "body { background-color: orangered; }" }));
413+
File.WriteAllLines(scopedCssFile, File.ReadAllLines(scopedCssFile).Concat(["body { background-color: orangered; }"]));
414+
415+
build = CreateBuildCommand(ProjectDirectory);
416+
ExecuteCommand(build, "/t:UpdateStaticWebAssetsDesignTime").Should().Pass();
417+
418+
// Verify the generated file contains newly added css
419+
AssertFileContains(bundlePath, "background-color: orangered");
420+
421+
// Verify that CSS edits continue to apply after new JS modules are added to the project
422+
// https://github.com/dotnet/aspnetcore/issues/57599
423+
var collocatedJsFile = Path.Combine(ProjectDirectory.TestRoot, "Components", "Pages", "Index.razor.js");
424+
File.WriteAllLines(collocatedJsFile, ["console.log('Hello, world!');"]);
425+
File.WriteAllLines(scopedCssFile, File.ReadAllLines(scopedCssFile).Concat(["h1 { color: purple; }"]));
414426

415427
build = CreateBuildCommand(ProjectDirectory);
416428
ExecuteCommand(build, "/t:UpdateStaticWebAssetsDesignTime").Should().Pass();
417429

418-
var fileInfo = new FileInfo(bundlePath);
419-
fileInfo.Should().Exist();
420430
// Verify the generated file contains newly added css
421-
fileInfo.ReadAllText().Should().Contain("background-color: orangered");
431+
AssertFileContains(bundlePath, "color: purple");
432+
433+
static void AssertFileContains(string fileName, string content)
434+
{
435+
var fileInfo = new FileInfo(fileName);
436+
fileInfo.Should().Exist();
437+
fileInfo.ReadAllText().Should().Contain(content);
438+
}
422439
}
423440
}
424441

0 commit comments

Comments
 (0)