【问题标题】:How to minify files in asp.net core?如何缩小 asp.net core 中的文件?
【发布时间】:2016-10-24 21:06:50
【问题描述】:

asp.net core 的文档展示了如何使用 grunt 或 gulp 处理 bundling and minification cssjs 文件。 但是,当我使用 vs 2015 创建项目时,它会将bundleconfig.json 文件添加到项目中。我想缩小 wwwroot/js 文件夹中的所有 js 文件。所以我更新了 bundleconfig.json 中的现有行以使用通配符 *

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

但是当我发布项目时出现错误

处理 wwwroot/js/*.min.js 路径中的非法字符。范围 名称:路径

【问题讨论】:

    标签: asp.net-core asp.net-core-mvc minify coreclr


    【解决方案1】:

    截至 2021 年 3 月 - Microsoft-recommended approach 将利用 Mads Kristensen(Visual Studio 团队的 PM)创建的 WebOptimizer Core nuget 包 - https://www.nuget.org/packages/LigerShark.WebOptimizer.Core/

    它不使用bundleConfig.json 文件,并且支持通过 Startup.ConfigureServices() 进行配置,但是您当然可以编写一个小的静态帮助程序来返回一个操作并通过 JSON.NET 或解析出您的 bundleConfig.json 文件System.Text.Json,如果你有大量的包并且已经花时间以正确的方式获取你的 json 文件。然后在 Startup 中,您可以为调用 services.AddWebOptimizer(action) 提供辅助操作。

    WebOptimizer 具有标签助手,可帮助在 CSHTML 文件中添加 <link><script> 标签。

    您可以从其 GitHub 存储库中查看 README 的详细信息: https://github.com/ligershark/WebOptimizer

    【讨论】:

      【解决方案2】:

      我认为outputFileName 中不能有通配符,所以在这里使用绝对路径。要创建多个捆绑包,请在数组中创建多个条目。

      [
        {
          "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
        }
      ]
      

      上面这个来自默认bundleconfig.json

      附注:

      *.min.js 也是*.js 顺便说一句。因此,如果您不删除前一个,它将在每个捆绑中递归添加,所以要小心。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      • 1970-01-01
      • 2021-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-11
      相关资源
      最近更新 更多