0

Might be stupid, but just wanted to ask if the syntax I'm using is correct when publishing asset's files. The Guide instructs to use folder names without trailing asterisk, however for me it does not work that way.

The weird thing is even if I specify to only publish css and images folder, the assetManager also publishes other folders in $sourcePath (luckily without files inside). What's more, when new files are added to images folder, they remain unpublished until I delete @web/assets folder. Is this to be expected?

<?php
namespace app\views\layouts\main\assets;
use yii\web\AssetBundle;

class ThemeAsset extends AssetBundle
{
    public $sourcePath = '@theme';
    public $baseUrl = '@web';
    public $css = [
        'css/site.css',
    ];

    public $publishOptions = [
        "only" => [
            "css/*",
            "images/*",
        ],
        "forceCopy" => false,
    ];
}
1
  • Last question...they become unpublished...that's because the force copy parameter value is false.
    – StalkAlex
    Sep 16, 2016 at 19:49

1 Answer 1

3

The first note that you should set $sourcePath or $baseUrl not both of them (If you set the both, the former overwrites the last when assetManager calls publish method).

The weird thing is even if I specify to only publish css and images folder, the assetManager also publishes other folders in $sourcePath (luckily without files inside).

If you wanna avoid asset manager to publish this folders, you must mention them on except parameter of publisOptions:

'except' => [
    "doc/",
    "img/",
],

What's more, when new files are added to images folder, they remain unpublished until I delete @web/assets folder. Is this to be expected?

Yes! It's a natural behavior. You may set forceCopy property of publishOptions to true in development mode. This causes to update asset contents in each refresh. Easily you can use Yii production constant: YII_DEBUG.

public $publishOptions = [
    'only' => [
        'js/',
        'css/',
    ],
    'forceCopy' => YII_DEBUG,
];

So totally you have an asset manager like this:

<?php
namespace app\views\layouts\main\assets;
use yii\web\AssetBundle;

class ThemeAsset extends AssetBundle
{
    public $sourcePath = '@theme';

    public $css = [
        'css/site.css',
    ];

    public $publishOptions = [
        "only" => [
            "css/*",
            "images/*",
        ],
        'except' => [
            "doc/",
            "img/",
        ],
        "forceCopy" => YII_DEBUG,
    ];
}
2
  • do you know how the only and except works with .less files? I want the less file to be converted but not copied. But when i add 'only' => 'css/.css' the less file isn't converted. If I add 'except' => '*.less' the conversion is also not done...
    – BHoft
    Nov 16, 2016 at 9:18
  • Actually now I don't have any idea about your problem and also free time for reading the source of AssetBundle for finding a solution. I suggest you to post a new question. @BHoft
    – meysam
    Nov 17, 2016 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.