Pass Grunt config options from task.run

别等时光非礼了梦想. 提交于 2019-12-24 04:22:33

问题


Tried a few things and cannot seem to get this to work, but I would have thought it was something quite simple. I am trying to pass a variable|option into a Grunt initialize config when the task is run.

At the minute I have two separate configs:

sass: {
  dev: {
    options: {
      style: 'expanded'
    },
    files: [{
      expand: true,
      cwd: source + 'scss/',
      src: '*.scss',
      dest: destination + 'css',
      ext: '.css'
    }]
  },
  production: {
    options: {
      style: 'compressed'
    },
    files: [{
      expand: true,
      cwd: source + 'scss/',
      src: '*.scss',
      dest: destination + 'css',
      ext: '.css'
    }]
  }
}

As you can see, these are the same except the style option. What I would like to do is:

sass: {
  build: {
    options: {
      style: style
    },
    files: [{
      expand: true,
      cwd: source + 'scss/',
      src: '*.scss',
      dest: destination + 'css',
      ext: '.css'
    }]
  }
}

...

grunt.registerTask("sass", function () {
  grunt.task.run('sass:build:style=expanded');
});

But I cannot work out how to pass the option from grunt.task.run to the initialize config in this way. Any Grunt experts know how to do this? Google didn't seem to have the answer either... Not sure if I'm approaching this wrong?

Thanks for any help!


回答1:


see Grunt API example - it is exactly what are you looking for.

$ grunt --type=dev

Now you can get this value by:

grunt.registerTask('default','description...', function() {
    var type = grunt.option('type') || 'dev'; //if nothing passed from cli set to 'dev'
    if (type == 'dev') {
        grant.task.run('sass:dev');
    } else if (type == 'production') {
        grant.task.run('sass:production');
    }
});

I think it must be something like I pointed above..



来源:https://stackoverflow.com/questions/20347253/pass-grunt-config-options-from-task-run

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!