diff --git a/.changeset/support-server-client-only.md b/.changeset/support-server-client-only.md new file mode 100644 index 000000000..935fe31ee --- /dev/null +++ b/.changeset/support-server-client-only.md @@ -0,0 +1,5 @@ +--- +"@solidjs/start": minor +--- + +Add support for `server-only` and `client-only` modules diff --git a/packages/start/env.d.ts b/packages/start/env.d.ts index 2c9155d1c..c638b96cd 100644 --- a/packages/start/env.d.ts +++ b/packages/start/env.d.ts @@ -7,3 +7,15 @@ declare namespace App { [key: string | symbol]: any; } } + +/** + * Import `server-only` to ensure this module is never bundled for the client. + * Importing it in a client module will throw a build error. + */ +declare module "server-only" {} + +/** + * Import `client-only` to ensure this module is never bundled for the server. + * Importing it in a server module will throw a build error. + */ +declare module "client-only" {} diff --git a/packages/start/src/config/index.ts b/packages/start/src/config/index.ts index 9cdc6830e..77b828901 100644 --- a/packages/start/src/config/index.ts +++ b/packages/start/src/config/index.ts @@ -200,6 +200,29 @@ export function solidStart(options?: SolidStartOptions): Array { }, filter: options?.serverFunctions?.filter, }), + { + name: "solid-start:boundary-modules", + enforce: "pre", + resolveId(id, importer, { ssr }) { + if (id === "server-only") { + if (!ssr) + this.error( + `Attempt to import 'server-only' in a client module: ${importer}`, + ); + } else if (id === "client-only") { + if (ssr) + this.error( + `Attempt to import 'client-only' in a server module: ${importer}`, + ); + } else { + return null; + } + return "\0solid-start:boundary-modules:id"; + }, + load(id) { + if (id === "\0solid-start:boundary-modules:id") return "export {}"; + }, + }, { name: "solid-start:virtual-modules", async resolveId(id) {