AFAIK, there is no way to inject an existing managed package to be a part of another managed package. Obviously, you could combine the source codes of both packages and create a new package, but that would really not be preferable or even worthy of being considered an OK approach. Instead of trying to combine 2 packages into a single managed package,
- Retain 2 different managed packages so you can leverage the benefits of modular development approach and keep the code maintenance effort optimal in the future.
- Define the dependency over one or more packages. This will also enable you to be able to create patch versions for each of these packages and be able have a mix of different version dependencies among these packages for various customers in the future. Or, you can be able to opt to have one package as Unlocked, if needed.
You can define
dependencies in the sfdx-project.json file (under
packageDirectories). Sample snippet can be found here & more details about package dependency can be found here.
If the customer tries to install a package in their org before installing a dependency, a user-friendly message will be displayed asking to install the dependencies first.
Or, you can provide them with a script that would automatically install the dependencies and then the actual package. Sample script is present here.
You can have any # of package directories in a single SFDX project (although, only one package directory can be set as default) and have them defined in the sfdx-project.json file. In the screenshot below, you can see that there are 2 package directories
customer-app. One will be used for the source code of your core package, while the other for the customer related package. Note that the SFDX source folders and structure under each of this package directories will be the same. In the
sfdx-project.json file, you can see that I've defined the both of these package directories, while having customer package as dependent on core package via
dependencies. You can define any # of dependencies for a single package.
Within a single SFDX project, you can use
force:package CLI commands to generate package for each of the package directories individually. You might want to check out 2GP & Unlocked Packages. This github project is a good reference for seeing how multiple package directories with dependencies can be placed within a single SFDX project.