【问题标题】:How do you make grunt.js not crash on warnings by default?默认情况下,如何使 grunt.js 不会因警告而崩溃?
【发布时间】:2013-03-03 15:54:08
【问题描述】:

我正在使用 Grunt 编译 CoffeeScript 和 Stylus 并执行监视任务。我还设置了我的编辑器 (SublimeText) 以在每次离开文件时保存文件(我讨厌失去工作)。

不幸的是,如果 Grunt 在它正在编译的任何文件中遇到语法错误,它会抛出警告并以Aborted due to warnings 退出。我可以通过传递--force 来阻止它这样做。有什么方法可以不中止默认行为(或控制哪些任务的警告足够重要以退出 Grunt?

【问题讨论】:

    标签: gruntjs


    【解决方案1】:

    注册您自己的任务,它将运行您想要的任务。然后你必须通过force 选项:

    grunt.registerTask('myTask', 'runs my tasks', function () {
        var tasks = ['task1', ..., 'watch'];
    
        // Use the force option for all tasks declared in the previous line
        grunt.option('force', true);
        grunt.task.run(tasks);
    });
    

    【讨论】:

    • 这可行,但是对于序列中的所有剩余任务,强制选项处于打开状态。我对this question 的回答有另一个技巧
    • 你不能只做 grunt.option('force', false);运行任务后?
    【解决方案2】:

    我根据Adam Hutchinson 的建议尝试了asgoth 的解决方案,但发现强制标志立即被设置为false。阅读 grunt.task.run 的grunt.task API 文档,它指出

    taskList 中的每个指定任务将在当前任务完成后立即按照指定的顺序运行。

    这意味着我不能在调用 grunt.task.run 后立即将 force 标志设置回 false。我找到的解决方案是让明确的任务在之后将 force 标志设置为 false:

    grunt.registerTask('task-that-might-fail-wrapper','Runs the task that might fail wrapped around a force wrapper', function() {
        var tasks;
        if ( grunt.option('force') ) {
            tasks = ['task-that-might-fail'];
        } else {
            tasks = ['forceon', 'task-that-might-fail', 'forceoff'];
        }
        grunt.task.run(tasks);
    });
    
    grunt.registerTask('forceoff', 'Forces the force flag off', function() {
        grunt.option('force', false);
    });
    
    grunt.registerTask('forceon', 'Forces the force flag on', function() {
        grunt.option('force', true);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-19
      • 2013-05-24
      • 2018-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-07
      相关资源
      最近更新 更多