简体   繁体   English

使用PHP自动将引用的LESS文件编译到CSS中

[英]Compile a referenced LESS file into CSS with PHP automatically

I want the following things to occur: 我希望发生以下事情:

  1. Have the process automated server side. 让流程自动化服务器端。

  2. Simply be able to reference the LESS file as I would a CSS file in my code. 只需能够像我在代码中的CSS文件一样引用LESS文件。

  3. The user is returned minified CSS instead of the LESS file - cached so the compiler doesn't need to run unless the LESS file has been updated. 用户返回缩小的CSS而不是LESS文件 - 缓存,因此除非LESS文件已更新,否则编译器不需要运行。

  4. For this to work with any LESS file that is referenced anywhere within my domain. 为此,可以使用在我的域中任何位置引用的任何 LESS文件。

I spotted Lessphp , but the documentation isn't very clear, nor does it explain how to dynamically get any LESS file to it. 我发现Lessphp ,但文档不是很清楚,也没有解释如何动态获取任何LESS文件。 I thought I would post up how I got it all working as I haven't seen a run through on how to achieve this with PHP. 我想我会发布我是如何完成所有工作的,因为我还没有看到如何通过PHP实现这一点。

THIS ASSUMES LESSPHP v0.3.8+ Unsure about earlier versions, but you'll get the gist of how it works if it doesn't straight out of the box. 这个假设LESSPHP v0.3.8 +对早期版本不确定,但如果没有开箱即用,你会得到它的工作原理。

<link rel="stylesheet" type="text/css" href="styles/main.less" />

If you were using less.js to compile client side, make sure you change rel="stylesheet/less" to rel="stylesheet" 如果您使用less.js编译客户端,请确保将rel="stylesheet/less"更改为rel="stylesheet"

1) Grab Lessphp I placed these files in /www/compilers/lessphp/ for the context of this demo 1)Grab Lessphp我将这些文件放在/www/compilers/lessphp/用于本演示的上下文

2) Make a PHP script that we can throw out LESS files at. 2)制作一个我们可以丢弃LESS文件的PHP脚本。 This will deal with caching, compiling to CSS and returning the CSS as a response. 这将处理缓存,编译为CSS并返回CSS作为响应。 I have placed this file at /www/compilers/ and called it lessphp.php 我已将此文件放在/www/compilers/并将其命名为lessphp.php

Most of this code was on the Lessphp site, except there were errors in it, and I have added the response at the end. 大部分代码都在Lessphp站点上,除了它有错误,我最后添加了响应。

<?php
require "lessphp/lessc.inc.php";
$file = $_GET["file"];
function autoCompileLess($inputFile, $outputFile) {
    // load the cache
    $cacheFile = $inputFile.".cache";
    if (file_exists($cacheFile)) {
        $cache = unserialize(file_get_contents($cacheFile));
    } else {
        $cache = $inputFile;
    }
    $less = new lessc;
    $less->setFormatter("compressed");
    $newCache = $less->cachedCompile($cache);
    if (!is_array($cache) || $newCache["updated"] > $cache["updated"]) {
        file_put_contents($cacheFile, serialize($newCache));
        file_put_contents($outputFile, $newCache['compiled']);
    }
}
autoCompileLess('../' . $file, '../' . $file . '.css');
header('Content-type: text/css');
readfile('../' . $file . '.css');
?>

This will compile the LESS file (eg, styles/main.less ) to a cache file and a CSS file (eg, styles/main.less.css ). 这将把LESS文件(例如, styles/main.less )编译为缓存文件和CSS文件(例如, styles/main.less.css )。

3) Add a mod_rewrite rule so that any LESS files a user requests are redirected to our compiler, giving it its path. 3)添加mod_rewrite规则,以便用户请求的任何LESS文件被重定向到我们的编译器,为其提供路径。 This was placed in the root .htaccess file. 这被放在根.htaccess文件中。

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^([^.]*\.less)$ compilers/lessphp.php?file=$1 [R,QSA,L]
</ifModule>

If you are using WordPress, this rule will need to come after it - even if WordPress is in a sub directory, it seems to overwrite these rules, and LESS compilation will not occur for referenced files which exist below (directory wise) WordPress's .htaccess rules. 如果你正在使用WordPress,这个规则将需要在它之后 - 即使WordPress在子目录中,它似乎覆盖这些规则,并且对于下面存在的引用文件(目录明智)不会发生LESS编译WordPress的.htaccess规则。

4) Your LESS code should be relatively referenced in relation to the compilers location. 4)您的LESS代码应该与编译器位置相对引用。 Additionally, Lessphp compiler will fail if there are empty attributes, eg. 此外,如果存在空属性,则Lessphp编译器将失败,例如。 background-color: ;


If all is working well, the following should occur: 如果一切正常,应该发生以下情况:

  1. Directly browse your LESS file http://domain.com/styles/main.less 直接浏览您的LESS文件http://domain.com/styles/main.less

  2. Be automatically redirected to http://domain.com/compilers/lessphp?file=styles/main.less 自动重定向到http://domain.com/compilers/lessphp?file=styles/main.less

  3. Be presented with minified CSS 提供缩小的CSS

  4. main.less.css and main.less.cache should now exist in the same directory as your LESS file main.less.cssmain.less.cache现在应该与LESS文件存在于同一目录中

  5. The last modified dates shouldn't change unless you update your LESS file 除非您更新LESS文件,否则不应更改上次修改日期

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM