-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Monorepo / workspaces imports attempt to reference rootDir / outDir literally instead of package path reference #45964
Comments
TypeScript does not support subpath exports yet, which would be the modern way of configuring your package. Currently, you wan work around this issue by adding a {
"main": "./dist/lib.js",
"exports": {
".": "./dist/lib.js",
"./*": "./dist/*.js"
},
"types": "./dist/lib.d.ts",
"typesVersions": {
"*": {
"dist/lib.d.ts": ["./dist/lib.d.ts"],
"*": ["./dist/*"]
}
}
} |
Hmmm seems weird, workspace packages do not act like real packages? |
AFAIK, this has nothing to do with monorepos. It's just that, if the package is meant to be consumed by importing its sub modules, additional configuration for both NodeJS and TypeScript is required.
Node 12 added support for this However, TypeScript is not aware of the mapping done by |
The salient point here is we have no idea what directory you plan to // packages/main/tsconfig.json
{
"compilerOptions": {
"paths": {
"dep-a/*": ["../dep-a/src/*"]
}
} The paths referencing |
Doesn't #45964 (comment) solve this more cleanly by allowing the package to specify how consumers should resolve these paths? This seems to solve the problem in lieu of TS 4.5, but I'm hesitant to go full bore with this without understanding where the edge cases may be. Is this primarily because auto-imports won't behave as expected? |
Yeah have gone with #45964 (comment) until a more concrete solution / answer arises. Both current solutions don't feel great. |
I think export maps are probably going to be a better long-term solution for most Node projects, but it’s kind of a big “it depends,” especially for the short term when export maps are pretty nascent. When you use path mapping in this case, it feels very kludgy that you can still resolve the relative paths that won’t actually work at runtime. The node12+ module resolution stuff is really nice because the package.json-level config specifies exactly what should happen at runtime, and TypeScript can just follow that. But if you’re not actually writing for node, what then? Path mapping is a more versatile tool, and that has both upsides and downsides. It’s not always the right tool for the job, and it’s not perfect even when it is the right tool for the job. There are so many different JS runtimes and bundlers and ways of organizing projects that TypeScript will never have a tailored solution for each one. |
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow. |
Bug Report
🔎 Search Terms
Monorepo workspaces
import from distribution instead of source
auto-import from package not source
import from source incorrectly
🕗 Version & Regression Information
💻 Code
When trying to resolve a packages exports the paths do not resolve as a normal packages would, except for the case of the index, where it instead tries to use either the
outDir
or therootDir
.I have created a minimum reproduction of the issue here: https://github.com/Codex-/monorepo-issue
Does not work:
Works when referenced via the
outDir
:Works when referenced via the
rootDir
:🙁 Actual behavior
When importing from these monorepo/workspaces packages the path should be that of the package import path, opposed to the source:
(Should not have
src
but instead the package path of@monorepo-issue/dep-a/eggman/eggman
)🙂 Expected behavior
I would expect that as this is imported to
main
as a package, it would behave like a package and the imports to be valid without requiring the full path including the package root and either theoutDir
orrootDir
.I'm hoping this is a simple repository configuration issue, from the way workspaces and package references are described I would expect this to work as described above though.
The text was updated successfully, but these errors were encountered: