我的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'
如果部署了命令行arg,则dest:'src / main / resources / cachebuster.json'

我该如何实现?

最佳答案

您可以尝试以下代码块。默认情况下,如果不提供任何参数,第一个目标将被执行(在这种情况下为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'
  }
}

关于javascript - 如何使用咕target咕target的目标,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31366273/

10-16 03:43