How to use Bundle&Minifier and bundleconfig.json in ASP.NET Core


2017 年9月27日更新
在.csproj 文件中的配置

<Project Sdk="Microsoft.NET.Sdk.Web">
//...
<ItemGroup>
    <PackageReference Include="BundlerMinifier.Core" Version="2.4.401" /> 
  </ItemGroup>

  <ItemGroup>
    <DotNetCliToolReference Include="BundlerMinifier.Core" Version="2.4.337" />
  </ItemGroup>

  <Target Name="PrepublishScript" BeforeTargets="PrepareForPublish">
    <Exec Command="dotnet bundle" />
  </Target>
//...
</Project>

引言

我們在ASP.NET MVC 中經常會用到 bundleConfig.cs 文件來進行我們 css 和 js 的綁定, 那么在ASP.NET Core 中我們應該如何使用呢?

步驟一
在 Visual Studio 2015 工具->擴展和更新 中下載插件

然后重啟 Visual Studio 2015

步驟二

在你的ASP.NET Core 項目的 Web 層中 的 project.json 文件中添加如下配置:

"tools": {
    "BundlerMinifier.Core": "2.0.238"
  },
  "scripts": {
    "precompile": [ "dotnet bundle" ]
  }

在上面的

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

則會在編譯時就自動再重新生成一遍,如果不想這樣的話,可以去除。

步驟三

在 程序包管理器控制台 上輸入 dotnet bundle 程序就會自動找到 bundleconfig.json文件 進行編譯。

差不多就是這樣的啦。

步驟四
如何配置 bundleconfig.json 文件 :

[
  {
    "outputFileName": "output/bundle.css",
    "inputFiles": [
      "css/lib/**/*.css", // globbing patterns are supported
      "css/input/site.css"
    ],
    "minify": {
        "enabled": true,
        "commentMode": "all"
    }
  },
  {
    "outputFileName": "output/all.js",
    "inputFiles": [
      "js/*.js",
      "!js/ignore.js" // start with a ! to exclude files
    ]
  },
  {
    "outputFileName": "output/app.js",
    "inputFiles": [
      "input/main.js",
      "input/core/*.js" // all .js files in input/core/
    ]
  }
]

參考文獻

http://dotnetthoughts.net/bundling-and-minification-in-aspnet-core/

https://github.com/madskristensen/BundlerMinifier/wiki


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM