Managing multi-package Flutter projects with Melos
By: Sagar Suri
Tags: iOS, Google, Flutter, Android, Dart

Most of the time when you create a Flutter project. You work with a single package. The project consists of a single pubspec.yaml , lib folder. You put all your features and utilities in the same package. But there are projects which break their features and utilities into multiple packages. This helps in better separation of concerns and allows the team to open-source some of their packages. Here is a diagram of how a multi-package project will look like:

Here we have divided the project into three layers. The first layer is the root project which will hold common configurations that applies to all the different packages in the project. The 2nd layer is having independent feature packages that do not depend on each other. The 3rd layer consists of utility packages that are used in multiple feature packages. I won’t dive deep into how to create or structure a multi-package flutter project. This article will focus on solving a particular challenge that is typically faced in a multi-package flutter project.

The Challenge

In a single package flutter project running the following tasks is dead simple:

flutter pub get flutter test flutter analyze Generating code coverage i.e flutter test --coverage

But running the same tasks in a multi-package flutter project can be challenging because you need to run those tasks in every package inside your project and give a summarised result at the end of completion of the task. Now we know what the challenge is. Let’s discuss what are the possible solutions for it.

The Solution

There are 2 possible solutions to solve this challenge. Let’s look at the first one:

Writing bash scripts for various tasks.

This is definitely a solution but not a smart solution. You need to first write a script that will find out all the packages in your project and run one of the above tasks in it. You also need to make sure that you display output in a pretty format to make things readable. If you are more of a GUI guy then you need to create some sort of configuration in your IDE to run your scripts through GUI. Integrating Melos into your project.

This is a smart solution over the first one which I highly recommend. So let’s discuss in details what Melos is and how to integrate it into your multi-package project.

Introducing Melos

M elos is a CLI tool for managing Flutter/Dart project with multiple packages.

Melos is developed by a well-known team in the Flutter community i.e invertase. You can read in detail about Melos on their website but here is a quick list of features Melos provide:

Automatic versioning & changelog generation.

Automated publishing of packages to pub.dev .

. Local package linking and installation.

Executing simultaneous commands across packages.

Listing of local packages & their dependencies.

Now let’s see how we can perform all the above tasks using Melos.

Note: Please download the starter project if you wanna learn by doing. There is another branch where you can find the final version of the project.

Installing Melos

Let’s install Melos first. I assume you have already installed Flutter SDK and set the Flutter and Dart path to bash_profile . Run the following command in your terminal:

You can copy the command from the above image

The next step is to open up your starter project in your IDE. I prefer using Intellij and would show you some awesome GUI features provided by Melos. The project structure should look like the following:

Now create a file named melos.yaml in your root app. and copy the following content to it:

You can copy the code from the above image

Let’s understand the above script:

a) name : You have to give the name of your project. You can find it inside the pubspec.yaml of the root project.

b) packages : This list should contain paths to the individual packages within your project. Each path can be defined using the glob pattern expansion format.

Melos Bootstrap

Now run the following command in your terminal from your root project to link all your local packages together and update the dependencies i.e flutter pub get .

Make sure to run this command from inside the root project

You should see the following output after running the above command:

You can read why you need to bootstrap Melos here. To be precise it’s one of the important commands you should execute when setting up Melos in a project or you performed a cleanup in your project.

Melos Clean

You can execute this command when you want to remove temporary files from your project(build artifacts, pub files etc). This is how the command looks like:

You can copy the command from the above image

Commands

Now you will work on creating different commands to achieve the tasks which we mentioned at the starting of the article.