leafo/lessphp
leafo/lessphp is a PHP compiler for the LESS CSS language. Compile .less files to CSS in your apps or build scripts, with support for variables, mixins, nesting, imports, and other core LESS features, plus caching and CLI usage.
leafo/lessphp is a direct fit for Laravel applications requiring client-side LESS compilation without relying on Node.js or external tools (e.g., lessc). It enables server-side LESS-to-CSS conversion, reducing build complexity and improving performance by precompiling styles during runtime.mix, vite, or manual blade directives) but requires explicit integration since Laravel does not natively support LESS.ext-dom for advanced features), reducing deployment friction.@less) or middleware for on-demand compilation.less). Must choose one pipeline.storage/framework/views or Redis).@import rules, advanced variables) compared to Node’s less.sassc) or PostCSS, which have more active ecosystems.php-sass).lessphp as a singleton for global use.@less directive to compile LESS in templates.lessphp in Laravel’s app.blade.php or service provider.storage/app/less_cache/ with versioned filenames.node_modules/less and @import 'less' from webpack.mix.js.package.json to exclude LESS-related devDependencies.composer require leafo/lessphp.$less = new Less_Parser;
$less->parseFile('resources/less/styles.less');
$css = $less->getCss();
AppServiceProvider:
Blade::directive('less', function ($path) {
$less = new Less_Parser;
$less->parseFile(resource_path("less/{$path}"));
return "<?php echo \$less->getCss(); ?>";
});
<style>{{ @less('styles.less') }}</style>
$cacheKey = 'less:styles:' . filemtime(resource_path('less/styles.less'));
$css = Cache::remember($cacheKey, now()->addHours(1), function () use ($less) {
return $less->getCss();
});
Less_Parser constructor).@import rules (use manual @include or concatenate files).darken(), lighten()) may behave differently.tideways/xhprof or Laravel Debugbar.composer.json to avoid breaking changes.@import).try-catch to avoid 500 errors:
try {
$css = $less->getCss();
} catch (\Exception $e) {
Log::error("LESS compilation failed: {$e->getMessage()}");
$css = File::get(resource_path('css/fallback.css'));
}
memory_get_usage()).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| LESS syntax error | Broken CSS → visual regression | Fallback to precompiled CSS |
| PHP memory limit exceeded | Compilation fails | Increase memory_limit or optimize LESS |
| Cache corruption | Stale CSS served | Use versioned filenames or Redis TTLs |
| PHP version incompatibility | Package fails to load | Downgrade PHP or patch locally |
| High traffic spike | Increased server load | Rate-limit compilation or precompile |
How can I help you explore Laravel packages today?