如何将Blazor CSS隔离文件复制到WWWROOT文件夹?

发布于 2025-01-25 01:25:00 字数 1344 浏览 2 评论 0 原文

目前,我在使用Glazor Integration的插件系统上构建,我在运行时引用了RCLS的组件。到目前为止,我已经能够克服大多数问题(路由,激活,终生,Interop和大多数基本的静态Web资产问题)。

,但是现在我在试图支持组件CSS隔离的同时跑了一个障碍。
实际上,输出文件 project.styles.css 或其已知的对手 project.bundle.scp.css 未被ASP.NET Core Core Runner拾取可以理解的是,不知道如何在构建时间生成的 project.staticwebassets.runtime.json.json 文件中引用开发路径(这是一个小片段):

{
    "ContentRoots": [
        "D:\\source\\project\\wwwroot\\",
        "D:\\source\\project\\obj\\Release\\net6.0\\scopedcss\\bundle\\"
    ],
    "Root": {
        "Children": {
            "SocialGuard.YC.styles.css": {
                "Children": null,
                "Asset": {
                    "ContentRootIndex": 1,
                    "SubPath": "project.styles.css"
                },
                "Patterns": null
            }
        },
        "Asset": null,
        "Patterns": [
            {
                "ContentRootIndex": 0,
                "Pattern": "**",
                "Depth": 0
            }
        ]
    }
}

为简单起见,更改了路径和项目名称。 em>

我的第一次尝试是找出如何将文件嵌入到输出.dll文件中,但会陷入屈辱的失败,以及对基于Nuget的部署的最大关注点。

现在的一个大问题是,因为我已经不得不在路径上妥协了很多,是否可以直接通过CSS隔离输出文件复制到 wwwroot 文件夹,在构建时间?

我对MSBuild不太了解,所以我祈祷有人对此有某种答案... ^^

Currently building on a plugin system with Blazor integration, I'm referencing the RCLs' components at runtime. So far so good, I've been able to overcome most issues (routing, activation, lifetime, interop, and most basic static web assets issues thinkable).

But now I'm running up a snag whilst trying to support Component CSS Isolation.
Indeed, the output file project.styles.css or its known counterpart project.bundle.scp.css aren't picked up by the ASP.NET Core runner, that has understandably no idea how to reference the dev paths in the buildtime-generated project.staticwebassets.runtime.json file (here's a small snippet) :

{
    "ContentRoots": [
        "D:\\source\\project\\wwwroot\\",
        "D:\\source\\project\\obj\\Release\\net6.0\\scopedcss\\bundle\\"
    ],
    "Root": {
        "Children": {
            "SocialGuard.YC.styles.css": {
                "Children": null,
                "Asset": {
                    "ContentRootIndex": 1,
                    "SubPath": "project.styles.css"
                },
                "Patterns": null
            }
        },
        "Asset": null,
        "Patterns": [
            {
                "ContentRootIndex": 0,
                "Pattern": "**",
                "Depth": 0
            }
        ]
    }
}

Paths and project name were changed for simplicity's sake

My first try was to figure out how to embed the file into the output .dll file, but running into a humiliating failure, and a big concern for release time NuGet-based deployment.

The big question now lies, as I've already had to compromise quite a bit on paths, is it possible to copy over the CSS Isolation output file straight into the wwwroot folder, at build time?

I'm not very knowledgeable with MSBuild, so I'm praying someone holds some kind of answer to this... ^^

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月下客 2025-02-01 01:25:00

我试图出于不同的目的将我的手放在同一文件上。
也许在 wwwroot 中复制它会为您提供帮助。

文档陈述以下内容:

在构建时间,与公约创建了一个项目捆绑包
obj/{configuration}/{target
框架}/scopedcss/projectBundle/{assembly name} .bundle.scp.css,
占位符在哪里:

{configuration}:应用程序的构建配置(例如,调试,
释放)。
{目标框架}:目标框架(例如,
net6.0)。
{汇编名称}:应用程序的汇编名称(例如,
Blazorsample)。

因此,我们可以使用 MSBUILD属性并在Visual Studio Post Build Events上复制文件。

首先,您可以使用以下路径测试:

ECHO "$(ProjectDir)obj\$(Configuration)\$(TargetFramework)\scopedcss\projectbundle\$(AssemblyName).bundle.scp.css"

下一步您可以 wwwroot $(projectDir)的文件

