Grunt - 从命令行传递filename变量

时间:2015-02-12 23:26:56

标签: terminal gruntjs command-line-arguments

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

我希望能够做的是配置一系列任务以从命令行获取filename参数。

我已尝试在此页http://chrisawren.com/posts/Advanced-Grunt-tooling上重新制作最后一个例子,但我有点在黑暗中刺伤了一下。以为有人会快速回答。

这是我的Gruntfile:

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');
    });
};

我尝试从命令行运行它,如下所示:

grunt go:app

定位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.

由于

2 个答案:

答案 0 :(得分:6)

你可以使用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 =&#39; xyzfile&#39;

答案 1 :(得分:1)

我最终能够达到我想要的效果,但不确定这是否是一种标准方式。

我未能做到的是首先全局声明globalConfig变量,这样我就可以在运行我的grunt任务时从终端重新定义它。

这是一个例子。使用HTML电子邮件时,我需要:

  1. 将我的sass文件处理为css(grunt-contrib-sass)
  2. 在生成的css上运行autoprefixer(grunt-autoprefixer)
  3. 缩小我的CSS并删除CSS评论(grunt-contrib-cssmin)
  4. 在我的html文件的标记中包含我的完整CSS(使用grunt-include-replace)
  5. 最后,在文件上运行预编译器以内联所有样式(grunt-premailer)
  6. 关键是,如果我在同一个项目中处理几个不同的HTMl电子邮件,我需要能够根据需要逐个在html文件上运行所有这些任务。下面的Gruntfile允许我这样做。

    这是做什么的:

    如果你进入终端grunt它只会运行sass任务,它处理所有sass文件 - 终端不需要文件参数。

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

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

    关键是:

    1. 声明全局变量
    2. 将globalConfig变量加载到grunt.initConfig
    3. 在任务中需要的地方使用grunt变量声明
    4. 注册您的自定义任务,fileName变量用作参数。
    5. 希望能有所帮助。

      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');
      });
      

      }

      编辑:显然目前这只接受一个我相信的参数。在其他用例中,上面的grunt.option版本可以提供更多功能,能够在一个命令中提交多个参数。如果我发现需要这样做,我会继续尝试grunt.option。