Anonymous Penguin
Anonymous Penguin

Reputation: 2137

How to make grunt-contrib-copy not copy LESS files?

I have the following Gruntfile:

module.exports = function(grunt) {
    //Project configuration.
    grunt.initConfig({      
        copy: {
            main: {
                files: [
                    {
                        expand: true,
                        cwd: "source/",
                        src: ["!**/*.less", "**"],
                        dest: "compiled/"
                    },
                ],
            },
        },
    });

    grunt.loadNpmTasks("grunt-contrib-copy");
    grunt.registerTask("default", ["copy"]);
};

My goal is to have it copy everything from the source folder to the compiled folder. For example, if this was my file structure before running Grunt...

[root]
 - Gruntfile.js
 - source
    \- test.txt
    \- sample.html
    \- file.less
 - compiled

...I am expecting to get...

[root]
 - Gruntfile.js
 - source
    \- test.txt
    \- sample.html
    \- file.less
 - compiled
    \- test.txt
    \- sample.html

...but instead I get:

[root]
 - Gruntfile.js
 - source
    \- test.txt
    \- sample.html
    \- file.less
 - compiled
    \- test.txt
    \- sample.html
    \- file.less

I thought that setting the source to ["!**/*.less", "**"] would fix this, but that isn't the case. Why doesn't this work (what is it actually targeting) and how can I fix this?

Upvotes: 0

Views: 96

Answers (1)

steveax
steveax

Reputation: 17753

Negating patterns should be placed after matching patterns since they negate the previous match:

copy: {
  main: {
    files: [
      {
        expand: true,
        cwd: "source/",
        src: ["**", "!**/*.less"],
        dest: "compiled/"
      },
    ],
  },
}

See the globbing patterns docs for examples, like:

// All files in alpha order, but with bar.js at the end.
{src: ['foo/*.js', '!foo/bar.js', 'foo/bar.js'], dest: ...}

By placing "**" after your negating pattern, you're overriding it.

Upvotes: 1

Related Questions