dotnet建立不建立非npm SPA

问题描述 投票:1回答:1

我目前正试图将一个单页应用程序集成到C# .Net Core 3.1项目中,但我很难说服MSBuild编译SPA并将其捆绑到运行时可执行文件中。

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

我的.csproj.cs中有趣的部分。

<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
msbuild single-page-application elm asp.net-core-3.1
1个回答
1
投票

所以,当我发布而不仅仅是构建应用程序时,这个配置确实有效,具体来说就是使用这个命令。

 dotnet publish -c Release
© www.soinside.com 2019 - 2024. All rights reserved.