2013-12-11 65 views
0

虽然试图做一些通用的咕噜任务,我卡住了访问变量/选项。grunt中是否有全局变量?

我需要某种全局变量或类似的东西。 看来我忽略了一些东西。

如果我运行grunt sassCompile:myprojectFolder,一切工作正常 而grunt sassWatch:myprojectFolder没有。

我在详细模式下运行它,它似乎项目路径是空的,而指南针被监视。

罗盘选项(从详细输出):

sassCompileconfig="projectRoot/myprojectFolder/config.rb" ...

sassWatchconfig="config.rb" ...

这在Gruntfile.js被用于测试:

什么我做错了?

(function() { 
    'use strict'; 
    module.exports = function (grunt) { 
     grunt.initConfig({ 
      compass: { 
       dev: { 
        options: { 
         config: "<%= projectPath %>config.rb", 
         basePath: "<%= projectPath %>", 
         specify: ["<%= projectPath %>src/sass/style*.scss","!**/*ie*.scss"], 
         bundleExec: true 
        } 
       } 
      }, 
      watch: { 
       css: { 
        files: ['<%= projectPath %>../**/*.scss'], 
        tasks: ['compass'] 
       } 
      } 
     }); 
     grunt.registerTask('sassCompile', 'compass', function (project) { 
      grunt.config('projectPath', 'projectRoot/' + project + '/'); 
      grunt.task.run('compass'); 
     }); 
     grunt.registerTask('sassWatch', 'watch', function (project) { 
      grunt.config('projectPath', 'projectRoot/' + project + '/'); 
      grunt.task.run('watch'); 
     }); 

     grunt.loadNpmTasks('grunt-contrib-watch'); 
     grunt.loadNpmTasks('grunt-contrib-compass'); 
    }; 
}()); 

回答

0

这绝对是一个有趣的方法,你有 - 我花了一段时间才明白发生了什么。

运行grunt sassWatch:myprojectFolder时会发生什么情况是,watch任务获取开始与projectPath您冒号后提供的,,当watch检测到变化它运行compass任务,而不配置。

这是因为grunt-contrib-watch通过启动新的grunt进程来运行它的任务。你可能通过使用options: { spawn: false }运行在同一进程内的任务,但似乎不鼓励。

我建议你试试这个:

watch: { 
    css: { 
     files: ['<%= projectPath %>../**/*.scss'], 
     tasks: ['sassCompile:<%= projectPath %>'] 
    } 
} 

这样watch将运行在催生咕噜过程sassCompile:myprojectFolder,确保配置文件会转移到compass任务。

+0

感谢您提供有关流程的提示。 我现在将全部运行为假。 (这个任务会比最后的样本大得多) – evilru

0

在grunt页面有一个section about global variables,但它们不适用于多个进程。

手动启动一个新的异步过程并切换所需的变量将是可能的。

您可以查看here,并可能将其扩展为接受新过程的参数。