Background

Previously, for Dart’s own file conditional method, we made functional modifications to the Conditional Import mechanism of the Dart language to accept user-defined condition variables to control file dependencies at compile time:

Previous Blog: https://lfkdsk.github.io/dart-conditional-import/

However, only supporting the conditional compilation relationship between files cannot completely solve this problem. For example, in the branch merge of Vessel, many functions (including various native libraries and libraries of Dart and Native) are merged, but we will still encounter some libraries that cannot be used under the two major versions of the branch.

For example, the bridge_client I developed by myself relies on analyzer , source_gen and other basic libraries that are strongly related to the Dart version, so it is difficult to directly make this library run directly on the two Dart versions on the two basic libraries.

Therefore, by modifying Dart Pub to make it open with Conditional Import, we can also choose according to different conditions in the configuration file of Dart Pub.

How to impl ?

Details in : https://github.com/lfkdsk/dart_pub

How to use ?

How to use Conditional Import, please refer to: https://lfkdsk.github.io/dart-conditional-import/

For example, in pubspec.yaml configuration file, we need to rely on two versions branch_1 and branch_1:

dependencies:
  flutter:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.0
  bridge_client:
    condition.branch_1: ^1.0.0
    condition.branch_2: ^2.0.0

Among them condition.branch_1 and condition.branch_2 are automatically injected by the flutter_tools frame part. At this time we use flutter pub get:

flutter pub get --local-engine-src-path=xxxxx

Note that pub get can pass dependencies in pubspec.lock:

  bridge_client:
    dependency: "direct main"
    description:
      name: bridge_client
    source: hosted
    version: "2.0.2"

Dependencies can also be pulled correctly to ^ 2.0.0 and above, supporting the version of the flutter 1.22 engine.

Advanced mode

Version can be customized

dependencies:
  flutter:
    sdk: flutter
    
  bd_bridge_client:
    condition.branch_1: 
        git: xxx.git
        ref: xxxx
    condition.branch_2: ^2.0.0

Everything in Conditional Pub is the same as the rules pubspec.yaml before, so we can easily use git dependencies, hosted dependencies, and normal version number descriptions for different versions.

Conditions can be customized

dependencies:
  flutter:
    sdk: flutter
    
  cupertino_icons: ^1.0.0
  hook_coverage:
    condition.hooked: 
        git: xxx.git
        ref: xxxx
    condition.no_hooked: ^2.0.0-wrapper

conditions:
   condition.hooked: true
   condition.no_hooked: false        

The solution gets through Conditional Import, so you can freely use custom conditions for version selection. For example, we can use it according to whether to turn on the hook switch.