Grunt:为每个文件而不是整个目录运行babel任务

Grunt: run babel task for each file instead of entire directory

本文关键字:运行 任务 babel 文件 Grunt      更新时间:2023-09-26

我有这个Grunfile,它"监视"src/目录中的.js文件,当其中一个文件被修改时,babel (https://github.com/babel/grunt-babel)任务运行以在dist/目录中生成ES5文件:

module.exports = function(grunt) {
    require('load-grunt-tasks')(grunt);
    grunt.initConfig({
        babel: {
            options: {
                sourceMap: false
            },
            dist: {
                 files: [{
                    expand: true,
                    cwd: 'src/',
                    src: ['*.js'],
                    dest: 'dist/',
                    ext: '.js'
                }]
            }
        },
        watch: {
            ej6: {
                files: "src/*.js",
                tasks: ['babel']
            }
        }
    });
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.registerTask('default', ['babel', 'watch']);
};

是否可以监视src/目录并仅为该特定文件运行babel任务?因为,如果我在src/path中有'n'个文件,脚本会重新生成所有的文件。

尝试使用此包https://www.npmjs.com/package/grunt-newer

我使用与您描述的方法类似的gulp等效