node.jsgruntjsfile-copyinggrunt-contrib-copy

grunt custom task copying files that should be omitted


I have a pair of grunt copy tasks defined:

copy : {
    develop : {
        expand : true,
        flatten : false,
        cwd : "develop/",
        src : ["index.html", "gwt/**/*", "!**/*.less", "!**/*.scss", "modules/**/*", "components/**/*", "resources/**/*", "!resources/styles/**"],
        dest : "build/"
    },
    // TODO: figure out how to update glyphicon paths to build
    bootstrap_assets : {
        expand : true,
        flatten : false,
        src : ["bower_components/bootstrap-sass/assets/fonts/**/*"],
        dest : "build/"
    },
}

and combine them in the custom task

// Intermediate Task - Copy dev resources to build
grunt.registerTask("copy_dev", ["copy:develop", "copy:bootstrap_assets"]);

Inside the modules folder, I want to copy all the contents BUT the scss files. When I run grunt copy:develop, that has the behavior I expect, with all files except *.scss copying over. However, when I run grunt copy_dev, all the contents of modules gets copied over.

This is part of the output from grunt copy_dev --verbose:

Running "copy:develop" (copy) task
Verifying property copy.develop exists in config...OK
Files: develop/modules/front-page -> build/modules/front-page
Files: develop/modules/front-page/front-page.html -> build/modules/front-page/front-page.html
Files: develop/modules/front-page/front-page.scss -> build/modules/front-  page/front-page.scss
Files: develop/modules/login -> build/modules/login
Files: develop/modules/login -> build/modules/login
Files: develop/modules/login/bigLogo.png -> build/modules/login/bigLogo.png
Files: develop/modules/login/login-view.html -> build/modules/login/login-view.html
Files: develop/modules/login/login.js -> build/modules/login/login.js
Files: develop/modules/login/login.scss -> build/modules/login/login.scss
Files: develop/modules/login/logo -> build/modules/login/logo
Files: develop/modules/login/logo/lg.png -> build/modules/login/logo/lg.png
Files: develop/modules/login/logo/md.png -> build/modules/login/logo/md.png
Files: develop/modules/login/logo/sm.png -> build/modules/login/logo/sm.png

As you can see, there are a number of *.scss files being copied when executing the custom task, whereas they do not via the regular copy:$name call.

Why is this?


Solution

  • Your issue is that Grunt processes your src/dest directives from left to right to build the list of files to copy, rather than apply all rules at the same time (http://gruntjs.com/configuring-tasks#globbing-patterns), so:

    So you need to change your order if you want to prevent all less and scss from being copied:

    src : ["index.html", "gwt/**/*", "modules/**/*", "components/**/*", "resources/**/*", "!**/*.less", "!**/*.scss", "!resources/styles/**"]