p-hatan/ mirror

pub repository

mirror.p-hatan.com

hotreloader

4.4.0

Automatic hot code reloader for Dart projects that monitors the source files of a Dart project for changes and automatically applies them to the running Dart process.

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 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.3.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/.

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]

[4.4.0] - 2026-04-05

Added

  • #27 Watching dependencies based on package graph
  • #26 Support monorepo with pub workspace

[4.3.0] - 2025-01-20

Changed

  • support vm_service 15.x

[4.2.0] - 2024-02-12

Changed

  • support vm_service 14.x

[4.1.0] - 2023-10-26

Changed

  • support vm_service 12.x and 13.x

[4.0.0] - 2023-09-18

Changed

  • require/support Dart 3.0.0 or higher

[3.0.6] - 2023-04-03

Changed

  • support vm_service 11.x

[3.0.5] - 2022-07-11

Changed

  • support vm_service 9.x

[3.0.4] - 2022-04-25

Fixed

  • #9 multiple directories starting with same characters are not watched

[3.0.3] - 2022-03-20

Fixed

  • #6 Awaiting HotReloader.stop() causes program to wait forever

Changed

  • support vm_service 8.x

[3.0.2] - 2022-01-04

Changed

  • support vm_service 7.x

[3.0.1] - 2021-04-18

Changed

  • improve file watching
  • replace some sync calls with async calls

[3.0.0] - 2021-03-16

Added

  • enable null safety support

Changed

  • require Dart 2.12.0 for null safety support
  • upgrade dependencies:
    • logging 1.0.0
    • path 1.8.0
    • stream_transform 2.0.0
    • vm_service 6.1.0+1
    • watcher 1.0.0

[2.0.2] - 2020-10-19

Added

  • support version 5.x of vm_service library

[2.0.1] - 2020-10-18

Fixed

  • FormatException: Scheme not starting with alphabetic character with Dart 2.9 or higher

[2.0.0] - 2020-04-09

Added

  • watch dependencies for source code changes

Changed

  • support slightly older versions of dependencies

[1.0.0] - 2020-04-08

  • initial release

Installing

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

description: >
  Automatic hot code reloader for Dart projects that monitors the source files of a Dart
  project for changes and automatically applies them to the running Dart process.

repository: https://github.com/vegardit/dart-hotreloader.git
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: ">=3.0.0 <4.0.0"

# https://dart.dev/tools/pub/dependencies#version-constraints
dependencies:
  logging:          ^1.0.0  # https://pub.dev/packages/logging
  path:             ^1.8.0  # https://pub.dev/packages/path
  stream_transform: ^2.0.0  # https://pub.dev/packages/stream_transform
  vm_service:       ">=6.0.1 <16.0.0"  # https://pub.dev/packages/vm_service
  watcher:          ^1.1.0  # https://pub.dev/packages/watcher

dev_dependencies:
  lint: ^2.1.2

Version history

VersionPublishedArchiveSHA-256
1.0.02020-04-08T09:43:14.716851ZDownload6b1f04e0318514481fe60394b2176c56817c85f01f43b82ee36d3badef2d32ea
2.0.02020-04-09T15:17:27.254927ZDownload5a8af98cbca2223c37c4b2e2bd9bcd3be4cec421d6e3540b19af0ba99a1b24fb
2.0.12020-10-18T18:47:24.629194ZDownload16146cf5ebfc1ce4c9f80feecd268b174c260d1c64c2fd23f1255d5c094f5d6e
2.0.22020-10-19T17:37:21.043652ZDownload5a14de7814bfbeded507913d24ada6d32337f41ec13c708b0bbad23f1c96bfbf
3.0.02021-03-16T22:15:30.870092ZDownloadeb6b4993cb0b67264bc42982dac7291fe01109d7094b185e73ae6dc1bb2595c5
3.0.12021-04-18T12:47:25.910393ZDownloadc2eca0055f0c3cde6a9d775e624bbdf99d2eb68d0b283bd01cecb76155faad13
3.0.22022-01-04T18:51:23.144850ZDownload15f360e86e4f6056b6bd4426c7a9d376ccd6cf6873e90d60c758c1319ab152a2
3.0.32022-03-20T20:24:25.992763ZDownload90bb3ae5d72cc87b6251da956958a47b49914a5732736671089f7e6febd66ad0
3.0.42022-04-25T09:51:20.385023ZDownload906f81803b6e81d9908e95fac387daa5a6ba779ff494b438f0c82eca16f6da28
3.0.52022-07-11T12:00:54.957662ZDownload66b78edf340352fcdd2043ab6116e58ec018c5968cab6485cde0f1769d28e0b8
3.0.62023-04-03T13:30:12.804535ZDownload728c0613556c1d153f7e7f4a367cffacc3f5a677d7f6497a1c2b35add4e6dacf
4.0.02023-09-18T20:17:06.259881ZDownload6c61cfa809dc9d35ab759c3a4fb96548d699103ee9c5b9412b90c8b770926da6
4.1.02023-10-26T17:15:34.485449ZDownload94ee21a60ea2836500799f3af035dc3212b1562027f1e0031c14e087f0231449
4.2.02024-02-12T11:44:24.973660ZDownloaded56fdc1f3a8ac924e717257621d09e9ec20e308ab6352a73a50a1d7a4d9158e
4.3.02025-01-20T17:13:18.345881ZDownloadbc167a1163807b03bada490bfe2df25b0d744df359227880220a5cbd04e5734b
4.4.02026-04-04T22:53:18.913943ZDownload66871df468fc24eee81f1a0a7cb98acc104716f9b7376d355437b48d633c4ebf

Dependencies

logging "^1.0.0"

path "^1.8.0"

stream_transform "^2.0.0"

vm_service "\u003e=6.0.1 \u003c16.0.0"

watcher "^1.1.0"