How to minify files in asp.net core?
Asked Answered
J

2

4

Documentation for asp.net core shows how to do bundling and minification css and js files using grunt or gulp. However when i create a project using vs 2015 it adds bundleconfig.json file into project. I want to minify all the js files inside wwwroot/js folder. So i updated the existing lines inside bundleconfig.json to use wildcard character *

{
    "outputFileName": "wwwroot/js/*.min.js",
    "inputFiles": [
      "wwwroot/js/*.js"
    ],
    // Optionally specify minification options
    "minify": {
      "enabled": true,
      "renameLocals": true
    },
    // Optinally generate .map file
    "sourceMap": false
  }

however when i publish the project i get error

Processing wwwroot/js/*.min.js Illegal characters in path. Parameter name: path

Judiciary answered 24/10, 2016 at 21:6 Comment(0)
L
1

I think you can't have wildcards in outputFileName, so use an absolute path here. To create multiple bundles create multiple entries in the array.

[
  {
    "outputFileName": "wwwroot/css/site.min.css",
    // An array of relative input file paths. Globbing patterns supported
    "inputFiles": [
      "wwwroot/css/site.css"
    ]
  },
  {
    "outputFileName": "wwwroot/js/site.min.js",
    "inputFiles": [
      "wwwroot/js/site.js"
    ],
    // Optionally specify minification options
    "minify": {
      "enabled": true,
      "renameLocals": true
    },
    // Optinally generate .map file
    "sourceMap": false
  }
]

This one above is from the default bundleconfig.json.

On a side note:

*.min.js is also a *.js btw. So if you don't delete the previous one it will be added recursively with each bundling, so be careful.

Links answered 24/10, 2016 at 22:6 Comment(1)
by reading msdn, the bundleconfig.json doesnt work for .net core quote "ASP.NET Core doesn't provide a native bundling and minification solution." source learn.microsoft.com/en-us/aspnet/core/client-side/…Hatter
R
8

As of March 2021 - the Microsoft-recommended approach is to leverage the WebOptimizer Core nuget package created by Mads Kristensen (PM on Visual Studio team) - https://www.nuget.org/packages/LigerShark.WebOptimizer.Core/

It does not use the bundleConfig.json files, and favors configuration via the Startup.ConfigureServices(), but you can certainly write a little static helper that returns an action and parse out your bundleConfig.json file via JSON.NET or System.Text.Json, if you've got a ton of bundles and have already spent the time to get your json file just the right way. Then in the Startup, you can provide the helper action to the call to services.AddWebOptimizer(action).

WebOptimizer has tag helpers that help with adding the <link> and <script> tags in the CSHTML files.

You can see details on the README from its GitHub repo: https://github.com/ligershark/WebOptimizer

Reeta answered 16/3, 2021 at 16:52 Comment(0)
L
1

I think you can't have wildcards in outputFileName, so use an absolute path here. To create multiple bundles create multiple entries in the array.

[
  {
    "outputFileName": "wwwroot/css/site.min.css",
    // An array of relative input file paths. Globbing patterns supported
    "inputFiles": [
      "wwwroot/css/site.css"
    ]
  },
  {
    "outputFileName": "wwwroot/js/site.min.js",
    "inputFiles": [
      "wwwroot/js/site.js"
    ],
    // Optionally specify minification options
    "minify": {
      "enabled": true,
      "renameLocals": true
    },
    // Optinally generate .map file
    "sourceMap": false
  }
]

This one above is from the default bundleconfig.json.

On a side note:

*.min.js is also a *.js btw. So if you don't delete the previous one it will be added recursively with each bundling, so be careful.

Links answered 24/10, 2016 at 22:6 Comment(1)
by reading msdn, the bundleconfig.json doesnt work for .net core quote "ASP.NET Core doesn't provide a native bundling and minification solution." source learn.microsoft.com/en-us/aspnet/core/client-side/…Hatter

© 2022 - 2024 — McMap. All rights reserved.