Answer the question
In order to leave comments, you need to log in
Why can't AssetConverter in Yii2 minify pure CSS?
If you do this:
class AppAsset extends AssetBundle
{
public $sourcePath = '@common/assets/css';
public $css = [
'styles.less',
'foo.bar',
'hello.css',
];
}
'assetManager' => [
'converter' => [
'class' => yii\web\AssetConverter::className(),
'commands' => [
'bar' => ['css', 'lessc {from} {to} --clean-css'],
'less' => ['css', 'lessc {from} {to} --clean-css --no-color'],
],
],
],
'assetManager' => [
'converter' => [
'class' => yii\web\AssetConverter::className(),
'commands' => [
'css' => ['css', 'lessc {from} {to} --clean-css'], // добавим это
'bar' => ['css', 'lessc {from} {to} --clean-css'],
'less' => ['css', 'lessc {from} {to} --clean-css --no-color'],
],
],
],
Answer the question
In order to leave comments, you need to log in
In general, I found out with a debugger that the whole point is in this line:
/**
* Converts a given asset file into a CSS or JS file.
* @param string $asset the asset file path, relative to $basePath
* @param string $basePath the directory the $asset is relative to.
* @return string the converted asset file path, relative to $basePath.
*/
public function convert($asset, $basePath)
{
$pos = strrpos($asset, '.');
if ($pos !== false) {
$ext = substr($asset, $pos + 1);
if (isset($this->commands[$ext])) {
list ($ext, $command) = $this->commands[$ext];
$result = substr($asset, 0, $pos + 1) . $ext;
// все дело в этой проверке
// |||||||||||||||||||||||||||||||||||||||||||
// VVVVVVVVVVVVVVVVVV
if ($this->forceConvert || @filemtime("$basePath/$result") < @filemtime("$basePath/$asset")) {
$this->runCommand($command, $basePath, $asset, $result);
}
return $result;
}
}
return $asset;
}
foo.bar
, the file foo.bar
is copied to assets
with the same name. Then, according to $commands
in AssetConverter
, the name should change to foo.css
. And since we do not yet foo.css
exist, then both filemtime('foo.css') = false
, and if forceConvert = false
, and putting it in true
makes little sense, then the launch occurs runCommand()
. hello.css
, it gets into assets
already with the extension css
, and if we compare filemtime
the sort and the result, they will be equal, and no magic will happen.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question