oyejorge / less.php

less.js ported to PHP.
http://lessphp.typesettercms.com
Apache License 2.0
655 stars 2 forks source link

Slow compiling & big cache size #123

Open matyushen opened 10 years ago

matyushen commented 10 years ago

I'm looking for an optimal and according to best practices solution for compiling less files on WordPress. I'm using redux framework. I was able to get things up and running but still have some major concerns. Compiling latest bootstrap takes about 5,8 sec according to redux and about 5,1 without fontawesome. With my custom styles on top of bootstrap compiling time boosts up to over 10 sec. Caching helps, after first cache it takes about half a second to reload styles. But the size of newly created cache folder is over 8mb and almost two times bigger with my custom styles. Also if I use caching with parser (like in my code above) compiling time stays on the same level, it drops only by using Less_Cache

Have a look at my code, would be happy to hear any suggestions:

function compiler_action() {

    // Redux global variable
    global $mytheme;

    $file_name = '/custom-style' . '.css';
    $folder_path = get_template_directory() . '/assets/css';
    $file_path = $folder_path . $file_name;

    $options = array( 
        'compress' => true,
        'cache_dir'=> wp_upload_dir()['basedir'] . '/wp-less-cache/'
    );

    // this is basically only for fontawesome to get a proper path to font, but obviously it's better to load fontawesome separately for time saving
    $directories = array( get_template_directory() . '/assets/less/' => get_template_directory_uri() . '/assets/' );

    $parser = new Less_Parser($options);
    $parser->SetImportDirs( $directories );
    $parser->parseFile( get_template_directory() . '/assets/less/_app.less', '' );
    $parser->ModifyVars( 
        array(
            'brand-primary' => $mytheme['brand_primary']
        ) 
    );
    $parser->parse( $mytheme['custom_less'] );

    $css = $parser->getCss();

    global $wp_filesystem;
    if( empty( $wp_filesystem ) ) {
        require_once( ABSPATH .'/wp-admin/includes/file.php' );
        WP_Filesystem();
    }

    if( $wp_filesystem ) {
        $wp_filesystem->put_contents($file_path,$css,FS_CHMOD_FILE);
    }

}

add_filter('redux/options/mytheme/compiler', 'compiler_action', 10, 2);
caiosm1005 commented 10 years ago

I am working on an utility called CSScompiler that probably would sort this issue. It is basically a helper class bundled with less.php and scssphp which simplifies compiling LESS and Sass/SCSS into CSS.

Since it has its own caching system (with optional MySQL support), it should have a nice performance.

It's still a WIP, but I am currently working hard on finishing it as soon as possible. If you're interested I suggest you keep an eye on it :bowtie:

matyushen commented 10 years ago

Thanks, interesting to see the result!

PeterKnight commented 10 years ago

@caiosm1005 how is your CSScompiler coming along?