symfony2:使用 assetic-dump,是否可以只转储一个文件?

symfony2: using assetic-dump, is possible to dump just one file?

运行

之后
php app/console assetic:dump --env=prod

所有资产被转储

有没有办法只转储一个文件?

看来您必须创建自己的命令:

<?php

namespace Your\Namespace\Command;

use Symfony\Bundle\AsseticBundle\Command\AbstractCommand;

class DumpSingleAsset extends AbstractCommand
{
    protected function configure()
    {
        $this
            ->setName('assetic:dump_single_asset')
            ->setDescription('Dumps a single asset')
            ->addArgument('name', InputArgument::REQUIRED, 'The name of the asset')
        ;
    }


    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $name = $input->getArgument('name');
        $this->dumpAsset($name, $output); // Inherited from AbstractCommand
    }
}

Assetic docs shows a way more simple way to dump assets, but I could not find any documentation of the AsseticBundle internals, I just read the code of the Command.

这是一个仅使用配置的解决方案。在配置文件中,将捆绑包保留为:

bundles: []

除非您手动指定,否则不会加载任何捆绑包中的资产。

使用此处所述的命名资产来单独加载您想要的资产。

http://symfony.com/doc/current/cookbook/assetic/asset_management.html#using-named-assets

对于类似的问题,我有自己的棘手解决方案,因为我需要转储资产,这些资产不存在于来自数据库或 json 文件的树枝模板上。

只有资产名称,如果没有进一步的解释,我不明白你是怎么做到的。如果你在 运行 assetic dump 时打印 $name 值,你会得到类似 'afd49f7' 的东西。 Symfony2 读取 twig 模板上的所有 javascript 和样式表块并自动分配此键名。

如果你尝试手动缩小一个文件,你最好直接使用 yui-compressor 或类似的工具,否则如果你真的需要将一组资产转储到一个文件(一个集合可能只包含一个文件)或单个文件但使用symfony2 你必须使用 "named assets" 和类似 parla. See the proper section on How to Use Assetic for Asset Management and also check AsseticBundle Configuration.

建议的命令

无论如何,上面的命令在 Symfony2 v2.3 (LTS) 上不起作用,因为 dumpAsset 方法在 DumpCommand 上被声明为私有的,而 AbstractCommand 不存在。

如果你使用 Symfony2 v2.3,你需要重写整个命令添加选项 --name 并更改 ->setName('assetic:dump') 为其他东西。