简体   繁体   English

与MVC Core捆绑和缩小

[英]Bundling & Minification with MVC Core

I have found plenty of articles that pointed out to me that BundleConfig.cs is no longer a thing with MVC. 我发现很多文章向我指出BundleConfig.cs不再是MVC的东西。 Instead I am suppose to use third party tools to achieve this. 相反,我想使用第三方工具来实现这一目标。 At least, this is my understanding. 至少,这是我的理解。 I have spent a lot of time researching and trying to understand, but can't find anywhere any clear instructions on how to achieve this with bundlingconfig.json . 我花了很多时间研究并试图理解,但无法找到任何关于如何使用bundlingconfig.json实现此目的的明确说明。 Some of the articles by microsoft, such as this one https://docs.microsoft.com/en-us/aspnet/core/client-side/bundling-and-minification?view=aspnetcore-2.1&tabs=visual-studio%2Caspnetcore2x talk about how the default template uses it, but it doesn't tell me how exactly they achieve it. 微软的一些文章,例如这篇文章https://docs.microsoft.com/en-us/aspnet/core/client-side/bundling-and-minification?view=aspnetcore-2.1&tabs=visual-studio% 2Caspnetcore2x谈论默认模板如何使用它,但它没有告诉我他们是如何实现它的。 Also, when I tried to create a new template with core 2.1 project, it wasn't there. 此外,当我尝试使用核心2.1项目创建新模板时,它不存在。 So I'm quite confused as to how to get my bundleconfig.json to work. 所以我很困惑如何让我的bundleconfig.json工作。

Now, I'm a fan of starting from scratch and building things up so I get a good understanding of how they work so I can fix them if things go wrong in future. 现在,我是一个从头开始构建的粉丝所以我很好地理解它们是如何工作的,所以如果将来出现问题我可以修复它们。 As such, I created a brand new project with nothing in it and added my controllers, views, everything I need to get a basic website. 因此,我创建了一个没有任何内容的全新项目,并添加了我的控制器,视图,以及获得基本网站所需的一切。 However, simply just by adding the config file on its own, it does nothing. 但是,只需单独添加配置文件,它就什么都不做。 I was hoping it was part of the mvc framework and it would pick it up and know what to do with it. 我希望它是mvc框架的一部分,它会接受它,并知道如何处理它。 But I guess that is not the case and I can't find anywhere instructions on what I need to add in addition to the config file for this to work. 但我想情况并非如此,除了配置文件之外,我无法找到关于我需要添加什么的说明。

Could anyone point me in the right direction? 有人能指出我正确的方向吗?

[
  {
    "outputFileName": "wwwroot/css/Test.css",
    "inputFiles": [
      "wwwroot/css/Global.css"
    ],
    "minify": {
      "enabled": true,
      "renameLocals": true
    }
  }
]

and then in my cshtml page I added 然后在我的cshtml页面中我添加了

<link rel="stylesheet" href="~/css/Test.css" />

From the section Build-time execution of bundling and minification in your link: 从链接中的捆绑和缩小的构建时执行部分:

The BuildBundlerMinifier NuGet package enables the execution of bundling and minification at build time. BuildBundlerMinifier NuGet包允许在构建时执行捆绑和缩小。 The package injects MSBuild Targets which run at build and clean time. 该软件包注入MSBuild Targets,它在构建和清理时运行。 The bundleconfig.json file is analyzed by the build process to produce the output files based on the defined configuration. 构建过程分析bundleconfig.json文件,以根据定义的配置生成输出文件。

All you need to do then is to add package to main project 您需要做的就是将包添加到主项目中

<PackageReference Include="BuildBundlerMinifier" Version="2.8.391" />

Or if you want to bundle and minify from console, add following nuget: 或者,如果要从控制台进行捆绑和缩小,请添加以下nuget:

<DotNetCliToolReference Include="BundlerMinifier.Core" Version="2.8.391" />

and in console from project directory dotnet bundle 并在项目目录dotnet bundle控制台中

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

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