如何在Visual Studio 2017中将npm软件包与ASP.NET CORE 2一起使用? [英] How to use npm packages with ASP.NET CORE 2 in Visual Studio 2017?

查看:174
本文介绍了如何在Visual Studio 2017中将npm软件包与ASP.NET CORE 2一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Studio 2017的ASP.NET Core 2项目中添加了一些npm程序包.现在,我想使用这些程序包中的css和js文件,但是VS看不到它们,因为node_modules文件夹位于wwwroot之外.此处的常见做法是如何使Visual Studio与node_modules一起使用?

I added some npm packages to my ASP.NET Core 2 project in Visual Studio 2017. Now I want to use css and js files from these packages, but VS doesn't see them because node_modules folder is outside wwwroot. What is the common practice here, how to make Visual Studio working with node_modules?

推荐答案

常见的做法是捆绑您的Web资产,并且仅将编译后的捆绑软件放入您的wwwroot文件夹中.

The common practice is to bundle your web assets, and put only the compiled bundle into your wwwroot folder.

自Visual Studio 2015以来,我们有来自NPM World的多个Taskrunner:

Since Visual Studio 2015 we have multiple Taskrunners from the NPM World:

  • Gulp
  • 矮人

与他们一起,您可以编写一个脚本,该脚本会自动捆绑您的Web资产.该脚本具有对NPM基础结构的完全访问权限.

With them you could write a script, which automatically bundles your web assets. This script has full access to the NPM infrastructure.

有一些工具可以使超级简单:

There are some tools out there which make this super easy:

  • gulp-uglify
  • gulp-cssmin

更多信息: https://docs.microsoft.com/zh-我们/aspnet/core/client-side/using-gulp

也就是说,它也是可能包括您的npm文件夹:

That said, it is also possible to include your npm folder:

public void Configure(IApplicationBuilder app)
{
    app.UseStaticFiles(); // For the wwwroot folder

    app.UseStaticFiles(new StaticFileOptions
    {
        FileProvider = new PhysicalFileProvider(
            Path.Combine(Directory.GetCurrentDirectory(), "node_modules")),
        RequestPath = "/node_modules"
    });
}

但这是错误的方法.

这篇关于如何在Visual Studio 2017中将npm软件包与ASP.NET CORE 2一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