Mix/version images in Laravel 5.4?

耗尽温柔 提交于 2019-12-10 11:06:20

问题


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?


回答1:


I know it's an old question, but for 2019 - laravel mix 4 you can use:

mix.copy('resources/images/*', 'public/images');
mix.version();

This will version all your copied files. DON'T use

mix.copy('resources/images/*', 'public/images/*'); 
mix.copy('resources/images/', 'public/images/*'); 
mix.copyDirectory('resources/images/*', 'public/images'); 

-> it will not version the files then.

The see the result, take a look in the public/mix-manifest.json:

"/favicon.ico": "/favicon.ico?id=ecb5fdce0172885513c8",

To use it in code, use the laravel mix helper method: mix();

<link rel="icon" type="image/x-icon" href="{{ mix('favicon.ico') }}" />

which will generate something like this:

<link rel="icon" type="image/x-icon" href="/favicon.ico?id=ecb5fdce0172885513c8" />



回答2:


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.



来源:https://stackoverflow.com/questions/44157047/mix-version-images-in-laravel-5-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!