Skip to content

Commit

Permalink
fix: fix fast-refresh for files that are transformed into jsx (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
csr632 authored Jun 27, 2023
1 parent f8bfc8d commit caa9b53
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 5 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ To use breakpoints and explore code execution, you can use the ["Run and Debug"]

Some errors are masked and hidden away because of the layers of abstraction and sandboxed nature added by Vitest, Playwright, and Chromium. In order to see what's actually going wrong and the contents of the devtools console in those instances, follow this setup:

1. Add a `debugger` statement to the `playground/vitestSetup.ts` -> `afterAll` hook. This will pause execution before the tests quit and the Playwright browser instance exits.
1. Add a `debugger` statement to the `playground/vitestSetup.ts` -> `afterAll` hook. This will pause execution before the tests quit and the Playwright browser instance exits. You can also add `debugger` statements in your tests (`.spec.ts`) files.

2. Run the tests with the `debug-serve` script command, which will enable remote debugging: `pnpm run debug-serve react-sourcemap`.
2. Run the tests with the `debug-serve` script command, which will enable remote debugging: `pnpm run debug-serve react-sourcemap`. Remember to run this command within the **"JavaScript Debug Terminal"** of VS Code.

3. Wait for inspector devtools to open in your browser and the debugger to attach.

Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
!ssr &&
(isJSX ||
(opts.jsxRuntime === 'classic'
? code.includes(devRuntime)
: importReactRE.test(code)))
? importReactRE.test(code)
: code.includes(devRuntime)))
if (useFastRefresh) {
plugins.push([
await loadPlugin('react-refresh/babel'),
Expand Down
20 changes: 20 additions & 0 deletions playground/mdx/__tests__/mdx.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ test('should render', async () => {
expect(await page.textContent('h1')).toMatch('Vite + MDX')
})

test('.md extension should work', async () => {
expect(await page.getByText('.md extension works.').textContent()).toEqual(
'.md extension works. This is bold text.',
)
})

if (isServe) {
test('should hmr', async () => {
editFile('src/demo.mdx', (code) => code.replace('Vite + MDX', 'Updated'))
Expand All @@ -20,4 +26,18 @@ if (isServe) {
)
await untilUpdated(() => page.textContent('h1'), 'Updated')
})

test('should hmr with .md extension', async () => {
await untilBrowserLogAfter(
() =>
editFile('src/demo2.md', (code) =>
code.replace('`.md` extension works.', '`.md` extension hmr works.'),
),
'[vite] hot updated: /src/demo2.md',
)
await untilUpdated(
() => page.getByText('.md extension hmr works.').textContent(),
'.md extension hmr works. This is bold text.',
)
})
}
3 changes: 3 additions & 0 deletions playground/mdx/src/demo2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## test md extension

`.md` extension works. This is **bold text**.
2 changes: 2 additions & 0 deletions playground/mdx/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import Demo from './demo.mdx'
import Demo2 from './demo2.md'

ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<Demo />
<Demo2 />
</React.StrictMode>,
)
5 changes: 5 additions & 0 deletions playground/mdx/src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ declare module '*.mdx' {
import { JSX } from 'react'
export default () => JSX.Element
}

declare module '*.md' {
import { JSX } from 'react'
export default () => JSX.Element
}
2 changes: 1 addition & 1 deletion playground/mdx/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import mdx from '@mdx-js/rollup'
export default defineConfig({
plugins: [
{ enforce: 'pre', ...mdx() },
react({ include: /\.(mdx|ts|tsx)$/ }),
react({ include: /\.(mdx|md|ts|tsx)$/ }),
],
})

0 comments on commit caa9b53

Please sign in to comment.