2015-07-12 62 views
0

我Gruntfile.js是一样的东西:如何使用咕噜目标

'use strict'; 

module.exports = function(grunt) { 
    grunt.initConfig({ 
     grunt.config.set('targ', grunt.option('target')); 
     cachebuster: { 
      build: { 
       options: { 
        basedir: 'WebContent', 
        formatter: function(hashes) { 
         var json = {}; 
         for (var filename in hashes) { 
          json["/" + filename] = hashes[filename]; 
         } 
         return JSON.stringify(json); 
        } 
       }, 
       src: [ 'WebContent/assets/**/*.js', 'WebContent/assets/**/*.css' ], 
       dest: 'src/main/resources/cachebusters.json' 
      } 
     } 
    }); 

// These plugins provide necessary tasks. 
grunt.loadNpmTasks('grunt-cachebuster');  

// Default task. 
grunt.registerTask('default', ['cachebuster']); 

};

我想基于dev的改变dest的位置或部署命令行参数..

即,如果命令行arg是dev的则dest:“cachebuster.json” 如果命令行参数是部署然后dest:'src/main/resources/cachebuster.json'

我该如何做到这一点?

回答

2

您可以尝试下列代码块。默认情况下,如果您不提供参数,则会执行第一个目标(在此情况下为dev):

cachebuster: { 
    dev: { 
    options: { 
     basedir: 'WebContent', 
     formatter: function(hashes) { 
     var json = {}; 
     for (var filename in hashes) { 
      json["/" + filename] = hashes[filename]; 
     } 
     return JSON.stringify(json); 
     } 
    }, 
    src: ['WebContent/assets/**/*.js', 'WebContent/assets/**/*.css'], 
    dest: 'src/main/resources/cachebusters.json' 
    }, 
    deploy: { 
    options: { 
     basedir: 'WebContent', 
     formatter: function(hashes) { 
     var json = {}; 
     for (var filename in hashes) { 
      json["/" + filename] = hashes[filename]; 
     } 
     return JSON.stringify(json); 
     } 
    }, 
    src: ['WebContent/assets/**/*.js', 'WebContent/assets/**/*.css'], 
    dest: 'src/main/resources/cachebusters.json' 
    } 
} 

如果要跨目标共享选项,请使用以下代码块。

cachebuster: { 
    options: { 
    basedir: 'WebContent', 
    formatter: function(hashes) { 
     var json = {}; 
     for (var filename in hashes) { 
     json["/" + filename] = hashes[filename]; 
     } 
     return JSON.stringify(json); 
    } 
    }, 
    dev: { 
    src: ['WebContent/assets/**/*.js', 'WebContent/assets/**/*.css'], 
    dest: 'src/main/resources/cachebusters.json' 
    }, 
    deploy: { 
    src: ['WebContent/assets/**/*.js', 'WebContent/assets/**/*.css'], 
    dest: 'src/main/resources/cachebusters.json' 
    } 
} 
+0

谢谢。您描述的在目标间共享选项的第二种方法效果很好。 –