Skip to content

Commit

Permalink
fix(vite): make server .client exports undefined (#8220)
Browse files Browse the repository at this point in the history
  • Loading branch information
markdalgleish authored Dec 6, 2023
1 parent 48cde3b commit 1534c78
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions .changeset/rude-keys-heal.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
"@remix-run/dev": patch
---

Vite: Preserve names for exports from .client imports
Vite: Preserve names for exports from `.client` modules

Unlike `.server` modules, the main idea is not to prevent code from leaking into the server build
since the client build is already public. Rather, the goal is to isolate the SSR render from client-only code.
Routes need to import code from `.client` modules without compilation failing and then rely on runtime checks
to determine if the code is running on the server or client.
or otherwise ensure that execution only happens within a client-only context (e.g. event handlers, `useEffect`).

Replacing `.client` modules with empty modules would cause the build to fail as ESM named imports are statically analyzed.
So instead, we preserve the named export but replace each exported value with an empty object.
That way, the import is valid at build time and the standard runtime checks can be used to determine if then
So instead, we preserve the named export but replace each exported value with `undefined`.
That way, the import is valid at build time and standard runtime checks can be used to determine if the
code is running on the server or client.
4 changes: 2 additions & 2 deletions packages/remix-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -958,8 +958,8 @@ export const remixVitePlugin: RemixVitePlugin = (options = {}) => {
code: exports
.map(({ n: name }) =>
name === "default"
? "export default {};"
: `export const ${name} = {};`
? "export default undefined;"
: `export const ${name} = undefined;`
)
.join("\n"),
map: null,
Expand Down

0 comments on commit 1534c78

Please sign in to comment.