TL;DR How can I create an alias for a local yarn workspace dependency?
I've tried yarn workspaces before and never succeeded, and I'm giving it another try.
I've set "workspaces": ["packages/*"] in package.json.
For each package, I decided to use the naming convention @-/package-name to prevent naming conflicts and without worrying about having namespaces for internal packages.
When adding packages as dependencies, I've been following a style where I use an interface name for resolution, but point that towards a concrete implementation. This is what I did before using yarn workspaces:
"dependencies": {
"my-interface-name": "file:some/path/to/packages/some-concrete-implementation"
}
This is basically to allow what I like to call compile-time static dependency injection. And it also means each package can individually name their interface dependencies appropriately to their need and to prevent naming conflicts.
However, I can't figure out how to accomplish this with yarn workspaces. How do I create an alias for my yarn workspaces package @-/some-concrete-implementation called my-interface-name?
What I've already tried with no success:
- Defining the dependency like
"my-interface-name": "@-/some-concrete-implementation"}- for some reason this causes yarn to look for@-/some-concrete-implementationon the npm registry instead of in the local workspace - I've also tried to use the workspace protocol:
"my-interface-name": "workspace:@-/some-concrete-implementation"}but it still looks for the package on the npm registry!
What I haven't yet tried and could work but removes benefits of using yarn workspaces in the first place:
"dependencies": {"my-interface-name": "file:../../node_modules/@-/some-concrete-implementation"}"
Have you seen the
resolutionspackage.json key? Is it what you need?I've used it for aliasing/overriding external packages but the example in the docs shows it working with local packages.
From the RFC:
So, I believe the rule you need is:
I believe it works in the package's package.json. Worst case you can hoist it to the workspace root and make the rule specific to the workspace e.g. "a/b".