javascript - 如何在 Grunt 目标之间共享文件?

我的 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"]);
};

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

最佳答案

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"]);
};

https://stackoverflow.com/questions/17520015/

相关文章:

build - Dojo 1.8 和构建(或缺少)

powershell - 如何显示 Visual Studio Online 构建任务 Write-

eclipse - 用ant构建eclipse项目

process - 为什么要自动化构建?

c++ - 在 Visual Studio 中运行小型 C++ 程序而不创建项目

node.js - Nodejs npm 步骤在 TeamCity 的每个构建中下载包

build - 何时使用门控值机?

java - Gradle:如何使用并排项目配置多项目设置

android - 特拉维斯-CI `Android 28 licenses have not be

java - 如何创建一个包含 shell 脚本和不在 jar 中的属性文件的 maven 程序集?