You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Monorepo + server-only enforcement for code in modules
#13968
I am planning on refactoring my monolithic SvelteKit app into various various smaller apps (not quite microservices). I have a monorepo and these modules will be used exclusively by my app. One of the modules I am planning on creating will hold database access logic.
So far I have been relying on the Svelte+Vite's ability to flag code that mistakenly references server-side code in a client context. As I move this database access logic code to its own module, I am loosing this crucial safety check. I have explored different approaches:
altering the database-access module's package.json to treat the code as server-only:
adding my database access module to the ssr.noExternal and optimezeDeps.exclude elements of vite.config.ts.
Even when applying the above configuration changes, I can still add references to this database access module within my custom Svelte components and not cause any build failures.
Should my changes from above have worked but I missed something subtle? Short of creating my own build plugin to identify uses of this server side-only module in client code, is there anything else I can try?
Those package.json fields aren't what the check looks at. Kit only marks a file server-only when it lives inside that SvelteKit project and the path has .server. or /server/ in it. A workspace package sits outside the project root (or comes in through node_modules), and those are skipped on purpose. ssr.noExternal just pulls it into the server bundle. It doesn't forbid the client import, so a .svelte file can still import it and the build stays green.
The check you already have keeps working if this app's database calls live under src/lib/server. The other apps in the repo can import the shared package directly. If you want this app's build to fail when a component imports the package, that has to be a small Vite plugin. Kit won't do it for a package outside the app.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I am planning on refactoring my monolithic SvelteKit app into various various smaller apps (not quite microservices). I have a monorepo and these modules will be used exclusively by my app. One of the modules I am planning on creating will hold database access logic.
So far I have been relying on the Svelte+Vite's ability to flag code that mistakenly references server-side code in a client context. As I move this database access logic code to its own module, I am loosing this crucial safety check. I have explored different approaches:
package.jsonto treat the code as server-only:ssr.noExternalandoptimezeDeps.excludeelements ofvite.config.ts.Even when applying the above configuration changes, I can still add references to this database access module within my custom Svelte components and not cause any build failures.
Should my changes from above have worked but I missed something subtle? Short of creating my own build plugin to identify uses of this server side-only module in client code, is there anything else I can try?
Any and all help appreciated.
All reactions