Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/support-server-client-only.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/start": minor
---

Add support for `server-only` and `client-only` modules
12 changes: 12 additions & 0 deletions packages/start/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" {}
23 changes: 23 additions & 0 deletions packages/start/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,29 @@ export function solidStart(options?: SolidStartOptions): Array<PluginOption> {
},
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) {
Expand Down
Loading