thom4parisot / wp-less

WordPress plugin which seemlessly compiles, caches and rebuilds your LESS stylesheets.
https://wordpress.org/plugins/wp-less/
88 stars 40 forks source link

Minify Less output #87

Closed pravdomil closed 9 years ago

thom4parisot commented 9 years ago

You can hook on wp-less_init and do something like:

add_action('wp-less_init', 'configure_wp_less');

function configure_wp_less($WPLess) {
  $WPLess->compiler->setFormatter('...');
}

Otherwise please provide more ample reasons than a pretty much impolite please merge.

pravdomil commented 9 years ago

Well I got with your snippet: cannot access protected property wplessplugin::$compiler

thom4parisot commented 9 years ago

@Pravdomil so instead of $WPLess->compiler use the public method $WPLess->getCompiler(). I reckon it is not documented but this is something you can look out by browsing the Plugin.php class.

pravdomil commented 9 years ago

OK, thanks for support.

The last issue, if I switched to oyejorge less compiler and use $WPLess->getCompiler->setFormatter it doesn't work.

In lessc->compileFile function is Less_Parser always loaded with no args so compression cannot be applied. I found workaround for it:

// turn on compression for oyejorge less compiler
add_action('wp-less_compiler_construct_pre', function($compiler) {
    Less_Parser::$options['compress'] = true;
});

I use it here https://github.com/Pravdomil/wp-pravdomil/blob/master/less.php#L26

thom4parisot commented 9 years ago

Well, setFormatter exists in oyejorge flavour of wp-less: https://github.com/oyejorge/less.php/blob/master/lessc.inc.php#L38

Your workaround seems legit, it seems to be enabled if the compressed formatter is used (cf. https://github.com/oyejorge/less.php/blob/master/lessc.inc.php#L81)

pravdomil commented 9 years ago

I download the latest version from https://wordpress.org/plugins/wp-less/ and it seems that oyejorge library is older then master on Github. Comparing compileFile function.

wp-less/vendor/oyejorge/less.php/lessc.inc.php: 127

$parser = new Less_Parser();
$parser->SetImportDirs($this->getImportDirs());
if( count( $this->registeredVars ) ) $parser->ModifyVars( $this->registeredVars );
$parser->parseFile($fname);
$out = $parser->getCss();

https://github.com/oyejorge/less.php/blob/master/lessc.inc.php#L141

$parser = new Less_Parser($this->getOptions());
$parser->SetImportDirs($this->getImportDirs());
if( count( $this->registeredVars ) ) $parser->ModifyVars( $this->registeredVars );
foreach ($this->libFunctions as $name => $func) {
    $parser->registerFunction($name, $func);
}
$parser->parseFile($fname);
$out = $parser->getCss();

I'm right?

thom4parisot commented 9 years ago

Yep you are right. Bundled versions were out of date, released as v1.7.6. Give it a try and let us know :-)

pravdomil commented 9 years ago

I think that it will work, can you please update the oyejorge compiler? Can I help you some how?

After that, we can use this snippet for compressed output:

add_filter('wp_less_compiler', function() { return 'less.php'; } );

add_action('wp-less_init', function($WPLess) {
  $WPLess->getCompiler()->setFormatter('compressed');
} );
thom4parisot commented 9 years ago

@Pravdomil I updated both of them (leafo/lessphp bumped from 0.4 to 0.5). Cf. https://plugins.trac.wordpress.org/changeset/1241501/wp-less#file1

pravdomil commented 9 years ago

Great, now the compression works perfectly with both compilers.

I would recommend to use oyejorge library since it's official php less compiler and more up to date, what do you think?

thom4parisot commented 9 years ago

It would be just a matter of changing the default value for the filter (to less.php): https://github.com/oncletom/wp-less/blob/master/lib/Plugin.class.php#L68

Feel free to open a PR for this :-)

pravdomil commented 9 years ago

https://github.com/oncletom/wp-less/pull/90