Quick start for grunt under Windows

Source: Internet
Author: User

1. Understanding Grunt

What is grunt: He is a set of front-end automation tools and is a NODEJS-based command-line tool. (Grunt and grunt plugins are installed and managed via NPM, so install Nodejs first).

What can grunt do: 1. compressing files 2. merging files 3. Simple grammar checking 4.5.less compilation of monitoring file changes

Grunt Advantages: 1. Reduce labor and streamline work 2. Free, no piracy 3. Plug-ins, and can write their own plug-ins

2.grunt Installation and use

First step: Add Package.json and Gruntfile.js files

Package.json How to add a configuration item:

A: Add manually (the basic grunt plugin is already written in the following section)

{
"Name": "Grunt_test",
"Version": "0.0.1",
"description": "Grunt Test",
"Main": "Index.js",
"Scripts": {
"Test": "Echo \" Error:no test specified\ "&& exit 1"
},
"Devdependencies": {
"Grunt": "^0.4.5",
"Grunt-contrib-jshint": "*",
"Grunt-contrib-uglify": "*",
"Grunt-contrib-cssmin": "*",
"Grunt-contrib-imagemin": "*",
"Grunt-contrib-watch": "*",
"Grunt-contrib-concat": "*"
},
"keywords": [
"Grunt"
],
"Author": "Caihe",
"License": "ISC"
}

Description

Grunt-contrib-jshint: Checking JavaScript syntax

Grunt-contrib-uglify: Compressing and merging JS files

Grunt-contrib-cssmin: Compressing and merging CSS files

Grunt-contrib-imagemin: Image Compression Module

Grunt-contrib-watch: Monitor file changes, make corresponding actions

Grunt-contrib-concat: Merging Files

The cmd window then enters the root directory of the current project Input command line: NPM Install

B: Command line implementation: NPM install Grunt-contrib-jshint--save-dev, etc.

Gruntfile.js File configuration:

The role of Gruntfile.js 1. Read Package.json information 2. Plug-in loading, registering tasks, running tasks

Module.exports = function (grunt) {
Project configuration
Grunt.initconfig ({
Reading configuration Items
Pkg:grunt.file.readJSON ("Package.json"),
Specific tasks
uglify:{
options:{
Add Comment
Banner: "/* This file <%= pkg.name%><%= pkg.version%> \n*/"
},
build:{
The path of the compressed file
SRC: "Src/jquery-1.9.1.js",
The file path after being compressed
Dest: "Dest/jquery-1.9.1.min.js"
}
},
Merge
concat:{
options:{
Add Comment
Banner: "/* This file <%= pkg.name%><%= pkg.version%> merged js\n*/"
},
build:{
The path of the compressed file
SRC: "Src/*.js",
The file path after being compressed
Dest: "Dest/concat.js"
}
},
Jshint: {
Files: [' Gruntfile.js '],
Options: {
}
},
Watch: {
Files: [' src/*.html ', ' src/*.css '],
tasks: [' Jshint ']
}
});
Loading plugins
Grunt.loadnpmtasks ("grunt-contrib-uglify");
Grunt.loadnpmtasks (' Grunt-contrib-jshint ');
Grunt.loadnpmtasks (' Grunt-contrib-watch ');
Grunt.loadnpmtasks (' Grunt-contrib-concat ');
Default execution Tasks
Grunt.registertask ("Default", ["Uglify", "concat", "Jshint", "Watch"]);
};

Then the cmd window enters the root directory of the current project Input command line: Grunt

Executable Grunt Task Grunt Watch can monitor file changes

Quick start for grunt under Windows

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.