Sub topic 15
Splitting a Large Project into Modules and Unlocked Packages
This chapter assumes you can run the Salesforce CLI, you know the trunk-based flow from earlier chapters, and you understand how sfdx-project.json points to source directories. It covers when one force-app directory stops being enough and what to do about it.
Why a single directory strains
A single force-app directory works well for a small team. As the project grows, three problems tend to appear:
- Pull requests from unrelated teams touch the same folders, so reviews and merge conflicts increase.
- Every check-only deployment validates the whole project, so validation gets slower.
- Nobody can say which team owns a given class or object.
You can address these in two steps. The first step is to split the source into several package directories in the same repository. The second step is to turn some of those directories into unlocked packages.
Step one: multiple package directories
A package directory is a folder listed in packageDirectories. The repository stays whole, and main stays the source of truth. Each folder gets a clear purpose.
{
"packageDirectories": [
{ "path": "modules/core", "default": true },
{ "path": "modules/service" },
{ "path": "modules/sales" }
],
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "62.0"
}The order of the list matters. Salesforce deploys directories in the order they appear, so shared metadata such as custom objects and fields belongs in the first directory. Later directories can then depend on it.
Once the folders exist, you can deploy and validate one module at a time:
sf project deploy start --source-dir modules/service --target-org case-routingAdd a CODEOWNERS file so each team reviews its own folder. This ties into the branch protection rules you set on main.
Step two: unlocked packages
An unlocked package is a versioned, installable set of metadata. Each package directory gets a package name and a declared dependency list. The package version is then created from a commit and installed into orgs.
{
"packageDirectories": [
{
"path": "modules/core",
"package": "core",
"versionName": "Core 1.0",
"versionNumber": "1.0.0.NEXT",
"default": true
},
{
"path": "modules/service",
"package": "service",
"versionName": "Service 1.0",
"versionNumber": "1.0.0.NEXT",
"dependencies": [{ "package": "core" }]
}
],
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "62.0"
}Creating a version requires a Dev Hub org and an existing package record:
sf package create --name core --package-type Unlocked --path modules/core --target-dev-hub devhub
sf package version create --package core --installation-key-bypass --wait 20 --target-dev-hub devhubPackage versions fit naturally into the release flow. Tagging a release in Git can then mean "these package versions went to production."
What changes in the branching flow
The branching model does not change. You still use short-lived feature branches, pull request validation, and merges into main. The differences are in what the pipeline does:
| Concern | Single directory | Unlocked packages |
|---|---|---|
| Unit of release | Whole project | Package version |
| Validation | Check-only deploy | Version create with code coverage checks |
| Promotion | Deploy source from a tag | Install a version into each org |
| Dependencies | Implicit | Declared and enforced |
Trade-offs
Packaging has costs. Not every metadata type is supported in packages. Dependencies must form a tree with no cycles, and removing a component takes planning. You also must maintain a Dev Hub and a version creation step in CI.
The diagram shows how modules relate.
Exercise
Your team has a core module and a service module. Why should core be listed first in packageDirectories, and what extra setting does service need if both are unlocked packages?
Show answer
Directories deploy in the listed order, so shared metadata in core must exist before service deploys. If both are unlocked packages, service needs a dependencies entry naming core so the dependency is declared and enforced when installing.