ASP.NET vNext總結:Grunt

來源:互聯網
上載者:User

標籤:

1.概述

      Grunt同樣是基於Javascript(使用NPM)技術的task runners。task runners是一個應用程式,或者說它是一個任務工具。本章繼續在前文介紹的項目基礎上來示範!

2.編譯less

2.1建立less檔案

      先在"vNext.WebStarterTemplate"項目根目錄下建立Lesses檔案夾。在其下建立兩個less檔案:

     

      color.less代碼:

//顏色樣式定義@bg-color-red: #ff0000;

      site.less代碼: 

//嵌入color.less樣式@import "color.less";body {    background-color: @bg-color-red;}.test {    width: 100px;}

 2.2安裝grunt-less

      開啟package.json:

     

      然後安裝:

     

      成功後,NPM目錄會出現:

     

2.3修改gruntfile.js

module.exports = function (grunt) {    grunt.initConfig({        bower: {            //bower下載和安裝預設配置            install: {                options: {                    targetDir: "wwwroot/lib",                    layout: "byComponent",                    cleanTargetDir: false                }            }        },        less: {            //開發版(無壓縮)            dev: {                files: {                    "wwwroot/css/site.css": "Lesses/site.less"                },                options: {                    sourceMap: true,                    //paths: ["Lesses"]//或指定路徑                }            },            //生產版(壓縮)            prod: {                files: {                    "wwwroot/css/site.min.css": "Lesses/site.less"                },                options: {                    cleancss: true,                }            }        }    });    // This command registers the default task which will install bower packages into wwwroot/lib    grunt.registerTask("default", ["bower:install"]);    // The following line loads the grunt plugins.    // This line needs to be at the end of this this file.    grunt.loadNpmTasks("grunt-bower-task");    grunt.loadNpmTasks("grunt-contrib-less");};

       註:關於最新版本的grunt-contrib-less配置,可以參見:https://www.npmjs.com/package/grunt-contrib-less ,其壓縮功能需要另安裝外掛程式了,我一時沒“研究”明白,沒測試成功,可能vs暫時整合問題吧。

2.4執行task

      右鍵“gruntfile.js”,開啟Explorer後,再分別右鍵dev和prod後執行Run:

     

      可以看到產生兩個css檔案大小區別:

     

      這時css目錄:

     

      開啟site.css其代碼已經修改為(和前面定義的less樣式一致,但沒有縮排壓縮):

body {  background-color: #ff0000;}.test {  width: 100px;}/*# sourceMappingURL=data:application/json;
base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIkxlc3Nlcy9zaXRlLmxlc3MiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBSUE7RUFDSSx5QkFBQTs7QUFHSjtFQUNJLFlBQUEifQ== */

      再開啟site.min.css(去掉空格,注釋,換行)

body{background-color:red}.test{width:100px}

 

未完待續(還有watch等功能介紹),出去討飯……

 

ASP.NET vNext總結:Grunt

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.