. 安装Node.js
    http://nodejs.org/download/

2. 加速NPM安装
    npm install -g cnpm --registry=http://r.cnpmjs.org
    安装cnpm国内镜像, 以后所有npm命令换成用cnpm执行即可

3. 安装grunt
    npm install -g grunt-cli

4. 在工程目录下建立配置文件Gruntfile.js
    本例中, 所做的事情包括:
      a. 组合一些less到一个文件
      b. 用less编译不同版本的css
      c. 填充html模版并部署
      d. 实时侦听以上变化并自动做相应编译

<!-- code begin -->
        module.exports = function(grunt) {
          grunt.initConfig({
             pkg: grunt.file.readJSON('package.json'),
             concat: {
                        options: {
                                banner: '/*! APP.common.css@<%= pkg.name %> - v<%= pkg.version %> - ' +
                                        '<%= grunt.template.today("yyyy-mm-dd") %> */'
                            },
                            mobileLess: {
                              src: ['src/mobile/less/APP_common/*.less'],
                              dest: 'src/mobile/less/APP.common_grunt.less',
                         }
            },
            less: {
              development: {
                options: {
                  compress: false,
                  yuicompress: false
                },
                files: {
                  "css/APP.common.css": "src/mobile/less/APP.common_grunt.less",
                  "css/APP.web.index.css": "src/web/less/APP.web.index.less"
                }
              },
              production: {
                options: {
                  modifyVars: {
                            imagepath_page: '"/misc/images/"',
                            imagepath: '"/misc/images/"'
                  },
                  compress: true,
                  yuicompress: true,
                  optimization: 2
                },
                files: {
                  "css/pub/APP.common.css": "src/mobile/less/APP.common_grunt.less",
                  "css/pub/APP.web.index.css": "src/web/less/APP.web.index.less"
                }
              }
            },
            htmlbuild: {
                        mobile: {
                                src: 'src/mobile/html/*.html',
                                desc: './',
                                options: {
                                        beautify: true,
                                        relative: true,
                                        sections: {
                                                layout: {
                                                        footbar: 'src/mobile/html/inc/footbar.html'
                                                }
                                        }
                                }
                        },
                        web: {
                                src: 'src/web/html/*.html',
                                desc: './'
                        }
            },
            watch: {
              options: {
                livereload: true
              },
              grunt: {
                files: ['Gruntfile.js']
              },

styles: {
                files: [
                        'src/**/less/*.less',
                        'src/**/less/**/*.less'
                ],
                tasks: [
                        'concat:mobileLess',
                        'less'
                ],
                options: {
                  nospawn: true
                }
              },
              htmls: {
                files: [
                        'src/**/html/*.html',
                        'src/**/html/**/*.html'
                ],
                tasks: [
                        'htmlbuild'
                ],
                options: {
                  nospawn: true
                }
              }
            }
          });
         
          grunt.loadNpmTasks('grunt-contrib-concat');
          grunt.loadNpmTasks('grunt-contrib-less');
          grunt.loadNpmTasks('grunt-contrib-watch');
          grunt.loadNpmTasks('grunt-html-build');
         
          grunt.registerTask('default', ['watch']);
        };
  <!-- code end -->

5. 建立并配置 package.json
    在项目目录下运行 npm init , 填写出现的各种选项, 或者直接回车到完成
    打开生成的 package.json, 并添加 devDependencies 节点 --如果后续的编译出错, 往往是这里的版本号不够新

<!-- code begin -->
        {
          "name": "html",
          "version": "0.0.0",
          "description": "",
          "main": "Gruntfile.js",
          "scripts": {
            "test": "echo \"Error: no test specified\" && exit 1"
          },
          "author": "",
          "license": "ISC",
          "devDependencies": {
            "grunt": "~0.4.1",
            "grunt-contrib-concat": "~0.4.0",
            "grunt-contrib-less": "~0.11.0",
            "grunt-contrib-watch": "~0.6.1",
            "grunt-html-build": "~0.3.2"
          }
        }
  <!-- code end -->

6. 安装项目依赖包
    在项目目录下运行 cnpm install , 将把 package.json->devDependencies下注明的依赖包下载到 node_modules

7. 运行grunt
    在项目目录下执行 grunt 即可

最新文章

  1. haproxy+keepalived
  2. 《C++ API设计》作者Martin Reddy访谈问题征集
  3. Android开发--Adapter的应用
  4. iOS 解压打包静态库命令
  5. 【即时通讯】即时通讯及XMPP概述及…
  6. iptables一些经常忘掉易混淆的参数
  7. Windows 下使用 GNUstep 编译并运行 Objective-C 程序
  8. 【Robot Framework】robot framework 学习以及selenium、appnium、requests实践(三)
  9. UML快速指南(摘要)转载
  10. seajs构建方法
  11. 模板:LCS(最长公共子序列)
  12. rhel Linux 网络配置
  13. secureCRT自动化脚本
  14. 惊心动魄的SAP S4客户额度调整运动
  15. weui复选框无法传值
  16. WebSocket 快速开始
  17. cdnbest区域自定义配置里添加防xss攻击配置
  18. JS 自己实现Map
  19. 为什么样本方差除以(n-1)而不是n ?(自由度)
  20. kafka学习之-集群配置及安装

热门文章

  1. SPOJ 694. Distinct Substrings (后缀数组不相同的子串的个数)转
  2. php 自带过滤和转义函数
  3. A+B问题 涉及EOF
  4. jQuery-ajax: 取消关注|关注
  5. java多线程之AtomicInteger
  6. XML中五个转义字符
  7. centos的软件安装方法rpm和yum
  8. [ActionScript 3.0] AS3实现滤镜叠加效果
  9. 如何让你的Python代码更加pythonic ?
  10. linux入门学习1