Node.js-based automated building tools Grunt.js

Source: Internet
Author: User

Maven in the Java World provides powerful package dependency management and build lifecycle management. In the JavaScript world, with the popularity of Node.js, JavaScript native building tools have become possible.

Grunt.js is a node.js automated task Manager. Grunt.js combined with NPM's package dependency management, it's perfectly comparable to Maven. Grunt.js is natural for building front-end applications-not just JavaScript projects, but also application building in other languages. More and more JavaScript projects are already using grunt, with the largest users including the famous jquery project.

Grunt's ecosystem is growing rapidly and there are hundreds of plug-ins available to choose from on NPM. At the same time, anyone can easily release their plug-ins to NPM for other people to use.

Grunt does not emphasize the lifecycle of the build as Maven does, the sequence of tasks can be configured randomly. The grunt itself is only an actuator, and a large number of features exist in NPM-managed plug-ins. In particular, the core plug-ins that start with grunt-contrib-cover most of the core functions, such as handlebars,jade,less, Compass,jshint,jasmine,clean,concat,minify,copy, Uglify,watch,minify, uglify and so on.

By providing a common interface for code Instrumentation (Lint), merging, compression, testing, and versioning, Grunt makes the entry threshold much less critical.

Installation

The latest version of Grunt is the v0.4 version. Compared to the v0.3 version, grunt itself is no longer installed as a whole global module, but is divided into three parts: Grunt,grunt-cli,grunt-init.

GRUNT-CLI is used for command line startup grunt and must be installed as a global module:

NPM install-g GRUNT-CLI

Grunt-init is optional, used to create a new Grunt project template as a command line, and must be installed as a global module:

NPM install-g Grunt-init

The grunt core exists as a dependent package for a project or plug-in and avoids conflicts caused by different versions of grunt that are dependent on different plug-ins.

New Project Engineering

Executing grunt init on the command line

Whether the new project is an application or a grunt plug-in, the project generated by Grunt is a standard NPM module.

It is important to have two files in the new project directory:

Package.json:NPM's release profile, which contains the package dependency information and metadata for project engineering. The reader who is familiar with node.js should be no stranger.

Gruntfile.js:Grunt configuration files, for configuring or defining grunt tasks, and loading grunt plug-ins.

Grunt Task Configuration

All of the task configurations exist in Gruntfile.js. As a JavaScript source file, its configuration information is stored as a JSON object and can be dynamically generated using the JS function, which is simpler and more flexible than MAVEN's XML configuration. A typical gruntfile is as follows:

Module.exports = function (grunt) {
    
    
  //project configuration information.
    
  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 "uglify" plug-in ...
    
  Grunt.loadnpmtasks (' grunt-contrib-uglify ');
    
    
  Registers a default task.
    
  Grunt.registertask (' Default ', [' uglify ']);
    
    

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.