Skip to content

Instantly share code, notes, and snippets.

@bdanin
Created April 12, 2019 16:28
Show Gist options
  • Save bdanin/0c2c301ca3e8e9aa0be4947fc78bac98 to your computer and use it in GitHub Desktop.
Save bdanin/0c2c301ca3e8e9aa0be4947fc78bac98 to your computer and use it in GitHub Desktop.
Compiler written in PHP for SCSS files. Compile SASS with leafo/scssphp.
<?php
use Leafo\ScssPhp\Compiler;
require_once('vendor/leafo/scssphp/scss.inc.php');
$theme_root = 'docroot/themes/custom/THEME_NAME';
$scss_source = $theme_root . '/stylesheets';
$scss_contents = file_get_contents($scss_source . '/style.scss');
$css_dist = $theme_root . '/css';
// Make the CSS directory if needed
if ( !file_exists($css_dist) ) {
mkdir($css_dist, 0755);
}
$scss = new Compiler();
$scss->setLineNumberStyle(Compiler::SOURCE_MAP_INLINE);
$scss->setFormatter('Leafo\ScssPhp\Formatter\Expanded');
$scss->setImportPaths($scss_source . '/scss-imports/');
file_put_contents( $css_dist . '/compiled.css',
$scss->compile("
@import 'imports';
$scss_contents
")
);
echo "CSS compiled!\n";
exit;
@bdanin
Copy link
Author

bdanin commented Apr 12, 2019

To get this working, composer require leafo/scssphp, change the name of THEME_NAME to the theme's directory name. Make this executable and then it will compile the style.scss file inside the stylesheets directory and output the results to css/compiled.css (ignore this file in .gitignore). I put this file in a scripts directory in my theme.

Don't forget to have an _imports.scss file in the stylesheets directory to include assets such as custom variables and mixins.

See more at https://github.com/leafo/scssphp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment