HMR Dart is a Hot Module Replacement system specially designed for command-line Dart applications.
It allows you to :
- Hot reload code without restarting the application
- Maintain application status during updates
- Monitor files in real time
- Intelligently manage recompilations
Feature | Description |
---|---|
🪄 Ready to use | No configuration required |
⚡ Instant reloading | Instant code update |
🎯 Targeted surveillance | Filtering by glob patterns (includes /excludes ) |
🔍 Visual feedback | ANSI colours + change counter |
⏱️ Debounce mechanism | Delayed recompilation |
🔄 Error management | Compiler error messages with highlight |
📝 Extendable | Build your own HMR system using event handling |
📦 Extremely small size | Package size < 10kb |
Install the package globally in your environment.
dart pub global activate hmr
In your pubspec.yaml
, you can add an additional configuration to the hmr
.
hmr:
# Change the location of the input file
entrypoint: bin/main.dart
# Delay before recompilation in milliseconds
# If not specified, the recompilation will be immediate and you don't have any debounce
debounce: 5
# Only include files that meet the following criteria
includes:
- '**/*.txt'
- '**/*.dart'
# Exclude files meeting the following criteria
excludes:
- '.dart_tool/**'
The inclusion and exclusion of files is optional.
If you don't specify any criteria, all files matching the Glob(‘**.dart’)
criterion will be monitored.
$ cd /path/to/your/project
$ hmr
The Glob library is used to manage the inclusion and exclusion of monitored files.
Add to your pubspec.yaml
:
dependencies:
hmr: ^1.0.0
Create a bin/hmr.dart
file.
import 'package:hmr/hmr.dart';
void main() {
final runner = Runner(
tempDirectory: Directory.systemTemp,
entrypoint: File(
path.join([
Directory.current.path,
'bin',
'main.dart'
])
));
final watcher = Watcher(
onStart: () => print('Watching for changes...'),
middlewares: [
IgnoreMiddleware(['~', '.dart_tool', '.git', '.idea', '.vscode']),
DebounceMiddleware(Duration(milliseconds: 5), dateTime),
IncludeMiddleware([Glob("**.dart")]),
],
onFileChange: (int eventType, File file) async {
final action = switch (eventType) {
FileSystemEvent.create => 'created',
FileSystemEvent.modify => 'modified',
FileSystemEvent.delete => 'deleted',
FileSystemEvent.move => 'moved',
_ => 'changed'
};
print('File $action ${file.path}');
await runner.reload();
});
watcher.watch();
runner.run();
}
Start HMR mode:
$ dart run bin/hmr.dart