Aspnetcore: Razor Class Libraries need a way to exclude some items from their static web assets

Created on 5 Aug 2019  路  15Comments  路  Source: dotnet/aspnetcore

Is your feature request related to a problem? Please describe.

I'm trying to build a nuget package which contains some, but not all of the items in wwwroot. Currently this isn't possible, due to the following glob in the ResolveStaticWebAssetsInputs target from Microsoft.NET.Sdk.Razor.StaticWebAssets.targets:

<_ThisProjectStaticWebAsset Include="$(MSBuildProjectDirectory)\wwwroot\**" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />

Describe the solution you'd like

Any kind of condition or property to control exclusion would work. Perhaps it could pick up whether the files are Content items with Pack="False"?
Alternatively, "wwwroot\**" could be defined by an overridable property.

Docs area-mvc question

All 15 comments

As a workaround, I've done the sdk-overriding dance - <Import Sdk="Microsoft.NET.Sdk.Razor" Project="Sdk.props"/> at the start, <Import Sdk="Microsoft.NET.Sdk.Razor" Project="Sdk.targets"/> at the end, then copying the whole ResolveStaticWebAssetsInputs target and altering its internals. But, this is obviously not great since it's going to be vulnerable to sdk changes.

Thanks for contacting us, @gulbanana.
Can't you simply move these out of the wwwroot folder?

/cc @javiercn

I鈥檓 using BundlerMinifier/dotnet bundle, which creates sourcemaps and minified files in place. Debug builds of my library come with the unminified assets, so wwwroot/ is the appropriate place for them - I exclude them only in releases.

StaticWebAsset鈥檚 dev time support is pretty great too, so having the unbundled assets available as _content to test projects in the same solution is something i鈥檇 rather not give up.

This can be done today with MSBuild if you write your own target. Something like this should work.

<Target Name="RemoveDevTimeArtifacts" DependsOnTargets="ResolveStaticWebAssetsInputs" Condition="$(Configuratio)n == 'Release'">
  <ItemGroup>
    <StaticWebAsset Remove="**\*.map.js" />
  </ItemGroup>
</Target>

@javiercn - should we add this to docs?

It鈥檚 definitely a better technique than the one I was using. I guess removing the StaticWebAsset items at this point has the correct impact on the manifest file thing?

@rynowak I think so, but it might need modifications. Now that I look at it again, it probably needs to be hook into some other target.

@danroth27 Can you figure out the right place to add this to the docs?

Can you figure out the right place to add this to the docs?

@javiercn https://docs.microsoft.com/aspnet/core/razor-pages/sdk

@danroth27 I meant what do you want it to say :)

@danroth27 Should we instead of doing a 1-off capture 4 or 5 scenarios we care about (this, typescript, etc) and write a section with all those?

@javiercn The first thing is to make sure all of the targets (ex ResolveStaticWebAssetsInputs), items (ex StaticWebAsset ), and properties are documented.

After the change in https://github.com/aspnet/AspNetCore-Tooling/pull/958 this is somewhat better; it gives us the ability to remove wwwroot files from SWAs as long as we didn't need them to be Content items for some other purpose. I'm hoping that once that version's released I'll be able to exclude files without overriding any SDK targets, by using

  <ItemGroup>
    <Content Remove="wwwroot\lib.css" />
    <None Include="wwwroot\lib.css" />
  </ItemGroup>

This can be done by excluding content as described in https://github.com/aspnet/AspNetCore/issues/12871#issuecomment-522236482
or simply by adding elements to $(DefaultItemExcludes) which prevent's them to be included as content in the first place.

Filed a doc issue for this https://github.com/aspnet/AspNetCore.Docs/issues/13967. Closing as there's no further action to be taken here.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

KerolosMalak picture KerolosMalak  路  269Comments

Rast1234 picture Rast1234  路  104Comments

davidfowl picture davidfowl  路  126Comments

oliverjanik picture oliverjanik  路  91Comments

barrytang picture barrytang  路  89Comments