feat(plugin-react): allow options.babel to be a function#6238
feat(plugin-react): allow options.babel to be a function#6238patak-cat merged 4 commits intovitejs:mainfrom
Conversation
|
Any reason for making it |
|
Alternatively, we could do an |
Because
This is probably the way to go. I'll update my branch at the first opportunity. |
|
@cyco130 couldn't this be done with conditional config? It doesn't seem scalable to add |
|
@patak-dev it would work for build but not in dev. The idea is to be able to use different transforms when BTW I noticed the new |
I like this direction! |
…el plugins for SSR
c1376b3 to
9c412a1
Compare
|
@aleclarson Updated to implement that idea. |
|
Hmm, maybe it would be best to pass |
|
@patak-dev LGTM 🎸 |
babel option be a function + add file/ssr props to api.reactBabel hooks
babel option be a function + add file/ssr props to api.reactBabel hooksbabel option be a function + expose file/ssr values to api.reactBabel hooks
|
This makes a lot more sense to me now. Isn't it too much to redo the babel plugins on each transform call? It seems inefficient. For the original use case, wouldn't a set of plugins that are computed once be enough? (like it was before being a function?). |
|
Even before, Babel would resolve the plugins on every transformation, so the performance impact here is negligible.
The Babel options object is cached in the
I'd say it's ideal to allow per-file configuration whenever possible, since it provides more flexibility. For example, higher level frameworks built on Vite might need to apply certain plugins based on file structure or in SSR only. It would hurt performance to implement their own Babel transformer. |
|
We got another +1 to merge this in today's team meeting. @cyco130 we can move forward once the tests are green 👍🏼 |
|
I've fixed the related test but I don't quite understand the (probably unrelated) failing IIFE tests on MacOS. I'd appreciate some help with it :) |
That was a glitch in Vite's CI, sorry about that |
1d120d2 to
c3e8c5d
Compare
packages/plugin-react/src/index.ts
Outdated
| ) { | ||
| const babelOptions = { | ||
| ssr, | ||
| file, |
There was a problem hiding this comment.
Another option is to use Object.defineProperties to define ssr and file options with enumerable: false (note this is the default when using defineProperties). That will prevent Babel from getting them, since non-enumerable properties are ignored by the spread operator.
There was a problem hiding this comment.
I feel the intention would be less obvious and require a comment. But I can do that if you feel it'd be better.
There was a problem hiding this comment.
Why are these in the same object? I think the API would be more clear with three args. ssr and file are quite different than the rest of the object
type ReactBabelHook = (
babelConfig: ReactBabelOptions,
options: { ssr, file }, // or context?
config: ResolvedConfig
) => voidThere was a problem hiding this comment.
ok let's do that, and I vote context as the name
There was a problem hiding this comment.
Thanks for the changes @cyco130!
Sorry, one last question 👀
Why is it called file? Now that it is in the second parameter, should we rename it to id? file isn't that good in case there are virtual modules, no?
There was a problem hiding this comment.
Fixed (wasn't my idea anyway :P )
7d3fcad to
284fc18
Compare
babel option be a function + expose file/ssr values to api.reactBabel hooks
Description
This PR adds an
ssrPluginsoptions to plugin-react that overrides thebabel.pluginsoption for SSR because sometimes different transforms are needed in the browser than on the server.UPDATE: We settled on allowing
options.babelto be a function to solve the same problem more generally instead.What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123).