Skip to content
Draft
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
9 changes: 5 additions & 4 deletions packages/plugin-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand All @@ -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(
Expand Down
13 changes: 10 additions & 3 deletions packages/plugin-react/tests/reactCompiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)')
Comment on lines +96 to +97

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The output looks like

import { jsxs as _jsxs } from "react/jsx-runtime";
export function App(t0) {
	const { name } = t0;
	const count = 0;
	return /* @__PURE__ */ _jsxs("div", { children: [name, count] });
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expect(output.code).not.toContain('useState(0)')
expect(output.code).not.toContain('useState')

})

test('uses the Vite build sourcemap setting', async () => {
Expand Down Expand Up @@ -144,9 +146,14 @@ async function transformWithBuildConfig(
}
return plugin.transform.handler.call(
context as any,
`export function App({ name }) { return <div>{name}</div> }`,
/* jsx */ `import { useState } from 'react';

export function App({ name }) {
const [count] = useState(0);
return <div>{name}{count}</div>
}`,
'/entry.tsx',
)
) as { code: string; map: any }
}

async function getViteReactConfig(
Expand Down
Loading