flutterdartcode-generationbuild-runner

flutter build_runner: Build for specific file extensions in build.yaml


I want to speed up the build time that build_runner takes, and I also want to prevent a re-build when running build watch when I am editing files that don't have any files to generate.

I know that this needs to be edited in the build.yaml file, but I can't get anything to work.

this is my build file

targets:
  $default:
    builders:
      auto_route_generator:
        generate_for: 
          include: [".route.dart"]
      freezed:
        generate_for: 
          include: [".model.dart"]
      json_serializable:
        generate_for:
          include: [".model.dart"]
        options:
          explicit_to_json: true
          include_if_null: false

Even trying to add enabled: false, which makes me believe that it would disable the build for that dependency, does nothing and the files continue to be generated.

Maybe I am just misunderstanding how the build file is supposed to be used...?


Solution

  • I found the solution. It is very close igokom's answer.

    targets:
      $default:
        builders:
          auto_route_generator|autoRouteGenerator:
            enabled: true
            generate_for:
              include:
                - lib/infrastructure/routes/app_routes.routes.dart
              # [exclude] is not needed if at least one path is provided
              # in [include]. It will only target [include]d paths
              # exclude:
              #   - ...
    
          json_serializable:
            enabled: true
            generate_for:
              include:
                - lib/**.model.dart
                - lib/**_bloc.dart
                - lib/**_cubit.dart
    
            options:
              explicit_to_json: true
              include_if_null: false
          freezed|freezed:
            enabled: true
            generate_for:
              include:
                - lib/**.model.dart
                - lib/**_bloc.dart
                - lib/**_cubit.dart
    
          injectable_generator|injectable_builder:
            enabled: true
            generate_for:
              include:
                - lib/**_bloc.dart
                - lib/**_cubit.dart
                - lib/**.dao.dart
    
          injectable_generator|injectable_config_builder:
            enabled: true
            generate_for:
              include:
                - lib/infrastructure/injection/injection.dart
    

    I was getting a Warning previously which read [WARNING] Configuring 'auto_route_generator:auto_route_generator' in target 'my_app:my_app' but this is not a known Builder

    I was getting this error because the name of the generator for auto_route is not auto_route_generator, rather autoRouteGenerator.

    These values can be found within the plugins build.yaml file

    builders:
      # [autoRouteGenerator] is the name of the generator
      # that needs to be referenced
      autoRouteGenerator:
        import: "package:auto_route_generator/builder.dart"
        builder_factories: ["autoRouteGenerator"]
        build_extensions: {'.dart': ['.gr.dart']}
        auto_apply: dependents
        build_to: source
    

    Targets can be referred to in '$definingPackageName:$targetname'. When the target name matches the package name it can also be referred to as just the package name. One target in every package must use the package name so that consumers will use it by default. In the build.yaml file this target can be defined with the key $default or with the name of the package.

    You can find more info about it here and this is a pretty good article that gives some good tips for the build.yaml file


    For those using v6 for auto_route_library

    Use auto_route_generator|autoRouteBuilder and auto_route_generator|autoRouterBuilder

    Can refer to v6-build.yaml