2013-12-11 63 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,並可能將其擴展爲接受新過程的參數。