asp.net-core – Bundling and Minification – The dotnet bundle Command

The ASP.NET Core RTM release introduced BundlerMinifier.Core, a new Bundling and Minification tool that can be easily integrated into existing ASP.NET Core applications and doesn’t require any external extensions or script files.

Using BundlerMinifier.Core

To use this tool, simply add a reference to BundlerMinifier.Core within the tools section of your existing project.json file as seen below :

"tools": {
  "BundlerMinifier.Core": "2.0.238",
  "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
  "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
}

Configuring Your Bundles

After adding the tool, you’ll need to add a bundleconfig.json file in your project that will be used to configure the files that you wish to include within your bundles. A minimal configuration can be seen below :

[
  {
    "outputFileName": "wwwroot/css/site.min.css",
    "inputFiles": [
      "wwwroot/css/site.css"
    ]
  },
  {
    "outputFileName": "wwwroot/js/site.min.js",
    "inputFiles": [
      "wwwroot/js/site.js"
    ],
    "minify": {
      "enabled": true,
      "renameLocals": true
    },
    "sourceMap": false
  },
  {
    "outputFileName": "wwwroot/js/semantic.validation.min.js",
    "inputFiles": [
      "wwwroot/js/semantic.validation.js"
    ],
    "minify": {
      "enabled": true,
      "renameLocals": true
    }
  }
]

Creating / Updating Bundles

After your bundles have been configured, you can bundle and minify your existing files via the following command :

dotnet bundle

Automated Bundling

The Bundling and Minification process can be automated as part of the build process by adding the dotnet bundle command in the precompile section of your existing project.json file :

"scripts": {
  "precompile": [
    "dotnet bundle"
  ]
}

Available Commands

You can see a list of all of the available commands and their descriptions below :

  • dotnet bundle – Executes the bundle command using the bundleconfig.json file to bundle and minify your specified files.
  • dotnet bundle clean – Clears all of the existing output files from disk.
  • dotnet bundle watch – Creates a watchers that will automatically run dotnet bundle whenever an existing input file from the bundleconfig.json configuration to bundle your files.
  • dotnet bundle help – Displays all available help options and instructions for using the command-line interface.

if you want to reproduce, please indicate the source:
asp.net-core – Bundling and Minification – The dotnet bundle Command - CodeDay