-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
feat(plugin-react): change how babel.include/exclude
options behave
#6202
Conversation
By default, Babel plugins passed to plugin-react are only applied to modules in the project root (as well as virtual modules), except when they live in a node_modules folder. To override this behavior, you can now define `babel.include` to ensure a module is transformed by those particular plugins (note that babel.config.js can be used to apply plugins/presets to your entire bundle, but you need to pass `configFile: true` to plugin-react first). Similarly, you can define `babel.exclude` to stop a module from being transformed by the Babel plugins passed to plugin-react (as well as .babelrc plugins).
If I understand this correctly, this would still place the burden on the consumer to correctly specify a regex/glob pattern to include/exclude files? Is it possible for monorepo setups (at least the most common ones) to work out of the box? Perhaps by excluding |
@NMinhNguyen Good point! We could look for monorepo config files (eg: |
Is checking for |
The majority use case is interested in applying Babel plugins to project files only. I think it makes sense for monorepo users to explicitly opt-in specific local packages to be parsed/transformed, for performance reasons. So, on second thought, I think checking for workspace configs by default is unnecessary, but there could be an option for that. For a monorepo user, it's as easy as this: reactPlugin({
babel: {
include: ['..']
}
}) Or you can put |
Isn't "project files only" roughly equivalent to
I'm essentially using this internal packages approach from https://turborepo.com/posts/you-might-not-need-typescript-project-references where I'd like to author the majority of packages in TSX + Emotion and for them to be automatically processed in my Vite app. |
No it's more like A local package being referenced by the project doesn't imply that the project's Babel plugins should be applied to it. Better not to assume that, for best performance.
Vite uses Esbuild to compile node_modules for production builds, so support for esnext syntax in node_modules is enabled by default. As for browserslist, it's recommended to generate two production builds (modern and legacy) if you want to support older browsers. Use
Have you tried using You'll only need to add the Emotion babel plugin, since TSX is compiled by Esbuild by default (even for node_modules). |
Ah I see what you mean, so you don't want to apply the project's Babel plugins to the rest of the monorepo. I see now.
Yes, I have it, but it doesn't seem to be applied to packages outside of the project root because of the |
You have to pass |
Ah that worked (I also set |
Those plugins will be applied, so your build will be slower, but it shouldn't break anything. You can set
That's a Babel convention, not Vite's doing.
vite/packages/plugin-react/src/index.ts Lines 226 to 229 in 8fb854f
That said, because Btw, you'll probably want to use the |
@aleclarson that was really insightful, thanks! I did notice major slowdowns with |
projectRoot | ||
) | ||
shouldProjectExclude = createFileFilter( | ||
{ include: opts.babel?.exclude }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be "exclude" instead of "include"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No but it does need a comment to clarify that :)
It's creating a function that returns true if a babel.exclude
pattern is matched.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh, got it, thanks!
@@ -112,20 +135,23 @@ export default function viteReact(opts: Options = {}): PluginOption[] { | |||
const isNodeModules = id.includes('/node_modules/') | |||
const isProjectFile = | |||
!isNodeModules && (id[0] === '\0' || id.startsWith(projectRoot + '/')) | |||
? !shouldProjectExclude(id) | |||
: shouldProjectInclude(id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would any "exclude" settings be ignored in this case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good observation! With how this is currently written, one cannot exclude
a subset of paths matched by include
, which is certainly a valid use case.
Should be a simple fix, just add exclude: opts.babel?.exclude
to the shouldProjectInclude
definition.
Hi everyone, |
I don't think that's true. The
That shouldn't be happening. Do you have a repro? |
Hi @aleclarson,
I'm sorry, this specific problem happens when I use the
Sure, here it is: https://codesandbox.io/s/vite-react-monorepo-x2gldz Thanks! |
Maybe a possible improvement is the following: |
With the completion of #9590, |
@aleclarson would you move this PR to the new repo for plugin-react? |
If someone wants to write tests for this, you're more than welcome! 😄
Description
By default, Babel plugins passed to plugin-react are only applied to modules in the project root (as well as virtual modules), except when they live in a node_modules folder. To override this behavior, you can now define
babel.include
to ensure a module is transformed by those particular plugins (note that babel.config.js can be used to apply plugins/presets to your entire bundle, but you need to passconfigFile: true
to plugin-react first). Similarly, you can definebabel.exclude
to stop a module from being transformed by the Babel plugins passed to plugin-react (as well as .babelrc plugins).There's also a breaking change in this PR: The
include/exclude
options have been moved into thefastRefresh
option, so as to avoid confusing users into thinking they affect Babel transformation.Additional context
Closes vitejs/vite-plugin-react#16
What is the purpose of this pull request?