【问题标题】:How to share files between Grunt targets?如何在 Grunt 目标之间共享文件?
【发布时间】:2013-07-08 06:01:03
【问题描述】:

我的 Gruntfile 重复了"files",在相同任务的两个目标distdev 之间共享。这是一个仅包含 Stylus 问题的示例:

"use strict";

module.exports = function (grunt) {
    grunt.loadNpmTasks("grunt-contrib-stylus");

    grunt.initConfig({
        stylus: {
            dist: {
                files: { "www/bundle.css": ["stylus/*.styl"] },
                options: { compress: true, linenos: false }
            },
            dev: {
                files: { "www/bundle.css": ["stylus/*.styl"] },
                options: { compress: false, linenos: true }
            }
        }
    });

    grunt.registerTask("dev", ["stylus:dev"]);
    grunt.registerTask("prod", ["stylus:prod"]);
};

有没有办法将文件配置上移一个级别,这样我就不必在两个目标中重复它?

【问题讨论】:

  • 你为什么不能说 "var myFiles = { "www/bundle.css": ["stylus/*.styl"] };"在 grunt.initConfig 之前,然后在需要的地方说“files:myFiles”?它只是一个对象,不是吗?我相信您也可以编写一个将文件属性注入每个对象的函数,但是...我的意思是 DRY,但在某些时候您会为了一点方便而牺牲很多复杂性。
  • 这是这个问题的重复:stackoverflow.com/questions/15927368
  • 追踪器上的问题:github.com/gruntjs/grunt/issues/1029

标签: javascript build gruntjs


【解决方案1】:

Domenic,您可以使用 POJS 变量:

"use strict";

module.exports = function (grunt) {
    grunt.loadNpmTasks("grunt-contrib-stylus");

    var stylusFiles = { "www/bundle.css": ["stylus/*.styl"] };

    grunt.initConfig({
        stylus: {
            dist: {
                files: stylusFiles,
                options: { compress: true, linenos: false }
            },
            dev: {
                files: stylusFiles,
                options: { compress: false, linenos: true }
            }
        }
    });

    grunt.registerTask("dev", ["stylus:dev"]);
    grunt.registerTask("prod", ["stylus:prod"]);
};

或者您可以使用Grunt "Configuring Tasks" Guide 中的模板。

"use strict";

module.exports = function (grunt) {
    grunt.loadNpmTasks("grunt-contrib-stylus");

    grunt.initConfig({
        stylus: {
            dist: {
                files: { "www/bundle.css": ["stylus/*.styl"] },
                options: { compress: true, linenos: false }
            },
            dev: {
                files: "<%= stylus.dist.files %>",
                options: { compress: false, linenos: true }
            }
        }
    });

    grunt.registerTask("dev", ["stylus:dev"]);
    grunt.registerTask("prod", ["stylus:prod"]);
};

【讨论】:

  • 我知道这是一个重复的答案,但我想指出“模板”和 POJS 变量方法同样有效。
  • 这是我能看到的唯一一个带有 POJS 变量的变量,而且看起来确实很简单,所以可以接受。尽管如此,你可以提升 options 但不能提升 files 还是很奇怪。
  • POJS = 普通旧 JavaScript
【解决方案2】:

查看模板:http://gruntjs.com/configuring-tasks#templates

"use strict";

module.exports = function (grunt) {
  grunt.loadNpmTasks("grunt-contrib-stylus");

  grunt.initConfig({
    stylus: {
      dist: {
        files: {
          "www/bundle.css": ["stylus/*.styl"],
        },
        options: { compress: true, linenos: false }
      },
      dev: {
        files: "<%= stylus.dist.files %>",
        options: { compress: false, linenos: true }
      }
    }
  });

  grunt.registerTask("dev", ["stylus:dev"]);
  grunt.registerTask("prod", ["stylus:prod"]);
};

【讨论】:

    【解决方案3】:

    您可以将配置传递给 grunt,我没有测试以下代码,但我认为它应该可以工作。我只是之前没有使用过键的配置,只有值。希望这至少是一个开始。

    "use strict";
    
    module.exports = function (grunt) {
      grunt.loadNpmTasks("grunt-contrib-stylus");
    
      var buildConfig = {
        output: "www/bundle.css",
        files: ["stylus/*.styl"],
      };
    
      grunt.initConfig({
        config: buildConfig,
        stylus: {
            dist: {
                files: {<%= config.output%>: <%= config.files %>},
                options: { compress: true, linenos: false }
            },
            dev: {
                files: {<%= config.output%>: <%= config.files %>},
                options: { compress: false, linenos: true }
            }
        }
      });
    
      grunt.registerTask("dev", ["stylus:dev"]);
      grunt.registerTask("prod", ["stylus:prod"]);
    };
    

    【讨论】:

    • 这不是有效的 JavaScript 语法。
    【解决方案4】:

    我过去曾以几种不同的方式处理过这个问题。一种是利用环境变量并使用环境变量来切换简单的标志,如手写笔标志。扩展这种方法,您甚至可以注册一个为您设置标志的任务。例如

    "use strict";
    
    var env = 'DEV';
    
    module.exports = function (grunt) {
        grunt.loadNpmTasks("grunt-contrib-stylus");
    
        grunt.initConfig({
            stylus: {
                dist: {
                    files: { "www/bundle.css": ["stylus/*.styl"] },
                    options: { compress: env === 'PROD', linenos: env === 'DEV' }
                }
            }
        });
    
        grunt.registerTask('production', function () {
            env = 'PROD';
        });
    
        grunt.registerTask("dev", ["stylus"]);
        grunt.registerTask("prod", ["production", "dev"]);
    };
    

    您也可以使用模板路线或扩展基础对象,但我通常发现标志很容易使用。

    【讨论】:

    • 您不应该为此使用环境变量吗?比如process.env.NODE_ENV判断是在DEV还是PROD环境中。
    • @Spoike 你可以,这就是第一句话所说的。但是,这可能会导致更改每个任务的环境变量或将命令放入 make 文件或 package.json
    【解决方案5】:

    您可以将它作为一个行项目添加到您的 grunt 配置中:

    "use strict";
    
    module.exports = function (grunt) {
        grunt.loadNpmTasks("grunt-contrib-stylus");
    
        grunt.initConfig({
            cssFiles: ["stylus/*.styl"],
            stylus: {
                dist: {
                    files: { "www/bundle.css": '<%= cssFiles %>' },
                    options: { compress: true, linenos: false }
                },
                dev: {
                    files: { "www/bundle.css": '<%= cssFiles %>' },
                    options: { compress: false, linenos: true }
                }
            }
        });
    
        grunt.registerTask("dev", ["stylus:dev"]);
        grunt.registerTask("prod", ["stylus:prod"]);
    };
    

    如果您需要更多示例,请查看我在我的一个项目中使用的文件: https://github.com/sugendran/cheatsheet.js/blob/master/Gruntfile.js

    【讨论】:

      【解决方案6】:

      很确定这样的东西可以工作......

      "use strict";
      
      module.exports = function (grunt) {
      
          grunt.loadNpmTasks("grunt-contrib-stylus");
      
          var files = { "www/bundle.css": ["stylus/*.styl"] };
          var options;
      
          grunt.registerTask("config", function() {
              grunt.initConfig({
                  stylus: {
                      main: {
                          files: files,
                          options: options
                      }
                  }
              });
          });
      
          grunt.registerTask("setup-prod", function () {
              options = { compress: false, linenos: true };
          });
      
          grunt.registerTask("setup-dev", function () {
              options: { compress: true, linenos: false };
          });
      
          grunt.registerTask("dev", ["setup-dev", "config", "stylus"]);
          grunt.registerTask("prod", ["setup-prod", "config", "stylus"]);
      };
      

      看起来您也可以通过直接编辑 grunt.config.data 来更改配置而无需重新调用 initConfig()。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-10-16
        • 1970-01-01
        • 2010-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-26
        相关资源
        最近更新 更多