bugo / scss-php
Pure PHP compiler for SCSS/Sass, fully compatible with the Dart Sass specification
Requires
- php: ^8.2
- ext-ctype: *
- bugo/iris: ^0.4
- psr/log: ^3.0
- psr/simple-cache: ^3.0
- symfony/filesystem: ^7|^8
Requires (Dev)
- axy/sourcemap: ^1.1
- bugo/sass-embedded-php: ^1.1
- bugo/scss-benchmark-utils: dev-main
- dg/bypass-finals: ^1.11
- friendsofphp/php-cs-fixer: ^3.95
- mockery/mockery: ^1.6.15
- monolog/monolog: ^3.10
- pestphp/pest: ^3|^4|^5
- phpstan/phpstan: ^2.2
- rector/rector: ^2.6
- scssphp/scssphp: ^2.1
- shyim/sasso-ffi: ^0.1.1
- symfony/cache: ^7|^8
- symfony/var-dumper: ^7|^8
- vimeo/psalm: ^6.16
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Features
- Sass and SCSS compilation to CSS
@use,@forward,@import, built-in Sass modules, and modern color functions- Optional source maps and rule splitting
- PSR-3 logging for
@debug,@warn, and@error - PSR-16 support for caching compiled files
Installation via Composer
composer require bugo/scss-php
Usage examples
Compiling from a string
<?php require __DIR__ . '/vendor/autoload.php'; use Bugo\SCSS\Compiler; use Bugo\SCSS\Parser\Syntax; $compiler = new Compiler(); // SCSS $scss = <<<'SCSS' @use 'sass:color'; $color: red; body { color: $color; } footer { background: color.adjust(#6b717f, $red: 15); } SCSS; $css = $compiler->compileString($scss); var_dump($css); // Sass $sass = <<<'SASS' @use 'sass:color'; $color: red; body color: $color; footer background: color.adjust(#6b717f, $red: 15); SASS; $css = $compiler->compileString($sass, Syntax::SASS); var_dump($css);
Compiling from a file
<?php require __DIR__ . '/vendor/autoload.php'; use Bugo\SCSS\Compiler; use Bugo\SCSS\CompilerOptions; use Bugo\SCSS\Loader; use Bugo\SCSS\Style; $compiler = new Compiler( options: new CompilerOptions(style: Style::COMPRESSED, sourceMapFile: 'assets/app.css.map'), loader: new Loader(['styles/']), ); $css = $compiler->compileFile(__DIR__ . '/assets/app.scss'); file_put_contents(__DIR__ . '/assets/app.css', $css); echo "CSS compiled!\n";
If sourceMapFile is set, the compiler writes the source map itself and appends a sourceMappingURL comment to the returned CSS.
CSS output profiles
DefaultOutputProfile uses the standard presentation rules for each output style. Use OutputProfiles::both(new OptimizedOutputProfile()) to opt into smaller CSS in both styles. This profile shortens exact color literals, removes proven redundant declarations, and merges equal selectors where the cascade permits. With source maps enabled, it uses the standard output rules.
In compressed style, the optimized profile also shortens validated box shorthands, zero lengths in known properties, and simple filter lists. For recognized color properties, it shortens complete rgb() and rgba() values when every channel and the alpha are exactly representable in hex.
Set only expanded: or only compressed: in OutputProfiles to use the default profile for the other style. Implement OutputProfileInterface to configure the rules for each style and optionally transform the assembled CSS. This example adds a banner in expanded style and selects OptimizedOutputProfile in compressed style.
use Bugo\SCSS\CompilerOptions; use Bugo\SCSS\Output\OptimizedOutputProfile; use Bugo\SCSS\Output\OutputProfileInterface; use Bugo\SCSS\Output\OutputProfileRules; use Bugo\SCSS\Output\OutputProfiles; use Bugo\SCSS\Style; final readonly class BannerProfile implements OutputProfileInterface { public function rulesFor(Style $style): OutputProfileRules { return new OutputProfileRules( transformCss: static fn(string $css): string => "/*! My application */\n" . $css, dartSassCompatibility: $style === Style::COMPRESSED, ); } public function cacheKey(Style $style): string { return 'my-application-banner:1'; } } $options = new CompilerOptions( style: Style::COMPRESSED, profiles: new OutputProfiles( expanded: new BannerProfile(), compressed: new OptimizedOutputProfile(), ), );
Use OutputProfiles::both(new BannerProfile()) to select the same profile for both styles. To reuse the built-in optimizations in a custom profile, set optimizeStyleRules for the desired styles and enable compressStaticValues alongside it for compressed output. For cached compilation, return a distinct cacheKey() for every configuration or revision that changes the output. The callback receives assembled CSS before charset and source-map metadata are added.
Caching compiled files with PSR-16
CachingCompiler caches only compileFile(). It tracks every loaded dependency and invalidates the cache if any imported or used file changes.
<?php require __DIR__ . '/vendor/autoload.php'; use Bugo\SCSS\Cache\CachingCompiler; use Bugo\SCSS\Cache\TrackingLoader; use Bugo\SCSS\Compiler; use Bugo\SCSS\CompilerOptions; use Bugo\SCSS\Loader; use Bugo\SCSS\Style; use Symfony\Component\Cache\Adapter\FilesystemAdapter; use Symfony\Component\Cache\Psr16Cache; $options = new CompilerOptions( style: Style::COMPRESSED, sourceMapFile: __DIR__ . '/assets/app.css.map', ); $trackingLoader = new TrackingLoader(new Loader([__DIR__ . '/styles'])); $compiler = new Compiler($options, $trackingLoader); $cache = new Psr16Cache(new FilesystemAdapter(namespace: 'scss', directory: __DIR__ . '/var/cache/scss')); $cachedCompiler = new CachingCompiler( $compiler, $cache, $trackingLoader, $options, ); $css = $cachedCompiler->compileFile(__DIR__ . '/assets/app.scss'); file_put_contents(__DIR__ . '/assets/app.css', $css);
Notes:
- Pass the same
TrackingLoaderinstance to bothCompilerandCachingCompiler. compileString()is delegated directly and is not cached.psr/simple-cacheis included by this package, but you still need a PSR-16 cache implementation such assymfony/cache, Laravel cache, or another compatible backend.
CompilerOptions reference
| Option | Type | Default | Description |
|---|---|---|---|
style |
Style |
Style::EXPANDED |
Output style: EXPANDED or COMPRESSED |
sourceFile |
string |
'input.scss' |
Source file name used in source maps |
outputFile |
string |
'output.css' |
Output file name used in source maps |
sourceMapFile |
?string |
null |
Path to write the source map file; null disables source maps |
includeSources |
bool |
false |
Embed source content in source map (sourcesContent) |
verboseLogging |
bool |
false |
Log all @debug messages (otherwise only @warn/@error) |
Logging @debug, @warn, @error with any PSR-3 logger
<?php require __DIR__ . '/vendor/autoload.php'; use Bugo\SCSS\Compiler; use Monolog\Formatter\LineFormatter; use Monolog\Handler\StreamHandler; use Monolog\Logger; $formatter = new LineFormatter("[%datetime%] %level_name%: %message%\n"); $handler = new StreamHandler('php://stdout'); $handler->setFormatter($formatter); $logger = new Logger('sass'); $logger->pushHandler($handler); // Inject logger in constructor $compiler = new Compiler(logger: $logger); $scss = <<<'SCSS' @debug "Build started"; @warn "Using deprecated token"; // @error "Fatal style issue"; .button { color: red; } SCSS; $css = $compiler->compileString($scss); echo $css;
Notes:
@debug->$logger->debug(...)@warn->$logger->warning(...)@error->$logger->error(...)and compilation throwsBugo\SCSS\Exceptions\SassErrorException
Comparison with other packages
See the benchmark.md file for results.
benchmark.php includes both the regular bugo/scss-php compiler and a separate bugo/scss-php+cache scenario so you can compare repeated compileFile() runs with warm cache hits.
Found a bug?
Paste the problematic code into the sandbox, then send:
- the sandbox link
- the actual result from this package
- the expected result
Want to add something?
Don't forget to test and tidy up your code before submitting a pull request.