1

我想加快 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,这让我相信它会禁用该依赖项的构建,但什么也不做,文件继续生成。

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

4

2 回答 2

3

我找到了解决方案。这是非常接近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

可以在“$definingPackageName:$targetname”中引用目标。当目标名称与包名称匹配时,它也可以仅称为包名称。每个包中的一个目标必须使用包名称,以便消费者默认使用它。在 build.yaml 文件中,可以使用键 $default 或包的名称来定义此目标。

您可以在此处找到有关它的更多信息这是一篇非常好的文章,为build.yaml文件提供了一些很好的提示

于 2021-06-02T14:21:59.193 回答
1

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

    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
于 2021-06-01T18:06:24.690 回答