import type { Plugin } from 'vite'; const SERVER_ONLY = /^@webavatar\/webavatar-core\/(server|vite)(\/|$)/; /** * Build-time backstop for the server-only boundary: SvelteKit's own * `$lib/server` guard is path-based and doesn't reach into `node_modules`, so * once core's server code is a package this is what makes a client-side * import of `@webavatar/webavatar-core/server` (or `/vite`) fail the build * rather than only failing at runtime via the package's `browser` stub. See * docs/ARCHITECTURE.md's "Server-only safety" section. */ export function serverOnlyGuard(): Plugin { return { name: 'webavatar-server-only-guard', enforce: 'pre', resolveId(id, importer, options) { const isClient = this.environment ? this.environment.name === 'client' : !options?.ssr; if (isClient && SERVER_ONLY.test(id)) { throw new Error( `${id} is server-only; import it through $lib/server instead (importer: ${importer}).` ); } return null; } }; }