Skip to content

Commit 981fb24

Browse files
pcattorihi-ogawa
andauthored
Vite: Enable HMR for .md and .mdx files (#8711)
Co-authored-by: Hiroshi Ogawa <[email protected]>
1 parent 525f8bf commit 981fb24

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

integration/vite-hmr-hdr-test.ts

+55
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,61 @@ test("Vite / HMR & HDR / express", async ({ page, browserName, customDev }) => {
5757
await workflow({ page, browserName, cwd, port });
5858
});
5959

60+
test("Vite / HMR & HDR / mdx", async ({ page, viteDev }) => {
61+
let files: Files = async ({ port }) => ({
62+
"vite.config.ts": `
63+
import { defineConfig } from "vite";
64+
import { unstable_vitePlugin as remix } from "@remix-run/dev";
65+
import mdx from "@mdx-js/rollup";
66+
67+
export default defineConfig({
68+
${await viteConfig.server({ port })}
69+
plugins: [
70+
mdx(),
71+
remix(),
72+
],
73+
});
74+
`,
75+
"app/component.tsx": `
76+
import {useState} from "react";
77+
78+
export const Counter = () => {
79+
const [count, setCount] = useState(0);
80+
return <button onClick={() => setCount(count => count + 1)}>Count: {count}</button>
81+
}
82+
`,
83+
"app/routes/mdx.mdx": `
84+
import { Counter } from "../component";
85+
86+
# MDX Title (HMR: 0)
87+
88+
<Counter />
89+
`,
90+
});
91+
92+
let { port, cwd } = await viteDev(files);
93+
let edit = createEditor(cwd);
94+
await page.goto(`http://localhost:${port}/mdx`, {
95+
waitUntil: "networkidle",
96+
});
97+
98+
await expect(page.locator("h1")).toHaveText("MDX Title (HMR: 0)");
99+
let button = page.locator("button");
100+
await expect(button).toHaveText("Count: 0");
101+
await button.click();
102+
await expect(button).toHaveText("Count: 1");
103+
104+
await edit("app/routes/mdx.mdx", (contents) =>
105+
contents.replace("(HMR: 0)", "(HMR: 1)")
106+
);
107+
await page.waitForLoadState("networkidle");
108+
109+
await expect(page.locator("h1")).toHaveText("MDX Title (HMR: 1)");
110+
await expect(page.locator("button")).toHaveText("Count: 1");
111+
112+
expect(page.errors).toEqual([]);
113+
});
114+
60115
async function workflow({
61116
page,
62117
browserName,

packages/remix-dev/vite/plugin.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1491,7 +1491,8 @@ export const remixVitePlugin: RemixVitePlugin = (remixUserConfig = {}) => {
14911491
if (id.includes("/node_modules/")) return;
14921492

14931493
let [filepath] = id.split("?");
1494-
if (!/.[tj]sx?$/.test(filepath)) return;
1494+
let extensionsRE = /\.(jsx?|tsx?|mdx?)$/;
1495+
if (!extensionsRE.test(filepath)) return;
14951496

14961497
let devRuntime = "react/jsx-dev-runtime";
14971498
let ssr = options?.ssr === true;

0 commit comments

Comments
 (0)