0

I'm using Grunt as a build tool (surprise) and I'm using grunt-contrib-copy as a debugging and early development tool. I've scouted my code multiple times, and I don't understand why I'm getting an error. Here is my code.

Gruntfile.js

module.exports = function(grunt) {
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        concurrent: {
            watch: {
                options: {
                    logConcurrentOutput: true
                },
                tasks: ['watch:css', 'watch:html', 'watch:js', 'watch:img']
            }
        },
        watch: {
            css: {
                files: ['./src/scss/*.scss'],
                tasks: ['sass', 'autoprefixer']
            },
            js: {
                files: ['./src/js/*.js'],
                tasks: ['copy:js']
            },
            html: {
                files: ['./src/html/*.html'],
                tasks: ['copy:html']
            },
            img: {
                files: ['./src/img/*'],
                tasks: ['copy:img']
            }
        },
        sass: {
            files: {
                expand: true,
                cwd: './src/scss/',
                src: ['*.scss'],
                dest: './src/no-prefix-css',
                ext: '.css'
            }
        },
        autoprefixer: {
            options: {
                browsers: ['all']
            },
            files: {
                expand: true,
                cwd: './src/no-prefix-css',
                src: ['*.css'],
                dest: './build/css/',
            }
        },





        // Affected code
        copy: {
            html: {
                files: {
                    expand: true,
                    cwd: './src/html/',
                    src: ['*.html'],
                    dest: './build/html/'
                }
            },
            js: {
                files: {
                    expand: true,
                    cwd: './src/js/',
                    src: ['*.js'],
                    dest: './build/js/'
                }
            },
            img: {
                files: {
                    expand: true,
                    cwd: './src/img/',
                    src: ['*.bmp', '*.jpg', '*.jpeg', '*.svg', '*.png'],
                    dest: './build/img/'
                }
            }
        }
    });

    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-sass');
    grunt.loadNpmTasks('grunt-contrib-copy');
    grunt.loadNpmTasks('grunt-autoprefixer');
    grunt.loadNpmTasks('grunt-concurrent');

    grunt.registerTask('default', 'Run build tools', ['concurrent:watch']);
}

Here is the output when I run grunt copy:html -v

Initializing
Command-line options: --verbose

Reading "Gruntfile.js" Gruntfile...OK

Registering Gruntfile tasks.
Reading package.json...OK
Parsing package.json...OK
Initializing config...OK

Registering "grunt-contrib-watch" local Npm module tasks.
Reading C:\Projects\tonal-music-visualizer\node_modules\grunt-contrib-watch\pack
age.json...OK
Parsing C:\Projects\tonal-music-visualizer\node_modules\grunt-contrib-watch\pack
age.json...OK
Loading "watch.js" tasks...OK
+ watch

Registering "grunt-contrib-sass" local Npm module tasks.
Reading C:\Projects\tonal-music-visualizer\node_modules\grunt-contrib-sass\packa
ge.json...OK
Parsing C:\Projects\tonal-music-visualizer\node_modules\grunt-contrib-sass\packa
ge.json...OK
Loading "sass.js" tasks...OK
+ sass

Registering "grunt-contrib-copy" local Npm module tasks.
Reading C:\Projects\tonal-music-visualizer\node_modules\grunt-contrib-copy\packa
ge.json...OK
Parsing C:\Projects\tonal-music-visualizer\node_modules\grunt-contrib-copy\packa
ge.json...OK
Loading "copy.js" tasks...OK
+ copy

Registering "grunt-autoprefixer" local Npm module tasks.
Reading C:\Projects\tonal-music-visualizer\node_modules\grunt-autoprefixer\packa
ge.json...OK
Parsing C:\Projects\tonal-music-visualizer\node_modules\grunt-autoprefixer\packa
ge.json...OK
Loading "autoprefixer.js" tasks...OK
+ autoprefixer

Registering "grunt-concurrent" local Npm module tasks.
Reading C:\Projects\tonal-music-visualizer\node_modules\grunt-concurrent\package
.json...OK
Parsing C:\Projects\tonal-music-visualizer\node_modules\grunt-concurrent\package
.json...OK
Loading "concurrent.js" tasks...OK
+ concurrent
Loading "Gruntfile.js" tasks...OK
+ default

Running tasks: copy:html

Running "copy:html" (copy) task
Verifying property copy.html exists in config...OK
Warning: undefined is not a function Use --force to continue.

Aborted due to warnings.
Zac Canoy
  • 102
  • 12

2 Answers2

2

Although this doesn't work:

    copy: {
        buildImages: {
            files: {
                    expand: true,
                    cwd: paths.dev.cssImages,
                    src: '**/*',
                    dest: paths.prod.cssImages,
                    filter: 'isFile'
                }
        },
        buildBlub: {
            files: {
                    expand: true,
                    cwd: paths.dev.cssImages,
                    src: '**/*',
                    dest: paths.prod.cssImages,
                    filter: 'isFile'
                }
        }
    }

..this does:

    copy: {
        buildImages: {
            files: [
                {
                    expand: true,
                    cwd: paths.dev.cssImages,
                    src: '**/*',
                    dest: paths.prod.cssImages,
                    filter: 'isFile'
                }
            ]
        },
        buildBlub: {
            files: [
                {
                    expand: true,
                    cwd: paths.dev.cssImages,
                    src: '**/*',
                    dest: paths.prod.cssImages,
                    filter: 'isFile'
                }
            ]
        }

    }

"files" simply expects an array of objects, why it works with a single object if only one copy task is given.. no clue :)

Pape
  • 291
  • 1
  • 9
0

I'm unsure of why this is, but for those with a similar problem, this is what was wrong with my code:

Copy didn't allow me to make sub-objects within it, like I thought was allowed for all Grunt commands. To solve my problem, I changed my code to look like the following:

copy: {
    default: {
        files: [
        {
            expand: true,
            cwd: './src/html/',
            src: ['*.html'],
            dest: './build/html/'
        },
        {
            expand: true,
            cwd: './src/js/',
            src: ['*.js'],
            dest: './build/js/'
        },
        {
            expand: true,
            cwd: './src/img/',
            src: ['*'],
            dest: './build/img/'
        }]
    }
}

Apparently copy requires a sub-object, but it can't handle multiple ones. If someone can provide a reason for this functionality, I would be extremely happy. It seems that either this could be considered a bug in grunt-contrib-copy, or my findings are incorrect. And I don't like either.

Zac Canoy
  • 102
  • 12