在尝试组合我在网站上使用的所有预编译前端库之前,我需要检查文件是否存在。
这是我到目前为止的 gulpfile.js:
var elixir = require('laravel-elixir'),
fs = require('fs'),
assetsPath = './resources/assets/',
bowerPath = assetsPath + 'bower/',
cssPath = assetsPath + 'css/';
elixir(function(mix) {
var cssLibraries = [
bowerPath + 'bootstrap/dist/css/bootstrap.min.css',
bowerPath + 'font-awesome/css/font-awesome.min.css',
bowerPath + 'Ionicons/css/ionicons.min.css',
bowerPath + 'AdminLTE/dist/css/AdminLTE.min.css',
bowerPath + 'AdminLTE/dist/css/skins/skin-black-light.css',
bowerPath + 'iCheck/skins/flat/blue.css',
bowerPath + 'morris.js/morris.css',
bowerPath + 'AdminLTE/plugins/jvectormap/jquery-jvectormap-1.2.2.css',
bowerPath + 'bootstrap-datepicker/dist/css/bootstrap-datepicker3.min.css',
];
fs.access(cssPath + '000-processed.css', function(err) {
// if libraries are already combined I don't neet to do it again
if (!err) {
console.log('I will *NOT* combine pre-compiled CSS.');
return;
}
console.log('I will combine pre-compiled CSS.');
mix.combine(cssLibraries, cssPath + '000-processed.css');
});
mix.sass('**', cssPath + '001-sass.css');
});
尽管 mix 是一个有效对象并且 console.log 函数被执行,但 mix.combine() 函数永远不会被执行。
有人可以告诉我这是否是一个有效的策略以及为什么它不起作用?
最佳答案
您可以使用stat来检查Gulp中是否存在文件:
fs.stat('file.txt', function(err, stat) {
if(err == null) {
console.log('File exists');
} else {
console.log(err.code);
}
});
关于laravel - 在 Laravel Elixir 5.2 上调用任务之前检查文件是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34789592/