D
D
Dmitry Kim2016-12-08 12:38:06
Yii
Dmitry Kim, 2016-12-08 12:38:06

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',
        ];
    }

so:
'assetManager' => [
                'converter'       => [
                    'class'    => yii\web\AssetConverter::className(),
                    'commands' => [
                        'bar' => ['css', 'lessc {from} {to} --clean-css'],
                        'less' => ['css', 'lessc {from} {to} --clean-css --no-color'],
                    ],
                ],
            ],

then on the site we will see the connection of foo.css, and minified from foo.bar.
But I can't figure out why if you do this:
'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'],
                    ],
                ],
            ],

then nothing happens to the original hello.css file. I’m looking for and can’t find what to inherit from and what to change there so that bare CSS is also minified?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kim, 2016-12-08
@kimono

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;
    }

When we publish foo.bar, the file foo.baris copied to assetswith the same name. Then, according to $commandsin AssetConverter, the name should change to foo.css. And since we do not yet foo.cssexist, then both filemtime('foo.css') = false, and if forceConvert = false, and putting it in truemakes little sense, then the launch occurs runCommand().
In the case with hello.css, it gets into assetsalready with the extension css, and if we compare filemtimethe sort and the result, they will be equal, and no magic will happen.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question