镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
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
14 changes: 11 additions & 3 deletions packages/plugin-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,15 @@ function createReactCompilerPlugin(
},
},
async handler(code, id) {
const isClient = this.environment?.config.consumer !== 'server'
const environmentConfig = this.environment?.config
const isClient = environmentConfig?.consumer !== 'server'
const environmentSourcemap = environmentConfig
? environmentConfig.command !== 'build' ||
!!environmentConfig.build.sourcemap
: sourcemap
const environmentJsxDevelopment = environmentConfig
? !environmentConfig.isProduction
: jsxDevelopment
const shouldCompile =
isClient &&
(options.compilationMode === 'annotation'
Expand All @@ -354,12 +362,12 @@ function createReactCompilerPlugin(
const result = await transform(id.split('?')[0]!, code, {
jsx: {
runtime: reactOptions.jsxRuntime,
development: jsxDevelopment,
development: environmentJsxDevelopment,
importSource: reactOptions.jsxImportSource,
refresh: isClient && isFastRefreshEnabled(),
},
reactCompiler: shouldCompile ? options : false,
sourcemap,
sourcemap: environmentSourcemap,
})
const diagnostics = result.errors.map(
(error) =>
Expand Down
67 changes: 58 additions & 9 deletions packages/plugin-react/tests/reactCompiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,56 @@ describe('compiler option', () => {
expect(withoutSourcemap.map).toBeFalsy()
expect((await transformWithBuildConfig({}, true)).map).toBeTruthy()
})

test('uses the current environment sourcemap setting for shared plugins', async () => {
const plugin = pluginReact({ compiler: true }).find(
(plugin) => plugin.name === 'vite:react-compiler',
)!
const clientConfig = {
command: 'build',
isProduction: true,
build: { sourcemap: true },
consumer: 'client',
}
const serverConfig = {
command: 'build',
isProduction: false,
build: { sourcemap: false },
consumer: 'server',
}
const context = {
error(message: unknown): never {
throw new Error(String(message))
},
warn() {},
environment: { config: clientConfig },
}

if (typeof plugin.config !== 'function')
throw new Error('Missing config hook')
await plugin.config.call(
context as any,
{},
{ command: 'build', mode: 'production' },
)

if (typeof plugin.configResolved !== 'function')
throw new Error('Missing configResolved hook')
await plugin.configResolved.call(context as any, clientConfig as any)
await plugin.configResolved.call(context as any, serverConfig as any)

if (typeof plugin.transform !== 'object')
throw new Error('Missing transform hook')
const result = await plugin.transform.handler.call(
context as any,
`export function App({ name }) { return <div>{name}</div> }`,
'/entry.tsx',
)

expect(result.map).toBeTruthy()
expect(result.code).toContain('react/jsx-runtime')
expect(result.code).not.toContain('react/jsx-dev-runtime')
})
})

async function transformWithBuildConfig(
Expand All @@ -111,12 +161,18 @@ async function transformWithBuildConfig(
const plugin = pluginReact({ compiler }).find(
(plugin) => plugin.name === 'vite:react-compiler',
)!
const config = {
command: 'build',
isProduction: true,
build: { sourcemap: buildSourcemap },
consumer,
}
const context = {
error(message: unknown): never {
throw new Error(String(message))
},
warn() {},
environment: { config: { consumer } },
environment: { config },
}

if (typeof plugin.config !== 'function')
Expand All @@ -130,14 +186,7 @@ async function transformWithBuildConfig(
if (typeof plugin.configResolved !== 'function') {
throw new Error('Missing configResolved hook')
}
await plugin.configResolved.call(
context as any,
{
command: 'build',
isProduction: true,
build: { sourcemap: buildSourcemap },
} as any,
)
await plugin.configResolved.call(context as any, config as any)

if (typeof plugin.transform !== 'object') {
throw new Error('Missing transform hook')
Expand Down