From c1376b3f8060aae7872cb9eabb476c0d7683b779 Mon Sep 17 00:00:00 2001 From: cyco130 Date: Wed, 22 Dec 2021 15:10:55 +0300 Subject: [PATCH] feat(plugin-react): Add possibility to specify a different set of Babel plugins for SSR --- packages/plugin-react/src/index.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/plugin-react/src/index.ts b/packages/plugin-react/src/index.ts index 5cbfc83c9bbbd5..2f05d61ed9984c 100644 --- a/packages/plugin-react/src/index.ts +++ b/packages/plugin-react/src/index.ts @@ -37,6 +37,12 @@ export interface Options { * Babel configuration applied in both dev and prod. */ babel?: TransformOptions + + /** + * Overrides `babel.plugins` in SSR. + */ + ssrPlugins?: TransformOptions['plugins'] + /** * @deprecated Use `babel.parserOpts.plugins` instead */ @@ -55,6 +61,8 @@ export default function viteReact(opts: Options = {}): PluginOption[] { const useAutomaticRuntime = opts.jsxRuntime !== 'classic' const userPlugins = opts.babel?.plugins || [] + const userSsrPlugins = opts.ssrPlugins || userPlugins + const userParserPlugins = opts.parserPlugins || opts.babel?.parserOpts?.plugins || [] @@ -113,7 +121,9 @@ export default function viteReact(opts: Options = {}): PluginOption[] { const isProjectFile = !isNodeModules && (id[0] === '\0' || id.startsWith(projectRoot + '/')) - const plugins = isProjectFile ? [...userPlugins] : [] + const plugins = isProjectFile + ? [...(ssr ? userSsrPlugins : userPlugins)] + : [] let useFastRefresh = false if (!skipFastRefresh && !ssr && !isNodeModules) {