COPY /D /Y "$(ProjectDir)obj\$(Configuration)\$(TargetFramework)\scopedcss\projectbundle\$(AssemblyName).bundle.scp.css" "$(ProjectDir)wwwroot\css\$(AssemblyName).bundle.scp.css"
  • 是您项目文件夹的路径。
  • $(configuration)返回 debug Release
  • $(targetFramework)返回框架版本。例如 net5.0
  • $(assemblyName)当然返回您的项目组装名称,默认情况下,这也是示波器CSS捆绑文件的名称。

I was trying to get my hands on the same file for different purposes.
Perhaps copying it in wwwroot will help you.

The documentation states the following:

At build time, a project bundle is created with the convention
obj/{CONFIGURATION}/{TARGET
FRAMEWORK}/scopedcss/projectbundle/{ASSEMBLY NAME}.bundle.scp.css,
where the placeholders are:

{CONFIGURATION}: The app's build configuration (for example, Debug,
Release).
{TARGET FRAMEWORK}: The target framework (for example,
net6.0).
{ASSEMBLY NAME}: The app's assembly name (for example,
BlazorSample).

So we can built this exact path with MSBuild properties and copy the file at Visual Studio post build events.

First you can test the path with:

ECHO "$(ProjectDir)obj\$(Configuration)\$(TargetFramework)\scopedcss\projectbundle\$(AssemblyName).bundle.scp.css"

Next you may copy the file to wwwroot

COPY /D /Y "$(ProjectDir)obj\$(Configuration)\$(TargetFramework)\scopedcss\projectbundle\$(AssemblyName).bundle.scp.css" "$(ProjectDir)wwwroot\css\$(AssemblyName).bundle.scp.css"
  • $(ProjectDir) is the path to your project folder.
  • $(Configuration) returns either Debug or Release.
  • $(TargetFramework) returns framework version. For example net5.0.
  • $(AssemblyName) of course returns your project assembly name, which also by default is the name of your scoped css bundle file.
长不大的小祸害 2025-02-01 01:25:00

我认为您正在寻找的是复制在建造和捆绑CSS之后,制定动作以触发。因此,在您的示例中,您将在应用程序的CSPROJ文件的末尾添加类似的内容:

<Target Name="CopyCssBundles" AfterTargets="AfterBuild">
    <ItemGroup>
        // Include path to whatever file(s) you want to copy from RCL, separated by a semi-colon
        // Also works with relative paths
        // Also allows glob patterns (e.g., ...\scopedcss\**\*.* to include all files in the scopedcss directory)
        <MyCssBundles Include="{path_to_RCL_project}\obj\$(Configuration)\$(TargetFramework)\scopedcss\bundle\{RCL_assembly_name}.styles.css" />
    </ItemGroup>

    // Source files are whatever you named the above tag ("MyCssBundles" in this example)
    <Copy SourceFiles="@(MyCssBundles)" DestinationFiles="wwwroot\%(RecursiveDir)%(Filename)%(Extension)" OverwriteReadOnlyFiles="true" />
</Target>

%(recriveRivedir)将根据Include模式保留您包含的任何文件的目录树。如果您希望在 wwwroot 中直接使用文件,则可以删除该文件,也可以指定子目录以也将其放入中(例如, wwwroot \ css \ css \%(fileName)%(extension)%(扩展)< /代码>)。如果目录不存在,则将在复制时创建它。

如果您想将内容分开以更可读,则还可以让Multilpe ItemGroup /复制对配对。

I think what you're looking for is a Copy build action to trigger after building and bundling the css. So in your example, you would add something like this to the end of your app's csproj file:

<Target Name="CopyCssBundles" AfterTargets="AfterBuild">
    <ItemGroup>
        // Include path to whatever file(s) you want to copy from RCL, separated by a semi-colon
        // Also works with relative paths
        // Also allows glob patterns (e.g., ...\scopedcss\**\*.* to include all files in the scopedcss directory)
        <MyCssBundles Include="{path_to_RCL_project}\obj\$(Configuration)\$(TargetFramework)\scopedcss\bundle\{RCL_assembly_name}.styles.css" />
    </ItemGroup>

    // Source files are whatever you named the above tag ("MyCssBundles" in this example)
    <Copy SourceFiles="@(MyCssBundles)" DestinationFiles="wwwroot\%(RecursiveDir)%(Filename)%(Extension)" OverwriteReadOnlyFiles="true" />
</Target>

%(RecusriveDir) will preserve the directory tree of whatever files you included based on the include pattern. If you want the files directly in wwwroot you can remove that or you can specify a subdirectory to place them in as well (e.g., wwwroot\css\%(Filename)%(Extension)). If the directory does not exist it will create it when copying.

You can also have multilpe ItemGroup/Copy pairs in the same target if you want to separate things out to be more readable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文