Skip to content

Commit f3e84ec

Browse files
YanAnghelpbrenelz
andauthored
feat: support server-only and client-only modules (#2162) (#2167)
Co-authored-by: Brenley Dueck <brenleydueck@gmail.com>
1 parent a8e5a6d commit f3e84ec

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/start": minor
3+
---
4+
5+
Add support for `server-only` and `client-only` modules

packages/start/env.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,15 @@ declare namespace App {
77
[key: string | symbol]: any;
88
}
99
}
10+
11+
/**
12+
* Import `server-only` to ensure this module is never bundled for the client.
13+
* Importing it in a client module will throw a build error.
14+
*/
15+
declare module "server-only" {}
16+
17+
/**
18+
* Import `client-only` to ensure this module is never bundled for the server.
19+
* Importing it in a server module will throw a build error.
20+
*/
21+
declare module "client-only" {}

packages/start/src/config/index.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,29 @@ export function solidStart(options?: SolidStartOptions): Array<PluginOption> {
209209
}),
210210
lazy(),
211211
envPlugin(options?.env),
212+
{
213+
name: "solid-start:boundary-modules",
214+
enforce: "pre",
215+
resolveId(id, importer, { ssr }) {
216+
if (id === "server-only") {
217+
if (!ssr)
218+
this.error(
219+
`Attempt to import 'server-only' in a client module: ${importer}`,
220+
);
221+
} else if (id === "client-only") {
222+
if (ssr)
223+
this.error(
224+
`Attempt to import 'client-only' in a server module: ${importer}`,
225+
);
226+
} else {
227+
return null;
228+
}
229+
return "\0solid-start:boundary-modules:id";
230+
},
231+
load(id) {
232+
if (id === "\0solid-start:boundary-modules:id") return "export {}";
233+
},
234+
},
212235
{
213236
name: "solid-start:virtual-modules",
214237
async resolveId(id) {

0 commit comments

Comments
 (0)