Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vite plugin does not accept options #12352

Open
jgarplind opened this issue Nov 22, 2024 · 19 comments
Open

Vite plugin does not accept options #12352

jgarplind opened this issue Nov 22, 2024 · 19 comments

Comments

@jgarplind
Copy link

Describe what's incorrect/missing in the documentation

https://reactrouter.com/upgrading/router-provider#2-install-the-vite-plugin describes how to adopt the new React Router framework, coming from RouterProvider.

I started this migration, but ran into issues when I realized that the reactRouter plugin does not accept the same options as the react plugin, which stopped me in my tracks.

Specifically, I have the need to add babel plugins both for React compiler: https://react.dev/learn/react-compiler#usage-with-vite and Lingui: https://lingui.dev/tutorials/setup-vite.

In Discord, Kosire suggested I could patch the plugin: https://discord.com/channels/770287896669978684/1309472866453946368/1309472866453946368, which would probably work, but is not very accessible to the average user of the library, and would also be a significant maintenance burden over time, so it's not something I would like to do without being sure it is the only way.

Is this the state of things? Or is there a more appropriate way to handle this scenario, which could then be added to the documentation?

@jgarplind jgarplind added the docs label Nov 22, 2024
@timdorr
Copy link
Member

timdorr commented Nov 23, 2024

Currently, it takes no options, so that would be the place to start. Unfortunately, it doesn't look like an easy task.

@jgarplind
Copy link
Author

Thanks for weighing in! Yeah, that much is clear from looking at the source code. Whether it is an easy task or not is harder for me to tell.

I'm not sure why/if it would be difficult to accept the same arguments as the react-plugin (or at least the babel option). I wrote this issue hoping it was more or less forgotten, but of course it seems more likely that there are legitimate technical reasons.

@romulovalez
Copy link

I also need to pass the babel option in order to use styled-components, so I think is a must for a lot of users

@timdorr timdorr changed the title [Docs]: Framework Adoption from RouterProvider not covering (missing) Vite plugin options Vite plugin does not accept options Nov 24, 2024
@faradaytrs
Copy link

We also need babel options because we are using react compiler

@MylesWardell

This comment was marked as off-topic.

@AlemTuzlak
Copy link
Contributor

I'll shortly send a PR link on how to add this to your project

@AlemTuzlak
Copy link
Contributor

forge-42/supabase-realtime#6
Here is a reference implementation that successfully adds it into a react router v7 framework mode project. also attached are the images of all the relevant changes
package json
vite config ts
babelrc

@jgarplind
Copy link
Author

Thanks @AlemTuzlak, looks promising! I'll report back here once I've had the opportunity to test it, and confirmed that it covers my use cases, including Lingui.

@jgarplind
Copy link
Author

Documenting as I go:

export default defineConfig({
  plugins: [
    babel({
      babelConfig: {
        plugins: ["@lingui/babel-plugin-lingui-macro"],
      },
    }),
    ...

This unblocks me and lets me continue my migration (which I don't have the time to finalize right now).

Unclear so far for me if I need to add @babel/preset-typescript.

I'll update this later as I find the time to finalize the migration, but this is very promising.

Hopefully we can add this to the documentation once we've validated it works widely.

@timdorr
Copy link
Member

timdorr commented Dec 5, 2024

Note that having multiple plugins will instantiate multiple copies of Babel, transform your code multiple times, and result in both memory bloat and slower performance. That is why the official React plugin takes these options. We should do the same so builds remain as fast as possible.

@AlemTuzlak
Copy link
Contributor

@timdorr I'm not sure that's the case? It runs a single transform with multiple plugins from what I saw while trying it out?

@timdorr
Copy link
Member

timdorr commented Dec 5, 2024

The React Router plugin runs its own Babel instance and transforms, depending on the module type:

let ast = parse(code, { sourceType: "module" });
if (!options?.ssr) {
removeExports(ast, SERVER_ONLY_ROUTE_EXPORTS);
}
WithProps.transform(ast);
return generate(ast, {
sourceMaps: true,
filename: id,
sourceFileName: filepath,
});

let result = await babel.transformAsync(code, {
babelrc: false,
configFile: false,
filename: id,
sourceFileName: filepath,
parserOpts: {
sourceType: "module",
allowAwaitOutsideFunction: true,
},
plugins: [[require("react-refresh/babel"), { skipEnvCheck: true }]],
sourceMaps: true,
});

But this is also what the official React plugin does:

https://github.com/vitejs/vite-plugin-react/blob/2e368a65aaf8314142538c369c296e277fdfb69b/packages/plugin-react/src/index.ts#L232-L258

So, you're creating at least two instances of Babel. It would be far better for us to just take options directly so that transform step is done one time.

@GrahamQuan
Copy link

This would be useful for react compiler

@crispiestsquid
Copy link

Stumbled upon this while also hoping for a simple way to implement React Compiler.

Would love to see the reactRouter plugin take options. If there is anything I can do to help move that along please reach out.

Otherwise I will keep playing with the workaround while patiently wait for this

@sindras
Copy link

sindras commented Dec 27, 2024

Thanks for starting this thread. I also noticed that when I'm migrating from Remix to RRv7, there is no option to pass presets, which makes it difficult to implement PWA using @vite-pwa/remix.

@kazem3d
Copy link

kazem3d commented Dec 27, 2024

Thanks for starting this thread. I also noticed that when I'm migrating from Remix to RRv7, there is no option to pass presets, which makes it difficult to implement PWA using @vite-pwa/remix.

I also have this problem,How to have a pwa application in RRv7 ?

@marcebdev
Copy link

marcebdev commented Jan 3, 2025

Same, this is a big blocker. I was pretty interested in how RR7 is using a plugin to handle type safety, etc. but now seeing that it replaces the base @vitejs/plugin-react and also doesn't offer any options, it seems like the plugin will be substantially more difficult to deal with, which superficially isn't very encouraging. Obviously the option of the SWC version already goes out the door anyways, so that piece isn't a loss

I'm sure there's some technical reasons but it's not very confidence inducing that the plugin seemingly doesn't wrap the react plugin in some way or this isn't made available as it's own babel transform.

Def know the team needs time, so I am patient and curious to see what the team comes up with, but I think a pressing question is how will the plugin keep up with features/optimizations done in the official vite react plugin?

And in the most sincere way, since Vite + RR seems like the main/recommended candidate for framework mode it's troubling how this was missed when making the decision to use a plugin?

@sdavids
Copy link

sdavids commented Jan 28, 2025

Stumbled upon this issue via:

https://react.dev/learn/react-compiler#usage-with-vite
https://react.dev/learn/react-compiler#usage-with-remix

@mikkpokk
Copy link

Stumbled upon this issue via:

https://react.dev/learn/react-compiler#usage-with-vite https://react.dev/learn/react-compiler#usage-with-remix

Doesn't work with RR7 as expected. Causes randomly Invalid Hook Call errors during navigation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests