Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Css Compress Laravel Package

wyrihaximus/css-compress

A lightweight PHP library for minifying CSS, stripping comments, whitespace, and redundant characters to shrink stylesheets and speed up page loads. Easy to drop into any PHP or Laravel project, it produces smaller CSS output with minimal setup and no external dependencies.

View on GitHub
Deep Wiki
Context7

Getting Started

This is a small, single-purpose runtime CSS minifier — not an asset pipeline. It takes a CSS string and returns a smaller CSS string. Treat it as a library you call, not a Laravel package you install and forget.

composer require wyrihaximus/css-compress

There is no service provider, no facade, no config file, and no auto-discovery. Open vendor/wyrihaximus/css-compress/src first — the entire public surface is one or two classes (a factory and a compressor with a compress(string $css): string-style method). The README is short; the source is the documentation.

Bind it once so you aren't constructing it per call:

// app/Providers/AppServiceProvider.php
public function register(): void
{
    $this->app->singleton(\WyriHaximus\CssCompress\Factory::class, fn () => /* factory or compressor */);
}

First real use case: you already generate CSS somewhere at runtime (theme settings in the database, per-tenant branding, critical-CSS for emails) and you want it smaller before it hits disk or the response. That is the sweet spot. If your CSS comes from resources/css and a build step, stop here and use Vite — it already minifies.

$css = $compressor->compress($rawCss);

Storage::disk('public')->put('themes/acme.css', $css);

Implementation Patterns

Cache aggressively, minify once. Minification is pure PHP string work — cheap per call, but pointless per request.

$css = Cache::remember(
    'css.theme.' . $theme->id . '.' . $theme->updated_at->timestamp,
    now()->addDay(),
    fn () => $compressor->compress($theme->buildCss())
);

Key on a hash of the source (or the model's updated_at) so edits invalidate naturally. Never key on the theme id alone.

Serve it from a route with real cache headers. For per-tenant CSS you can't precompile:

Route::get('/t/{tenant}/theme.css', function (Tenant $tenant) {
    $css = Cache::remember("css.{$tenant->id}." . md5($tenant->css), 86400,
        fn () => app(Compressor::class)->compress($tenant->css));

    return response($css, 200, [
        'Content-Type' => 'text/css; charset=utf-8',
        'Cache-Control' => 'public, max-age=86400',
        'ETag' => '"' . md5($css) . '"',
    ]);
});

Skip minification outside production. Keep raw output in local so you can read stack traces against unminified CSS, and keep line numbers in dev:

$css = app()->isProduction() ? $compressor->compress($css) : $css;

Artisan command for precompiled output. If the CSS only changes on deploy or on save, write it to public/ from a command or an observer, then let nginx serve it statically. This is the pattern that scales — the route approach above is for genuinely dynamic cases.

Blade integration. Use a small component or directive for inline critical CSS rather than {!! !!} everywhere:

@php($critical = \Illuminate\Support\Facades\Cache::remember(
    'critical.' . md5($criticalSource), 3600, fn () => app(Compressor::class)->compress($criticalSource)
))
<style>{!! $critical !!}</style>

If it's injected into a <style> block, you don't need to escape it as HTML — but do not reuse that same string in an attribute.

Pair with a test. Keep one fixture of "CSS that must survive" (custom properties, calc(), data URIs, content: " ") and assert on the output. This is your upgrade canary for a low-traffic dependency.

Gotchas and Tips

  • It will not do what Vite does. No bundling, no @import resolution, no autoprefixing, no source maps, no PostCSS/Tailwind. It is whitespace-and-comments level minification. Do not try to replace your build step with it.
  • Test calc() and custom properties first. Aggressive whitespace stripping is where minifiers break: calc(100% - 10px) needs its spaces around -, and content: " " must keep the quoted space. Run your real CSS through it before trusting it in production, not after.
  • Important comments. Verify whether /*! ... */ license banners are preserved. If your theme CSS bundles third-party fonts or icons under a license requiring attribution, check the output — don't assume.
  • Minify once, not twice. Don't run the output back through the compressor on every layer (middleware + route + command). It's usually idempotent, but the failure mode is confusing and the CPU is wasted.
  • Memory and speed. It's PHP regex/string work on one big string. A few hundred KB is fine; multi-MB generated CSS in a request cycle is not. Move that to a queued job writing to storage.
  • Debugging output diffs. When the compressed CSS looks wrong, diff input against output and bisect. If the package exposes its internal compressor chain, disable individual rules to isolate the culprit rather than reading the regexes.
  • No Laravel version constraints to lean on. With zero dependents and a low star count, you are effectively the QA. Pin the version ("wyrihaximus/css-compress": "~x.y"), commit the lock file, and keep the fixture test green when you bump.
  • Extension points are in the underlying compressor abstraction, not in Laravel. If you need a custom rule (say, stripping a specific vendor prefix), you'll add it to the compressor chain rather than hooking into an event — there are no events here.
  • Don't ship it if Vite covers you. For 90% of Laravel apps the honest answer is "your existing build already minifies." Reserve this package for runtime-generated CSS: multi-tenant themes, user-authored styles, email templates, and inline critical CSS computed per request.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
microsoft/fluentui-system-icons
mmccook/php-json-canonicalization-scheme
middlewares/firewall
qubiqx/montapacking
nyoncode/laravel-package-console
pnscripts/filament-broadquill-lite
pnscripts/laravel-sentfold-lite
vleap/mx-sdk-laravel-internal
twidpay/utils
htetmyathlaing/scout-elasticsearch-driver
artisan-toolbox/front-awesome
ls/client-assistant
users-au/laravel-client
cgv/localized-resource
noith/settings
kda/laravel-package
kda/filament-dynamic-navigation
bypersoft/signbox
pairus/product-data
fayne/laravel-filament-cms-core