[Grunt] External Config

Let's combine uglifying, watching, and config stuff into one Grunt file to make it more of a standard Grunt file.

Install:


npm install grunt
npm install grunt-contrib-watch
npm install grunt-contrib-uglify

Example:


/**
 * Created by Answer1215 on 11/15/2014.
 */
module.exports = function(grunt){

    // Version one
    //grunt.initConfig({
    //
    //    uglify:{
    //        dist:{
    //            files:{
    //               "dist/app.min.js": "app/**/*.js"
    //            }
    //        }
    //    },
    //    watch:{
    //        files:"app/**/*.js",
    //        tasks: 'uglify'
    //   }
    //});

    //version two: using template
    //grunt.initConfig({
    //    conf: {
    //        input: "app/**/*.js"
    //    },
    //    uglify:{
    //        dist:{
    //            files:{
    //                "dist/app.min.js": "<%= conf.input %>"
    //            }
    //        }
    //    },
    //    watch:{
    //        files: "<%= conf.input %>",
    //        tasks: 'uglify'
    //   }
    //});


    //version three:  using config json file

    grunt.initConfig({
        conf: grunt.file.readJSON('config.json'),
        uglify: {
            dist:{
                files:{
                    "dist/app.min.js": "<%= conf.input %>"
                }
            }
        },
        watch: {
            files: "<%= conf.input %>",
            tasks: ['uglify']
        }
    });

    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-uglify');
}

 config.json

{
  "input": "app/**/*.js"
}
原文地址:https://www.cnblogs.com/Answer1215/p/4100953.html