yiisoft / yiisoft/assets

Filter `only` not working

Open
#143 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type:docs
Dominant language
PHP
Stars
21
Forks
17
Avg merge
20m
Merged PRs (30d)
1

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how AssetBundle handles publishOptions and how the documented only option differs from the working PathMatcher filter workaround. Confirm the change by publishing the BootstrapAsset example and checking that only bootstrap.min.css and bootstrap.bundle.min.js are present.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.