简体   繁体   English

将 ASP.NET Core 3.1 与 Node.js 一起使用?

[英]Using ASP.NET Core 3.1 with Node.js?

I have read that NodeServices have been deprecated for ASP.NET Core 3.1 .我已经读到NodeServices 已被 ASP.NET Core 3.1 弃用 Is it still possible to use Node.js to build my JavaScript files into the dist folder in wwwroot ?是否仍然可以使用 Node.js 将我的 JavaScript 文件构建到wwwrootdist文件夹中?

According to the NodeServices package , it allows you to "invoke Node.js modules at runtime in ASP.NET Core applications" (emphasis my own).根据NodeServices 包,它允许您“在 ASP.NET Core 应用程序中在运行时调用 Node.js 模块”(强调我自己的)。 This is reiterated in more detail on the GitHub ReadMe :GitHub 自述文件中更详细地重申了这一点:

This NuGet package provides a fast and robust way to invoke Node.js code from a .NET application (typically ASP.NET Core web apps).此 NuGet 包提供了一种从 .NET 应用程序(通常是 ASP.NET Core Web 应用程序)调用 Node.js 代码的快速而可靠的方法。 You can use this whenever you want to use Node/NPM-supplied functionality at runtime in ASP.NET.只要您想在 ASP.NET 中在运行时使用 Node/NPM 提供的功能,就可以使用它。

That's entirely independent of the ability to eg precompile, minimize, or move JavaScript files at build time .这完全独立于在构建时预编译、最小化或移动 JavaScript 文件的能力。

Build Time Tasks构建时间任务

You don't—and won't—need NodeServices in order to download npm package dependencies via:你不需要——也不会——需要 NodeServices 来通过以下方式下载npm包依赖:

  • The command prompt on your local workstation,本地工作站上的命令提示符,
  • From Visual Studio 's built-in integration, orVisual Studio的内置集成,或
  • From a task on your build server (eg, the npm task on Azure Pipelines ).来自构建服务器上的任务(例如, Azure Pipelines上的npm任务)。

Likewise, to precompile, minimize, and move client-side dependencies from their source directory to their distribution directory, you can (continue to?) use tools like Gulp.js , Grunt , or WebPack , which are each build systems that operate on top of Node.js.同样,要预编译、最小化客户端依赖项并将其从源目录移动到分发目录,您可以(继续?)使用Gulp.jsGruntWebPack等工具,它们都是在顶层运行的构建系统的 Node.js。

Important: The critical distinction here is that you don't need to call these tools at runtime from your .NET application.重要提示:此处的关键区别在于您不需要在运行时从 .NET 应用程序调用这些工具。 You are incorporating the output from your Node.js build tools into your .NET application, but you aren't executing Node.js code as part of your .NET application.您正在将 Node.js 构建工具的输出合并到您的 .NET 应用程序中,但您没有将 Node.js 代码作为.NET 应用程序的一部分执行

The one exception to this is if you're using NodeService to dynamically perform these build tasks at runtime.一个例外是,如果您使用 NodeService 在运行时动态执行这些构建任务。 For example, if your application is configured with the UseWebpackDevMiddleware() then that will no longer work.例如,如果您的应用程序配置了UseWebpackDevMiddleware()那么它将不再起作用。 In that case, you would need to migrate to a build process that occurs prior to (or during) deployment.在这种情况下,您需要迁移到在部署之前(或期间)发生的构建过程。

Webpack网络包

If you are using UseWebpackDevMiddleware() , then I'd recommend looking into configuring Webpack locally.如果您正在使用UseWebpackDevMiddleware()那么我建议考虑配置的WebPack本地。 That should be a pretty seamless transition.这应该是一个非常无缝的过渡。 You can run it manually via the Webpack CLI , use a Visual Studio extension , or potentially even integrate it into your build process.您可以通过 Webpack CLI 手动运行它使用 Visual Studio 扩展,甚至可能将其集成到您的构建过程中。 Personally, I run it manually on my development server, then integrate it into my Azure Pipelines build process .就我个人而言,我在我的开发服务器上手动运行它,然后将它集成到我的 Azure Pipelines 构建过程中

Alternatively, if you really want to maintain “just in time” build support for Webpack files, you can look into using the Webpack dev server in conjunction with ASP.NET Core, as discussed in Best way to integrate webpack builds with ASP.NET Core 3.0?或者,如果您真的想保持对 Webpack 文件的“及时”构建支持,您可以考虑将Webpack 开发服务器与 ASP.NET Core 结合使用,如将 webpack 构建与 ASP.NET Core 集成的最佳方式中所述3.0? . .

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

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