barrel_generator 1.0.1 barrel_generator: ^1.0.1 copied to clipboard
Generate barrel files for Dart using build_runner.
Barrel Generator #
Generate barrel files for each folder for Dart and Flutter projects. A barrel file is a file that exports all the files in a directory. This is useful when you have a lot of files in a directory and you want to import them all in one go.
If you want to support this project, please leave a star, share this project, or consider donating through Github Sponsor.
Getting Started #
Add the following dependencies to your pubspec.yaml
:
dependencies:
barrel_annotation: ^1.0.0
dev_dependencies:
build_runner: ^2.4.13
barrel_generator: ^1.0.1
Decorate one of your files placed in the source root of your project with the BarrelConfig
annotation:
import 'package:barrel_annotation/barrel_annotation.dart';
@BarrelConfig(exclude: [
'lib/lib.barrel.dart',
])
void main() {
print('Hello, World!');
}
Run the following command to generate the barrel files:
dart run build_runner build --delete-conflicting-outputs
Features #
- ✅ Generate barrel files for each folder
- ✅ Exclude files and directories
- ✅ Generate barrel files for Flutter projects
- ✅ Generate barrel files for Dart projects
- ✅ Generate barrel files for packages
Configuration #
The BarrelConfig
annotation has the following properties:
exclude
: A list of files and directories to exclude from the barrel file.
Example #
Given the following directory structure:
lib/
excluded/
f.dart
g.dart
folder/
subfolder/
a.dart
b.dart
c.dart
example.dart
With the following configuration:
import 'package:barrel_annotation/barrel_annotation.dart';
@BarrelConfig(exclude: [
'lib/lib.barrel.dart',
'lib/excluded/**',
])
void main() {
print('Hello, World!');
}
The generated barrel files structure will look like this:
lib/
excluded/
f.dart
g.dart
folder/
subfolder/
a.dart
b.dart
c.dart
folder.barrel.dart
example.dart
The content of lib/folder/folder.barrel.dart
will be:
// This file is generated by barrel_generator
// Barrel files
export 'subfolder/subfolder.barrel.dart';
// Other files
export 'c.dart';
export 'a.dart';
export 'b.dart';
Questions and bugs #
Please feel free to submit new issues if you encounter problems while using this library.
If you need help with the use of the library or you just want to request new features, please use the Discussions section of the repository. Issues opened as questions will be automatically closed.
License #
Barrel is available under the MIT license. See the LICENSE file for more info.