Grunt Copy Flatten not working as expected
Asked Answered
K

1

14

I have a directory structure as follows:

source/
    libraries/
        d3.js
        lodash.js
        //etc

I have grunt-copy setup as follows:

copy: {
  main: {
    files: [
      {
        src: ["source/libraries/*.js"], 
        dest: "build/", 
        flatten: true
      }

I expect it to flatten the output into build, so that I will have

build/
    d3.js
    //etc

Instead, I get a reproduction of the original directory structure in build:

build/
    source/
        libraries/
            d3.js
            //etc

What gives? Am I not using flatten properly?

Kinder answered 23/12, 2013 at 23:3 Comment(0)
P
21

Well, if you're only using flatten because you want everything in source/libraries to go into build, I would suggest actually using the cwd (current working directory) option instead. If, on the other hand, you actually have subfolders in source/libraries then you probably want that src line to be source/libraries/**/*.js.

In any case, if you can use cwd instead it would look like this:

copy: {
  main: {
    files: [
      {
        src: ["*.js"],
        dest: "build/",
        cwd: "source/libraries/"
      }
    ]
  }

For the other case, maybe this? (Notice the expand option set to true)

copy: {
  main: {
    files: [
      {
        src: ["source/libraries/**/*.js"],
        dest: "build/",
        flatten: true,
        expand: true
      }
    ]
  }
}
Paxwax answered 23/12, 2013 at 23:28 Comment(3)
Using cwd does fix my issue. However, based on the grunt docs, it looks like flatten should work too: "flatten Remove all path parts from generated dest paths." Do you know why it doesnt?Kinder
Did you try adding the expand option as well? Every time I've ever seen flatten I see it with expand.Paxwax
heh... just read up on the docs... right above that line you mention is this: "expand Set to true to enable the following options:" (flatten is one of the "following options"). ;)Paxwax

© 2022 - 2024 — McMap. All rights reserved.