How to set up default manifest_json_path while having multiple configurations?

311 views Asked by At

I'm working on setting up a project for multiple configurations. I know that if you want to have multiple json_manifest_paths, you need modify config.yml like this:

assets:
base_urls:
    - "%cdn_url%"
packages:
    project_1:
        json_manifest_path: '%kernel.project_dir%/web/build/project_1/manifest.json'
    projest_2:
        json_manifest_path: '%kernel.project_dir%/web/build/project_2/manifest.json'
    assets:
        version: "%asset_version_generated%"
        base_path: "/build"

And then you call your assets like this:

    <img src="{{ asset("build/desktop/images/background.png", 'project_1') }}" />

But I also want to have a default json_manifest_path which will be targeted if I call my assets without the second parameter, like this:

    <img src="{{ asset("build/images/background.png") }}" />

So, sometimes I'll have the project project_1 built inside /build/project_1 and sometimes I'll have project_2, and so on, you get the idea.

I need to have a third option where a project will be built directly inside /build and the way I call assets will be by not giving it a second argument. In that case, manifest.json path will be directly inside /build.

I tried with this but it's giving me an error that it doesn't exist, even though I am not calling my assets without a second argument:

assets:
json_manifest_path: '%kernel.project_dir%/web/build/manifest.json'
base_urls:
    - "%cdn_url%"
packages:
    project_1:
        json_manifest_path: '%kernel.project_dir%/web/build/project_1/manifest.json'
    project_2:
        json_manifest_path: '%kernel.project_dir%/web/build/project_2/manifest.json'
    assets:
        version: "%asset_version_generated%"
        base_path: "/build"

TL;DR

How do I set up a default json_manifest_path inside config.yml which will be targeted if I call my assets without the second parameter?

1

There are 1 answers

0
Alexander Dimitrov On

I did something similar with EventListener whitch listen for onKernelController even and adds the necessary assets package like so:

$strategy = new JsonManifestVersionStrategy(
        $this->parameterBag->get('kernel.project_dir').'/public/build/'.$your_project_n.'/manifest.json'
    );
    $this->assetPackages->setDefaultPackage(new Package($strategy));

Then you don’t need to specify the second argument of the asset function at all.

Similarly you can dynamically add the default package and leave the rest hardcoded In the config file