【问题标题】:How to embed the scoped css bundle file generated by Razor class library?如何嵌入 Razor 类库生成的作用域 css 包文件?
【发布时间】:2021-11-20 18:01:13
【问题描述】:

我希望嵌入一个 razor 类库项目生成的作用域 css 包文件。该文件在构建时放置在 obj 文件夹中。我在定义正确的 msbuild 目标以在构建期间的正确时间发出嵌入式资源时遇到了挑战。

在深入研究 dotnet 目标文件之后,这就是我目前在 .csproj 文件中的内容。

<Target Name="EmbedScopedCss" BeforeTargets="BeforeResGen" AfterTargets="BundleScopedCssFiles">
    <ItemGroup>
        <EmbeddedResource Include="$(IntermediateOutputPath)scopedcss\bundle\$(PackageId).styles.css" Type="Non-Resx" WithCulture="false" />
    </ItemGroup>
</Target>

这应该使嵌入的资源项在 BeforeResGen 目标之前发出,据我所知,这是在枚举资源以嵌入到程序集中之前的最后时刻。

但是,这似乎仍然在构建过程中运行得太早,因为此时文件本身并不存在。

我已经从 razor sdk 作用域 css 目标文件中推断出 AfterTargets="BundleScopedCssFiles",但这似乎还不够。

有没有办法做到这一点?

【问题讨论】:

    标签: css asp.net-core razor msbuild blazor


    【解决方案1】:

    如果我正确理解了 MSBuild 日志,则在项目的程序集已经编译后调用负责为当前项目生成 CSS 包的目标。

    我个人已经通过使用单独的 Razor 类库项目来存储具有范围样式表的所有组件来解决此问题。然后我在主项目的.csproj文件中将其生成的.styles.css文件标记为EmbeddedResource

      <PropertyGroup>
        <!-- Disable CSS bundle generation for the main project as we cannot embed it -->
        <DisableScopedCssBundling>true</DisableScopedCssBundling>
    
        <!-- Define the base namespace for embedded CSS bundles -->
        <ScopedCssEmbeddedResourceNamespace>$(RootNamespace).wwwroot</ScopedCssEmbeddedResourceNamespace>
      </PropertyGroup>
    
      <Target Name="EmbedScopedCssBundles" AfterTargets="BundleScopedCssFiles" BeforeTargets="MainResourcesGeneration">
        <!-- Search for generated *.styles.css files for the current build configuration in neighbor projects and embed them -->
        <ItemGroup>
          <ScopedCssBundle Include="$(ProjectDir)../**/$(Configuration)/**/scopedcss/bundle/*.styles.css" />
          <ScopedCssEmbeddedResource Include="@(ScopedCssBundle)"
                                     ResourceName="$(ScopedCssEmbeddedResourceNamespace).%(Filename)%(Extension)" />
          <EmbeddedResource Include="@(ScopedCssEmbeddedResource)"
                            LogicalName="%(ScopedCssEmbeddedResource.ResourceName)" />
        </ItemGroup>
    
        <!-- Optional messages for MSBuild log -->
        <Message Condition="'@(ScopedCssEmbeddedResource)' == ''"
                 Text="No scoped CSS bundles found for $(Configuration) configuration" />
        <Message Condition="'@(ScopedCssEmbeddedResource)' != ''"
                 Text="Embedding scoped CSS bundles for $(Configuration) configuration:" />
        <Message Condition="'@(ScopedCssEmbeddedResource)' != ''"
                 Text="  %(ScopedCssEmbeddedResource.Identity) -> %(ScopedCssEmbeddedResource.ResourceName)" />
      </Target>
    

    如果主项目名为MyProject,而相邻的 Razor 类库项目名为MyProject.RazorLib,则上述代码生成的作用域 CSS 包将嵌入到名为 MyProject.wwwroot.MyProject.RazorLib.styles.css 的主项目的程序集中。

    这种方法的缺点是每个嵌入的 CSS 包都必须在标记中单独引用。

    【讨论】:

    • 我最终也不得不使用两个项目的方法。这不是很好,但它有效。
    猜你喜欢
    • 2022-08-02
    • 2020-12-12
    • 2020-11-15
    • 2020-04-27
    • 2019-01-07
    • 2014-05-12
    • 2019-11-28
    • 1970-01-01
    • 2017-11-24
    相关资源
    最近更新 更多