From 4e8d424f87dfa86d2d35fe871eb9c84eb288fa9e Mon Sep 17 00:00:00 2001 From: marek Date: Sun, 6 Mar 2016 23:54:58 +0100 Subject: [PATCH] added sourcemaps generation while WP_DEBUG is true --- lib/Compiler.class.php | 38 ++++++++++++++++++++++++++++++++++++++ lib/Stylesheet.class.php | 23 ++++++++++++++++++----- 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/lib/Compiler.class.php b/lib/Compiler.class.php index ca61bcb..acf315d 100644 --- a/lib/Compiler.class.php +++ b/lib/Compiler.class.php @@ -11,6 +11,24 @@ */ class WPLessCompiler extends lessc { + + private $compilerOpts = [ + 'sourceMap' => false,// whether to output a source map + 'sourceMapBasepath' => null, + 'sourceMapWriteTo' => null, + 'sourceMapURL' => null, + ]; + + protected function setOptions(array $opts){ + $this->compilerOpts = $opts; + } + + protected function getOptions(){ + $lesscOptions = parent::getOptions(); + $options = array_merge($lesscOptions, $this->compilerOpts); + return $options; + } + /** * Instantiate a compiler * @@ -71,6 +89,17 @@ public function cacheStylesheet(WPLessStylesheet $stylesheet, $force = false) $compiled_cache = get_transient($cache_name); if( !$force && !file_exists( $stylesheet->getTargetPath() ) ) $force = true; + wp_mkdir_p(dirname($stylesheet->getTargetPath())); + + if(defined('WP_DEBUG') && WP_DEBUG) { + $this->setOptions([ + 'sourceMap' => true,// whether to output a source map + 'sourceMapBasepath' => get_template_directory(), + 'sourceMapRootpath' => get_template_directory_uri(), + 'sourceMapWriteTo' => $stylesheet->getTargetMapPath(), + 'sourceMapURL' => $stylesheet->getTargetMapUri(), + ]); + } $compiled_cache = $this->cachedCompile($compiled_cache ? $compiled_cache : $stylesheet->getSourcePath(), $force); @@ -104,6 +133,15 @@ public function saveStylesheet(WPLessStylesheet $stylesheet, $css = null) if ($css === null) { + if(defined('WP_DEBUG') && WP_DEBUG) { + $this->setOptions([ + 'sourceMap' => true,// whether to output a source map + 'sourceMapBasepath' => get_template_directory(), + 'sourceMapRootpath' => get_template_directory_uri(), + 'sourceMapWriteTo' => $stylesheet->getTargetMapPath(), + 'sourceMapURL' => $stylesheet->getTargetMapUri(), + ]); + } $css = $this->compileFile($stylesheet->getSourcePath()); } diff --git a/lib/Stylesheet.class.php b/lib/Stylesheet.class.php index 5164632..bba14b4 100644 --- a/lib/Stylesheet.class.php +++ b/lib/Stylesheet.class.php @@ -17,7 +17,9 @@ class WPLessStylesheet $source_timestamp, $source_uri, $target_path, - $target_uri; + $target_map_path, + $target_uri, + $target_map_uri; public static $upload_dir, $upload_uri; @@ -61,12 +63,12 @@ public function __construct(_WP_Dependency $stylesheet, array $variables = array * @version 1.0 * @return string */ - public function computeTargetPath() + public function computeTargetPath($ext = 'css') { $target_path = preg_replace('#^'.get_theme_root_uri().'#U', '', $this->stylesheet->src); $target_path = preg_replace('/.less$/U', '', $target_path); - $target_path .= '-%s.css'; + $target_path .= '-%s.'.$ext; return apply_filters('wp-less_stylesheet_compute_target_path', $target_path); } @@ -83,6 +85,7 @@ public function computeTargetPath() protected function configurePath() { $target_file = $this->computeTargetPath(); + $target_map_file = $this->computeTargetPath('map'); // path to local "wp-content" dir does not match URI in multisite. $wp_content_dir = str_replace(ABSPATH, '', WP_CONTENT_DIR); // get the 'wp-content' part of the path $lessfile_in_theme = preg_replace ('#^.*?' . DIRECTORY_SEPARATOR . $wp_content_dir . DIRECTORY_SEPARATOR . '(.*)$#', '$1', $this->stylesheet->src, 1); // the part after 'wp-content' @@ -90,7 +93,9 @@ protected function configurePath() $this->source_uri = $this->stylesheet->src; $this->target_path = self::$upload_dir.$target_file; $this->target_uri = self::$upload_uri.$target_file; - + $this->target_map_path = self::$upload_dir.$target_map_file; + $this->target_map_uri = self::$upload_uri.$target_map_file; + $this->setSourceTimestamp(filemtime($this->source_path)); } @@ -161,7 +166,11 @@ public function getTargetPath() { return sprintf($this->target_path, $this->signature); } - + + public function getTargetMapPath() + { + return sprintf($this->target_map_path, $this->signature); + } /** * Returns target URI * @@ -177,6 +186,10 @@ public function getTargetUri() return sprintf($this->target_uri, $this->signature); } + public function getTargetMapUri() + { + return sprintf($this->target_map_uri, $this->signature); + } /** * Tells if compilation is needed *