Yii2 AssetBundle publishOptions 模式语法?

Yii2 AssetBundle publishOptions pattern syntax?

可能是愚蠢的,但只是想问问我在发布资产文件时使用的语法是否正确。 The Guide 指示使用不带尾随星号的文件夹名称,但对我来说它不起作用。

奇怪的是,即使我指定只发布 css 和图像文件夹,assetManager 也会发布 $sourcePath 中的其他文件夹(幸运的是里面没有文件)。更重要的是,当新文件添加到图像文件夹时,它们将保持未发布状态,直到我删除 @web/assets 文件夹。这是意料之中的事吗?

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

首先注意你应该设置$sourcePath$baseUrl而不是两者都设置(如果你设置两者,前者会覆盖最后一个assetManager调用publish 方法)。

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).

如果您想避免资产管理器发布此文件夹,您必须在 publisOptionsexcept 参数中提及它们:

'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?

是的!这是一种自然的行为。您可以在开发模式下将 publishOptionsforceCopy 属性 设置为 true。这会导致在每次刷新时更新资产内容。你可以轻松地使用 Yii 生产常量:YII_DEBUG.

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

所以你完全有这样的资产经理:

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