简体   繁体   English

如何使用咕unt目标

[英]how to use grunt target

My Gruntfile.js is something like: 我的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']);

}; };

I would like to change the location of dest based on dev or deploy command line argument.. 我想基于dev更改dest的位置或部署命令行参数。

ie if command line arg is dev then dest : 'cachebuster.json' if command line arg is deploy then dest : 'src/main/resources/cachebuster.json' 也就是说,如果命令行arg是dev,那么dest:'cachebuster.json'如果部署了命令行arg,则dest:'src / main / resources / cachebuster.json'

How do i achieve this ?? 我该如何实现?

You can try the following code blocks. 您可以尝试以下代码块。 By default the first target will get executed (dev in this case) if you supply no arguments: 默认情况下,如果不提供任何参数,第一个目标将被执行(在这种情况下为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'
  }
}

Use the following code block if you want to share the options across targets. 如果要跨目标共享选项,请使用以下代码块。

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'
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM