I want to use mix on a set of images. First I copy them:
mix.copy('resources/images', 'public/images');
Then version:
mix.version();
The above does nothing to the images.
I've also tried specifying the path:
mix.version('public/images/*');
But I get a no such file or directory error.
How can I version the images?
version()
(without arguments) is not applied to files passed to copy()
and copyDirectory()
.
If you'll look at the source of mix.version
you'll see that it expands glob synchronously. But all laravel-mix
operations such as copy
and version
are executed asynchronously. This means that public/images/*
is empty because there are no files yet in public
directory.
As a workaround you can list files in source directory (from which you copy files, for example resources
), replace resources
path segment with public
and pass this list to version()
.
In my case I have various assets in resources
directory so directory tree looks like:
- resources
| - css
| - fonts
| - images
| - js
| - less
I need to copy to public
and version all these directories except less
which I need to preprocess and also version.
This is like my webpack.mix.js
looks like:
const mix = require('laravel-mix'),
glob = require('glob');
mix.disableNotifications();
const
directoriesToCopy = ['css', 'fonts', 'images', 'js'],
publicDir = 'public/',
publicCssDir = publicDir + 'css/',
resourcesDir = 'resources/',
resourcesLessDir = resourcesDir + 'less/',
lessFiles = glob.sync('**/*.less', {cwd: resourcesLessDir});
directoriesToCopy.forEach(d => mix.copyDirectory(resourcesDir + d, publicDir + d));
lessFiles.forEach(f => mix.less(resourcesLessDir + f, publicCssDir + f.slice(0, -'less'.length) + 'css'));
mix.version([].concat(...directoriesToCopy.map(d => glob.sync('**/*', {cwd: resourcesDir + d}).map(f => d + '/' + f))).map(f => publicDir + f));
Basically I use glob
to recursively get a list of all files in each copied directory, replace resources
with public
in their paths and then pass list of all such files to mix.version
.