flutter build_runner:为 build.yaml 中的特定文件扩展名构建

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

我想加快 build_runner 所用的构建时间,并且我还想在 运行 build watch 编辑不存在的文件时阻止重新构建没有要生成的文件。

我知道这需要在 build.yaml 文件中进行编辑,但我什么也做不了。

这是我的构建文件

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

即使尝试添加 enabled: false,这让我相信它会禁用该依赖项的构建,但什么都不做,文件继续生成。

也许我只是误解了应该如何使用构建文件...?

尝试遵循语法并检查这个:

    targets:
      $default:
        builders:
          # also be sure, that builder name is correct
          # i guess it has pattern like this X:X:
          # in my case json_serializable:json_serializable:
          auto_route_generator:auto_route_generator:
            generate_for:
              include:
                - lib/**.route.dart
                # or full path to file here
    #... and so on

我找到了解决方案。这是非常接近 igokom 的答案。

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

我之前收到一条警告,上面写着 [WARNING] Configuring 'auto_route_generator:auto_route_generator' in target 'my_app:my_app' but this is not a known Builder

我收到此错误是因为 auto_route 的生成器名称不是 auto_route_generator,而是 autoRouteGenerator

这些值可以在插件 build.yaml 文件中找到

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 articlebuild.yaml 文件提供了一些很好的提示