【问题标题】:Print out filepath in grunt在 grunt 中打印文件路径
【发布时间】:2014-10-25 19:21:11
【问题描述】:

我目前有一些代码可以监视 .less 文件的变化。但是我想打印出那里的文件路径,以便以后可以将其添加到日志文件中。

grunt.initConfig({
 watch: {
  less: {
    files: ['vendor/*.less'],
    tasks: ['lessTask'],
    options: {
      spawn: false,
      interrupt: true,
    },
  },
 },
});

grunt.registerTask('lessTask', function(filepath){
   grunt.log.writeln(filepath + ': has changed');
   //Compile less files to CSS
   //Run acceptance tests for UI changes
 });

我的问题是如何传递“文件路径”参数?

【问题讨论】:

    标签: javascript node.js coffeescript gruntjs


    【解决方案1】:

    每个 Grunt 任务都有一个名为 filter 的属性,旨在使用回调过滤掉某些文件路径。但它会给你提供给任务的每个文件的文件路径:

    grunt.initConfig({
      less: {
        target: {
          src: 'vendor/*.less',
          dest: 'dist/style.css',
          filter: function(filepath) {
            grunt.log.writeln(filepath + ' fed to less task');
            return true;
          },
        },
      },
    });
    

    否则,如果您想知道watch 任务更改了哪个文件;它有一个watch 事件:

    grunt.event.on('watch', function(action, filepath, target) {
      grunt.log.writeln(target + ': ' + filepath + ' has ' + action);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-12
      • 1970-01-01
      • 2020-01-24
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-21
      相关资源
      最近更新 更多