hotreloader 1.0.0 hotreloader: ^1.0.0 copied to clipboard
Hot code reloader
hotreloader (Dart) #
What is it? #
This Dart library package provides a code reloading service that monitors the local file system for changes to a Dart project's source files and automatically applies them using the Dart VM's hot reload capabilities to the running Dart process.
Requirements #
Dart SDK 2.6.0 or higher.
How to use #
-
Add this to your pubspec.yml
dependencies: hotreloader: ^1.0.0
-
Enable hot reloading in your entry point dart file, e.g.
bin/main.dart
import 'package:hotreloader/hotreloader.dart'; Future<void> main(List<String> args) async { // instantiate a reloader that by default monitors the lib directory for source file changes final reloader = await HotReloader.create(); // ... your other code // cleanup reloader.stop(); }
-
Run the dart program using the Dart VM with the
--enable-vm-service
flag enabled, e.g.dart --enable-vm-service bin/main.dart
-
You can now change dart files under the
lib
and the changes should be applied to the running process.
The reloader service can be further customized, e.g.
import 'package:hotreloader/hotreloader.dart';
Future<void> main(List<String> args) async {
final reloader = await HotReloader.create(
paths: [ // changes to .dart files in these directories trigger code reload
'bin',
'lib',
'test'
],
debounceInterval: Duration(seconds: 2), // wait up to 2 seconds after file change before reloading
onBeforeReload: (ctx) => //
ctx.isolate.name != 'foobar' && // never reload the isolate named 'foobar'
ctx.event?.path.contains('/mymodel/')) ?? true, // only perform reload when dart files under ../mymodel/ are changed
onAfterReload: (ctx) => print('Hot-reload result: ${ctx.result}')
);
// ... your other code
await reloader.reloadCode(); // programmatically trigger code reload
// ... your other code
// cleanup
reloader.stop();
}
Logging #
This library uses the logging package for logging.
You can configure the logging framework and change the log-level programmatically like this:
import 'dart:isolate';
import 'package:hotreloader/hotreloader.dart';
import 'package:logging/logging.dart' as logging;
Future<void> main() async {
logging.hierarchicalLoggingEnabled = true;
// print log messages to stdout
logging.Logger.root.onRecord.listen(
(record) => print('${record.time} ${record.level.name} [${Isolate.current.debugName}] ${record.loggerName}: ${record.message}')
);
HotReloader.logLevel = logging.Level.FINEST;
final reloader = await HotReloader.create();
// ... your other code
// cleanup
reloader.stop();
}
Alternatives #
- https://pub.dev/packages/angel_hot
- https://pub.dev/packages/jaguar_hotreload
- https://pub.dev/packages/recharge
- https://pub.dev/packages/reloader
Changelog / Version History #
This project maintains a changelog and adheres to Semantic Versioning and Keep a CHANGELOG
License #
All files are released under the Apache License 2.0.