MAC终端安装grunt--javascript世界得构建工具

      祝贺我成为前端啦!~~从年前得小测试到今年得前端,成功转型!我真是一个进步得好青年,好少女!

      这两天出去受虐,面了两家前端,表现非常不好,还是回到我现在得公司好好沉淀技术,做前端,要经常性得整理总结,才是王道嘛!我要在现在得公司认真工作每一天,把自己得技术水平多多提高,明年一定出去有底气得挑工作!有了能力才能撑得起那么多得报酬,脊背才会直硬!、

      废话不多说,如何mac下安装grunt来也。。。

       Q:为何要用构建工具?

       一句话:自动化。对于需要反复重复的任务,例如压缩(minification)、编译、单元测试、linting等,自动化工具可以减轻你的劳动,简化你的工作。当你在 Gruntfile 文件正确配置好了任务,任务运行器就会自动帮你或你的小组完成大部分无聊的工作。

       是一套前端自动化工具,一个基于nodeJs的命令行工具,一般用于:

  •  压缩文件
  •  合并文件
  •  简单语法检查

      安装homebrew任务管理器

      下载homebrew很简单,只需要用下面得ruby语句,在官网中介绍了homebrew得用法。

      ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

       url: http://brew.sh/      是Mac OSX上的软件包管理工具,能在Mac中方便的安装软件或者卸载软件, 只需要一个命令, 非常方便。

          下面若有安装不成功,可前面加sudo ,以管理员身份安装。

      安装NodeJS

      运行grunt得首要条件是你的系统上运行安装了NodeJs和NPM,简单了解下安装NodeJs和NPM。

       可以到官网下载NodeJs : https://nodejs.org/en/

       也可以在终端以命令行形式安装: 

$ brew install node

      安装NPM

     终端执行下面的命令安装NPM

 

curl http://npmjs.org/install.sh | sh

 

      检查NodeJs和NPM有没有安装完成

$ node -v

  v0.12.6

$ npm -v

  2.11.2

       安装grunt

      若以前安装过grunt,要先卸载以前版本得grunt,再升级新版本。

$ npm uninstall -g grunt

 

       为了方便使用Grunt,要全局范围安装grunt得命令行接口CLI。

$ npm install -g grunt-cli
成功后:

grunt-cli@0.1.13 /usr/local/lib/node_modules/grunt-cli

├── resolve@0.3.1

├── nopt@1.0.10 (abbrev@1.0.7)

└── findup-sync@0.1.3 (lodash@2.4.2, glob@3.2.11) 

      这条命令会把grunt 命令移植在你得系统路径中,这样就允许你从任意目录中运行grunt命令。

      安装了grunt-cli,不意味着安装了grunt任务运行器,Grunt CLI的工作很简单:在Gruntfile所在目录调用运行已安装好的相应版本的Grunt。这就意味着可以在同一台机器上同时安装多个版本的Grunt。

 

     CLI如何工作

 

      每次运行grunt时,它都会使用node的require()系统查找本地已安装好的grunt。正因为如此,你可以从你项目的任意子目录运行grunt

 

      如果找到本地已经安装好的Grunt,CLI就会加载这个本地安装好的Grunt库,然后应用你项目中的Gruntfile中的配置(这个文件用于配置项目中使用的任务,Grunt也正是根据这个文件中的配置来处理相应的任务),并执行你所指定的所有任务。

 

读下面源码了解工作原理: 

#!/usr/bin/env node

'use strict';

process.title = 'grunt';

// Especially badass external libs.
var findup = require('findup-sync');
var resolve = require('resolve').sync;

// Internal libs.
var options = require('../lib/cli').options;
var completion = require('../lib/completion');
var info = require('../lib/info');
var path = require('path');


var basedir = process.cwd();
var gruntpath;

// Do stuff based on CLI options.
if ('completion' in options) {
  completion.print(options.completion);
} else if (options.version) {
  info.version();
} else if (options.gruntfile) { //Note: if both `gruntfile` and `base` are set, use `gruntfile`
  basedir = path.resolve(path.dirname(options.gruntfile));
} else if (options.base) {
  basedir = path.resolve(options.base);
}

