简体   繁体   English

在 .NET 核心应用程序中导入 Javascript 包的最佳方式

[英]Best way to import Javascript packages in .NET core app

I'm new to the .NET Core framework and currently setting up a dashboard that uses Razor pages and some JavaScript interactions.我是 .NET Core 框架的新手,目前正在设置一个使用 Razor 页面和一些 JavaScript 交互的仪表板。 For the few example projects I've created none of them came with a package manager from the get go, which imho is needed to keep the application maintainable.对于我创建的几个示例项目,从一开始它们都没有附带包管理器,恕我直言,这是保持应用程序可维护性所必需的。 Coming from Node.js an Ruby on Rails background my default solution is NPM (or Yarn if you want).来自 Node.js 和 Ruby on Rails 背景,我的默认解决方案是 NPM(如果需要,也可以使用 Yarn)。

I noticed however that Microsoft excluded Node from their base Docker images for .Net about 2 years ago (see here: https://github.com/aspnet/Announcements/issues/298 ).然而,我注意到微软大约在 2 年前从他们的 .Net 基本 Docker 镜像中排除了 Node(参见这里: https : //github.com/aspnet/Announcements/issues/298 )。

This led me to questioning my solution to use NPM and I went looking for alternatives.这让我质疑我使用 NPM 的解决方案,我开始寻找替代方案。 I found that you can install Javascript libraries by wrapping them in Nuget packages and installing them via Nuget.我发现您可以通过将 Javascript 库包装在 Nuget 包中并通过 Nuget 安装它们来安装它们。 But I'm hesitant to go this way as I've seen this pattern causing issues in Ruby on Rails.但是我很犹豫要不要这样做,因为我已经看到这种模式会导致 Ruby on Rails 出现问题。 It adds an extra layer of maintenance and often the Ruby Gems where no longer maintained, meaning you're unable to move to the newest versions.它增加了额外的维护层,并且通常不再维护 Ruby Gems,这意味着您无法迁移到最新版本。

Then we have Libman, which is integrated into Visual Studio ( https://docs.microsoft.com/en-us/aspnet/core/client-side/libman/libman-vs?view=aspnetcore-2.2 ) and draws packages from https://cdnjs.com , which I believe contains less packages (but according to Microsoft has everything you need).然后我们有 Libman,它被集成到 Visual Studio ( https://docs.microsoft.com/en-us/aspnet/core/client-side/libman/libman-vs?view=aspnetcore-2.2 ) 并从https://cdnjs.com ,我相信它包含较少的包(但据微软称拥有您需要的一切)。 But their own blog ( https://devblogs.microsoft.com/aspnet/library-manager-client-side-content-manager-for-web-apps/ ) says:但是他们自己的博客( https://devblogs.microsoft.com/aspnet/library-manager-client-side-content-manager-for-web-apps/ )说:

LibMan is not a package management system. LibMan 不是包管理系统。 If you're happily using npm/yarn/(or something else), we encourage you to continue doing so.如果您喜欢使用 npm/yarn/(或其他东西),我们鼓励您继续这样做。 LibMan was not developed as a replacement for these tools. LibMan 并不是为了替代这些工具而开发的。 For example, the SPA templates (ie Angular and React) we ship for ASP.NET Core depend on npm and WebPack, and we have no plans to change that.例如,我们为 ASP.NET Core 提供的 SPA 模板(即 Angular 和 React)依赖于 npm 和 WebPack,我们没有计划改变这一点。

Then we're left with Bower, which is no longer an option, from https://bower.io/ :然后我们剩下 Bower,它不再是一个选项,来自https://bower.io/

...psst! ……嘘! While Bower is maintained, we recommend using Yarn and Webpack or Parcel for front-end projects read how to migrate!在维护 Bower 的同时,我们建议前端项目使用 Yarn 和 Webpack 或 Parcel 阅读如何迁移!

So I wonder, for a monolith app, is NPM or Yarn still the right choice and if so, why did Microsoft decide to exclude Node from their base images?所以我想知道,对于单体应用程序,NPM 或 Yarn 仍然是正确的选择,如果是,为什么微软决定从他们的基础镜像中排除 Node?

您提到LibMan工作正常,但您需要将其配置为从unpkg下载软件包,这是 NPM 的 CDN,应该包含您可以在 npm 上找到的所有内容。

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

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