All backend imports becoming accessible on frontend?
Node Environment Version: v20.10.0
Package Manager: PNPM Workspaces
Repo Setup: Monorepo/Turborepo
Environment: WSL with VSCode
TRPC Versions:
Hello! First time user of TRPC.
I've setup my project, and now have it so that it exports appRouter on the backend
Backend
Then on the frontend like the tutorial I have a hooks file for trpc that imports the backend
Frontend
The structure is setup as follows
However in VSCode all other backend imports are displayed during autoimport now in the frontend project? For example I can now do something like
When the line to import AppRouter is removed, none of the backend autocomplete imports appear.
I realistically would only like to expose that type and nothing else, I don't think imports actually work, as in Vite/React will throw errors when you try to use that backend import them during runtime. But is there a way to prevent the other imports from appearing? Is this behaviour expected?
I also tried to use pnpm link and export only a file containing the AppRouter but this didn't seem to prevent it either
Backend package.json
Backend trpc.types.ts
Frontend
6 Replies
If it's of use here is my Typescript configs for the backend/frontend
Backend
Frontend
Thanks for any help 🙏 If there's anything you need let me know
Just as an update, I found somewhat if a half solution. But definitely not fully resolved.
I've then added the following rule to my config on the frontend
And doing something like
Will error with
Any direction would be great :feelsadman:
I've been pretty surprised by how blase the internet has been about making the backend server package a dependency of the frontend with tRPC. It seems quite risky to accidentally pull in sensitive backend code. Here's what we did:
1. Add as the backend package as a TS project reference, and use a special path so it's easy to know:
2. Then in your frontend code:
3. Don't add the backend package to your frontend
package.json
. This means when you write out the type manually, TS knows what to do with it, but intellisense won't try to serve up exports from backend when you're in the frontend code.
It feels incredibly jank and hacky, but so far it seems to be working.Thanks @Andrew , appreciate your help with this. And I do agree, felt quite nervous about importing the backend just struggling to find solutions hah.
I've given it a shot but might have either misinterpreted what you've said or am doing something wrong as Intellisense still seems to serve other exports. I've created a minimal repo and have also reset my VSCode settings/removed all extensions. Using the TS configs from my main application and then slightly modified trpc's minimal app. https://github.com/trpc/examples-minimal/tree/main
https://github.com/davidtjones02/trpc-import-exclusion
Any thoughts? Thanks again let me know if you need anything additionally.
GitHub
GitHub - trpc/examples-minimal
Contribute to trpc/examples-minimal development by creating an account on GitHub.
GitHub
GitHub - davidtjones02/trpc-import-exclusion
Contribute to davidtjones02/trpc-import-exclusion development by creating an account on GitHub.
I think I can see benefit regardless versus importing the backend as a package like you've said however.
Oh I haven't tried in VS Code tbh, I work with WebStorm so the intellisense logic may be different (though usually it's the same)...
Ooooof yep just confirmed that VS Code just imports from the backend package. Whyyyy 😭
Argh so frustrating! Thank you for all the help though. The differences are still meaningful, and the ESLint rules are an added measure
Magic library tRPC. And possibly that Webstorm just has superior contextual understanding of a repo versus VSCode
But if anyone has any additional thoughts would be interesting to hear. For now though, I think the ESLint rules and not importing via a package will do for now 🙌