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

3

10

After running

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

all the assets are dumpped.

Is there any way to dump only one file?

Effulgent answered 15/3, 2015 at 23:33 Comment(1)
Where does that single file lives? If I got it right you want something like picking files to generate without breaking the current functionality of asseticPectase
E
9

Looks like you will have to create your own command :

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

Eoin answered 2/6, 2015 at 20:25 Comment(1)
I can't believe you saved my life, this is so useful. One important thing to add is that the name of the asset passed as argument is for ex. 352e254 and that is the initial name of the .js files dumped (still couldn't find the exact pattern they use to group this asset files, maybe files from the same folder).Merth
P
1

Here's a solution using only configurations. In the config file leave bundles as:

bundles: []

This will not load the assets from any bundle unless you specify it manually.

use named assets as described here to load the assets you want individually.

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

Pectase answered 2/6, 2015 at 23:16 Comment(0)
C
0

I have my own hard tricky solution for a similar problem cause i need dump assets not present on twig templates coming from database or json file.

With asset name only, i don't understand how you could do it without further explanation. If you print $name value when run assetic dump, you get something like 'afd49f7'. Symfony2 reads all javascripts and stylesheets blocks on twig templates and assign this key name automatically.

If you try to minify one file manually you better use yui-compressor or similar directly, otherwise if you really need dump a collection of assets to one file (A collection could contain only one file) or single file but using symfony2 you must use "named assets" and something like the command suggested by parla. See the proper section on How to Use Assetic for Asset Management and also check AsseticBundle Configuration.

Anyway the command above don't works on Symfony2 v2.3 (LTS) cause dumpAsset method is declared as private on DumpCommand, and AbstractCommand not exists.

If you use Symfony2 v2.3, you need rewrite the entire command adding option --name and changing ->setName('assetic:dump') for something else.

Chair answered 8/6, 2015 at 18:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.