【问题标题】:Pass Grunt config options from task.run从 task.run 传递 Grunt 配置选项
【发布时间】:2014-01-18 20:33:25
【问题描述】:

尝试了一些东西,但似乎无法让它发挥作用,但我认为这很简单。我试图在任务运行时将变量|选项传递给 Grunt 初始化配置。

目前我有两个单独的配置:

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

如您所见,除了样式选项外,它们是相同的。我想做的是:

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

但我不知道如何以这种方式将选项从 grunt.task.run 传递给初始化配置。任何 Grunt 专家都知道如何做到这一点?谷歌似乎也没有答案...不确定我是不是搞错了?

感谢您的帮助!

【问题讨论】:

    标签: gruntjs


    【解决方案1】:

    请参阅Grunt API example - 这正是您要找的。​​p>

    $ grunt --type=dev
    

    现在您可以通过以下方式获取此值:

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

    我认为它一定是我上面指出的......

    【讨论】:

    • 我认为他的问题是是否有办法抽象配置以便他不需要两次拥有相同的配置,两者的唯一区别是样式选项
    猜你喜欢
    • 1970-01-01
    • 2014-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多