grunt watch - 使用文件数组格式作为源

4

我正在同时使用grunt-contrib-less和grunt-contrib-watch。我的less任务使用文件数组格式来定义多个src和dest。我希望在watch任务中引用这些相同的文件。就像这样:

grunt.initConfig({
  less: {
    build: {
      files: [
        {src: 'src/aa.less', dest: 'dest/a.css'},
        {src: 'src/aa1.less', dest: 'dest/a1.css'}
      ]
    }
  },
  watch: {
    less: {
      files: '<%= less.build.files %>',
      tasks: ['less']
    }
  }
});

那个下划线模板是有效的,但watch无法处理文件数组格式,它只接受字符串或字符串数组作为输入。这是我尝试过的:
  • '<%= less.build.files.src %>'无法工作,因为less.build.files是一个数组,而不是对象。

  • '<%= _(less.build.files).pluck("src").value() %>'无法工作,即使它生成了正确的文件列表,它解析为单个字符串'src/aa.less,src/aa1.less',而不是数组。

  • '{<%= _(less.build.files).pluck("src") %>}'可以工作,如此建议https://dev59.com/h3DYa4cB1Zd3GeqPFOGa#21608021,但感觉不太对。我正在尝试针对特定的文件集,而不是从整个项目目录中进行模式匹配。

  • grunt.config.set('watch.less.files', _(grunt.config.get('less.build.files')).pluck('src').value());可以工作,但必须与initConfig分开。

  • 有更优雅的方法来完成这个任务吗?
    2个回答

    3

    确认grunt-contrib-watch不支持文件数组格式。我决定使用我上面提到的 grunt-config-set 技术。

    尽管watch不支持文件数组格式,但我确保我的自定义任务与其兼容,以便我不必使用我的问题中的解决方法。我附上了一个示例。对于只读任务,我添加了一个 useDest 选项,以便它们可以配置为在dest而不是src上运行。这有助于当您想将一个任务“管道”到另一个任务时。

    module.exports = function (grunt) {
    
      grunt.registerMultiTask('example', 'Example read-only task', function () {
        var options = this.options({
              useDest: false, // When true, operate on dest files, instead of src
            });
            files = this.files.map(function (file) {
              return { src: (options.useDest) ? [file.dest] : file.src }
            });
        files.forEach(function (file) {
          grunt.log.writeln('Source: ' + grunt.log.wordlist(file.src));
        });
      });
    
      grunt.loadNpmTasks('grunt-contrib-concat');
    
      grunt.initConfig({
        concat: {
          build: {
            files: [
              { src: 'node_modules/grunt/lib/grunt/*.js', dest: 'lib.js' },
              { src: 'node_modules/grunt/internal-tasks/*.js', dest: 'tasks.js' }
            ]
          }
        },
        example: {
          build: {
            options: {
              useDest: true
            },
            files: '<%= concat.build.files %>'
          }
        }
      });
    
    };
    

    任务将输出:
    Running "example:build" (example) task
    Source: lib.js
    Source: tasks.js
    

    2
    我不明白为什么你没有将文件部分重构成一个变量?这样可以实现你的目标:“想要从监视任务中引用相同的文件。”
    var yourFiles = [
        {src: 'src/aa.less', dest: 'dest/a.css'},
        {src: 'src/aa1.less', dest: 'dest/a1.css'}
    ];
    
    grunt.initConfig({
        less: {
            build: {
                files: yourFiles 
            }
        },
        watch: {
            less: {
                files: yourFiles 
                tasks: ['less']
            }
        }
    });
    

    附注:你可能会喜欢阅读这篇文章,了解在模板中引用变量时发生的内部情况,以便进行更高级的操作。


    1
    我认为你可能误读了问题。使用文件数组格式时,监视任务无法工作。 - Chad von Nau
    可能有。没有一组文件来测试配置。 - oligofren
    1
    Oligofren,我晚了才接受你的答案。最近有人给我的自答投了赞,现在看来,你的方法更好。需要做一个更改以解决 grunt-contrib-watch 与文件数组格式不兼容的问题,将监视文件设置为 yourFiles.map(i => i.src)。 - Chad von Nau

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接