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

vite: errors for server-only code point to new docs #8488

Merged
merged 2 commits into from
Jan 11, 2024
Merged
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/giant-otters-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/dev": patch
---

vite: errors for server-only code point to new docs
19 changes: 3 additions & 16 deletions integration/vite-dot-server-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ test.describe("Vite / route / server-only module referenced by client", () => {

` '${specifier}' imported by route 'app/routes/_index.tsx'`,

" The only route exports that can reference server-only modules are:",
" Remix automatically removes server-code from these exports:",
" `loader`, `action`, `headers`",

` But other route exports in 'app/routes/_index.tsx' depend on '${specifier}'.`,

" For more see https://remix.run/docs/en/main/discussion/server-vs-client",
" See https://remix.run/docs/en/main/future/vite#splitting-up-client-and-server-code",
].forEach(expect(stderr).toMatch);
});
}
Expand Down Expand Up @@ -209,20 +209,7 @@ test.describe("Vite / non-route / server-only module referenced by client", () =

` '${specifier}' imported by 'app/reexport-server-only.ts'`,

" * If all code in 'app/reexport-server-only.ts' is server-only:",

" Rename it to 'app/reexport-server-only.server.ts'",

" * Otherwise:",

` - Keep client-safe code in 'app/reexport-server-only.ts'`,
` - And move server-only code to a \`.server\` file`,
` e.g. 'app/reexport-server-only.server.ts'`,

" If you have lots of `.server` files, try using",
" a `.server` directory e.g. 'app/.server'",

"For more, see https://remix.run/docs/en/main/future/vite#server-code-not-tree-shaken-in-development",
" See https://remix.run/docs/en/main/future/vite#splitting-up-client-and-server-code",
].forEach(expect(stderr).toMatch);
});
}
Expand Down
28 changes: 3 additions & 25 deletions packages/remix-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1093,46 +1093,24 @@ export const remixVitePlugin: RemixVitePlugin = (options = {}) => {
"",
` '${id}' imported by route '${importerShort}'`,
"",
` The only route exports that can reference server-only modules are:`,
` Remix automatically removes server-code from these exports:`,
` ${serverOnlyExports}`,
"",
` But other route exports in '${importerShort}' depend on '${id}'.`,
"",
" For more see https://remix.run/docs/en/main/discussion/server-vs-client",
" See https://remix.run/docs/en/main/future/vite#splitting-up-client-and-server-code",
"",
].join("\n")
);
}

let importedBy = path.parse(importerShort);
let dotServerFile = vite.normalizePath(
path.join(
importedBy.dir,
importedBy.name + ".server" + importedBy.ext
)
);

throw Error(
[
colors.red(`Server-only module referenced by client`),
"",
` '${id}' imported by '${importerShort}'`,
"",

` * If all code in '${importerShort}' is server-only:`,
"",
` Rename it to '${dotServerFile}'`,
"",
` * Otherwise:`,
"",
` - Keep client-safe code in '${importerShort}'`,
` - And move server-only code to a \`.server\` file`,
` e.g. '${dotServerFile}'`,
"",
" If you have lots of `.server` files, try using",
" a `.server` directory e.g. 'app/.server'",
"",
" For more, see https://remix.run/docs/en/main/future/vite#server-code-not-tree-shaken-in-development",
" See https://remix.run/docs/en/main/future/vite#splitting-up-client-and-server-code",
"",
].join("\n")
);
Expand Down