assets icon indicating copy to clipboard operation
assets copied to clipboard

Filter `only` not working

Open BaBL86 opened this issue 1 year ago • 2 comments

According to documentation I add $publishOptions['only'] in my project, but it doesn't work

What steps will reproduce the problem?

class BootstrapAsset extends AssetBundle
{
    public ?string $basePath = '@assets';

    public ?string $baseUrl = '@assetsUrl';

    public ?string $sourcePath = '@assetsPath/bootstrap';

    public array $css = [
        'css/bootstrap.min.css',
    ];

    public array $js = [
        'js/bootstrap.bundle.min.js',
    ];

    public array $depends = [
        \App\Assets\JqueryAsset::class,
    ];

    public array $publishOptions = [
        'only' => [
            'css/bootstrap.min.css',
            'js/bootstrap.bundle.min.js',
        ]
    ];
}

What is the expected result?

I expect to have only 2 files in published assets, but I have full bootstrap library like in

ea9e2702
- css
- - bootstrap.min.css
- js
- - bootstrap.bundle.min.js

What do you get instead?

ea9e2702
- css
- - bootstrap.css
- - bootstrap.min.css
- - bootstrap.rtl.css
- - bootstrap-grid.css
- - bootstrap-grid.rtl.css
- - bootstrap-reboot.css
- - bootstrap-reboot.rtl.css
- - bootstrap-utilities.css
- - bootstrap-utilities.rtl.css
- js
- - bootstrap.bundle.js
- - bootstrap.bundle.min.js
- - bootstrap.esm.js
- - bootstrap.js

Workaround

This code works like a charm, but it's not comfort and documentation have another way.

    public function __construct()
    {
        $pathMatcher = new PathMatcher();

        $filters = [];
        foreach ($this->css as $file)
            $filters[] = '**/' . $file;
        foreach ($this->js as $file)
            $filters[] = '**/' . $file;

        $this->publishOptions = [
            'filter' => $pathMatcher->only(...$filters),
        ];
    }

Additional info

Q A
Version 4.0.0
PHP version 8.3
Operating system Debian GNU/Linux

BaBL86 avatar Jun 16 '24 11:06 BaBL86

According to this link: https://github.com/yiisoft/assets/blob/master/docs/guide/en/asset-bundles.md

BaBL86 avatar Jun 16 '24 11:06 BaBL86

Instead only need to use filter option, that use PathMatcherInterface. Need to fix docs.

vjik avatar Jun 16 '24 17:06 vjik