/** * Create a new css file based on sass file * @param string $sourceFilePath * @param string $targetFilenamePath * @throws Exception */ public function createNewCss($sourceFilePath, $targetFilenamePath, $importFallback = null) { $config = $this->_getConfig(); $targetDir = dirname($targetFilenamePath); $this->_createDir($targetDir); $this->_createDir($config['cache_dir']); if ($config['use_ruby']) { $options = '--cache-location ' . $config['cache_dir'] . ' --style ' . $config['output_style']; if ($config['debug']) { $options .= ' --debug-info --line-numbers'; } $command = $config['sass_command'] . ' ' . $options . ' ' . $sourceFilePath . ':' . $targetFilenamePath; $execResult = exec($command, $output); if ($execResult != '') { throw new Exception("Error while processing sass file with command '{$command}':\n" . implode("\n", $output)); } } else { /** @var \Leafo\ScssPhp\Compiler $compiler */ $compiler = new \Leafo\ScssPhp\Compiler(); switch ($config['output_style']) { case Laurent_Sass_Model_Config_Style::STYLE_COMPACT: default: $formatter = \Leafo\ScssPhp\Formatter\Compact::class; break; case Laurent_Sass_Model_Config_Style::STYLE_NESTED: $formatter = \Leafo\ScssPhp\Formatter\Nested::class; break; case Laurent_Sass_Model_Config_Style::STYLE_COMPRESSED: $formatter = \Leafo\ScssPhp\Formatter\Compressed::class; break; case Laurent_Sass_Model_Config_Style::STYLE_EXPANDED: $formatter = \Leafo\ScssPhp\Formatter\Expanded::class; break; } if (Mage::getIsDeveloperMode()) { $compiler->setLineNumberStyle(Leafo\ScssPhp\Compiler::LINE_COMMENTS); } $compiler->setFormatter($formatter); $compiler->setImportPaths(array(dirname($sourceFilePath), Mage::getBaseDir('lib') . '/scssphp/stylesheets', $importFallback)); file_put_contents($targetFilenamePath, $compiler->compile(sprintf('@import "%s"', basename($sourceFilePath)))); } }
public function run() { include getcwd() . '/vendor/autoload.php'; $config = (include getcwd() . '/config/scss.php'); $source = ''; echo "Loading files...\n"; foreach ($config['scssFiles'] as $file) { echo " {$file}\n"; $source .= file_get_contents(getcwd() . '/' . $file); } $importPaths = []; echo "Setting import paths...\n"; foreach ($config['importPaths'] as $path) { echo " {$path}\n"; $importPaths[] = getcwd() . '/' . $path; } $scss = new \Leafo\ScssPhp\Compiler(); $scss->setImportPaths($importPaths); # build a compressed version first echo "Writing production.css...\n"; $scss->setFormatter('Leafo\\ScssPhp\\Formatter\\Crunched'); $result = $scss->compile($source); $prodOutput = getcwd() . '/' . $config['outputPath'] . '/production.css'; if (file_exists($prodOutput) === true) { unlink($prodOutput); } file_put_contents($prodOutput, $result); # now build a debug version echo "Writing debug.css...\n"; $scss->setFormatter('Leafo\\ScssPhp\\Formatter\\Expanded'); $scss->setLineNumberStyle(\Leafo\ScssPhp\Compiler::LINE_COMMENTS); $result = $scss->compile($source); $debugOutput = getcwd() . '/' . $config['outputPath'] . '/debug.css'; if (file_exists($debugOutput) === true) { unlink($debugOutput); } file_put_contents($debugOutput, $result); echo "Complete.\n"; }
$path = realpath(__DIR__ . "/" . preg_replace("/(\\.min)(\\.scss)\$/i", "\$2", $url)); if (!is_string($path) || strpos($path, __DIR__) !== 0 || !is_string($content = file_get_contents($path))) { header("HTTP/1.1 404 Not Found"); echo "404 Not Found"; } $debugging = !isset($_GET["debug"]) || $_GET["debug"] == "true" ? true : false; $cache = $debugging === true || !isset($_GET["cache"]) || $_GET["cache"] == "false" ? false : true; $minified = strtolower(substr($url, -9, 4)) == ".min" ? true : false; // Get a cached copy $cachepath = $realpath . "-" . md5($content) . ($minified ? ".min" : "") . ".css"; if ($cache === true && is_string($compiled = file_get_contents($cachepath))) { header("Content-Type: text/css"); echo $compiled; } $scss = new Leafo\ScssPhp\Compiler(); $scss->setImportPaths(array(__DIR__, dirname($path))); if ($minified === true) { // Minified output? $scss->setFormatter("Leafo\\ScssPhp\\Formatter\\Crunched"); } if ($debugging === true) { // Debugging output? $scss->setLineNumberStyle(Leafo\ScssPhp\Compiler::LINE_COMMENTS); } $compiled = $scss->compile($content, $path); if ($cache === true) { file_put_contents($cachepath, $compiled); } // Output the compiled content as css header("Content-Type: text/css"); echo $compiled;
function nebula_render_scss($specific_scss = null, $child = false) { $override = apply_filters('pre_nebula_render_scss', false, $specific_scss, $child); if ($override !== false) { return $override; } if (nebula_option('nebula_scss', 'enabled') && (isset($_GET['sass']) || isset($_GET['scss']) || $_GET['settings-updated'] == 'true') && (is_dev() || is_client())) { $specific_scss = 'all'; } $theme_directory = get_template_directory(); $theme_directory_uri = get_template_directory_uri(); if ($child) { $theme_directory = get_stylesheet_directory(); $theme_directory_uri = get_stylesheet_directory_uri(); } $stylesheets_directory = $theme_directory . '/stylesheets'; $stylesheets_directory_uri = $theme_directory_uri . '/stylesheets'; require_once get_template_directory() . '/includes/libs/scssphp/scss.inc.php'; //SCSSPHP is a compiler for SCSS 3.x $scss = new \Leafo\ScssPhp\Compiler(); $scss->addImportPath($stylesheets_directory . '/scss/partials/'); if (nebula_option('nebula_minify_css', 'enabled') && !is_debug()) { $scss->setFormatter('Leafo\\ScssPhp\\Formatter\\Compressed'); //Minify CSS (while leaving "/*!" comments for WordPress). } else { $scss->setFormatter('Leafo\\ScssPhp\\Formatter\\Compact'); //Compact, but readable, CSS lines if (is_debug()) { $scss->setLineNumberStyle(\Leafo\ScssPhp\Compiler::LINE_COMMENTS); //Adds line number reference comments in the rendered CSS file for debugging. } } if (empty($specific_scss) || $specific_scss == 'all') { //Partials $latest_partial = 0; foreach (glob($stylesheets_directory . '/scss/partials/*') as $partial_file) { if (filemtime($partial_file) > $latest_partial) { $latest_partial = filemtime($partial_file); } } //Combine Developer Stylesheets if (nebula_option('nebula_dev_stylesheets')) { nebula_combine_dev_stylesheets($stylesheets_directory, $stylesheets_directory_uri); } //Compile each SCSS file foreach (glob($stylesheets_directory . '/scss/*.scss') as $file) { //@TODO "Nebula" 0: Change to glob_r() but will need to create subdirectories if they don't exist. $file_path_info = pathinfo($file); if (is_file($file) && $file_path_info['extension'] == 'scss' && $file_path_info['filename'][0] != '_') { //If file exists, and has .scss extension, and doesn't begin with "_". $file_counter++; $css_filepath = $file_path_info['filename'] == 'style' ? $theme_directory . '/style.css' : $stylesheets_directory . '/css/' . $file_path_info['filename'] . '.css'; if (!file_exists($css_filepath) || filemtime($file) > filemtime($css_filepath) || $latest_partial > filemtime($css_filepath) || is_debug() || $specific_scss == 'all') { //If .css file doesn't exist, or is older than .scss file (or any partial), or is debug mode, or forced ini_set('memory_limit', '512M'); //Increase memory limit for this script. //@TODO "Nebula" 0: Is this the best thing to do here? Other options? WP_Filesystem(); global $wp_filesystem; $existing_css_contents = file_exists($css_filepath) ? $wp_filesystem->get_contents($css_filepath) : ''; if (!strpos(strtolower($existing_css_contents), 'scss disabled')) { //If the correlating .css file doesn't contain a comment to prevent overwriting $this_scss_contents = $wp_filesystem->get_contents($file); //Copy SCSS file contents $compiled_css = $scss->compile($this_scss_contents); //Compile the SCSS $enhanced_css = nebula_scss_variables($compiled_css); //Compile server-side variables into SCSS $wp_filesystem->put_contents($css_filepath, $enhanced_css); //Save the rendered CSS. } } } } if (!$child && is_child_theme()) { //If not in the second (child) pass, and is a child theme. nebula_render_scss($specific_scss, true); //Re-run on child theme stylesheets } } else { if (file_exists($specific_scss)) { //If $specific_scss is a filepath WP_Filesystem(); global $wp_filesystem; $scss_contents = $wp_filesystem->get_contents($specific_scss); $compiled_css = $scss->compile($scss_contents); //Compile the SCSS $enhanced_css = nebula_scss_variables($compiled_css); //Compile server-side variables into SCSS $wp_filesystem->put_contents(str_replace('.scss', '.css', $specific_scss), $enhanced_css); //Save the rendered CSS in the same directory. } else { //If $scss_file is raw SCSS string $compiled_css = $scss->compile($specific_scss); return nebula_scss_variables($compiled_css); //Return the rendered CSS } } }