Search code examples
gruntjsbabeljsgrunt-babel

grunt-babel not working as multi-task


The following config works as expected, but when the //build: { stuff is uncommented it either silently fails, or it does something unexpected to me.

    babel: {
        //build: {
            options: {
                sourceMap: true,
                presets: ['es2015']
            },
            dist: {
                files: [{
                    expand: true,
                    cwd: 'build/src/app',
                    src: ['**/*.js'],
                    dest: 'build/src/es5-app'
                }]
            }
        //}
    },

So, with //build: { commented out, the es5-app directory is created at build/src, but with //build: { uncommented, the directory is not created. In both instances grunt is run as grunt babel, and it returns Done, without errors.


Solution

  • Since grunt-babel is registered as a multi task, dist is actually the name of the target, with files being at the first level of the config. So when you run babel without build, it's actually running babel:dist (which you should see in the log).

    For it to work the way you want, you would need something like the following:

    babel: {
        options: {
            sourceMap: true,
            presets: ['es2015']
        },
        dist: {
            files: [{
                expand: true,
                cwd: 'build/src/app',
                src: ['**/*.js'],
                dest: 'build/src/es5-app'
            }]
        }
        build: {
            files: [{
                expand: true,
                cwd: 'build/src/app/test',
                src: ['test/**/*.js'],
                dest: 'build/test/es5-app'
            }]
        }
    },
    

    This would allow you to run either babel:dist or babel:build.

    See http://gruntjs.com/creating-tasks for more information on multi tasks.