Skip to content

Commit

Permalink
fix(vite): serve any file outside of project root via /@fs
Browse files Browse the repository at this point in the history
By default, Vite prevents access to files outside the workspace root
(when using workspaces) or outside of the project root (when not using
workspaces) unless user explicitly opts into it via Vite's `server.fs.allow`.
  • Loading branch information
pcattori committed Nov 7, 2023
1 parent 7d9da29 commit 9ef2f64
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
9 changes: 8 additions & 1 deletion .changeset/nasty-waves-whisper.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,11 @@
"@remix-run/dev": patch
---

fix(vite): use `/@fs/` path to support default server/client entry outside of app root
fix(vite): Let Vite handle serving files outside of project root via `/@fs`

This fixes errors when using default client entry or server entry in a pnpm project
where those files may be outside of the project root, but within the workspace root.

By default, Vite prevents access to files outside the workspace root
(when using workspaces) or outside of the project root (when not using
workspaces) unless user explicitly opts into it via Vite's `server.fs.allow`.
22 changes: 6 additions & 16 deletions packages/remix-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,14 @@ const resolveFileUrl = (
{ rootDirectory }: Pick<ResolvedRemixVitePluginConfig, "rootDirectory">,
filePath: string
) => {
if (filePath.includes("/node_modules/")) {
// use "/@fs/" url to workaround the case where remix client/server default entry files live outside of app root,
// for example, when using pnpm, the structure becomes:
// (workspace-root)
// |- node_modules/.pnpm/@remix-run+dev@xxx/node_modules/@remix-run/dev/dist/config/defaults/entry.client.tsx
// |- packages/remix-app/vite.config.ts
// note that, by default, vite allows serving files from workspace root.
// https://vitejs.dev/config/server-options.html#server-fs-allow
return `/@fs` + filePath;
}

let relativePath = path.relative(rootDirectory, filePath);
let isWithinRoot =
!relativePath.startsWith("..") && !path.isAbsolute(relativePath);

if (relativePath.startsWith("..") || path.isAbsolute(relativePath)) {
throw new Error(
`Cannot resolve asset path "${filePath}" outside of root directory "${rootDirectory}".`
);
}
// Vite will prevent serving files outside of the workspace
// unless user explictly opts in with `server.fs.allow`
// https://vitejs.dev/config/server-options.html#server-fs-allow
if (!isWithinRoot) return `/@fs` + filePath;

return `/${normalizePath(relativePath)}`;
};
Expand Down

0 comments on commit 9ef2f64

Please sign in to comment.