Skip to content

Commit

Permalink
vite: errors for server-only code point to new docs (#8488)
Browse files Browse the repository at this point in the history
  • Loading branch information
pcattori authored Jan 11, 2024
1 parent 0ece2a2 commit c0b1e17
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 41 deletions.
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

0 comments on commit c0b1e17

Please sign in to comment.