hmr 1.3.2
hmr: ^1.3.2 copied to clipboard
Hot Module Replacement system specially designed for command-line Dart applications.
π Hot Module Replacement for Dart Applications #
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
π Key features #
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 |
Global usage #
Installation #
Install the package globally in your environment.
dart pub global activate hmr
Configuration #
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.
Usage #
$ cd /path/to/your/project
$ hmr
The Glob library is used to manage the inclusion and exclusion of monitored files.
π¦ Manual installation #
Add to your pubspec.yaml
:
dependencies:
hmr: ^1.0.0
π Usage #
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