ltrzesniewski / RazorBlade

Compile Razor templates at build-time without a dependency on ASP.NET.
MIT License
149 stars 7 forks source link

Make the generator self-contained (no runtime dependency) #3

Closed Tyrrrz closed 1 year ago

Tyrrrz commented 1 year ago

If I understand correctly, RazorBlade doesn't have any run-time dependencies, which means it should be marked as a development dependency. Installing it will automatically add PrivateAssets="all" to the package reference.

To do it, you need to add this property to the project file:

<PropertyGroup>
  <DevelopmentDependency>true</DevelopmentDependency>
</PropertyGroup>
ltrzesniewski commented 1 year ago

It does contain some run-time dependencies in the RazorBlade assembly, which most notably includes the base classes for templates.

image

Tyrrrz commented 1 year ago

Ah, I see, sorry. I was under the impression that the base classes were also generated by the source generator. Do you think that would be doable? It will probably mean that templates compiled in two different assemblies won't have the same base class though.

ltrzesniewski commented 1 year ago

I suppose I could embed the code from the base classes directly into the template, but that would be a burden to maintain. Compatibility on shared stuff like HtmlString would also have this issue.

Why would you like to avoid the runtime library?

Tyrrrz commented 1 year ago

I don't have a direct need for it right now, but it would be beneficial in case I was planning to use RazorBlade inside a library. It was one of the things I was experimenting with in MiniRazor, and for some reason I thought RazorBlade already solved that problem. No problem though, I will close the issue :)

ltrzesniewski commented 1 year ago

Well, actually I think I could solve that by adding a MSBuild property such as <RazorBladeEmbedded>true</RazorBladeEmbedded> which would then embed an internal version of the runtime library and remove the dependency.

As everything would be internal, you shouldn't have issues with inter-assembly dependencies.

ltrzesniewski commented 1 year ago

I'll reopen this, I might want to play with this idea as well 🙂

ltrzesniewski commented 1 year ago

Oops, I closed the wrong issue

ltrzesniewski commented 1 year ago

This is now released in v0.4.0 🙂

The self-contained mode will be enabled by default with:

<ItemGroup>
  <PackageReference Include="RazorBlade" Version="0.4.0" ExcludeAssets="compile;runtime" PrivateAssets="all" />
</ItemGroup>

But you can also control this behavior manually with:

<PropertyGroup>
  <RazorBladeEmbeddedLibrary>true</RazorBladeEmbeddedLibrary>
</PropertyGroup>
Tyrrrz commented 1 year ago

Awesome!