Viewing version 1.0.0. Open the latest package page.

hotreloader

1.0.0

Hot code reloader

Download archive

Readme

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 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

  1. Add this to your pubspec.yml

    dependencies:
      hotreloader: ^1.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 default monitors the lib directory for source file 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(
    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

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.

Changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

[Unreleased]

[1.0.0] - 2020-04-08

  • initial release

Installing

export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add 'hotreloader:1.0.0'
pubspec.yaml
# https://dart.dev/tools/pub/pubspec
name: hotreloader
version: 1.0.0

description: Hot code reloader

repository: https://github.com/vegardit/dart-hotreloader
homepage: https://github.com/vegardit/dart-hotreloader
documentation: https://github.com/vegardit/dart-hotreloader/blob/master/README.md
issue_tracker: https://github.com/vegardit/dart-hotreloader/issues

environment:
  # https://dart.dev/tools/pub/pubspec#sdk-constraints
  sdk: '>=2.6.0 <3.0.0'

# https://dart.dev/tools/pub/dependencies#version-constraints
dependencies:
  logging: ^0.11.4         # https://pub.dev/packages/logging
  path: ^1.6.4             # https://pub.dev/packages/path
  stream_transform: ^1.1.0 # https://pub.dev/packages/stream_transform
  vm_service: ^4.0.0       # https://pub.dev/packages/vm_service
  watcher: ^0.9.7+14       # https://pub.dev/packages/watcher

dev_dependencies:
  lint: ^1.0.0

Version history

VersionPublishedArchiveSHA-256
1.0.0Cached · Upstream SHA-256 matchedDownload6b1f04e0…2d32ea
2.0.0Cached · Upstream SHA-256 matchedDownload5a8af98c…1b24fb
2.0.1Cached · Upstream SHA-256 matchedDownload16146cf5…4f5d6e
2.0.2Cached · Upstream SHA-256 matchedDownload5a14de78…96bfbf
3.0.0Cached · Upstream SHA-256 matchedDownloadeb6b4993…2595c5
3.0.1Cached · Upstream SHA-256 matchedDownloadc2eca005…faad13
3.0.2Cached · Upstream SHA-256 matchedDownload15f360e8…b152a2
3.0.3Cached · Upstream SHA-256 matchedDownload90bb3ae5…d66ad0
3.0.4Cached · Upstream SHA-256 matchedDownload906f8180…f6da28
3.0.5Cached · Upstream SHA-256 matchedDownload66b78edf…28e0b8
3.0.6Cached · Upstream SHA-256 matchedDownload728c0613…e6dacf
4.0.0Cached · Upstream SHA-256 matchedDownload6c61cfa8…926da6
4.1.0Cached · Upstream SHA-256 matchedDownload94ee21a6…231449
4.2.0Cached · Upstream SHA-256 matchedDownloaded56fdc1…d9158e
4.3.0Cached · Upstream SHA-256 matchedDownloadbc167a11…e5734b
4.4.0Cached · Upstream SHA-256 matchedDownload66871df4…3c4ebf

Dependencies

logging "^0.11.4"

path "^1.6.4"

stream_transform "^1.1.0"

vm_service "^4.0.0"

watcher "^0.9.7+14"