Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added sourcemaps generation while WP_DEBUG is true #107

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions lib/Compiler.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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());
}

Expand Down
23 changes: 18 additions & 5 deletions lib/Stylesheet.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -83,14 +85,17 @@ 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'
$this->source_path = WP_CONTENT_DIR . DIRECTORY_SEPARATOR . $lessfile_in_theme;
$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));
}

Expand Down Expand Up @@ -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
*
Expand All @@ -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
*
Expand Down