Laravel-mix: mix.copy does not allow for the glob params.

Created on 14 Jan 2017  路  16Comments  路  Source: JeffreyWay/laravel-mix

laravel.mix.js

mix.copy('src/images/*{.png,.jpg}', 'dist/images');

Let's say I have 2 files:

/src/images/example.png
/src/images/example.jpg

when I run the npm run webpack it copies whole images dir into dist:

/dist/images/src/images/example.png
/dist/images/src/images/example.jpg

and should be:

/dist/images/example.png
/dist/images/example.jpg

Most helpful comment

I'm not sure about this fix - it means you can't copy a tree without it being flattened.

My solution is to use the 'context' copy-webpack-plugin option. Context allows you to specify the root that the copy is relative to - rather than implying it all from the 'from' arg.

Something like:

mix.copy2 = function (from, to, root) {
    var options = {
        from: from,
        to: Mix.Paths.root(to)
    };
    if (root) {
        options.context = root;
    }
    Mix.copy = (Mix.copy || []).concat(options);
};

All 16 comments

Hmm - maybe update to the latest version of Mix and try again. I just tested this out in a new project, and it worked. I did...

mix.copy('tests/Unit/*{.txt,.php}', 'somewhere/else');

...and it only copied txt and php files.

@JeffreyWay Nope!

Still having that issue on version 0.5.1.

I even tried to implement directory structure according to your example and ended up with this:

screen shot 2017-01-22 at 20 48 26

+1 same issue to me. v0.5.1

screen shot 2017-01-23 at 23 40 55

This is fixed now in #135.

I'm not sure about this fix - it means you can't copy a tree without it being flattened.

My solution is to use the 'context' copy-webpack-plugin option. Context allows you to specify the root that the copy is relative to - rather than implying it all from the 'from' arg.

Something like:

mix.copy2 = function (from, to, root) {
    var options = {
        from: from,
        to: Mix.Paths.root(to)
    };
    if (root) {
        options.context = root;
    }
    Mix.copy = (Mix.copy || []).concat(options);
};

Just had a very similar problem myself:

.copy('resources/assets/app/**/*.html', 'public/templates')

This ends up making a path like this:

public/templates/resources/assets/app/modelName/show.html'`

When, really, all I need (and what exlixir's .html method did) is this:

public/templates/modelName/show.html'

Looking at the examples, I suspect ( need to do something like this:

{ context: 'resources/assets/app', from: '**/*.html', to: 'public/templates' },

Out of interest... The copy-webpack-plugin offers brilliant reconfigurability and options; why do we wrap it up and take all that away with such an oversimplified and opinionated from/to/flatten setup?

Why not just pass Mix.copy params DIRECTLY to the plugin? Have I missed something?

I added the following to my webpack.mix.js:

mix.copyContext = function (from, to, root) {
  var options = {
    from: from,
    to: mix.config.Paths.root(to)
  };
  if (root) {
    options.context = root;
  }
  mix.config.copy = (mix.config.copy || []).concat(options);
  return this;
};

Notable differences to @ianhk 's one...

  • It uses the mix instance, no need to require Mix (which I has issues doing for some reason)... Is this ok, or is it rubbish?! ;)
  • It returns this so we can continue to chain it like all the other commands.

I still believe that altering copy to accept the same structure (basically param passthru) to the plugin makes far more sense + makes less code for you to maintain ;)

Didn't need to add to mix

mix.copy('resources/assets/*/{.woff,eot}',
'public/test');

This works for me (flattens but that's what I wanted). If you are still using elixer and want to preserve structure just run the same in elixer. I currently work in laravel 5.2 so I use elixer for legacy and useful features and mix for all the added ones. Have Laravel Mix 0.8.8

This is still an issue.

mix.copy('resources/assets/**/*.svg', './public/images')

and get

/public/resources/assets/images/[svgs]

expected:

/public/images/[svgs]

Proof:

mix.copy('resources/assets/images/test.svg', './public/images')

produces:

/public/images/test.svg

Same problem!!! why this issue is closed?

I think the problem you're describing is what is described in the documentation of laravel 5.4

When copying a directory, the copy method will flatten the directory's structure. To maintain the directory's original structure, you should use the copyDirectory method instead:

mix.copyDirectory('assets/img', 'public/img');

@Tonio31 The problem is that the copy method doesn't flatten directory's structure. see the example of @tfsimondouglas
Cause of this I am using 0.10 version not the latest.

Same problem here using v0.12.1

Hi guys, please get latest codes from master branch which solved this issue.

@jeshtan Revert back to v0.11.4 first and wait for v1.0 release.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rlewkowicz picture rlewkowicz  路  3Comments

mstralka picture mstralka  路  3Comments

wendt88 picture wendt88  路  3Comments

jpriceonline picture jpriceonline  路  3Comments

hasnatbabur picture hasnatbabur  路  3Comments