Skip to content

[Proposal] Conditional bundling of assets #96514

@navaronbracke

Description

@navaronbracke

Use case

I was looking into setting up different environment configurations for an application that is both a mobile app and a web app.
The usual setup for an environment config involves an .env file as an asset for the app.
For multiple environments / .env files, different entrypoints can be used with the -t flag for build & run.

However, switching between these targets has one problem.
In terms of developer experience, I'd expect to be able to just change the argument for -t and nothing else.

In practice, I'd have to edit the pubspec.yaml before each build / run.
Otherwise I would be including .env files that are not used at runtime.
(I.e. when running with .env.test, you don't need/want .env.dev in your assets,
both to reduce file size and for security reasons)

Example:
For dev

flutter:
  assets:
    - .env.dev
    # - .env.test

For test

flutter:
  assets:
    # - .env.dev
    - .env.test

Proposal

There should be a way to conditionally include an asset, based on a predefined condition.
Perhaps there could be an extra pubspec section or file that allows developers to setup an environment specific part.

Example:

# pubspec.yaml
flutter:
  assets:
   - shared_asset.png
   
  environment-configuration:
    my-custom-dev-env: # Entries with custom keys, but with the same structure as a regular app (assets / fonts / etc)?
      assets:
        - .env.dev
    my-custom-test-env:
      assets:
        - .env.test

Switching between environment-configuration entries could be done using a new flag for flutter build / flutter run?
By default nothing would be included.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projecta: assetsPackaging, accessing, or using assetsc: new featureNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to Flutterteam-toolOwned by Flutter Tool teamtoolAffects the "flutter" command-line tool. See also t: labels.triaged-toolTriaged by Flutter Tool team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions