grunt学习笔记

gruntjs 是一个基于nodejs的自动化工具,只要熟悉nodejs或者又必定js经验就能够熟练应用。
最近很火的前端自动化小工具,基于任务的命令行构建工具 http://gruntjs.comjavascript

咱们须要一个自动化的工做流程,让咱们更专一于coding,而不是coding外的繁琐工做。因而Grunt应运而生。能够想像,若是在node环境下,一行命令搞定一个场景,So Cool…css

package.json //项目自动化所依赖的相关插件。
Gruntfile.js //项目自动化工做流配置文件,重要html

  1. 安装
    • 确保已经安装node和npm
    • 而后安装grunt的客户端命令行工具 npm install -g grunt-cli, 安装完以后就有了grunt
      压缩css js和合并文件须要grunt插件,grunt只是一个平台,完成各项任务有对应的插件。
    • grunt插件十分丰富,其中带contrib的为官方插件 其余的为第三方插件
  2. 安装grunt插件
    • 方式1 在工程目录中建立一个package.json文件,列出依赖的插件和对应的版本便可
      而后cd到工程目录下 执行 npm install
    • 方式2 使用命令行工具 npm install grunt-contrib-watch --save-dev
  3. 建立gruntjs工程文件
    gruntjs支持两种语言建立工程文件: gruntfile.js或者gruntfile.coffee
    grunt工程文件遵循node模块定义方式,主题结构为:前端

    module.exports = function(grunt){
            grunt.initConfig({
                clean:{
                    a:['<%=temp%>'] 
                },
                b:['<%=temp%>']
            });
    
        //获取工程中须要的插件
        grunt.loadNpmTasks('grunt-contrib-clean');
        grunt.loadNpmTasks('grunt-contrib-cssmin');
        grunt.loadNpmTasks('grunt-contrib-uglify');
        grunt.loadNpmTasks('grunt-contrib-watch');
        grunt.loadNpmTasks('grunt-contrib-copy');
        grunt.loadNpmTasks('grunt-contrib-includes');
    
        //注册任务
        grunt.registerTask('build', ['clean','uglify','cssmin','includes','copy','clean']);
        //在命令行中执行grunt,就会使用默认任务
        grunt.registerTask('default',['build']);
    //一个完整的gruntjs工程文件
    module.exports = function(grunt){
        var rt = 'src-dev/',
            indexDir = rt + 'index/',
            tempDir  = rt + 'temp/';
        console.log(grunt.option('keys'));
    
        grunt.file.exists(tempDir) && grunt.file.delete(tempDir);
        grunt.file.markdir(tempDir);
    
        grunt.initConfi({
            pkg: grunt.file.readJSON('package.json');
            rt: rt,
            tempDir: tempDir,
            indexDir = indexDir,
            clear: { build:['<%=temp%>']},
            jsdoc:{
                dist:{
                    src:'<%=index%>doc.js',
                    options:{
                        destination:'<%=rt%>../out/'
                    }
                }
            },
            cssmin:{
                combine:{
                    files:{
                        '<%=temp%>c.min.css':['<%=rt%>common/reset.css', '<%=index%>c.css']
                    }
                }
            },
            includes:{
                files:{
                    sr:['<%=rt%>**/*.html'],
                    dest:'<%=temp%>',
                    cwd:'.',
                    flatten:true,
                    options{
                        banner:''
                    }
                }
            },
            watch:{
                files:['<%=index%>j.js','<%=index%>*.html', '<%=index%>c.css']
                tasks:['clean','uglify','cssmin','includes','copy','clearn'],
                options:{ livereload:true}
            },
            uglify:{
                dist:{
                    files:{
                        '<%=temp%>j.js', ['<%=index%>*.js']
                    }
                }
            },
            copy:{
                main:{
                    files:[{
                        flatten: true,
                        expand: true,
                        filter:'isFile',
                        src:['<%=temp%>index.html'],
                        dest='<%=rt%>../src/'
                    }]
                }
            }
    
            //获取工程中须要的插件
            grunt.loadNpmTasks('grunt-contrib-clean');
            grunt.loadNpmTasks('grunt-contrib-cssmin');
            grunt.loadNpmTasks('grunt-contrib-uglify');
            grunt.loadNpmTasks('grunt-contrib-watch');
            grunt.loadNpmTasks('grunt-contrib-copy');
            grunt.loadNpmTasks('grunt-contrib-includes');
    
            //注册任务
            grunt.registerTask('build', ['clean','uglify','cssmin','includes','copy','clean']);
            //在命令行中执行grunt,就会使用默认任务
            grunt.registerTask('default',['build']);
相关文章
相关标签/搜索