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 @@ -209,6 +209,29 @@ export function solidStart(options?: SolidStartOptions): Array<PluginOption> {
}),
lazy(),
envPlugin(options?.env),
{
name: "solid-start:boundary-modules",
enforce: "pre",
resolveId(id, importer, { ssr }) {
Comment thread
YanAnghelp marked this conversation as resolved.
if (id === "server-only") {
if (!ssr)
this.error(
`Attempt to import 'server-only' in a client module: ${importer}`,
);
Comment on lines +218 to +220
} else if (id === "client-only") {
if (ssr)
this.error(
`Attempt to import 'client-only' in a server module: ${importer}`,
);
Comment on lines +223 to +225
} else {
return null;
}
return "\0solid-start:boundary-modules:id";
},
load(id) {
if (id === "\0solid-start:boundary-modules:id") return "export {}";
},
Comment on lines +229 to +233

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the returned value is just the fixed "export {}", this will not cause any actual bugs. This can be regarded as a style suggestion.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Starting from Vite 6, Each environment has an isolated module graph. However, here it is specifically bound to the environment, so server-only and client-only actually point to different modules.

},
{
name: "solid-start:virtual-modules",
async resolveId(id) {
Expand Down
Loading