Skip to content

Commit d4e6e09

Browse files
authoredApr 24, 2020
Introducing LazyNameAttribute (#23)
1 parent 3061664 commit d4e6e09

File tree

15 files changed

+87
-29
lines changed

15 files changed

+87
-29
lines changed
 

‎demo/Logger/Logger.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<ItemGroup Condition="!$(BLLUseLocalNugetPackages)">
1616
<ProjectReference Include="..\..\nuget\BlazorLazyLoading.Module\BlazorLazyLoading.Module.csproj" PrivateAssets="all" />
1717
</ItemGroup>
18-
<Import Project="..\..\nuget\BlazorLazyLoading.Module\build\BlazorLazyLoading.Module.props" />
19-
<Import Project="..\..\nuget\BlazorLazyLoading.Module\build\BlazorLazyLoading.Module.targets" />
18+
<Import Condition="!$(BLLUseLocalNugetPackages)" Project="..\..\nuget\BlazorLazyLoading.Module\build\BlazorLazyLoading.Module.props" />
19+
<Import Condition="!$(BLLUseLocalNugetPackages)" Project="..\..\nuget\BlazorLazyLoading.Module\build\BlazorLazyLoading.Module.targets" />
2020

2121
</Project>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@attribute [LazyName("NamedComponent")]
2+
3+
<h3>NAMED COMPONENT (LAZY)</h3>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@page "/simple-page"
2+
3+
<h3>SimplePage</h3>
4+
5+
@code {
6+
7+
}

‎demo/ModulesHost/ModulesHost.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<ItemGroup Condition="!$(BLLUseLocalNugetPackages)">
2828
<ProjectReference Include="..\..\nuget\BlazorLazyLoading.Module\BlazorLazyLoading.Module.csproj" PrivateAssets="all" />
2929
</ItemGroup>
30-
<Import Project="..\..\nuget\BlazorLazyLoading.Module\build\BlazorLazyLoading.Module.props" />
31-
<Import Project="..\..\nuget\BlazorLazyLoading.Module\build\BlazorLazyLoading.Module.targets" />
30+
<Import Condition="!$(BLLUseLocalNugetPackages)" Project="..\..\nuget\BlazorLazyLoading.Module\build\BlazorLazyLoading.Module.props" />
31+
<Import Condition="!$(BLLUseLocalNugetPackages)" Project="..\..\nuget\BlazorLazyLoading.Module\build\BlazorLazyLoading.Module.targets" />
3232

3333
</Project>

‎demo/ModulesHost/_Imports.razor

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@using System.Net.Http
2+
@using Microsoft.AspNetCore.Components.Forms
3+
@using Microsoft.AspNetCore.Components.Routing
4+
@using Microsoft.AspNetCore.Components.Web
5+
@using Microsoft.JSInterop
6+
7+
@using BlazorLazyLoading

‎demo/WasmHost/Pages/Counter.razor

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
88

9-
<Lazy Name="Component1" />
9+
<Lazy Name="SimpleComponent">
10+
<div>LOADING SIMPLECOMPONENT...</div>
11+
</Lazy>
12+
13+
<Lazy Name="NamedComponent" />
1014

1115
@code {
1216
private int currentCount = 0;

‎nuget/BlazorLazyLoading.Module/BlazorLazyLoading.Module.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
77
</PropertyGroup>
88

9+
<ItemGroup>
10+
<ProjectReference Include="..\BlazorLazyLoading.Components\BlazorLazyLoading.Components.csproj" />
11+
<ProjectReference Include="..\..\src\ManifestGenerator\ManifestGenerator.csproj" />
12+
</ItemGroup>
13+
914
<PropertyGroup>
1015
<IncludeSymbols>false</IncludeSymbols>
1116
<NuspecFile>$(MSBuildProjectName).nuspec</NuspecFile>
@@ -15,11 +20,6 @@
1520
<Description>Build tools for creating a Blazor Lazy Loadable module</Description>
1621
</PropertyGroup>
1722

18-
<ItemGroup>
19-
<ProjectReference Include="..\..\src\ManifestGenerator\ManifestGenerator.csproj" />
20-
<ProjectReference Include="..\BlazorLazyLoading.Components\BlazorLazyLoading.Components.csproj" />
21-
</ItemGroup>
22-
2323
<ItemGroup>
2424
<Compile Remove="build\bin\**" />
2525
<EmbeddedResource Remove="build\bin\**" />

‎nuget/BlazorLazyLoading.Module/BlazorLazyLoading.Module.nuspec

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
<title>$title$</title>
77
<description>$description$</description>
88
<authors>$author$</authors>
9+
<dependencies>
10+
<dependency id="BlazorLazyLoading.Components" version="$version$" />
11+
</dependencies>
912
</metadata>
1013
<files>
1114
<file src="build\*.*" target="build" />

‎nuget/BlazorLazyLoading.Server/BlazorLazyLoading.Server.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
<PropertyGroup>
44
<TargetFramework>netcoreapp3.1</TargetFramework>
55
<OutputType>Library</OutputType>
6+
<LangVersion>8.0</LangVersion>
7+
<Nullable>enable</Nullable>
8+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
69
</PropertyGroup>
710

811
<ItemGroup>
9-
<ProjectReference Include="..\..\src\AssemblyLoader.Server\AssemblyLoader.Server.csproj" />
1012
<ProjectReference Include="..\BlazorLazyLoading.Components\BlazorLazyLoading.Components.csproj" />
13+
<ProjectReference Include="..\..\src\AssemblyLoader.Server\AssemblyLoader.Server.csproj" />
1114
</ItemGroup>
1215

1316
<PropertyGroup>

‎nuget/BlazorLazyLoading.Wasm/BlazorLazyLoading.Wasm.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.1</TargetFramework>
5+
<LangVersion>8.0</LangVersion>
6+
<Nullable>enable</Nullable>
7+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
58
</PropertyGroup>
69

710
<ItemGroup>
8-
<ProjectReference Include="..\..\src\AssemblyLoader.Wasm\AssemblyLoader.Wasm.csproj" />
911
<ProjectReference Include="..\BlazorLazyLoading.Components\BlazorLazyLoading.Components.csproj" />
12+
<ProjectReference Include="..\..\src\AssemblyLoader.Wasm\AssemblyLoader.Wasm.csproj" />
1013
</ItemGroup>
1114

1215
<PropertyGroup>

‎nuget/NugetCommon.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<Copyright>Ivan Sanz Carasa (isc30)</Copyright>
3434
</PropertyGroup>
3535

36-
<Target Name="SetNuspecProperties" BeforeTargets="Build">
36+
<Target Name="SetNuspecProperties" BeforeTargets="Build;Pack;Restore">
3737
<CallTarget Targets="GetVersion" />
3838
<Message Importance="normal" Text="Stamping MsBuild vars into Nuspec" />
3939
<PropertyGroup>

‎src/LazyComponents/LazyComponent/Lazy.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class Lazy : ComponentBase
1717
public string Name { get; set; } = null!;
1818

1919
[Parameter]
20-
public Type? Fallback { get; set; } = null;
20+
public RenderFragment? ChildContent { get; set; } = null;
2121

2222
[Inject]
2323
public IAssemblyLoader AssemblyLoader { get; set; } = null!;
@@ -44,21 +44,20 @@ protected override async Task OnInitializedAsync()
4444
TypeFullName = o.Value<string?>("TypeFullName"),
4545
Name = o.Value<string?>("Name"),
4646
})
47-
.Where(i => !string.IsNullOrWhiteSpace(i.Name) && !string.IsNullOrWhiteSpace(i.TypeFullName))
47+
.Where(i => !string.IsNullOrWhiteSpace(i.TypeFullName))
4848
.Select(o => new
4949
{
5050
TypeFullName = o.TypeFullName!,
51-
Name = o.Name!,
51+
Name = o.Name,
5252
})
53-
.Where(n => n.Name.EndsWith(Name) || n.TypeFullName == Name)
5453
.Select(n => new
5554
{
5655
Match = n,
5756
Score = n.TypeFullName == Name
5857
? 3
5958
: n.Name == Name
6059
? 2
61-
: n.Name.EndsWith(Name)
60+
: n.TypeFullName.EndsWith(Name)
6261
? 1
6362
: 0,
6463
Manifest = m,
@@ -78,7 +77,7 @@ protected override async Task OnInitializedAsync()
7877

7978
if (bestMatches.Count > 1)
8079
{
81-
throw new NotSupportedException($"Multiple matches for Component with name '{Name}'");
80+
throw new NotSupportedException($"Multiple matches for Component with name '{Name}': '{string.Join(";", bestMatches.Select(m => m.Match.TypeFullName))}'");
8281
}
8382

8483
var bestMatch = bestMatches.First();
@@ -97,6 +96,8 @@ protected override async Task OnInitializedAsync()
9796

9897
protected override void BuildRenderTree(RenderTreeBuilder builder)
9998
{
99+
base.BuildRenderTree(builder);
100+
100101
if (Type == null)
101102
{
102103
BuildFallbackComponent(builder);
@@ -110,11 +111,9 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
110111

111112
private void BuildFallbackComponent(RenderTreeBuilder builder)
112113
{
113-
if (Fallback != null)
114+
if (ChildContent != null)
114115
{
115-
builder.OpenComponent(0, Fallback);
116-
builder.CloseComponent();
117-
116+
ChildContent.Invoke(builder);
118117
return;
119118
}
120119

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
namespace BlazorLazyLoading
4+
{
5+
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
6+
public class LazyNameAttribute : Attribute
7+
{
8+
public string ComponentName { get; }
9+
10+
public LazyNameAttribute(string componentName)
11+
{
12+
ComponentName = componentName;
13+
}
14+
}
15+
}

‎src/ManifestGenerator/ManifestGenerators/ComponentManifestGenerator.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,22 @@ public Dictionary<string, object> GenerateManifest(Assembly assembly)
1111
var componentTypes = assembly.GetTypes()
1212
.Where(t => t.BaseType.FullName == "Microsoft.AspNetCore.Components.ComponentBase");
1313

14-
var components = componentTypes
15-
.Select(t => new ComponentManifest(t.FullName))
16-
.ToList();
14+
var components = new List<ComponentManifest>();
15+
16+
foreach (var component in componentTypes)
17+
{
18+
var lazyNameAttribute = component.GetCustomAttributesData()
19+
.SingleOrDefault(a => a.AttributeType.FullName == "BlazorLazyLoading.LazyNameAttribute");
20+
21+
if (lazyNameAttribute == null)
22+
{
23+
components.Add(new ComponentManifest(component.FullName, null));
24+
continue;
25+
}
26+
27+
var lazyName = (string)lazyNameAttribute.ConstructorArguments[0].Value;
28+
components.Add(new ComponentManifest(component.FullName, lazyName));
29+
}
1730

1831
return new Dictionary<string, object>
1932
{
@@ -25,13 +38,14 @@ private sealed class ComponentManifest
2538
{
2639
public string TypeFullName { get; }
2740

28-
public string Name { get; }
41+
public string? Name { get; }
2942

3043
public ComponentManifest(
31-
string typeFullName)
44+
string typeFullName,
45+
string? lazyName)
3246
{
3347
TypeFullName = typeFullName;
34-
Name = typeFullName;
48+
Name = lazyName;
3549
}
3650
}
3751
}

0 commit comments

Comments
 (0)