dotnet build 不构建非 npm SPA

dotnet build not building non-npm SPA

我目前正在尝试将单页应用程序集成到 C# .Net Core 3.1 项目中,但我正在努力说服 MSBuild 编译 spa 并将其捆绑到运行时可执行文件中。

SPA 使用 Elm + create-elm-app,因此不使用 npm。我已经调整了我的 .csproj 文件以委托给 elm-app build 但这没有被调用。故意破坏 Elm 代码以再次强制构建失败没有效果。请注意,我已经用 elm-stuff 替换了 node_modules ,这是下载依赖项等的地方。我假设这与我的 ItemGroups 有关,但源文件在我的 (Rider) 项目浏览器中被识别。另请注意,

我的 .csproj 中有趣的部分:

<Project Sdk="Microsoft.NET.Sdk.Web">

    <PropertyGroup>
        <TargetFramework>netcoreapp3.1</TargetFramework>
        <IsPackable>false</IsPackable>
        <SpaRoot>ClientApp\</SpaRoot>
        <DefaultItemExcludes>$(DefaultItemExcludes);$(SpaRoot)elm-stuff\**</DefaultItemExcludes>
    </PropertyGroup>

    <ItemGroup>
      <PackageReference Include="EventStore.ClientAPI.NetCore" Version="4.1.0.23" />
      <PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="3.1.4" />
      <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.4" />
      <PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="3.1.4" />
      <PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.4" />
    </ItemGroup>

    <ItemGroup>
        <!-- Don't publish the SPA source files, but do show them in the project files list -->
        <Content Remove="$(SpaRoot)**" />
        <None Remove="$(SpaRoot)**" />
        <None Include="$(SpaRoot)**" Exclude="$(SpaRoot)elm-stuff\**" />
    </ItemGroup>

    <Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
        <!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
        <Exec WorkingDirectory="$(SpaRoot)" Command="elm-app build" />

        <!-- Include the newly-built files in the publish output -->
        <ItemGroup>
            <DistFiles Include="$(SpaRoot)build\**" />
            <ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
                <RelativePath>%(DistFiles.Identity)</RelativePath>
                <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
                <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
            </ResolvedFileToPublish>
        </ItemGroup>
    </Target>
</Project>

我的有趣部分 Startup.cs

        public void ConfigureServices(IServiceCollection services)
        {
            .... 
            if (_environment.IsDevelopment())
            {
                services.AddControllersWithViews()
                    .AddRazorRuntimeCompilation();
            }
            else
            {
                services.AddControllersWithViews();
            }

            services.AddSpaStaticFiles(configuration => { configuration.RootPath = "ClientApp/build"; });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

            ....

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseSpaStaticFiles();


            app.UseRouting();

            app.UseSpa(spa =>
            {
                spa.Options.SourcePath = "ClientApp";

                if (_environment.IsDevelopment())
                {
                    spa.UseProxyToSpaDevelopmentServer("http://localhost:3000/");
                }
            });
        }

dotnet 构建的结果显示 C# 源代码编译成功,但没有提及委托或未能委托给 elm-app:

will@cafell Application % dotnet build                                                                     
Microsoft (R) Build Engine version 16.5.0+d4cbfca49 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restore completed in 49.73 ms for /Users/will/src/startups/ideasforphotography/ideasforphotography/IdeasForPhotography.Framework/IdeasForPhotography.Framework.csproj.
  Restore completed in 49.74 ms for /Users/will/src/startups/ideasforphotography/ideasforphotography/Application/Application.csproj.
  Restore completed in 49.74 ms for /Users/will/src/startups/ideasforphotography/ideasforphotography/IdeasForPhotography.Domain.ClubIdeas/IdeasForPhotography.Domain.ClubIdeas.csproj.
  IdeasForPhotography.Framework -> /Users/will/src/startups/ideasforphotography/ideasforphotography/IdeasForPhotography.Framework/bin/Debug/netcoreapp3.1/IdeasForPhotography.Framework.dll
  IdeasForPhotography.Domain.ClubIdeas -> /Users/will/src/startups/ideasforphotography/ideasforphotography/IdeasForPhotography.Domain.ClubIdeas/bin/Debug/netcoreapp3.1/IdeasForPhotography.Domain.ClubIdeas.dll
Clubs/ClubApplicationService.cs(19,21): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). [/Users/will/src/startups/ideasforphotography/ideasforphotography/Application/Application.csproj]
Infrastructure/EventDeserializer.cs(21,30): warning CS0168: The variable 'ex' is declared but never used [/Users/will/src/startups/ideasforphotography/ideasforphotography/Application/Application.csproj]
  Application -> /Users/will/src/startups/ideasforphotography/ideasforphotography/Application/bin/Debug/netcoreapp3.1/Application.dll
  Application -> /Users/will/src/startups/ideasforphotography/ideasforphotography/Application/bin/Debug/netcoreapp3.1/Application.Views.dll

Build succeeded.

Clubs/ClubApplicationService.cs(19,21): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). [/Users/will/src/startups/ideasforphotography/ideasforphotography/Application/Application.csproj]
Infrastructure/EventDeserializer.cs(21,30): warning CS0168: The variable 'ex' is declared but never used [/Users/will/src/startups/ideasforphotography/ideasforphotography/Application/Application.csproj]
    2 Warning(s)
    0 Error(s)

Time Elapsed 00:00:03.34

所以,当我发布而不是仅仅构建应用程序时,这个配置确实有效,特别是使用这个命令

 dotnet publish -c Release