Laravel Help
rajivseelam opened this issue · comments
Hi,
Though I have been using this in each and every project, I haven't been able to successfully minify and concatenate using this package.
Whenever I minify js it throws errors of JSLint etc.,
Whenever I minify css, relative paths don't work well. I usually download themes from themeforest, so the folder organization of those files varies a lot. Usage of slider plugins like revolution slider, font-awesome etc., are common.
I use the following fetch command:
'fetch_command' => function ($asset) {
$content = file_get_contents($asset);
$regex = "/\burl\(\s?[\'\"]?(([^';]+)\.(jpg|eot|jpg|jpeg|gif|png|ttf|woff|svg|otf).*?)[\'\"]?\s?\)/";
$prefix = '/'.str_replace(public_path() . '/', '', dirname($asset)) . '/';
$filter = function ($match) use ($prefix) {
// Do not process absolute URLs
if('http://' === substr($match[1], 0, 7) or 'https://' === substr($match[1], 0, 8) or '//' === substr($match[1], 0, 2))
return $match[0];
// Add your filter logic here
return 'url(\''.$prefix.$match[1] . '\')';
};
// Apply filter
return preg_replace_callback($regex, $filter, $content);
},
I have developed from this one of your examples, the original one doesn't work for, and my one works partially as in, I have even tried only concatenation and no minification with following setting:
'no_minification_regex' => '/.[-.]((min\.css)|js|min\.js|css)*$/i',
With this my pages at least load, but some parts of my html don't work.
Hi @rajivseelam.
Since a few months ago you can provide your own minifier for both CSS and JS using the css_minifier and js_minifier configuration options.
The options need to be a closure that will receive as the unique parameter the content of the asset file that needs to minified. i.e:
'css_minifier' => function ($originalContent) {
$minifier = new MyCustomMinifier();
return $minifier->minifyCss($originalContent);
},
The fetch_command
approach is good for very basic processing but when your assets need more complex pre-processing it doesn't scale up. Find a solid library that can handle the specific needs of your assets and replace the default minifiers with that. I'm not familiar with any other library so I can't recommend one but I'm sure there are plenty of them.