You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

27 lines
981 B

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;
}
};
}