0

我正在开发一个重新加载 chrome tab 的自定义 grunt 扩展。当我在插件自己的文件夹中使用它时它工作正常,但是当我尝试从 NPM 下载它并在另一个项目中使用它时,它变得疯狂。

我把它包括在内:

grunt.loadNpmTasks('grunt-chrome-extension-reload');

我的自定义任务代码,位于tasks插件的文件夹中,如下所示:

/*
 * grunt-chrome-extension-reload
 * https://github.com/freedomflyer/grunt-chrome-extension-reload
 *
 * Copyright (c) 2014 Spencer Gardner
 * Licensed under the MIT license.
 */

'use strict';

module.exports = function(grunt) {

  var chromeExtensionTabId = 0;


  grunt.initConfig({

    /**
      Reloads tab in chrome with id of chromeExtensionTabId
      Called after correct tab number is found from chrome-cli binary. 
    */
    exec: {
      reloadChromeTab: {
        cmd: function() {
          return chromeExtensionTabId ? "chrome-cli reload -t " + chromeExtensionTabId : "chrome-cli open chrome://extensions && chrome-cli reload"; 
        }
      }
    },

    /**
      Executes "chrome-cli list tabs", grabs stdout, and finds open extension tabs ID's.
      Sets variable chromeExtensionTabId to the first extension tab ID
    */
    external_daemon: {
      getExtensionTabId: {
        options: {
          verbose: true,
          startCheck: function(stdout, stderr) {

            // Find any open tab in Chrome that has the extensions page loaded, grab ID of tab
            var extensionTabMatches = stdout.match(/\[\d{1,5}\] Extensions/);

            if(extensionTabMatches){
              var chromeExtensionTabIdContainer = extensionTabMatches[0].match(/\[\d{1,5}\]/)[0];

              chromeExtensionTabId = chromeExtensionTabIdContainer.substr(1, chromeExtensionTabIdContainer.length - 2);
              console.log("Chrome Extension Tab #: " + chromeExtensionTabId);
            }

            return true;
          }
        },
        cmd: "chrome-cli",
        args: ["list", "tabs"]
      }
    }

  });

  grunt.registerTask('chrome_extension_reload', function() {
    grunt.task.run(['external_daemon:getExtensionTabId', 'exec:reloadChromeTab']);
  });

};

所以,当我在一个外部项目中运行它时grunt watch,grunt 在退出之前会吐出这个错误几百次(无限循环?)

Running "watch" task
Waiting...Verifying property watch exists in config...ERROR
>> Unable to process task.
Warning: Required config property "watch" missing.
Fatal error: Maximum call stack size exceeded

有趣的是,甚至无法在watch任务中调用我的插件,并且问题仍然存在。只有删除grunt.loadNpmTasks('grunt-chrome-extension-reload');才能摆脱问题,这基本上意味着我的任务里面的代码是错误的。有任何想法吗?

4

1 回答 1

1

grunt.initConfig()适用于最终用户。因为它将完全删除任何现有配置(包括您的手表配置)并替换为您正在初始化的配置。因此,当您的插件运行时,它会将整个配置替换为execexternal_daemon任务配置。

尝试grunt.config.set()改用。因为它只设置配置的给定部分,而不是擦除整个内容。

但是插件更好的模式是让用户确定配置。只需一个插件来处理任务。换句话说,避免为用户设置配置。

于 2014-02-15T19:46:36.970 回答