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

fix(vite): support JSX in .jsx files without React import #7888

Merged
merged 5 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Loading