version()(不带参数)不适用于传递给copy() 和copyDirectory() 的文件。
如果您查看mix.version 的源代码,您会发现它同步扩展 glob。但所有laravel-mix 操作,如copy 和version 都是异步执行的。这意味着public/images/* 为空,因为public 目录中还没有文件。
作为一种解决方法,您可以列出源目录中的文件(从中复制文件,例如resources),将resources 路径段替换为public 并将此列表传递给version()。
在我的情况下,我在 resources 目录中有各种资产,所以目录树看起来像:
- resources
| - css
| - fonts
| - images
| - js
| - less
我需要复制到public 和版本所有这些目录除了less 我需要预处理和版本。
这就像我的webpack.mix.js 看起来像:
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));
基本上我使用glob 递归地获取每个复制目录中所有文件的列表,在它们的路径中将resources 替换为public,然后将所有此类文件的列表传递给mix.version。