什么" forceCompile"使用Less时YII意味着什么?

时间:2012-11-28 07:53:36

标签: yii

我有这个:

'components'=>array(
  'less'=>array(
    'class'=>'ext.less.components.LessCompiler',
    'forceCompile'=> true, //YII_DEBUG, // indicates whether to force compiling
    //'compress'=>false, // indicates whether to compress compiled CSS
    //'debug'=>false, // indicates whether to enable compiler debugging mode
    'paths'=>array(
      'less/style.less'=>'css/style.css',
    ),
  ),

如果我启用forceCompile,我的网站速度非常慢。我想是因为它会在每个页面加载时重新生成css。我的问题是禁用它。如果我禁用它:

  1. 我对style.less所做的任何更改都不会反映在浏览器中吗?
  2. 如果是这样,那么Less的重点是什么?当然它实际上不能用于生产呢?或者您是否禁用forceCompile,因此它只生成一次?
  3. 任何关于forceCompile的清晰度都将受到高度赞赏!

    (是的,我看了所有的明确解释......我能找到的最好的是this)。

1 个答案:

答案 0 :(得分:1)

首先让我告诉你less的重点是什么:

  1. 较少是meta language,因此一般而言,使用较少的内容有助于您编写易于维护的CSS ,尽管使用的“语言”较少句法。作为一个常见示例,您可以根据less文件中的其他语句定义编译为css值的较少的变量。或者您可以像使用支持OOP的大多数其他语言一样使用mixins,嵌套和继承概念。

  2. 所以你编写了可理解的,可读的 伪/元css 代码,在编译时转换为实际的css。


  3. 现在扩展名为:

    即使您禁用forceCompile style.less 中所做的更改也应该反映出来,因为扩展程序会检查文件是否已被修改( LessCompiler.php中的以下行应该说服你):

    if ($this->forceCompile || $this->hasChanges())
        $this->compileAll();
    
    // ...
    
    /**
     * Returns whether any of files configured to be compiled has changed.
     * @return boolean the result.
     */
    protected function hasChanges() {
        // ...
            $compiled = $this->getLastModified($destination);
        // ...
            $modified = $this->getLastModified($dir);
        // ...
    }
    
    /**
     * Returns the last modified for a specific path.
     * @param string $path the path.
     * @return integer the last modified (as a timestamp).
     */
    protected function getLastModified($path){ 
        //...
    }
    

    因此forceCompile将始终编译您在paths中指定的文件,并且不应在生产中启用它。 hasChanges调用应该处理更少的已修改文件并编译它们,如上所示,扩展会自动完成。