diff --git a/packages/plugin-react/src/index.ts b/packages/plugin-react/src/index.ts index b86d7c37b..f0d96447f 100644 --- a/packages/plugin-react/src/index.ts +++ b/packages/plugin-react/src/index.ts @@ -343,10 +343,9 @@ function createReactCompilerPlugin( async handler(code, id) { const isClient = this.environment?.config.consumer !== 'server' const shouldCompile = - isClient && - (options.compilationMode === 'annotation' + options.compilationMode === 'annotation' ? /['"]use memo['"]/.test(code) - : defaultCodeFilter.test(code)) + : defaultCodeFilter.test(code) // The config hook is not called when the plugin is used with Rolldown directly. const { transform } = compiler ?? (await loadCompiler((message) => this.error(message))) @@ -358,7 +357,9 @@ function createReactCompilerPlugin( importSource: reactOptions.jsxImportSource, refresh: isClient && isFastRefreshEnabled(), }, - reactCompiler: shouldCompile ? options : false, + reactCompiler: shouldCompile + ? { ...options, outputMode: isClient ? 'client' : 'ssr' } + : false, sourcemap, }) const diagnostics = result.errors.map( diff --git a/packages/plugin-react/tests/reactCompiler.test.ts b/packages/plugin-react/tests/reactCompiler.test.ts index fd7abbd2b..a4bcd3fee 100644 --- a/packages/plugin-react/tests/reactCompiler.test.ts +++ b/packages/plugin-react/tests/reactCompiler.test.ts @@ -88,11 +88,13 @@ describe('compiler option', () => { }) }) - test('uses the native JSX transform for server environments', async () => { + test('uses the SSR transform for server environments', async () => { const output = await transformWithBuildConfig({}, false, 'server') expect(output.code).toContain('react/jsx-runtime') expect(output.code).not.toContain('react/compiler-runtime') + expect(output.code).toContain('const count = 0') + expect(output.code).not.toContain('useState(0)') }) test('uses the Vite build sourcemap setting', async () => { @@ -144,9 +146,14 @@ async function transformWithBuildConfig( } return plugin.transform.handler.call( context as any, - `export function App({ name }) { return