avatarDaniel Varga

Summary

The Dart 2 update has modified the behavior of conditional imports, limiting them to checks for the presence of libraries and removing support for user-defined conditions.

Abstract

With the release of Dart 2, the functionality of conditional imports has been revised. Previously, in Dart 1, developers could use custom conditions with a -D command line argument to the Dart VM. However, in Dart 2, while some aspects of conditional imports are still supported, they are now restricted to conditions defined by the Dart VM, specifically for checking the existence of libraries. This change means that developers can no longer use arbitrary conditions as they could before. The Dart team has acknowledged this limitation and indicated that support for user-defined conditions is unlikely to be reintroduced. Developers are advised to adapt by checking deployment targets, such as whether the code is running in a browser or on a VM, and by using factories tied to environment variables for other use-cases.

Opinions

  • The author suggests that the Dart 1 method of using conditional imports with custom conditions was more flexible.
  • The Dart team's decision to limit conditional imports to library presence checks is seen as a deliberate choice, as the feature was previously undocumented.
  • The author expresses that the removal of custom conditions is a significant change that invalidates some of the advice given in their previous article on the topic.
  • Despite the limitations, the author acknowledges that the remaining functionality is sufficient for some use-cases, particularly for distinguishing between browser and VM deployment targets.
  • The author provides a workaround for other use-cases by suggesting the use of factories and environment variables.

Dart 2 conditional imports update

With Dart 2 being released for a while, it was time to check if conditional imports still work the same as in Dart 1. You can check what it looked like in this article: https://readmedium.com/dart-vm-tests-with-dart-html-yes-you-can-c15439e49cc1

The Dart 1 way

I would go around and write imports/exports like so:

import "something.dart" if (condition) "other.dart";

And run the code with a simple -D command line argument to the Dart VM.

dart -Dcondition=true myapp.dart

The Dart 2 way

As per this issue https://github.com/dart-lang/sdk/issues/34262 , conditional imports were an undocumented feature for a reason. They kept parts of it alive, while removing other functionality.

What you can still do is use the feature based on conditions defined by Dart VM. These are constrained to checking the presence of libraries.

import "something.dart" if (dart.library.io) "other.dart"

You can check this example of dart-sass library for a production use-case.

What you no longer can do is use custom conditions. Most of what I wrote in my previous article no longer applies. -D is no longer taken into consideration. This was confirmed by Dart Team members and support for user defined conditions is unlikely to land in Dart.

Upgrade tips

In my use-cases, most of what I needed was checking the deployment target. Browser or VM. This can still be done by checking if dart.library.io or dart.library.js is defined.

My other use-cases could be solved by wrapping the implementation classes into a factory and tie it with some environment variables.

Dart
Dartlang
Recommended from ReadMedium