Build Management with build_runner

Published on by Flutter News Hub

Build Management with build_runner

Build_runner is a powerful package for managing Dart code generation and watch processes. It provides a streamlined approach to generating files, running tests, and serving web applications.

Installation

Before using build_runner, add it as a dev dependency to your pubspec.yaml file:

dev_dependencies:
  build_runner: ^latest

Usage

Built-in Commands

Build_runner provides several built-in commands for common build tasks:

  • build: Performs a one-time build.
  • watch: Continuously monitors file changes and rebuilds as necessary.
  • serve: Combines watch with a development server that serves web applications.
  • test: Generates a merged output directory and runs dart run test on it.

Example Command:

$ dart run build_runner watch

This command starts a watch process that monitors file changes and automatically rebuilds the project.

Inputs and Outputs

  • Inputs: Builders can read files from the current package, its dependencies, and the lib folder of the top-level package.
  • Outputs: Builders can create new files anywhere in the current package, but not overwrite existing ones. Generated outputs can be used as inputs for subsequent builds.

Source Control

Generated files are typically not committed to source control. Build_runner creates a .dart_tool folder in the package to store generated code and build-related files.

Publishing Packages

Generated files should generally be published with the package, unless specific builders recommend otherwise.

Custom Build Scripts

While build_runner generates build scripts, you can also manually write your own. Each consuming package requires a unique script.

import 'package:build_runner/build_runner.dart';

void main() {
  run(BuilderApplication(// ...));
}

Contributing

Build_runner welcomes contributions, including bug reports, feature requests, and pull requests. Before implementing significant changes, consider opening an issue for discussion.

Testing

All pull requests must pass CI checks, which include:

  • Analyzer checks: dart analyze .
  • Formatting checks: dart format .
  • Unit tests: dart run test
Flutter News Hub