grunt学习笔记

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

我们需要一个自动化的工作流程,让我们更专注于coding,而不是coding外的繁琐工作。于是Grunt应运而生。可以想像,如果在node环境下,一行命令搞定一个场景,So Cool…

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

  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']);

转载于:https://www.cnblogs.com/stephenykk/p/4118576.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值