Skip to content

Commit

Permalink
test(vite): update checks for error messages for .server imports
Browse files Browse the repository at this point in the history
  • Loading branch information
pcattori committed Dec 12, 2023
1 parent 27826f0 commit 0ca4b4f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
15 changes: 9 additions & 6 deletions integration/vite-dot-server-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,10 @@ 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: `loader`, `action`, `headers`",
` but other route exports in 'app/routes/_index.tsx' depend on '${specifier}'.`,
" The only route exports that can reference server-only modules are:",
" `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",
].forEach(expect(stderr).toMatch);
Expand Down Expand Up @@ -204,11 +206,12 @@ test.describe("Vite / non-route / server-only module referenced by client", () =

" * Otherwise:",

" Keep client-safe code in 'app/reexport-server-only.ts'",
" and move server-only code:",
` - 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'`,

" - Into a `.server` directory e.g. 'app/.server/utils.ts'",
" - Or into 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",
].forEach(expect(stderr).toMatch);
Expand Down
14 changes: 9 additions & 5 deletions packages/remix-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -948,8 +948,11 @@ export const remixVitePlugin: RemixVitePlugin = (options = {}) => {

if (!importer) throw Error(`Importer not found: ${id}`);

let vite = importViteEsmSync();
let pluginConfig = await resolvePluginConfig();
let importerShort = path.relative(pluginConfig.rootDirectory, importer);
let importerShort = vite.normalizePath(
path.relative(pluginConfig.rootDirectory, importer)
);
let isRoute = getRoute(pluginConfig, importer);

if (isRoute) {
Expand All @@ -974,10 +977,11 @@ export const remixVitePlugin: RemixVitePlugin = (options = {}) => {
}

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

throw Error(
Expand Down

0 comments on commit 0ca4b4f

Please sign in to comment.