简体   繁体   English

ASP.NET Core 3.1 与 npm 的集成

[英]ASP.NET Core 3.1 integration with npm

I'm working on an ASP.NET Core 3.1 application and decided to use npm to manage front end libraries, right now they should be few, but that might change in the future.我正在开发一个 ASP.NET Core 3.1 应用程序并决定使用 npm 来管理前端库,现在它们应该很少,但将来可能会改变。

I have seen several similar questions, but they all seem to be a little outdated:我看过几个类似的问题,但它们似乎都有些过时了:

How to use NPM and install packages inside Visual Studio 2017? 如何在 Visual Studio 2017 中使用 NPM 和安装包?

How to use npm with ASP.NET Core 如何在 ASP.NET Core 中使用 npm

I intend to use npm to install the required libraries on the CI environment, I remember being able to integrate npm commands into the dotnet ones and would like to know if it is possible/recommendable.我打算使用 npm 在 CI 环境中安装所需的库,我记得能够将 npm 命令集成到dotnet命令中,并且想知道是否可行/推荐。

What is the recommended approach to use npm in this newer version of ASP.NET Core?在这个较新版本的 ASP.NET Core 中使用 npm 的推荐方法是什么?

I suggest using Yarn for two reasons:我建议使用Yarn有两个原因:

  • For your use case, it is a drop in replacement for npm.对于您的用例,它是 npm 的替代品。
  • An existing Nuget package, Yarn.MSBuild , integrates Yarn into MSBuild.现有的 Nuget 包Yarn.MSBuild将 Yarn 集成到 MSBuild 中。

Yarn.MSBuild allows you to specify Yarn commands in your .csproj . Yarn.MSBuild 允许您在.csproj指定Yarn 命令 These commands run when you build your project.这些命令在您构建项目时运行。 For example, to install front end packages on build:例如,要在构建时安装前端包:

  1. Add a reference to Yarn.MSBuild in your .csproj :.csproj添加对 Yarn.MSBuild 的引用:

     <ItemGroup> <PackageReference Include="Yarn.MSBuild" Version="*" /> </ItemGroup>
  2. Add a YarnBuildCommand property:添加一个YarnBuildCommand属性:

     <PropertyGroup> <YarnBuildCommand Condition="'$(Configuration)' == 'Release'">install</YarnBuildCommand> </PropertyGroup>

In the above example, yarn install runs when you build your project in release configuration.在上面的示例中,当您在发布配置中构建项目时,会运行yarn install You asked specifically about building in CI using dotnet - with this setup, when you run dotnet build -c Release in CI, your front end packages are installed.您专门询问了使用dotnet在 CI 中构建的问题 - 通过此设置,当您在 CI 中运行dotnet build -c Release时,您的前端包已安装。

Note that by default, the command executes in your .csproj 's directory.请注意,默认情况下,该命令在您的.csproj目录中执行。 You can change this by adding a YarnWorkingDir property:您可以通过添加YarnWorkingDir属性来更改此YarnWorkingDir

<PropertyGroup>
  <YarnWorkingDir>$(MSBuildProjectDirectory)/wwwroot/</YarnWorkingDir>
</PropertyGroup>

After some research, I ended up using my approach.经过一些研究,我最终使用了我的方法。
I created a Node folder on my web project to hold all the things node/npm, installed all the packages from there and also added gulpfile.js in it.我在我的 web 项目上创建了一个Node文件夹来保存所有的东西 node/npm,从那里安装所有的包,并在其中添加了gulpfile.js I made the copying of the CSS and JS files I needed part of the build process like this:我复制了我在构建过程中需要的 CSS 和 JS 文件,如下所示:

<Target Name="ImportLibsAndStyles" BeforeTargets="Build">
    <Exec WorkingDirectory="./Node" Command="gulp build" />
</Target>

and I intend to make the bundling and minification happen on the CI server before deploy.我打算在部署之前在 CI 服务器上进行捆绑和缩小。 I found this approach easier and with less conofiguration, if anyone can make improvements on it they will be greatly appreciated.我发现这种方法更容易并且配置更少,如果有人可以对其进行改进,他们将不胜感激。 I'm not closing the question in the hopes that more people will keep on adding.我不是在结束这个问题,希望更多的人会继续补充。

i am working with asp.net core 3.0 for api and i am using react for front-end我正在为 api 使用 asp.net core 3.0,我正在为前端使用 react

did you try to add your startup.cs您是否尝试添加您的 startup.cs

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

            if (env.IsDevelopment())
            {
                spa.UseReactDevelopmentServer(npmScript: "start");
            }
        });

When you start your solution .net will start your front-end project当您启动解决方案时,.net 将启动您的前端项目

and my .csproj file like this和我这样的 .csproj 文件

  <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)node_modules\**" />
  </ItemGroup>

  <ItemGroup>
    <Folder Include="Migrations\" />
  </ItemGroup>

  <Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') ">
    <!-- Ensure Node.js is installed -->
    <Exec Command="node --version" ContinueOnError="true">
      <Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
    </Exec>
    <Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE." />
    <Message Importance="high" Text="Restoring dependencies using 'npm'. This may take several minutes..." />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
  </Target>

  <Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
    <!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm run 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM