From c555b837835d2fe4fd1f705cc9ba65fe7de440db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arnaud=20Barr=C3=A9?= Date: Sun, 3 Dec 2023 21:46:53 +0100 Subject: [PATCH] fix: remove plugin type extension --- packages/plugin-react/CHANGELOG.md | 18 ++++++++++++++++++ packages/plugin-react/src/index.ts | 16 ++++++---------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/packages/plugin-react/CHANGELOG.md b/packages/plugin-react/CHANGELOG.md index 1717de4c..62341411 100644 --- a/packages/plugin-react/CHANGELOG.md +++ b/packages/plugin-react/CHANGELOG.md @@ -2,6 +2,24 @@ ## Unreleased +Remove generic parameter on `Plugin` to avoid type error with Rollup 4/Vite 5 and `skipLibCheck: false`. + +I expect very few people to currently use this feature, but if you are extending the React plugin via `api` object, you can get back the typing of the hook by importing `ViteReactPluginApi`: + +```ts +import type { Plugin } from 'vite' +import type { ViteReactPluginApi } from '@vitejs/plugin-react' + +export const somePlugin: Plugin = { + name: 'some-plugin', + api: { + reactBabel: (babelConfig) => { + babelConfig.plugins.push('some-babel-plugin') + }, + } satisfies ViteReactPluginApi, +} +``` + ## 4.2.0 (2023-11-16) ### Update peer dependency range to target Vite 5 diff --git a/packages/plugin-react/src/index.ts b/packages/plugin-react/src/index.ts index c1308519..1a133560 100644 --- a/packages/plugin-react/src/index.ts +++ b/packages/plugin-react/src/index.ts @@ -79,15 +79,11 @@ type ReactBabelHook = ( type ReactBabelHookContext = { ssr: boolean; id: string } -declare module 'vite' { - export interface Plugin { - api?: { - /** - * Manipulate the Babel options of `@vitejs/plugin-react` - */ - reactBabel?: ReactBabelHook - } - } +export type ViteReactPluginApi = { + /** + * Manipulate the Babel options of `@vitejs/plugin-react` + */ + reactBabel?: ReactBabelHook } const refreshContentRE = /\$Refresh(?:Reg|Sig)\$\(/ @@ -150,7 +146,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] { ) } - const hooks = config.plugins + const hooks: ReactBabelHook[] = config.plugins .map((plugin) => plugin.api?.reactBabel) .filter(defined)