Part 1: Flutter Monorepos, The Why and How Melos Can Help

David Cobbina
4 min readJul 14, 2024

--

Introduction

Melos has become a go-to tool for developers working with Flutter and mono repositories. This series of articles delves deeper into its functionalities, exploring how it empowers you to effectively manage and build complex Flutter projects. But before that, let’s understand why a team might opt to use a mono repo. If you want to skip ahead and dive straight into Melos, click here [Part 2].

Understanding Mono Repos

To truly understand the need for mono repos, let’s use an example.

Ecommerce App Dilemma: Separate Repos or Mono repo?
Imagine you’re building an e-commerce platform, and your company has decided to build two mobile apps.

  • Vendor App: Manages products, ad campaigns, customer chat, earnings, etc.
  • Customer App: Allows buying products, viewing promotions, making payments, etc.

While planning, your team notices that both apps share the following:

1. UI, Models and Business Logic

  • Common UI elements (buttons, icons, cards, bottom navbars, etc.)
  • Theming (colours, fonts, overall style)
  • Data models (product information, user details)
  • Network logic (API calls)

2. Features

  • Authentication flow (login, registration)
  • Chat functionality
  • Payment processing

Traditional Approach: Duplication and Maintenance Headaches

Traditionally, you’d use separate code repositories for each app. This leads to code duplication:

  • If the vendor app’s login logic is identical to the customer app’s, you’d copy the code.
  • Shared UI components and theming would be duplicated in both repos.
  • Any changes require updates in multiple repositories, increasing maintenance overhead.

Mono Repo to the Rescue: Shared Codebase and Centralized Management

A mono repo stores all your code in a single location, including:

  • Packages (reusable code modules)
  • Shared features (authentication, chat, payments)
  • Individual apps (vendor and customer apps)

Here’s how it benefits your e-commerce scenario:

  • You’d create a package containing common UI elements and theming.
  • Both apps reside within the mono repo, referencing the shared package.
  • This eliminates code duplication across projects.
  • Updates to the shared package are reflected in both apps automatically.

Benefits of the Mono Repo approach:

  • Reduced code duplication
  • Centralized code management
  • Easier collaboration on shared features
  • Improved code maintainability

Challenges of Managing Flutter Mono Repos Without Melos

Now that the team has switched from the traditional approach to using mono repos, another issue arises. Managing a mono repo for Flutter projects without specialized tools like Melos can be daunting. Here are some common challenges:

  1. Dependency Management
  • Manual Dependency Linking: Without a tool to automate the process, developers must manually link dependencies between packages. This can be error-prone and time-consuming. A real-life example is looking at a huge project like FlutterFire and Flame Engine. Both projects have over 12 packages, with some packages depending on each other. Trying to manually link dependencies will be time-consuming and likely lead to errors.
  • Version Conflicts: Managing package versions and ensuring compatibility across multiple packages becomes complex, leading to potential conflicts.

2. Script Management
Repetitive Tasks: Developers often need to repeat the same tasks (like formatting or running tests) for each package, which is inefficient and prone to human error.

3. Workspace Organization
Complex Structure: As the number of packages and apps grows, maintaining a well-organized structure without a clear system can become overwhelming.

  • For example, in a big mono repo with many nested folders, finding specific files or packages can become cumbersome because navigating such a deep hierarchy without a clear structure or tooling support can be time-consuming and error-prone.
  • Additionally, as packages depend on each other, the dependency graph can become tangled. Understanding which packages rely on others requires significant effort.

4. Discoverability Issues
Finding and managing specific packages within a large mono repo can be difficult without a centralized management tool.

  • Lack of Indexing: Without an organized indexing system, locating specific packages within a vast codebase can be like finding a needle in a haystack.

5. Collaboration Difficulties

  • Coordination Challenges: Coordinating changes across different packages without a centralized system can lead to missed updates and integration issues.
  • Onboarding: New team members may struggle to understand the structure and dependencies within a large mono repo without clear documentation and tools to aid navigation.

Introducing Melos: The Powerhouse for Mono Repos

Melos is an opensource command-line tool designed to facilitate the management of mono repositories in Dart and Flutter projects. It streamlines various aspects of development, making it easier to work with multiple packages and apps within a single repository.

Key Features of Melos

1. Package Management

  • Dependency Management: Automatically handles inter-package dependencies within the mono repo.
  • Versioning: Easily version packages and manage changelogs.

2. Script Runner: Execute custom scripts across multiple packages, enabling consistent build processes, testing, and more.

3. Workspace Management: Define package scopes and groups to organize and manage related packages efficiently.

4. Automation: Integrate with CI/CD pipelines to automate tasks such as testing, building, and publishing packages.

Conclusion

So here are the things we have covered:

  1. We have understood why there’s a need for mono repos and the kind of use cases that might call for a mono repo.
  2. We have also covered the challenges that arise once we switch to a mono repo without using a tool to help us.
  3. Finally, we have set the stage for how Melos comes in to solve these problems.

In the next article, we will cover in-depth how to set up Melos with an example project and learn more about how to use the features that Melos presents.

If you want to explore further, visit the links below

--

--

David Cobbina

Software Engineer & Co-founder at Explorease, crafting high-performance applications that make a real difference in people's lives.