GruntJs 簡介
Grunt 是一個 JavaScript 任務執行器,用於重複性任務的自動化,如縮小,編譯,單元測試,linting 等。
要開始使用,你需要全域性安裝 Grunt 的命令列介面(CLI)。
npm install -g grunt-cli
準備一個新的 Grunt 專案: 典型的設定將涉及向專案新增兩個檔案:package.json 和 Gruntfile。
package.json:npm 使用此檔案儲存作為 npm 模組釋出的專案的後設資料。你將列出 grunt 和專案所需的 Grunt 外掛作為此檔案中的 devDependencies。
Gruntfile:此檔名為 Gruntfile.js,用於配置或定義任務並載入 Grunt 外掛。
Example package.json:
{
"name": "my-project-name",
"version": "0.1.0",
"devDependencies": {
"grunt": "~0.4.5",
"grunt-contrib-jshint": "~0.10.0",
"grunt-contrib-nodeunit": "~0.4.1",
"grunt-contrib-uglify": "~0.5.0"
}
}
示例 gruntfile:
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
uglify: {
options: {
banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
},
build: {
src: 'src/<%= pkg.name %>.js',
dest: 'build/<%= pkg.name %>.min.js'
}
}
});
// Load the plugin that provides the "uglify" task.
grunt.loadNpmTasks('grunt-contrib-uglify');
// Default task(s).
grunt.registerTask('default', ['uglify']);
};