Terminal Grunt-从命令行传递文件名变量

Terminal Grunt-从命令行传递文件名变量,terminal,gruntjs,command-line-arguments,Terminal,Gruntjs,Command Line Arguments,我正在努力理解如何从grunt命令行传递部分文件名,以便在特定文件上运行任务(从安装的grunt模块) 我想能够做的是配置一系列任务,从命令行获取filename参数 我试着在这一页上重做最后一个例子,但我有点在暗中捅了一刀。我以为有人会很快回答 这是我的Grunfile文件: module.exports = function (grunt) { grunt.initConfig({ globalConfig: globalConfig, uglify:

我正在努力理解如何从grunt命令行传递部分文件名,以便在特定文件上运行任务(从安装的grunt模块)

我想能够做的是配置一系列任务,从命令行获取filename参数

我试着在这一页上重做最后一个例子,但我有点在暗中捅了一刀。我以为有人会很快回答

这是我的Grunfile文件:

module.exports = function (grunt) {
    grunt.initConfig({
      globalConfig: globalConfig,

        uglify: {
          js: {
            options: {
              mangle: true
            },
            files: {
              'js/<%= globalConfig.file %>.min.js': ['js/<%= globalConfig.file %>.js']
            }
          }
        },



    });

    // Load tasks so we can use them



    grunt.loadNpmTasks('grunt-contrib-uglify');

    grunt.registerTask('go', 'Runs a task on a specified file', function (fileName){
      globalConfig.file = fileName;
      grunt.task.run('uglify:js');
    });
};
以js/app.js为目标

我得到这个错误:

Aborted due to warnings.
roberts-mbp:150212 - Grunt Tasks robthwaites$ grunt go:app
Loading "Gruntfile.js" tasks...ERROR
>> ReferenceError: globalConfig is not defined
Warning: Task "go:app" not found. Use --force to continue.

谢谢

您可以使用grunt.option

您的grunt注册任务将如下所示

> grunt.option('fileName'); grunt.registerTask('go', 'Runs a task on a
> specified file', function (){     
>       grunt.task.run('uglify:js');
>     });
您的grunt配置将是

module.exports = function (grunt) {
 var fileName=grunt.option('fileName');
    grunt.initConfig({
        uglify: {
          js: {
            options: {
              mangle: true
            },
            files: {
              'js/fileName.min.js': ['js/fileName.js']
            }
          }
        },   
    });
从终端运行任务的命令:

$grunt go--fileName='xyzfile'


我最终能够像这样完成我想要的,但不确定这是否是一种标准的方式

我没有做到的是首先全局声明globalConfig变量,以便在运行grunt任务时从终端重新定义它

这里有一个例子。处理HTML电子邮件时,我需要:

  • 将我的sass文件处理为css(grunt contrib sass)
  • 在生成的css上运行autoprefixer(grunt autoprefixer)
  • 缩小我的CSS并删除CSS注释(grunt contrib cssmin)
  • 在html文件的标记中包含我的完整CSS(使用grunt Include replace)
  • 最后,在文件上运行premailer以内联所有样式(grunt premailer)
  • 关键是,如果我在同一个项目中处理多个不同的HTMl电子邮件,我需要能够根据需要在HTMl文件上逐个运行所有这些任务。下面的Gruntfile允许我这样做

    它的作用是:

    如果您进入terminal
    grunt
    ,它将只运行sass任务,该任务处理所有sass文件-不需要terminal的文件参数

    但是,如果我希望在单个html文件上运行一系列进程,则输入
    grunt process:fileName
    ,其中fileName是html文件的名称,扩展名不带.html

    您会注意到,唯一需要文件名的任务实际上包括replace和premailer。但是,我仍然希望在定位所选文件之前运行所有其他CSS清理任务

    关键是:

  • 声明全局变量
  • 将globalConfig变量加载到grunt.initConfig中
  • 在任务中需要的地方使用grunt变量声明
  • 注册自定义任务,将fileName变量用作参数
  • 希望这能帮助别人

    module.exports = function (grunt) {
    
        var globalConfig = {
            file: 'index' // this is the default value, for a single project.
        }
    
        grunt.initConfig({
    
            pkg: grunt.file.readJSON('package.json'),
    
            // load the globalConfig variables
    
            globalConfig: globalConfig,
    
        sass: {
            dev: {
                files: [{
                    expand: true,
                    cwd: 'scss',
                    src: ['*.scss'],
                    dest: 'css',
                    ext: '.css'
        }]
            }
        },
    
        cssmin: {
            options: {
                keepSpecialComments: 0,
                keepBreaks: true,
                advanced: false
            },
            target: {
                files: [{
                    expand: true,
                    cwd: 'css',
                    src: '*.css',
                    dest: 'css',
                    ext: '.css'
        }]
            }
        },
    
        autoprefixer: {
            css: {
                src: "css/*.css"
            }
        },
    
        includereplace: {
            your_target: {
                options: {
                    prefix: '\\/\\* ',
                    suffix: ' \\*\\/',
                },
                files: {
                    'inline/<%= globalConfig.file %>-inline.html': ['<%= globalConfig.file %>.html']
                }
            }
        },
    
        premailer: {
            main: {
                options: {
                    verbose: true,
                    preserveStyles: true,
                },
                src: 'inline/<%= globalConfig.file %>-inline.html',
                dest: 'inline/<%= globalConfig.file %>-inline.html'
            }
        },
    
    
    });
    
    
    grunt.loadNpmTasks('grunt-contrib-sass');
    grunt.loadNpmTasks('grunt-autoprefixer');
    grunt.loadNpmTasks('grunt-include-replace');
    grunt.loadNpmTasks('grunt-premailer');
    
    grunt.registerTask('default', 'sass');
    
    grunt.registerTask('process', 'Runs all processing tasks on a specific file to produce inlined file', function (fileName) {
        globalConfig.file = fileName;
        grunt.task.run('sass', 'autoprefixer', 'cssmin', 'includereplace', 'premailer');
    });
    
    module.exports=函数(grunt){
    变量globalConfig={
    file:'index'//这是单个项目的默认值。
    }
    grunt.initConfig({
    pkg:grunt.file.readJSON('package.json'),
    //加载globalConfig变量
    globalConfig:globalConfig,
    sass:{
    开发人员:{
    档案:[{
    是的,
    cwd:‘scss’,
    src:['*.scss'],
    dest:'css',
    分机:'.css'
    }]
    }
    },
    cssmin:{
    选项:{
    KeepsSpecialComments:0,
    keepBreaks:没错,
    高级:错误
    },
    目标:{
    档案:[{
    是的,
    cwd:‘css’,
    src:'*.css',
    dest:'css',
    分机:'.css'
    }]
    }
    },
    自动刷新器:{
    css:{
    src:“css/*.css”
    }
    },
    包括地点:{
    你的目标:{
    选项:{
    前缀:'\/\\*',
    后缀:“\\*\\/”,
    },
    档案:{
    'inline/-inline.html':['.html']
    }
    }
    },
    预卷机:{
    主要内容:{
    选项:{
    没错,
    是的,
    },
    src:'inline/-inline.html',
    dest:'inline/-inline.html'
    }
    },
    });
    grunt.loadNpmTasks(“grunt-contrib-sass”);
    grunt.loadNpmTasks('grunt-autoprefixer');
    grunt.loadNpmTasks('grunt-include-replace');
    grunt.loadNpmTasks(“grunt-premailer”);
    registerTask('default','sass');
    registerTask('process','运行特定文件上的所有处理任务以生成内联文件',函数(文件名){
    globalConfig.file=文件名;
    run('sass','autoprefixer','cssmin','includereplace','premailer');
    });
    
    }


    EDIT:显然,目前它只接受我提供的一个参数。在其他用例中,上面的grunt.option版本可以提供更多功能,能够在一个命令中提交多个参数。如果我觉得有必要的话,我会继续尝试grunt.option。

    Hi我找到了一个语法稍微简单一点的解决方案,至少可以从终端运行命令。我想知道你对此有何看法?无论如何,这是另一种解决办法。
    module.exports = function (grunt) {
    
        var globalConfig = {
            file: 'index' // this is the default value, for a single project.
        }
    
        grunt.initConfig({
    
            pkg: grunt.file.readJSON('package.json'),
    
            // load the globalConfig variables
    
            globalConfig: globalConfig,
    
        sass: {
            dev: {
                files: [{
                    expand: true,
                    cwd: 'scss',
                    src: ['*.scss'],
                    dest: 'css',
                    ext: '.css'
        }]
            }
        },
    
        cssmin: {
            options: {
                keepSpecialComments: 0,
                keepBreaks: true,
                advanced: false
            },
            target: {
                files: [{
                    expand: true,
                    cwd: 'css',
                    src: '*.css',
                    dest: 'css',
                    ext: '.css'
        }]
            }
        },
    
        autoprefixer: {
            css: {
                src: "css/*.css"
            }
        },
    
        includereplace: {
            your_target: {
                options: {
                    prefix: '\\/\\* ',
                    suffix: ' \\*\\/',
                },
                files: {
                    'inline/<%= globalConfig.file %>-inline.html': ['<%= globalConfig.file %>.html']
                }
            }
        },
    
        premailer: {
            main: {
                options: {
                    verbose: true,
                    preserveStyles: true,
                },
                src: 'inline/<%= globalConfig.file %>-inline.html',
                dest: 'inline/<%= globalConfig.file %>-inline.html'
            }
        },
    
    
    });
    
    
    grunt.loadNpmTasks('grunt-contrib-sass');
    grunt.loadNpmTasks('grunt-autoprefixer');
    grunt.loadNpmTasks('grunt-include-replace');
    grunt.loadNpmTasks('grunt-premailer');
    
    grunt.registerTask('default', 'sass');
    
    grunt.registerTask('process', 'Runs all processing tasks on a specific file to produce inlined file', function (fileName) {
        globalConfig.file = fileName;
        grunt.task.run('sass', 'autoprefixer', 'cssmin', 'includereplace', 'premailer');
    });