Skip to content

Commit

Permalink
fix(vite): support JSX in .jsx files without React import (#7888)
Browse files Browse the repository at this point in the history
Co-authored-by: Mark Dalgleish <mark.john.dalgleish@gmail.com>
  • Loading branch information
hi-ogawa and markdalgleish authored Nov 3, 2023
1 parent 3aa8b00 commit 3874120
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-months-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/dev": patch
---

Support JSX usage in `.jsx` files without manual `React` import in Vite
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@
- harmony7
- helderburato
- HenryVogt
- hi-ogawa
- hicksy
- himorishige
- Hirochon
Expand Down
42 changes: 39 additions & 3 deletions integration/vite-dev-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ test.describe("Vite dev", () => {
export const loader: LoaderFunction = () => {
const headers = new Headers();
headers.append(
"Set-Cookie",
"first=one; Domain=localhost; Path=/; SameSite=Lax"
Expand All @@ -110,12 +110,12 @@ test.describe("Vite dev", () => {
);
headers.set("location", "http://localhost:${devPort}/get-cookies");
const response = new Response(null, {
headers,
status: 302,
});
return response;
};
`,
Expand All @@ -136,6 +136,15 @@ test.describe("Vite dev", () => {
);
}
`,
"app/routes/jsx.jsx": js`
export default function JsxRoute() {
return (
<div id="jsx">
<p data-hmr>HMR updated: no</p>
</div>
);
}
`,
},
});

Expand Down Expand Up @@ -228,6 +237,33 @@ test.describe("Vite dev", () => {
"first=one; second=two; third=three"
);
});

test("handles JSX in .jsx file without React import", async ({ page }) => {
let pageErrors: unknown[] = [];
page.on("pageerror", (error) => pageErrors.push(error));

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

let hmrStatus = page.locator("#jsx [data-hmr]");
await expect(hmrStatus).toHaveText("HMR updated: no");

let indexRouteContents = await fs.readFile(
path.join(projectDir, "app/routes/jsx.jsx"),
"utf8"
);
await fs.writeFile(
path.join(projectDir, "app/routes/jsx.jsx"),
indexRouteContents.replace("HMR updated: no", "HMR updated: yes"),
"utf8"
);
await page.waitForLoadState("networkidle");
await expect(hmrStatus).toHaveText("HMR updated: yes");

expect(pageErrors).toEqual([]);
});
});

let bufferize = (stream: Readable): (() => string) => {
Expand Down
4 changes: 4 additions & 0 deletions packages/remix-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,10 @@ export const remixVitePlugin: RemixVitePlugin = (options = {}) => {
"react-dom/client",
],
},
esbuild: {
jsx: "automatic",
jsxDev: viteCommand !== "build",
},
resolve: {
// https://react.dev/warnings/invalid-hook-call-warning#duplicate-react
dedupe: ["react", "react-dom"],
Expand Down

0 comments on commit 3874120

Please sign in to comment.