try {
  gruntpath = resolve('grunt', {basedir: basedir});
} catch (ex) {
  gruntpath = findup('lib/grunt.js');
  // No grunt install found!
  if (!gruntpath) {
    if (options.version) { process.exit(); }
    if (options.help) { info.help(); }
    info.fatal('Unable to find local grunt.', 99);
  }
}

// Everything looks good. Require local grunt and run it.
require(gruntpath).cli();

     安装指定版本的Grunt

      要安装指定版本的Grunt或者Grunt插件,可以运行:

 

$ npm install grunt@VERSION --save-dev  

  $ sudo npm install -g grunt   全局安装

成功后

grunt@0.4.5 /usr/local/lib/node_modules/grunt

├── which@1.0.9

├── dateformat@1.0.2-1.2.3

├── eventemitter2@0.4.14

├── getobject@0.1.0

├── colors@0.6.2

├── rimraf@2.2.8

├── async@0.1.22

├── hooker@0.2.3

├── grunt-legacy-util@0.2.0

├── exit@0.1.2

├── lodash@0.9.2

├── nopt@1.0.10 (abbrev@1.0.7)

├── coffee-script@1.3.3

├── underscore.string@2.2.1

├── iconv-lite@0.2.11

├── minimatch@0.2.14 (sigmund@1.0.1, lru-cache@2.6.5)

├── grunt-legacy-log@0.1.2 (grunt-legacy-log-utils@0.1.1, underscore.string@2.3.3, lodash@2.4.2)

├── glob@3.1.21 (inherits@1.0.2, graceful-fs@1.2.3)

├── findup-sync@0.1.3 (lodash@2.4.2, glob@3.2.11)

└── js-yaml@2.0.5 (esprima@1.0.4, argparse@0.1.16)

 

      其中VERSION就是你所需要的版本(指定版本号即可)。这样会安装指定版本的Grunt或者插件,并将它作为你的项目依赖添加到package.json

      检查一下grunt是否安装成功:

$ grunt --version
grunt-cli v0.1.13
grunt v0.4.5

    要正确运行Grunt项目,必须依赖于package.jsonGruntfile.js文件,因此我们紧接下来需要在项目的根目录下创建这两个文件:

 

$ touch package.json Gruntfile.js

 

     我们常常安装Grunt和Grunt插件的方法和上面的是反过来的,先创建package.json文件中添加所需要的Grunt或Grunt插件,比如说现在需要在项目中添加一个Grunt的插件Sass,我们先在package.json文件中的devDependencies中添加相关的依赖关系,如果同时安装多个Grunt插件时,一条一条这样执行似乎很是麻烦,其实还有一种更好的方式,先在package.json文件中添加需要的Grunt插件依赖关系:

{
    "name":"install-grunt",
    "description": "Example project to demonstrate Grunt.",
    "version":"0.1.0",
    "private": true,
    "author": {
        "name": "w3cplus",
        "email": "w3cplus@hotmail.com"
    },
    "devDependencies": {
        "grunt": "~0.4.0",
        "grunt-contrib-sass":"*",
        "grunt-contrib-jshint":"*",
        "grunt-contrib-uglify":"*",
        "grunt-contrib-watch":"*"
    }
}

添加完所需的依赖关系之后,可以在终端执行:

$ npm install
installGrunt |----Gruntfile.js |----node_modules |----+----grunt |----+----grunt-contrib-jshint |----+----grunt-contrib-sass |----+----grunt-contrib-uglify |----+----grunt-contrib-watch |----package.json

就可以一次性安装package.json文件中所声明的依赖关系的grunt插件。同时会在你项目的根目录下添加一个node_modules目录,目录中会放置对应grunt插件所需的插件目录名。

更多详细内容可参考:

http://www.w3cplus.com/tools/grunt-tutorial-installing-grunt.html

http://www.cnblogs.com/yexiaochai/p/3594561.html

        根据上面得内容我把grunt在本地mac机中安装成功了!

  

       

 

posted @ 2015-09-02 11:16  前端之旅,羊羊  阅读(3456)  评论(1编辑  收藏  举报