hotreloader (Dart)

Build Status License Pub Package Contributor Covenant

  1. What is it?
  2. Requirements
  3. How to use
  4. Logging
  5. Alternatives
  6. Changelog / Version History
  7. License

What is it?

This Dart library provides a code reloading service that monitors the source files of a Dart project on the local file system for changes and automatically applies them using the Dart VM's hot reload capabilities to the running Dart process.

Requirements

hotreloader 4.x requires Dart SDK 3.0.0 or higher.

hotreloader 3.x requires Dart SDK 2.12.0 or higher.

hotreloader 1.x-2.x requires Dart SDK 2.6.0 or higher.

How to use

  1. Add hotreloader to your dev dependencies using this command

    dart pub add --dev hotreloader
    

    or by adding this to your pubspec.yaml:

    dev_dependencies:
      hotreloader: ^4.0.0
    
  2. 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 monitors the project's source code folders for changes
      final reloader = await HotReloader.create();
    
      // ... your other code
    
      // cleanup
      reloader.stop();
    }
    
  3. Run the dart program using the Dart VM with the --enable-vm-service flag enabled, e.g.

    dart --enable-vm-service bin/main.dart
    
  4. 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(
    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:io' as io;
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/stderr
  logging.Logger.root.onRecord.listen((msg) =>
    (msg.level < logging.Level.SEVERE ? io.stdout : io.stderr)
    .write('${msg.time} ${msg.level.name} [${Isolate.current.debugName}] ${msg.loggerName}: ${msg.message}\n')
  );


  HotReloader.logLevel = logging.Level.CONFIG;

  final reloader = await HotReloader.create();

  // ... your other code

  // cleanup
  reloader.stop();
}

Alternatives

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.

Individual files contain the following tag instead of the full license text:

SPDX-License-Identifier: Apache-2.0

This enables machine processing of license information based on the SPDX License Identifiers that are available here: https://spdx.org/licenses/.

Libraries

hotreloader