简体   繁体   中英

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. 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 . 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. Also, when I tried to create a new template with core 2.1 project, it wasn't there. So I'm quite confused as to how to get my bundleconfig.json to work.

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. 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

<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. The package injects MSBuild Targets which run at build and clean time. The bundleconfig.json file is analyzed by the build process to produce the output files based on the defined configuration.

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:

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

and in console from project directory dotnet bundle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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