AngularJs——grunt神器的使用

來源:互聯網
上載者:User

標籤:style   blog   io   ar   color   os   使用   sp   on   

 前面我們已經知道了如何安裝grunt,本章節給各位道友介紹如何使用 grunt 的外掛程式,grunt是重點在於如何配置使用 Gruntfile.js,官網上也有很多範例。

  1,封裝函數
module.exports=function(grunt){}

   我們的代碼要包括在這個方法裡。

 

 2,任務配置
// 封裝函數module.exports = function(grunt) {  // 任務配置  grunt.initConfig({  })    }

 在這裡我們需要初始化參數,這是grunt的最主要的部分

 

 3,匯入包檔案
// 封裝函數module.exports = function(grunt) {  // 任務配置  grunt.initConfig({     pkg:grunt.file.readJSON(‘package.json‘)  })    }

  使用 grunt.file.readJSON(‘package.json‘) 可以把外部的package.json檔案引入到 gruntfile.js,並產生一個JSON格式的對象可以訪問 package.json裡的資料,我們如果使用 grunt.file.readYAML() 產生 YAML格式的對象。

 

 4,配置任務
// 任務配置grunt.initConfig({  pkg: grunt.file.readJSON(‘package.json‘), concat: { // concat 任務的配置 },
uglify: { // uglify 任務的配置 },

my_property: ‘whatever‘, my_src_files: [‘foo/*.js‘, ‘bar/*.js‘]
// 其他與任務無關的屬性
 })

  任務配置定義在 Gruntfile 和 grunt.intiConfig()方法中,配置主要是以任務命名的屬性,也可以包含其他屬性,但是不能與任務所需要的屬性重合

 

5,任務和目標

    當任務啟動並執行時候(下面介紹的載入任務之後),Grunt會從同名的屬性中尋找配置(即從initConfig)。多重任務可以有多個配置,每個任務使用任意命名的目標定義。

grunt.initConfig({  concat: {    foo: {      // concat 任務的目標 "foo"    },    bar: {      // concat 任務的目標 "bar"    },  },  uglify: {    bar: {      // uglify 任務的目標 "bar"    },  },});

  運行 grunt concat:foo 或 grunt concat:bar 只會處理指定的目標配置,而運行 grunt concat都會運行

6,任務載入

  一些任務,比如合并,壓縮和校正等都可以作為 grunt外掛程式。只要外掛程式被指定 package.json中,並且通過 npm install 安裝好,就可以在 gruntfile中載入:

  

  

AngularJs——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.