From 6ecafc8c25c0b194eb2d381ac15a3601364aa070 Mon Sep 17 00:00:00 2001 From: Mark Dalgleish Date: Fri, 3 Nov 2023 08:18:42 +1100 Subject: [PATCH 1/3] perf(vite): fix LiveReload proxy bailout logic --- .changeset/nervous-fans-do.md | 5 +++++ packages/remix-dev/vite/plugin.ts | 8 ++++---- 2 files changed, 9 insertions(+), 4 deletions(-) create mode 100644 .changeset/nervous-fans-do.md diff --git a/.changeset/nervous-fans-do.md b/.changeset/nervous-fans-do.md new file mode 100644 index 00000000000..1a6c274c696 --- /dev/null +++ b/.changeset/nervous-fans-do.md @@ -0,0 +1,5 @@ +--- +"@remix-run/dev": patch +--- + +Improve performance of LiveReload proxy in Vite dev diff --git a/packages/remix-dev/vite/plugin.ts b/packages/remix-dev/vite/plugin.ts index 56703a8acf6..a69421462e5 100644 --- a/packages/remix-dev/vite/plugin.ts +++ b/packages/remix-dev/vite/plugin.ts @@ -666,11 +666,11 @@ export const remixVitePlugin: RemixVitePlugin = (options = {}) => { return; } + let referencesLiveReload = + code.includes("@remix-run/react") && code.includes("LiveReload"); + // Don't transform files that don't need the proxy - if ( - !code.includes("@remix-run/react") && - !code.includes("LiveReload") - ) { + if (!referencesLiveReload) { return; } From f112cd9ab2b4b6474a00e464e1dfc4ffbc3dfa0d Mon Sep 17 00:00:00 2001 From: Mark Dalgleish Date: Fri, 3 Nov 2023 08:47:30 +1100 Subject: [PATCH 2/3] refactor LiveReload check --- packages/remix-dev/vite/plugin.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/remix-dev/vite/plugin.ts b/packages/remix-dev/vite/plugin.ts index a69421462e5..51224a2f4a0 100644 --- a/packages/remix-dev/vite/plugin.ts +++ b/packages/remix-dev/vite/plugin.ts @@ -666,11 +666,11 @@ export const remixVitePlugin: RemixVitePlugin = (options = {}) => { return; } - let referencesLiveReload = - code.includes("@remix-run/react") && code.includes("LiveReload"); + let hasLiveReloadHints = + code.includes("LiveReload") && code.includes("@remix-run/react"); // Don't transform files that don't need the proxy - if (!referencesLiveReload) { + if (!hasLiveReloadHints) { return; } From e4d410ccb0fda2547884c579c87f1083692ef4d6 Mon Sep 17 00:00:00 2001 From: Mark Dalgleish Date: Fri, 3 Nov 2023 13:42:56 +1100 Subject: [PATCH 3/3] add remix-run/react to optimizeDeps array --- integration/vite-css-dev-test.ts | 36 ++++++++++++++++++---------- integration/vite-dev-express-test.ts | 4 ++-- integration/vite-dev-test.ts | 3 +++ 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/integration/vite-css-dev-test.ts b/integration/vite-css-dev-test.ts index 5401095de37..45681f7e815 100644 --- a/integration/vite-css-dev-test.ts +++ b/integration/vite-css-dev-test.ts @@ -36,6 +36,9 @@ test.describe("Vite CSS dev", () => { port: ${devPort}, strictPort: true, }, + optimizeDeps: { + include: ["react", "react-dom/client", "@remix-run/react"], + }, plugins: [remix()], }); `, @@ -60,28 +63,28 @@ test.describe("Vite CSS dev", () => { ); } `, - "app/routes/_index/styles-bundled.css": css` + "app/styles-bundled.css": css` .index_bundled { background: papayawhip; padding: ${TEST_PADDING_VALUE}; } `, - "app/routes/_index/styles-linked.css": css` + "app/styles-linked.css": css` .index_linked { background: salmon; padding: ${TEST_PADDING_VALUE}; } `, - "app/routes/_index/styles.module.css": css` + "app/styles.module.css": css` .index { background: peachpuff; padding: ${TEST_PADDING_VALUE}; } `, - "app/routes/_index/route.tsx": js` - import "./styles-bundled.css"; - import linkedStyles from "./styles-linked.css?url"; - import cssModulesStyles from "./styles.module.css"; + "app/routes/_index.tsx": js` + import "../styles-bundled.css"; + import linkedStyles from "../styles-linked.css?url"; + import cssModulesStyles from "../styles.module.css"; export function links() { return [{ rel: "stylesheet", href: linkedStyles }]; @@ -160,9 +163,16 @@ test.describe("Vite CSS dev", () => { test.describe("with JS", () => { test.use({ javaScriptEnabled: true }); test("updates CSS", async ({ page }) => { + let pageErrors: unknown[] = []; + page.on("pageerror", (error) => pageErrors.push(error)); + await page.goto(`http://localhost:${devPort}/`, { waitUntil: "networkidle", }); + + // Ensure no errors on page load + expect(pageErrors).toEqual([]); + await expect(page.locator("#index [data-css-modules]")).toHaveCSS( "padding", TEST_PADDING_VALUE @@ -177,11 +187,11 @@ test.describe("Vite CSS dev", () => { ); let bundledCssContents = await fs.readFile( - path.join(projectDir, "app/routes/_index/styles-bundled.css"), + path.join(projectDir, "app/styles-bundled.css"), "utf8" ); await fs.writeFile( - path.join(projectDir, "app/routes/_index/styles-bundled.css"), + path.join(projectDir, "app/styles-bundled.css"), bundledCssContents.replace( TEST_PADDING_VALUE, UPDATED_TEST_PADDING_VALUE @@ -190,11 +200,11 @@ test.describe("Vite CSS dev", () => { ); let linkedCssContents = await fs.readFile( - path.join(projectDir, "app/routes/_index/styles-linked.css"), + path.join(projectDir, "app/styles-linked.css"), "utf8" ); await fs.writeFile( - path.join(projectDir, "app/routes/_index/styles-linked.css"), + path.join(projectDir, "app/styles-linked.css"), linkedCssContents.replace( TEST_PADDING_VALUE, UPDATED_TEST_PADDING_VALUE @@ -203,11 +213,11 @@ test.describe("Vite CSS dev", () => { ); let cssModuleContents = await fs.readFile( - path.join(projectDir, "app/routes/_index/styles.module.css"), + path.join(projectDir, "app/styles.module.css"), "utf8" ); await fs.writeFile( - path.join(projectDir, "app/routes/_index/styles.module.css"), + path.join(projectDir, "app/styles.module.css"), cssModuleContents.replace( TEST_PADDING_VALUE, UPDATED_TEST_PADDING_VALUE diff --git a/integration/vite-dev-express-test.ts b/integration/vite-dev-express-test.ts index 558139db2fd..06d97f2eb5a 100644 --- a/integration/vite-dev-express-test.ts +++ b/integration/vite-dev-express-test.ts @@ -24,9 +24,9 @@ test.beforeAll(async () => { hmr: { port: ${hmrPort} } - }, + }, optimizeDeps: { - include: ["react", "react-dom/client"], + include: ["react", "react-dom/client", "@remix-run/react"], }, plugins: [remix()], }); diff --git a/integration/vite-dev-test.ts b/integration/vite-dev-test.ts index c9dd3cb2b7b..2a847db5d35 100644 --- a/integration/vite-dev-test.ts +++ b/integration/vite-dev-test.ts @@ -33,6 +33,9 @@ test.describe("Vite dev", () => { port: ${devPort}, strictPort: true, }, + optimizeDeps: { + include: ["react", "react-dom/client", "@remix-run/react"], + }, plugins: [remix()], }); `,