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: Fix server.fs.allow with default entry #8966

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Vite: Fix server.fs.allow with default entry
  • Loading branch information
markdalgleish committed Mar 4, 2024
commit e941c68cc9277bc9b61899b16a26c0db0cdea413
5 changes: 5 additions & 0 deletions .changeset/tall-frogs-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/dev": patch
---

Vite: Fix error when using Vite's `server.fs.allow` option without a client entry file
12 changes: 9 additions & 3 deletions integration/helpers/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@ const remixBin = "node_modules/@remix-run/dev/dist/cli.js";
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));

export const viteConfig = {
server: async (args: { port: number }) => {
server: async (args: { port: number; fsAllow?: string[] }) => {
let { port, fsAllow } = args;
let hmrPort = await getPort();
let text = dedent`
server: { port: ${args.port}, strictPort: true, hmr: { port: ${hmrPort} } },
server: {
port: ${port},
strictPort: true,
hmr: { port: ${hmrPort} },
fs: { allow: ${fsAllow ? JSON.stringify(fsAllow) : "undefined"} }
},
`;
return text;
},
basic: async (args: { port: number }) => {
basic: async (args: { port: number; fsAllow?: string[] }) => {
return dedent`
import { vitePlugin as remix } from "@remix-run/dev";

Expand Down
51 changes: 51 additions & 0 deletions integration/vite-server-fs-allow-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { test, expect } from "@playwright/test";
import getPort from "get-port";

import {
createProject,
customDev,
EXPRESS_SERVER,
viteConfig,
} from "./helpers/vite.js";

let files = {
"app/routes/test-route.tsx": String.raw`
export default function IndexRoute() {
return <div id="test">Hello world</div>
}
`,
};

test.describe(async () => {
let port: number;
let cwd: string;
let stop: () => void;

test.beforeAll(async () => {
port = await getPort();
cwd = await createProject({
"vite.config.ts": await viteConfig.basic({ port, fsAllow: ["app"] }),
"server.mjs": EXPRESS_SERVER({ port }),
...files,
});
stop = await customDev({ cwd, port });
});
test.afterAll(() => stop());

test("Vite / server.fs.allow / works with basic allow list", async ({
page,
}) => {
let pageErrors: unknown[] = [];
page.on("pageerror", (error) => pageErrors.push(error));

await page.goto(`http://localhost:${port}/test-route`, {
waitUntil: "networkidle",
});
expect(pageErrors).toEqual([]);

let testContent = page.locator("#test");
await expect(testContent).toBeAttached();

expect(pageErrors).toEqual([]);
});
});
15 changes: 15 additions & 0 deletions packages/remix-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,12 @@ export let getServerBuildDirectory = (ctx: RemixPluginContext) =>
let getClientBuildDirectory = (remixConfig: ResolvedVitePluginConfig) =>
path.join(remixConfig.buildDirectory, "client");

let defaultEntriesDir = path.resolve(__dirname, "..", "config", "defaults");
let defaultEntries = fse
.readdirSync(defaultEntriesDir)
.map((filename) => path.join(defaultEntriesDir, filename));
invariant(defaultEntries.length > 0, "No default entries found");

let mergeRemixConfig = (...configs: VitePluginConfig[]): VitePluginConfig => {
let reducer = (
configA: VitePluginConfig,
Expand Down Expand Up @@ -1087,6 +1093,15 @@ export const remixVitePlugin: RemixVitePlugin = (remixUserConfig = {}) => {
},
base: viteUserConfig.base,

// When consumer provides an allow list for files that can be read by
// the server, ensure that Remix's default entry files are included.
// If we don't do this and a default entry file is used, the server
// will throw an error that the file is not allowed to be read.
// https://vitejs.dev/config/server-options#server-fs-allow
server: viteUserConfig.server?.fs?.allow
? { fs: { allow: defaultEntries } }
: undefined,

// Vite config options for building
...(viteCommand === "build"
? {
Expand Down
Loading