Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vite): make server .client exports undefined #8220

Merged
merged 1 commit into from
Dec 6, 2023
Merged
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
8 changes: 4 additions & 4 deletions .changeset/rude-keys-heal.md
Original file line number Diff line number Diff line change
@@ -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
@@ -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,