Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Blazor] Fix hot reload for components with collocated JS files #43199

Merged
merged 7 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ Copyright (c) .NET Foundation. All rights reserved.
ResolveJsInitializerModuleStaticWebAssets;
</ResolveCoreStaticWebAssetsDependsOn>

<ResolveJSModuleStaticWebAssetsDependsOn>
$(ResolveJSModuleStaticWebAssetsDependsOn);
ResolveProjectStaticWebAssets;
</ResolveJSModuleStaticWebAssetsDependsOn>

<ResolveJSModuleStaticWebAssetsDependsOn Condition="'$(UsingMicrosoftNETSdkRazor)' == 'true'">
$(ResolveJSModuleStaticWebAssetsDependsOn);
ResolveRazorComponentInputs;
ResolveRazorGenerateInputs;
</ResolveJSModuleStaticWebAssetsDependsOn>
MackinnonBuck marked this conversation as resolved.
Show resolved Hide resolved

</PropertyGroup>

<PropertyGroup>
Expand Down Expand Up @@ -229,7 +240,7 @@ Copyright (c) .NET Foundation. All rights reserved.
>
<Output TaskParameter="ResolvedEndpoints" ItemName="_ResolvedJSBuildModuleEndpoints" />
</ResolveStaticWebAssetEndpointRoutes>

<GenerateJsModuleManifest
Condition="'@(_ExistingBuildJSModules)' != ''"
OutputFile="@(_JsModuleBuildManifestCandidate)"
Expand Down Expand Up @@ -341,7 +352,7 @@ Copyright (c) .NET Foundation. All rights reserved.
>
<Output TaskParameter="ResolvedEndpoints" ItemName="_ResolvedJSPublishModuleEndpoints" />
</ResolveStaticWebAssetEndpointRoutes>

<GenerateJsModuleManifest
Condition="'@(_ExistingPublishJSModules)' != ''"
OutputFile="@(_JsModulePublishManifestCandidate)"
Expand Down Expand Up @@ -389,7 +400,7 @@ Copyright (c) .NET Foundation. All rights reserved.
</Target>

<!-- JS file modules -->
<Target Name="ResolveJSModuleStaticWebAssets" DependsOnTargets="ResolveProjectStaticWebAssets">
<Target Name="ResolveJSModuleStaticWebAssets" DependsOnTargets="$(ResolveJSModuleStaticWebAssetsDependsOn)">

<ItemGroup>
<!-- To avoid situations where the content is defined twice, if the user defines the JS module explicitly as
Expand Down
25 changes: 21 additions & 4 deletions test/Microsoft.NET.Sdk.Razor.Tests/ScopedCssIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -410,15 +410,32 @@ public void RegeneratingScopedCss_ForProject()

// Make an edit
var scopedCssFile = Path.Combine(ProjectDirectory.TestRoot, "Components", "Pages", "Index.razor.css");
File.WriteAllLines(scopedCssFile, File.ReadAllLines(scopedCssFile).Concat(new[] { "body { background-color: orangered; }" }));
File.WriteAllLines(scopedCssFile, File.ReadAllLines(scopedCssFile).Concat(["body { background-color: orangered; }"]));

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

// Verify the generated file contains newly added css
AssertFileContains(bundlePath, "background-color: orangered");

// Verify that CSS edits continue to apply after new JS modules are added to the project
// https://github.com/dotnet/aspnetcore/issues/57599
var collocatedJsFile = Path.Combine(ProjectDirectory.TestRoot, "Components", "Pages", "Index.razor.js");
File.WriteAllLines(collocatedJsFile, ["console.log('Hello, world!');"]);
File.WriteAllLines(scopedCssFile, File.ReadAllLines(scopedCssFile).Concat(["h1 { color: purple; }"]));

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

var fileInfo = new FileInfo(bundlePath);
fileInfo.Should().Exist();
// Verify the generated file contains newly added css
fileInfo.ReadAllText().Should().Contain("background-color: orangered");
AssertFileContains(bundlePath, "color: purple");

static void AssertFileContains(string fileName, string content)
{
var fileInfo = new FileInfo(fileName);
fileInfo.Should().Exist();
fileInfo.ReadAllText().Should().Contain(content);
}
}
}

Expand Down
Loading