Skip to content
Closed
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
10 changes: 4 additions & 6 deletions packages/plugin-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,12 +382,10 @@ function createReactCompilerPlugin(
const refreshEnabled =
isClient &&
isFastRefreshEnabled() &&
((jsxRefreshFilter && !jsxRefreshFilter(id)) ||
!(
isJSX ||
code.includes(jsxImportRuntime) ||
code.includes(jsxImportDevRuntime)
))
(!jsxRefreshFilter || jsxRefreshFilter(id)) &&
(isJSX ||
code.includes(jsxImportRuntime) ||
code.includes(jsxImportDevRuntime))

const shouldCompile =
isClient &&
Expand Down
72 changes: 72 additions & 0 deletions packages/plugin-react/tests/reactCompiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,45 @@ describe('compiler option', () => {
})
})

test('enables Fast Refresh for JSX files only', async () => {
const component = await transformWithServeConfig(
{},
'/App.tsx',
`export function App() { return <div /> }`,
)
const hook = await transformWithServeConfig(
{},
'/useCounter.ts',
`
import { useState } from 'react'

export function useCounter() {
return useState(0)
}
`,
)

expect(component.code).toContain('$RefreshReg$')
expect(hook.code).not.toContain('$RefreshSig$')
})

test('uses refreshInclude and refreshExclude', async () => {
const code = `export function App() { return <div /> }`
const excluded = await transformWithServeConfig(
{ refreshExclude: /App\.tsx$/ },
'/App.tsx',
code,
)
const included = await transformWithServeConfig(
{ refreshInclude: /App\.tsx$/ },
'/App.tsx',
code,
)

expect(excluded.code).not.toContain('$RefreshReg$')
expect(included.code).toContain('$RefreshReg$')
})

test('uses the native JSX transform for server environments', async () => {
const output = await transformWithBuildConfig(
{},
Expand Down Expand Up @@ -205,6 +244,39 @@ async function transformWithBuildConfig(
}
}

async function transformWithServeConfig(
options: Options,
id: string,
code: string,
) {
const config = await resolveConfig(
{
configFile: false,
logLevel: 'silent',
plugins: [pluginReact({ compiler: true, ...options })],
},
'serve',
'development',
'development',
)
const plugin = config.plugins.find(
(plugin) => plugin.name === 'vite:react-compiler',
)!
const context = {
error(message: unknown): never {
throw new Error(String(message))
},
environment: { config: { consumer: 'client', command: 'serve' } },
}

if (typeof plugin.transform !== 'object') {
throw new Error('Missing transform hook')
}
return plugin.transform.handler.call(context as any, code, id) as {
code: string
}
}

async function getViteReactConfig(
options: Options,
command: 'serve' | 'build',
Expand Down
Loading